<commit_before>#
# Author:: <NAME> (<EMAIL>)
# Copyright:: Copyright (c) 2009 <NAME>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe Chef::Provider::Service::Freebsd do
  before do
    @node = Chef::Node.new
    @node[:command] = {:ps => "ps -ax"}
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Service.new("apache22")
    @new_resource.pattern("httpd")
    @new_resource.supports({:status => false})

    @current_resource = Chef::Resource::Service.new("apache22")

    @provider = Chef::Provider::Service::Freebsd.new(@new_resource,@run_context)
    Chef::Resource::Service.stub!(:new).and_return(@current_resource)
  end
  
  describe "load_current_resource" do
    before(:each) do
      @stdout = StringIO.new(<<-PS_SAMPLE)
413  ??  Ss     0:02.51 /usr/sbin/syslogd -s
539  ??  Is     0:00.14 /usr/sbin/sshd
545  ??  Ss     0:17.53 sendmail: accepting connections (sendmail)
PS_SAMPLE
      @status = mock(:stdout => @stdout, :exitstatus => 0)
      @provider.stub!(:shell_out!).with(@node[:command][:ps]).and_return(@status)

      ::File.stub!(:exists?).and_return(false)
      ::File.stub!(:exists?).with("/usr/local/etc/rc.d/apache22").and_return(true)
      @lines = mock("lines")
      @lines.stub!(:each).and_yield("sshd_enable=\"YES\"").
                          and_yield("apache22_enable=\"YES\"")
      ::File.stub!(:open).and_return(@lines)

    end
  
    it "should create a current resource with the name of the new resource" do
      Chef::Resource::Service.should_receive(:new).and_return(@current_resource)
      @provider.load_current_resource
    end

    it "should set the current resources service name to the new resources service name" do
      @provider.load_current_resource
      @current_resource.service_name.should == @new_resource.service_name
    end

    describe "when the service supports status" do
      before do
        @new_resource.supports({:status => true})
      end

      it "should run '/etc/init.d/service_name status'" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_return(@status)
        @provider.load_current_resource
      end
  
      it "should set running to true if the the status command returns 0" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_return(@status)
        @current_resource.should_receive(:running).with(true)
        @provider.load_current_resource
      end

      it "should set running to false if the status command returns anything except 0" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_raise(Mixlib::ShellOut::ShellCommandFailed)
        @current_resource.should_receive(:running).with(false)
        @provider.load_current_resource
      end
    end

    describe "when a status command has been specified" do
      before do
        @new_resource.status_command("/bin/chefhasmonkeypants status")
      end

      it "should run the services status command if one has been specified" do
        @provider.should_receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(@status)
        @provider.load_current_resource
      end
    
    end

    it "should set running to false if the node has a nil ps attribute" do
      @node.stub!(:[]).with(:command).and_return({:ps => nil})
      lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Service)
    end

    it "should set running to false if the node has an empty ps attribute" do
      @node.stub!(:[]).with(:command).and_return(:ps => "")
      lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Service)
    end

    describe "when we have a 'ps' attribute" do
      before do
        @node[:command] = {:ps => "ps -ax"}
      end

      it "should shell_out! the node's ps command" do
        @provider.should_receive(:shell_out!).with(@node[:command][:ps]).and_return(@status)
        @provider.load_current_resource
      end

      it "should read stdout of the ps command" do
        @provider.stub!(:shell_out!).and_return(@status)
        @stdout.should_receive(:each_line).and_return(true)
        @provider.load_current_resource
      end

      it "should set running to true if the regex matches the output" do
        @stdout.stub!(:each_line).and_yield("555  ??  Ss     0:05.16 /usr/sbin/cron -s").
                                  and_yield(" 9881  ??  Ss     0:06.67 /usr/local/sbin/httpd -DNOHTTPACCEPT")
        @provider.load_current_resource 
        @current_resource.running.should be_true
      end

      it "should set running to false if the regex doesn't match" do
        @provider.stub!(:shell_out!).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_false
      end

      it "should raise an exception if ps fails" do
        @provider.stub!(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed)
        lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Service)
      end
    end

    it "should return the current resource" do
      @provider.load_current_resource.should eql(@current_resource)
    end

    describe "when starting the service" do
      it "should call the start command if one is specified" do
        @new_resource.start_command("/etc/rc.d/chef startyousillysally")
        @provider.should_receive(:shell_out!).with("/etc/rc.d/chef startyousillysally")
        @provider.load_current_resource
        @provider.start_service()
      end

      it "should call '/usr/local/etc/rc.d/service_name faststart' if no start command is specified" do
        @provider.should_receive(:shell_out!).with("/usr/local/etc/rc.d/#{@new_resource.service_name} faststart")
        @provider.load_current_resource
        @provider.start_service()
      end
    end

    describe Chef::Provider::Service::Init, "stop_service" do
      it "should call the stop command if one is specified" do<commit_msg>Fix broken/inaccurate tests in freebsd_service_spec<commit_after>#
# Author:: <NAME> (<EMAIL>)
# Copyright:: Copyright (c) 2009 <NAME>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'

describe Chef::Provider::Service::Freebsd do
  before do
    @node = Chef::Node.new
    @node[:command] = {:ps => "ps -ax"}
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Service.new("apache22")
    @new_resource.pattern("httpd")
    @new_resource.supports({:status => false})

    @current_resource = Chef::Resource::Service.new("apache22")

    @provider = Chef::Provider::Service::Freebsd.new(@new_resource,@run_context)
    Chef::Resource::Service.stub!(:new).and_return(@current_resource)
  end
  
  describe "load_current_resource" do
    before(:each) do
      @stdout = StringIO.new(<<-PS_SAMPLE)
