summaryrefslogtreecommitdiff
path: root/spec/unit/provider/service/aixinit_service_spec.rb
blob: d47bebd0d3601f15c181f84d7669c1c9ca36e5ec (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#
# Author:: kaustubh (<kaustubh@clogeny.com>)
# 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::AixInit do
  before(:each) do
    @node = Chef::Node.new
    @node.automatic_attrs[:command] = { ps: "fuuuu" }
    @events = Chef::EventDispatch::Dispatcher.new
    @run_context = Chef::RunContext.new(@node, {}, @events)

    @new_resource = Chef::Resource::Service.new("chef")
    @provider = Chef::Provider::Service::AixInit.new(@new_resource, @run_context)

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

    @pid, @stdin, @stdout, @stderr = nil, nil, nil, nil
  end

  describe "load_current_resource" do
    it "sets current resource properties" do
      expect(@provider).to receive(:set_current_resource_attributes)

      @provider.load_current_resource
    end
  end

  describe "action_enable" do
    shared_examples_for "the service is up to date" do
      it "does not enable the service" do
        expect(@provider).not_to receive(:enable_service)
        @provider.action_enable
        @provider.set_updated_status
        expect(@provider.new_resource).not_to be_updated
      end
    end

    shared_examples_for "the service is not up to date" do
      it "enables the service and sets the resource as updated" do
        expect(@provider).to receive(:enable_service).and_return(true)
        @provider.action_enable
        @provider.set_updated_status
        expect(@provider.new_resource).to be_updated
      end
    end

    context "when the service is disabled" do
      before do
        @current_resource.enabled(false)
      end

      it_behaves_like "the service is not up to date"
    end

    context "when the service is enabled" do
      before do
        @current_resource.enabled(true)
        @current_resource.priority(80)
      end

      context "and the service sets no priority" do
        it_behaves_like "the service is up to date"
      end

      context "and the service requests the same priority as is set" do
        before do
          @new_resource.priority(80)
        end
        it_behaves_like "the service is up to date"
      end

      context "and the service requests a different priority than is set" do
        before do
          @new_resource.priority(20)
        end
        it_behaves_like "the service is not up to date"
      end
    end
  end

  describe "enable_service" do
    before do
      allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]#{@new_resource.service_name}"]).and_return([])
    end

    context "when the service doesn't set a priority" do
      it "creates symlink with status S" do
        expect(@provider).to receive(:create_symlink).with(2, "S", "")

        @provider.enable_service
      end
    end

    context "when the service sets a simple priority (integer)" do
      before do
        @new_resource.priority(75)
      end

      it "creates a symlink with status S and a priority" do
        expect(@provider).to receive(:create_symlink).with(2, "S", 75)

        @provider.enable_service
      end
    end

    context "when the service sets complex priorities (hash)" do
      before do
        priority = { 2 => [:start, 20], 3 => [:stop, 10] }
        @new_resource.priority(priority)
      end

      it "create symlink with status start (S) or stop (K) and a priority " do
        expect(@provider).to receive(:create_symlink).with(2, "S", 20)
        expect(@provider).to receive(:create_symlink).with(3, "K", 10)

        @provider.enable_service
      end
    end
  end

  describe "disable_service" do
    before do
      allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]#{@new_resource.service_name}"]).and_return([])
    end

    context "when the service doesn't set a priority" do
      it "creates symlinks with status stop (K)" do
        expect(@provider).to receive(:create_symlink).with(2, "K", "")

        @provider.disable_service
      end
    end

    context "when the service sets a simple priority (integer)" do
      before do
        @new_resource.priority(75)
      end

      it "create symlink with status stop (k) and a priority " do
        expect(@provider).to receive(:create_symlink).with(2, "K", 25)

        @provider.disable_service
      end
    end

    context "when the service sets complex priorities (hash)" do
      before do
        @priority = { 2 => [:start, 20], 3 => [:stop, 10] }
        @new_resource.priority(@priority)
      end

      it "create symlink with status stop (k) and a priority " do
        expect(@provider).to receive(:create_symlink).with(3, "K", 90)

        @provider.disable_service
      end
    end
  end

  describe "set_current_resource_attributes" do
    context "when rc2.d contains only start script" do
      before do
        files = ["/etc/rc.d/rc2.d/S20apache"]

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]chef"]).and_return(files)
      end

      it "the service is enabled" do
        expect(@provider.current_resource).to receive(:enabled).with(true)
        expect(@provider.current_resource).to receive(:priority).with(20)

        @provider.set_current_resource_attributes
      end
    end

    context "when rc2.d contains only stop script" do
      before do
        files = ["/etc/rc.d/rc2.d/K20apache"]
        @priority = { 2 => [:stop, 20] }

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]chef"]).and_return(files)
      end
      it "the service is not enabled" do
        expect(@provider.current_resource).to receive(:enabled).with(false)
        expect(@provider.current_resource).to receive(:priority).with(@priority)

        @provider.set_current_resource_attributes
      end
    end

    context "when rc2.d contains both start and stop scripts" do
      before do
        @files = ["/etc/rc.d/rc2.d/S20apache", "/etc/rc.d/rc2.d/K80apache"]
        # FIXME: this is clearly buggy the duplicated keys do not work
        # @priority = {2 => [:start, 20], 2 => [:stop, 80]}
        @priority = { 2 => [:stop, 80] }

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]chef"]).and_return(@files)
      end
      it "the service is enabled" do
        expect(@current_resource).to receive(:enabled).with(true)
        expect(@current_resource).to receive(:priority).with(@priority)

        @provider.set_current_resource_attributes
      end
    end

    context "when rc2.d contains only start script (without priority)" do
      before do
        files = ["/etc/rc.d/rc2.d/Sapache"]

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]#{@new_resource.service_name}"]).and_return(files)
      end

      it "the service is enabled" do
        expect(@provider.current_resource).to receive(:enabled).with(true)
        expect(@provider.current_resource).to receive(:priority).with("")

        @provider.set_current_resource_attributes
      end
    end

    context "when rc2.d contains only stop script (without priority)" do
      before do
        files = ["/etc/rc.d/rc2.d/Kapache"]
        @priority = { 2 => [:stop, ""] }

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]#{@new_resource.service_name}"]).and_return(files)
      end
      it "the service is not enabled" do
        expect(@provider.current_resource).to receive(:enabled).with(false)
        expect(@provider.current_resource).to receive(:priority).with(@priority)

        @provider.set_current_resource_attributes
      end
    end

    context "when rc2.d contains both start and stop scripts" do
      before do
        files = ["/etc/rc.d/rc2.d/Sapache", "/etc/rc.d/rc2.d/Kapache"]
        # FIXME: this is clearly buggy the duplicated keys do not work
        # @priority = {2 => [:start, ''], 2 => [:stop, '']}
        @priority = { 2 => [:stop, ""] }

        allow(Dir).to receive(:glob).with(["/etc/rc.d/rc2.d/[SK][0-9][0-9]#{@new_resource.service_name}", "/etc/rc.d/rc2.d/[SK]#{@new_resource.service_name}"]).and_return(files)
      end
      it "the service is enabled" do
        expect(@current_resource).to receive(:enabled).with(true)
        expect(@current_resource).to receive(:priority).with(@priority)

        @provider.set_current_resource_attributes
      end
    end
  end
end