Skip to content

Commit 870b560

Browse files
committed
rm unneeded mocks and stub better
1 parent 4827b67 commit 870b560

1 file changed

Lines changed: 31 additions & 75 deletions

File tree

chef/spec/unit/provider/service/windows_spec.rb

Lines changed: 31 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,27 +22,17 @@
2222
before(:each) do
2323
@init_command = "sc"
2424
@node = mock("Chef::Node", :null_object => true)
25-
@new_resource = mock("Chef::Resource::Service",
26-
:null_object => true,
27-
:name => "chef",
28-
:service_name => "chef",
29-
:running => false
30-
)
25+
@new_resource = Chef::Resource::Service.new("chef")
3126
@new_resource.stub!(:pattern).and_return("chef")
3227
@new_resource.stub!(:status_command).and_return(false)
3328

34-
@current_resource = mock("Chef::Resource::Service",
35-
:null_object => true,
36-
:name => "chef",
37-
:service_name => "chef",
38-
:running => false
39-
)
29+
@current_resource = Chef::Resource::Service.new("chef")
4030
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
4131
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
4232
IO.stub!(:popen).with("#{@init_command} query #{@new_resource.service_name}").and_return(['','','','4'])
43-
IO.stub!(:popen).with("#{@init_command} qc #{@new_resource.service_name}").and_return(['','','','','2'])
33+
IO.stub!(:popen).with("#{@init_command} qc #{@new_resource.service_name}").and_return(['','','','','2'])
4434
end
45-
35+
4636
it "should create a current resource with the name of the new resource" do
4737
Chef::Resource::Service.should_receive(:new).and_return(@current_resource)
4838
@provider.load_current_resource
@@ -61,125 +51,91 @@
6151

6252
describe Chef::Provider::Service::Windows, "start_service" do
6353
before(:each) do
64-
@new_resource = mock("Chef::Resource::Service",
65-
:null_object => true,
66-
:name => "chef",
67-
:service_name => "chef",
68-
:start_command => "sc start chef",
69-
:running => false
70-
)
71-
@new_resource.stub!(:start_command).and_return(false)
54+
@new_resource = Chef::Resource::Service.new("chef")
55+
@new_resource.start_command "sc start chef"
7256

7357
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
7458
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
7559
end
76-
60+
7761
it "should call the start command if one is specified" do
7862
@new_resource.stub!(:start_command).and_return("#{@new_resource.start_command}")
79-
IO.stub!(:popen).with("#{@new_resource.start_command}").and_return(IO.new(2,'w'))
80-
IO.popen("#{@new_resource.start_command}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","2 START_PENDING\n"])
63+
IO.should_receive(:popen).with("#{@new_resource.start_command}").and_return(StringIO.new("foo\nbar\nbaz\n2 START_PENDING\n"))
8164
@provider.start_service()
8265
end
8366
end
8467

8568
describe Chef::Provider::Service::Windows, "stop_service" do
8669
before(:each) do
8770
@init_command = "sc"
88-
@new_resource = mock("Chef::Resource::Service",
89-
:null_object => true,
90-
:name => "chef",
91-
:service_name => "chef",
92-
:stop_command => "sc stop chef",
93-
:running => false
94-
)
95-
@new_resource.stub!(:stop_command).and_return(false)
71+
@new_resource = Chef::Resource::Service.new("chef")
72+
@new_resource.stop_command "sc stop chef"
9673

9774
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
9875
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
9976
end
10077

10178
it "should call the stop command if one is specified" do
10279
@new_resource.stub!(:stop_command).and_return("#{@new_resource.stop_command}")
103-
IO.stub!(:popen).with("#{@new_resource.stop_command}").and_return(IO.new(2,'w'))
104-
IO.popen("#{@new_resource.stop_command}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","1 STOPPED\n"])
105-
@provider.stop_service()
80+
IO.should_receive(:popen).with("#{@new_resource.stop_command}").and_return(StringIO.new("foo\nbar\nbaz\n1 STOPPED\n"))
81+
@provider.stop_service().should be_true
10682
end
10783