413  ??  Ss     0:02.51 /usr/sbin/syslogd -s
539  ??  Is     0:00.14 /usr/sbin/sshd
545  ??  Ss     0:17.53 sendmail: accepting connections (sendmail)
PS_SAMPLE
      @status = mock(:stdout => @stdout, :exitstatus => 0)
      @provider.stub!(:shell_out!).with(@node[:command][:ps]).and_return(@status)

      ::File.stub!(:exists?).and_return(false)
      ::File.stub!(:exists?).with("/usr/local/etc/rc.d/apache22").and_return(true)
      @lines = mock("lines")
      @lines.stub!(:each).and_yield("sshd_enable=\"YES\"").
                          and_yield("apache22_enable=\"YES\"")
      ::File.stub!(:open).and_return(@lines)

    end
  
    it "should create a current resource with the name of the new resource" do
      Chef::Resource::Service.should_receive(:new).and_return(@current_resource)
      @provider.load_current_resource
    end

    it "should set the current resources service name to the new resources service name" do
      @provider.load_current_resource
      @current_resource.service_name.should == @new_resource.service_name
    end

    describe "when the service supports status" do
      before do
        @new_resource.supports({:status => true})
      end

      it "should run '/etc/init.d/service_name status'" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_return(@status)
        @provider.load_current_resource
      end
  
      it "should set running to true if the the status command returns 0" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_return(@status)
        @current_resource.should_receive(:running).with(true)
        @provider.load_current_resource
      end

      it "should set running to false if the status command returns anything except 0" do
        @provider.should_receive(:shell_out).with("/usr/local/etc/rc.d/apache22 status").and_raise(Mixlib::ShellOut::ShellCommandFailed)
        @current_resource.should_receive(:running).with(false)
        @provider.load_current_resource
      end
    end

    describe "when a status command has been specified" do
      before do
        @new_resource.status_command("/bin/chefhasmonkeypants status")
      end

      it "should run the services status command if one has been specified" do
        @provider.should_receive(:shell_out).with("/bin/chefhasmonkeypants status").and_return(@status)
        @provider.load_current_resource
      end
    
    end

    it "should raise error if the node has a nil ps attribute and no other means to get status" do
      @node[:command] = {:ps => nil}
      @provider.define_resource_requirements
      lambda { @provider.process_resource_requirements(:any) }.should raise_error(Chef::Exceptions::Service)
    end

    it "should raise error if the node has an empty ps attribute and no other means to get status" do
      @node[:command] = {:ps => ""}
      @provider.define_resource_requirements
      lambda { @provider.process_resource_requirements(:any) }.should raise_error(Chef::Exceptions::Service)
    end
    
    describe "when we have a 'ps' attribute" do
      before do
        @node[:command] = {:ps => "ps -ax"}
      end

      it "should shell_out! the node's ps command" do
        @provider.should_receive(:shell_out!).with(@node[:command][:ps]).and_return(@status)
        @provider.load_current_resource
      end

      it "should read stdout of the ps command" do
        @provider.stub!(:shell_out!).and_return(@status)
        @stdout.should_receive(:each_line).and_return(true)
        @provider.load_current_resource
      end

      it "should set running to true if the regex matches the output" do
        @stdout.stub!(:each_line).and_yield("555  ??  Ss     0:05.16 /usr/sbin/cron -s").
                                  and_yield(" 9881  ??  Ss     0:06.67 /usr/local/sbin/httpd -DNOHTTPACCEPT")
        @provider.load_current_resource 
        @current_resource.running.should be_true
      end

      it "should set running to false if the regex doesn't match" do
        @provider.stub!(:shell_out!).and_return(@status)
        @provider.load_current_resource
        @current_resource.running.should be_false
      end

      it "should raise an exception if ps fails" do
        @provider.stub!(:shell_out!).and_raise(Mixlib::ShellOut::ShellCommandFailed)
        @provider.load_current_resource
        @provider.define_resource_requirements
        lambda { @provider.process_resource_requirements(:any) }.should raise_error(Chef::Exceptions::Service)
      end
    end

    it "should return the current resource" do
      @provider.load_current_resource.should eql(@current_resource)
    end

    describe "when starting the service" do
      it "should call the start command if one is specified" do
        @new_resource.start_command("/etc/rc.d/chef startyousillysally")
        @provider.should_receive(:shell_out!).with("/etc/rc.d/chef startyousillysally")
        @provider.load_current_resource
        @provider.start_service()
      end

      it "should call '/usr/local/etc/rc.d/service_name faststart' if no start command is specified" do
        @provider.should_receive(:shell_out!).with("/usr/local/etc/rc.d/#{@new_resource.service_name} faststart")
        @provider.load_current_resource
        @provider.start_service()
      end
    end

    describe Chef::Provider::Service::Init, "stop_service" do
      it "should call the stop command if one is specified" do
{'fraction_non_alphanumeric': 0.11640055919358398, 'fraction_numerical': 0.010006622029284085, 'mean_word_length': 3.331421287444232, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4039852', 'n_tokens_mistral': 4183, 'n_tokens_neox': 3986, 'n_words': 1017}