summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/redhat_spec.rb
blob: ed3dd5bfa704cb649804c9681729e26201d79e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# Author:: AJ Christensen (<aj@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper"))
require 'ostruct'

shared_examples_for "define_resource_requirements_common" do
  it "should raise an error if /sbin/chkconfig does not exist" do
    allow(File).to receive(:exists?).with("/sbin/chkconfig").and_return(false)
    allow(@provider).to receive(:shell_out).with("/sbin/service chef status").and_raise(Errno::ENOENT)
    allow(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_raise(Errno::ENOENT)
    @provider.load_current_resource
    @provider.define_resource_requirements
    expect { @provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
  end

  it "should not raise an error if the service exists but is not added to any runlevels" do
    status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "")
    expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status)
    chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "", :stderr => "service chef supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add chef')")
    expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig)
    @provider.load_current_resource
    @provider.define_resource_requirements
    expect { @provider.process_resource_requirements }.not_to raise_error
  end
end

describe "Chef::Provider::Service::Redhat" do

  before(:each) do
    @node = Chef::Node.new
    @node.automatic_attrs[:command] = {:ps => 'foo'}
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Service.new("chef")

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

    @provider = Chef::Provider::Service::Redhat.new(@new_resource, @run_context)
    @provider.action = :start
    allow(Chef::Resource::Service).to receive(:new).and_return(@current_resource)
    allow(File).to receive(:exists?).with("/sbin/chkconfig").and_return(true)
  end

  describe "while not in why run mode" do
    before(:each) do
      Chef::Config[:why_run] = false
    end

    describe "load current resource" do
      it "sets the current enabled status to true if the service is enabled for any run level" do
        status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "")
        expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status)
        chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef    0:off   1:off   2:off   3:off   4:off   5:on  6:off", :stderr => "")
        expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig)
        expect(@provider.instance_variable_get("@service_missing")).to be_false
        @provider.load_current_resource
        expect(@current_resource.enabled).to be_true
      end

      it "sets the current enabled status to false if the regex does not match" do
        status = double("Status", :exitstatus => 0, :stdout => "" , :stderr => "")
        expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status)
        chkconfig = double("Chkconfig", :exitstatus => 0, :stdout => "chef    0:off   1:off   2:off   3:off   4:off   5:off   6:off", :stderr => "")
        expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig)
        expect(@provider.instance_variable_get("@service_missing")).to be_false
        expect(@provider.load_current_resource).to eql(@current_resource)
        expect(@current_resource.enabled).to be_false
      end
    end

    describe "define resource requirements" do
      it_should_behave_like "define_resource_requirements_common"

      context "when the service does not exist" do
        before do
          status = double("Status", :exitstatus => 1, :stdout => "", :stderr => "chef: unrecognized service")
          expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status)
          chkconfig = double("Chkconfig", :existatus=> 1, :stdout => "", :stderr => "error reading information on service chef: No such file or directory")
          expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig)
          @provider.load_current_resource
          @provider.define_resource_requirements
        end

        [ "start", "reload", "restart", "enable" ].each do |action|
          it "should raise an error when the action is #{action}" do
            @provider.action = action
            expect { @provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
          end
        end

        [ "stop", "disable" ].each do |action|
          it "should not raise an error when the action is #{action}" do
            @provider.action = action
            expect { @provider.process_resource_requirements }.not_to raise_error
          end
        end
      end
    end
  end

  describe "while in why run mode" do
    before(:each) do
      Chef::Config[:why_run] = true
    end

    after do
      Chef::Config[:why_run] = false
    end

    describe "define resource requirements" do
      it_should_behave_like "define_resource_requirements_common"

      it "should not raise an error if the service does not exist" do
        status = double("Status", :exitstatus => 1, :stdout => "", :stderr => "chef: unrecognized service")
        expect(@provider).to receive(:shell_out).with("/sbin/service chef status").and_return(status)
        chkconfig = double("Chkconfig", :existatus=> 1, :stdout => "", :stderr => "error reading information on service chef: No such file or directory")
        expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig --list chef", :returns => [0,1]).and_return(chkconfig)
        @provider.load_current_resource
        @provider.define_resource_requirements
        expect { @provider.process_resource_requirements }.not_to raise_error
      end
    end
  end

  describe "enable_service" do
    it "should call chkconfig to add 'service_name'" do
      expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig #{@new_resource.service_name} on")
      @provider.enable_service
    end
  end

  describe "disable_service" do
    it "should call chkconfig to del 'service_name'" do
      expect(@provider).to receive(:shell_out!).with("/sbin/chkconfig #{@new_resource.service_name} off")
      @provider.disable_service
    end
  end

end