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
|
#
# Author:: AJ Christensen (<aj@hjksolutions.com>)
# Copyright:: Copyright 2008-2020, 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::Debian 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::Debian.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
allow(File).to receive(:exist?).with("/etc/init.d/chef").and_return true
end
let(:init_lines) do
[
"### BEGIN INIT INFO",
"# Required-Start: hostname $local_fs",
"# Default-Start: 2 3 4 5",
"# Default-Stop: 0 1 6",
"### END INIT INFO",
]
end
describe "load_current_resource" do
it "ensures /usr/sbin/update-rc.d is available" do
expect(File).to receive(:exists?).with("/usr/sbin/update-rc.d") .and_return(false)
@provider.define_resource_requirements
expect do
@provider.process_resource_requirements
end.to raise_error(Chef::Exceptions::Service)
end
context "when update-rc.d shows init linked to rc*.d/" do
before do
allow(@provider).to receive(:assert_update_rcd_available)
allow(File).to receive(:readlines).with("/etc/init.d/chef").and_return(init_lines)
[0, 1, 6].each do |stop|
allow(Dir).to receive(:glob).with("/etc/rc#{stop}.d/[SK][0-9][0-9]chef").and_return(["/etc/rc#{stop}.d/K20chef"])
end
[2, 3, 4, 5].each do |start|
allow(Dir).to receive(:glob).with("/etc/rc#{start}.d/[SK][0-9][0-9]chef").and_return(["/etc/rc#{start}.d/S20chef"])
end
end
it "says the service is enabled" do
expect(@provider.service_currently_enabled?(@provider.get_priority)).to be_truthy
end
it "stores the 'enabled' state" do
allow(Chef::Resource::Service).to receive(:new).and_return(@current_resource)
expect(@provider.load_current_resource).to equal(@current_resource)
expect(@current_resource.enabled).to be_truthy
end
it "stores the start/stop priorities of the service" do
@provider.load_current_resource
expect(@provider.current_resource.priority).to eq(
{
"2" => [:start, "20"],
"3" => [:start, "20"],
"4" => [:start, "20"],
"5" => [:start, "20"],
"0" => [:stop, "20"],
"1" => [:stop, "20"],
"6" => [:stop, "20"],
}
)
end
end
context "when update-rc.d shows init isn't linked to rc*.d/" do
before do
allow(@provider).to receive(:assert_update_rcd_available)
allow(File).to receive(:readlines).with("/etc/init.d/chef").and_return(init_lines)
[0, 1, 6].each do |stop|
allow(Dir).to receive(:glob).with("/etc/rc#{stop}.d/[SK][0-9][0-9]chef").and_return([])
end
[2, 3, 4, 5].each do |start|
allow(Dir).to receive(:glob).with("/etc/rc#{start}.d/[SK][0-9][0-9]chef").and_return([])
end
end
it "says the service is disabled" do
expect(@provider.service_currently_enabled?(@provider.get_priority)).to be_falsey
end
it "stores the 'disabled' state" do
allow(Chef::Resource::Service).to receive(:new).and_return(@current_resource)
expect(@provider.load_current_resource).to equal(@current_resource)
expect(@current_resource.enabled).to be_falsey
end
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
def expect_commands(provider, commands)
commands.each do |command|
expect(provider).to receive(:shell_out!).with(command)
end
end
describe "enable_service" do
let(:service_name) { @new_resource.service_name }
context "when the service doesn't set a priority" do
it "assumes default priority 20 and calls update-rc.d remove => defaults 20 80" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults 20 80",
])
@provider.enable_service
end
end
context "when the service sets a simple priority 75" do
before do
@new_resource.priority(75)
end
it "calls update-rc.d remove => defaults 75 25" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults 75 25",
])
@provider.enable_service
end
end
context "when the service sets complex priorities using Hash" do
before do
@new_resource.priority(2 => [:start, 20], 3 => [:stop, 55])
end
it "calls update-rc.d remove => defaults => enable|disable <runlevel>" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults",
"/usr/sbin/update-rc.d #{service_name} enable 2",
"/usr/sbin/update-rc.d #{service_name} disable 3",
])
@provider.enable_service
end
end
end
describe "disable_service" do
let(:service_name) { @new_resource.service_name }
context "when the service doesn't set a priority" do
it "calls update-rc.d remove => defaults => disable" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults",
"/usr/sbin/update-rc.d #{service_name} disable",
])
@provider.disable_service
end
end
context "when the service sets a simple priority 75" do
before do
@new_resource.priority(75)
end
it "ignores priority and calls update-rc.d remove => defaults => disable" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults",
"/usr/sbin/update-rc.d #{service_name} disable",
])
@provider.disable_service
end
end
context "when the service sets complex priorities using Hash" do
before do
@new_resource.priority(2 => [:start, 20], 3 => [:stop, 55])
end
it "ignores priority and calls update-rc.d remove => defaults => enable|disable <runlevel>" do
expect_commands(@provider, [
"/usr/sbin/update-rc.d -f #{service_name} remove",
"/usr/sbin/update-rc.d #{service_name} defaults",
"/usr/sbin/update-rc.d #{service_name} enable 2",
"/usr/sbin/update-rc.d #{service_name} disable 3",
])
@provider.disable_service
end
end
end
end
|