summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/gentoo_service_spec.rb
blob: efab722ac8f54ab78a042acc6cf16beee4906e75 (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
#
# Author:: Lee Jensen (<ljensen@engineyard.com>)
# Author:: AJ Christensen (<aj@chef.io>)
# Copyright:: Copyright (c) Chef Software Inc.
# 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::Gentoo do
  before(:each) do
    @node = Chef::Node.new
    @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::Gentoo.new(@new_resource, @run_context)
    allow(Chef::Resource::Service).to receive(:new).and_return(@current_resource)
    @status = double("Status", exitstatus: 0, stdout: @stdout)
    allow(@provider).to receive(:shell_out).and_return(@status)
    allow(File).to receive(:exist?).with("/etc/init.d/chef").and_return(true)
    allow(File).to receive(:exist?).with("/sbin/rc-update").and_return(true)
    allow(File).to receive(:exist?).with("/etc/runlevels/default/chef").and_return(false)
    allow(File).to receive(:readable?).with("/etc/runlevels/default/chef").and_return(false)
  end
  # new test: found_enabled state
  #
  describe "load_current_resource" do
    it "should raise Chef::Exceptions::Service if /sbin/rc-update does not exist" do
      expect(File).to receive(:exist?).with("/sbin/rc-update").and_return(false)
      @provider.action = :start
      @provider.define_resource_requirements
      expect { @provider.process_resource_requirements }.to raise_error(Chef::Exceptions::Service)
    end

    it "should track when service file is not found in /etc/runlevels" do
      @provider.load_current_resource
      expect(@provider.instance_variable_get("@found_script")).to be_falsey
    end

    it "should track when service file is found in /etc/runlevels/**/" do
      allow(Dir).to receive(:glob).with("/etc/runlevels/**/chef").and_return(["/etc/runlevels/default/chef"])
      @provider.load_current_resource
      expect(@provider.instance_variable_get("@found_script")).to be_truthy
    end

    describe "when detecting the service enable state" do
      describe "and the glob returns a default service script file" do
        before do
          allow(Dir).to receive(:glob).with("/etc/runlevels/**/chef").and_return(["/etc/runlevels/default/chef"])
        end

        describe "and the file exists and is readable" do
          before do
            allow(File).to receive(:exist?).with("/etc/runlevels/default/chef").and_return(true)
            allow(File).to receive(:readable?).with("/etc/runlevels/default/chef").and_return(true)
          end
          it "should set enabled to true" do
            @provider.load_current_resource
            expect(@current_resource.enabled).to be_truthy
          end
        end

        describe "and the file exists but is not readable" do
          before do
            allow(File).to receive(:exist?).with("/etc/runlevels/default/chef").and_return(true)
            allow(File).to receive(:readable?).with("/etc/runlevels/default/chef").and_return(false)
          end

          it "should set enabled to false" do
            @provider.load_current_resource
            expect(@current_resource.enabled).to be_falsey
          end
        end

        describe "and the file does not exist" do
          before do
            allow(File).to receive(:exist?).with("/etc/runlevels/default/chef").and_return(false)
            allow(File).to receive(:readable?).with("/etc/runlevels/default/chef").and_return("foobarbaz")
          end

          it "should set enabled to false" do
            @provider.load_current_resource
            expect(@current_resource.enabled).to be_falsey
          end

        end
      end

    end

    it "should return the current_resource" do
      expect(@provider.load_current_resource).to eq(@current_resource)
    end

    it "should support the status command automatically" do
      @provider.load_current_resource
      expect(@provider.supports[:status]).to be true
    end

    it "should support the restart command automatically" do
      @provider.load_current_resource
      expect(@provider.supports[:restart]).to be true
    end

    it "should not support the reload command automatically" do
      @provider.load_current_resource
      expect(@provider.supports[:reload]).to be_falsey
    end

  end

  describe "action_methods" do
    before(:each) { allow(@provider).to receive(:load_current_resource).and_return(@current_resource) }

    describe Chef::Provider::Service::Gentoo, "enable_service" do
      it "should call rc-update add *service* default" do
        expect(@provider).to receive(:shell_out!).with("/sbin/rc-update add chef default")
        @provider.enable_service
      end
    end

    describe Chef::Provider::Service::Gentoo, "disable_service" do
      it "should call rc-update del *service* default" do
        expect(@provider).to receive(:shell_out!).with("/sbin/rc-update del chef default")
        @provider.disable_service
      end
    end
  end

end