10884
it "should use the built-in command if no stop command is specified" do
10985
@new_resource.stub!(:stop_command).and_return(nil)
11086
IO.stub!(:popen).with("#{@init_command} stop #{@new_resource.service_name}").and_return(IO.new(2,'w'))
111-
IO.popen("#{@init_command} stop #{@new_resource.service_name}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","1 STOPPED\n"])
112-
@provider.stop_service()
87+
IO.popen("#{@init_command} stop #{@new_resource.service_name}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","1 STOPPED\n"])
88+
@provider.stop_service().should be_true
11389
end
11490
end
11591

11692
describe Chef::Provider::Service::Windows, "restart_service" do
11793
before(:each) do
11894
@init_command = "sc"
119-
@new_resource = mock("Chef::Resource::Service",
120-
:null_object => true,
121-
:name => "chef",
122-
:service_name => "chef",
123-
:running => false
124-
)
125-
@new_resource.stub!(:restart_command).and_return(false)
126-
@new_resource.stub!(:supports).and_return({:restart => false})
95+
@new_resource = Chef::Resource::Service.new("chef")
12796

12897
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
12998
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
13099
end
131100

132-
it "should just call stop, then start when the resource doesn't support restart and no restart_command is specified" do
133-
@new_resource.stub!(:restart_command).and_return(nil)
134-
IO.stub!(:popen).with("#{@init_command} stop #{@new_resource.service_name}").and_return(IO.new(2,'w'))
135-
IO.stub!(:popen).with("#{@init_command} start #{@new_resource.service_name}").and_return(IO.new(2,'w'))
136-
IO.popen("#{@init_command} stop #{@new_resource.service_name}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","1 STOPPED\n"])
137-
IO.popen("#{@init_command} start #{@new_resource.service_name}").stub!(:readlines).and_return(["foo\n","bar\n","baz\n","2 START_PENDING\n"])
101+
it "should just call stop, then start when the resource doesn't support restart and no restart_command is specified" do
102+
IO.should_receive(:popen).with("#{@init_command} stop #{@new_resource.service_name}").and_return(StringIO.new("foo\nbar\nbaz\n1 STOPPED\n"))
103+
IO.should_receive(:popen).with("#{@init_command} start #{@new_resource.service_name}").and_return(StringIO.new("foo\nbar\nbaz\n2 START_PENDING\n"))
138104
@provider.restart_service()
139105
end
140106
end
141107

142108
describe Chef::Provider::Service::Windows, "enable_service" do
143109
before(:each) do
144110
@init_command = "sc"
145-
@node = mock("Chef::Node", :null_object => true)
146-
@new_resource = mock("Chef::Resource::Service",
147-
:null_object => true,
148-
:name => "chef",
149-
:service_name => "chef",
150-
:status_command => false,
151-
:running => false,
152-
:enabled => false
153-
)
111+
@node = Chef::Node.new
112+
@new_resource = Chef::Resource::Service.new("chef")
113+
@new_resource.running false
114+
@new_resource.enabled false
154115

155116
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
156117
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
157118
end
158119

159120
it "should enable service and set the startup type" do
160-
@new_resource.stub!(:startup_type).and_return(:automatic)
161-
IO.popen("sc config chef start= automatic").stub!(:readlines).and_return(["SUCCESS"])
162-
@provider.enable_service()
121+
@new_resource.startup_type :automatic
122+
IO.should_receive(:popen).with("sc config chef start= auto").and_return(StringIO.new("SUCCESS"))
123+
@provider.enable_service().should be_true
163124
end
164125
end
165126

166127
describe Chef::Provider::Service::Windows, "disable_service" do
167128
before(:each) do
168-
@node = mock("Chef::Node", :null_object => true)
169-
@new_resource = mock("Chef::Resource::Service",
170-
:null_object => true,
171-
:name => "chef",
172-
:service_name => "chef",
173-
:status_command => false
174-
)
129+
@node = Chef::Node.new
130+
@new_resource = Chef::Resource::Service.new("chef")
175131

176132
@provider = Chef::Provider::Service::Windows.new(@node, @new_resource)
177133
Chef::Resource::Service.stub!(:new).and_return(@current_resource)
178134
end
179135

180136
it "should disable service" do
181-
IO.popen("sc config chef start= disable").stub!(:readlines).and_return(["SUCCESS"])
182-
@provider.disable_service()
137+
IO.should_receive(:popen).with("sc config chef start= disabled").and_return(StringIO.new("SUCCESS"))
138+
@provider.disable_service().should be_true
183139
end
184140
end
185141

0 commit comments

Comments
 (0)