nginx_test_helper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe NginxTestHelper::Config do
4
+ let(:configuration) { {} }
5
+ let(:subject) { NginxTestHelper::Config.new("config_id", configuration) }
6
+
7
+ it "should include NginxTestHelper module" do
8
+ subject.class.included_modules.should include(NginxTestHelper::EnvMethods)
9
+ end
10
+
11
+ it "should define some keys with configuration filename, logs and temp dirs configurations" do
12
+ default_configuration = NginxTestHelper::Config.new("config_id", {}).configuration
13
+ default_configuration[:configuration_filename].should eql("/tmp/nginx_tests/config_id.conf")
14
+ default_configuration[:pid_file].should eql("/tmp/nginx_tests/nginx.pid")
15
+ default_configuration[:access_log].should eql("/tmp/nginx_tests/logs/access-config_id.log")
16
+ default_configuration[:error_log].should eql("/tmp/nginx_tests/logs/error-config_id.log")
17
+ default_configuration[:client_body_temp].should eql("/tmp/nginx_tests/client_body_temp")
18
+ end
19
+
20
+ it "should merge 'default configuration' with configuration filename, logs and temp dirs configurations" do
21
+ default_configuration = NginxTestHelper::Config.new("config_id", {}).configuration
22
+ default_configuration.keys.should eql([:conf_item, :configuration_filename, :pid_file, :access_log, :error_log, :client_body_temp])
23
+ end
24
+
25
+ it "should merge given configuration with 'default configuration', configuration filename, logs and temp dirs configurations" do
26
+ default_configuration = NginxTestHelper::Config.new("config_id", {:custom_key => "value"}).configuration
27
+ default_configuration.keys.should eql([:conf_item, :custom_key, :configuration_filename, :pid_file, :access_log, :error_log, :client_body_temp])
28
+ end
29
+
30
+ it "should create dirs where logs and client_body_temp will be stored" do
31
+ NginxTestHelper::Config.new("config_id", {})
32
+ File.exists?("/tmp/nginx_tests/logs").should be_true
33
+ File.exists?("/tmp/nginx_tests/client_body_temp").should be_true
34
+ end
35
+
36
+ it "should create the configuration file using the template configuration" do
37
+ File.read(subject.configuration[:configuration_filename]).should eql('"template configuration config_id"')
38
+ end
39
+
40
+ context "when using an alternative template" do
41
+ let(:configuration) { {:foo => "bar", :configuration_template => %("custom template writing <%=configuration[:foo]%>")} }
42
+
43
+ it "should create the configuration file using this template" do
44
+ File.read(subject.configuration[:configuration_filename]).should eql('"custom template writing bar"')
45
+ end
46
+ end
47
+
48
+ it "should has a method to delete config and log files" do
49
+ NginxTestHelper::Config.delete_config_and_log_files(subject.config_id)
50
+ File.exists?(subject.configuration[:configuration_filename]).should be_false
51
+ File.exists?(subject.configuration[:access_log]).should be_false
52
+ File.exists?(subject.configuration[:error_log]).should be_false
53
+ File.exists?(subject.configuration[:client_body_temp]).should be_false
54
+ end
55
+
56
+ it "should expose configurations keys as methods" do
57
+ config = NginxTestHelper::Config.new("config_id", { :foo => "bar", :xyz => 1})
58
+ config.foo.should eql("bar")
59
+ config.xyz.should eql(1)
60
+ end
61
+
62
+ it "should raise exception if configuration key does not exists" do
63
+ config = NginxTestHelper::Config.new("config_id", { :foo => "bar"})
64
+ expect { config.xyz }.to raise_error(NameError, "undefined local variable, method or configuration 'xyz'")
65
+ end
66
+
67
+ it "should return 'true' to respond_to method for a configuration key" do
68
+ config = NginxTestHelper::Config.new("config_id", { :foo => "bar"})
69
+ config.should respond_to("config_id")
70
+ config.should respond_to(:foo)
71
+ end
72
+
73
+ context "when using the write_directive helper method" do
74
+ let(:configuration) { {:foo => "bar", :xyz => nil, :configuration_template => %(\n<%= write_directive('name', foo, 'comment foo') %>\n<%= write_directive('without_comment', foo) %>\n<%= write_directive('xyz', xyz, 'comment xyz') %>\n<%= write_directive('xyz_without_comment', xyz) %>)} }
75
+
76
+ it "should comment the line when value is nil" do
77
+ conf = File.read(subject.configuration[:configuration_filename])
78
+ conf.should include(%(\n#comment xyz\n#xyz "";))
79
+ conf.should include(%(\n#xyz_without_comment "";))
80
+ end
81
+
82
+ it "should write the line when value is not nil" do
83
+ conf = File.read(subject.configuration[:configuration_filename])
84
+ conf.should include(%(\n#comment foo\nname "bar";))
85
+ conf.should include(%(\nwithout_comment "bar";))
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ class SomeClass
4
+ include NginxTestHelper::EnvMethods
5
+ end
6
+
7
+ describe NginxTestHelper::EnvMethods do
8
+ shared_examples_for "has nginx configuration methods" do
9
+ context "with default values" do
10
+ it "should return nginx executable" do
11
+ subject.nginx_executable.should eql("/usr/local/nginx/sbin/nginx")
12
+ end
13
+
14
+ it "should return nginx host" do
15
+ subject.nginx_host.should eql("127.0.0.1")
16
+ end
17
+
18
+ it "should return nginx port" do
19
+ subject.nginx_port.should eql("9990")
20
+ end
21
+
22
+ it "should return nginx workers" do
23
+ subject.nginx_workers.should eql("1")
24
+ end
25
+
26
+ it "should return nginx tests tmp dir" do
27
+ subject.nginx_tests_tmp_dir.should eql("/tmp/nginx_tests")
28
+ end
29
+ end
30
+
31
+ context "with environment values" do
32
+ it "should return nginx executable" do
33
+ ENV['NGINX_EXEC'] = "/path/to/nginx/executable"
34
+ subject.nginx_executable.should eql("/path/to/nginx/executable")
35
+ end
36
+
37
+ it "should return nginx host" do
38
+ ENV['NGINX_HOST'] = "some_host"
39
+ subject.nginx_host.should eql("some_host")
40
+ end
41
+
42
+ it "should return nginx port" do
43
+ ENV['NGINX_PORT'] = "some_port"
44
+ subject.nginx_port.should eql("some_port")
45
+ end
46
+
47
+ it "should return nginx workers" do
48
+ ENV['NGINX_WORKERS'] = "25"
49
+ subject.nginx_workers.should eql("25")
50
+ end
51
+
52
+ it "should return nginx tests tmp dir" do
53
+ ENV['NGINX_TESTS_TMP_DIR'] = "/path/to/tests/tmp/dir"
54
+ subject.nginx_tests_tmp_dir.should eql("/path/to/tests/tmp/dir")
55
+ end
56
+ end
57
+
58
+ it "should use nginx host and port to compose nginx address" do
59
+ subject.stub(:nginx_host).and_return("some_host")
60
+ subject.stub(:nginx_port).and_return("some_port")
61
+
62
+ subject.nginx_address.should eql("http://some_host:some_port")
63
+ end
64
+ end
65
+
66
+ context "when a class include the NginxTestHelper::EnvMethods module" do
67
+ let(:subject) { SomeClass }
68
+
69
+ it_should_behave_like "has nginx configuration methods"
70
+
71
+ context "their object instances" do
72
+ let(:subject) { SomeClass.new }
73
+
74
+ it_should_behave_like "has nginx configuration methods"
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe "rspec_utils" do
4
+
5
+ it "should include NginxTestHelper module on RSpec tests" do
6
+ self.class.included_modules.should include(NginxTestHelper)
7
+ end
8
+
9
+ context "when defining the 'be_in_the_interval' matcher" do
10
+ context "and checking if the number is in the interval" do
11
+ it "should be true when the actual value is equal to lower bound" do
12
+ expect { 10.should be_in_the_interval(10,12) }.to_not raise_error
13
+ end
14
+
15
+ it "should be true when the actual value is equal to upper bound" do
16
+ expect { 12.should be_in_the_interval(10,12) }.to_not raise_error
17
+ end
18
+
19
+ it "should be true when the actual value is between lower and upper bounds" do
20
+ expect { 11.should be_in_the_interval(10,12) }.to_not raise_error
21
+ end
22
+
23
+ it "should be false when the actual value is smaller than lower bound" do
24
+ expect { 9.should be_in_the_interval(10,12) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 9 would be in the interval from 10 to 12")
25
+ end
26
+
27
+ it "should be false when the actual value is greater than upper bound" do
28
+ expect { 13.should be_in_the_interval(10,12) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 13 would be in the interval from 10 to 12")
29
+ end
30
+
31
+ it "should accept float numbers" do
32
+ expect { 10.4.should be_in_the_interval(10.3,12.5) }.to_not raise_error
33
+ expect { 12.4.should be_in_the_interval(10.3,12.5) }.to_not raise_error
34
+ end
35
+ end
36
+
37
+ context "and checking if the number is out the interval" do
38
+ it "should be false when the actual value is equal to lower bound" do
39
+ expect { 10.should_not be_in_the_interval(10,12) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 10 would not be in the interval from 10 to 12")
40
+ end
41
+
42
+ it "should be false when the actual value is equal to upper bound" do
43
+ expect { 12.should_not be_in_the_interval(10,12) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 12 would not be in the interval from 10 to 12")
44
+ end
45
+
46
+ it "should be false when the actual value is between lower and upper bounds" do
47
+ expect { 11.should_not be_in_the_interval(10,12) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 11 would not be in the interval from 10 to 12")
48
+ end
49
+
50
+ it "should be true when the actual value is smaller than lower bound" do
51
+ expect { 9.should_not be_in_the_interval(10,12) }.to_not raise_error
52
+ end
53
+
54
+ it "should be true when the actual value is greater than upper bound" do
55
+ expect { 13.should_not be_in_the_interval(10,12) }.to_not raise_error
56
+ end
57
+
58
+ it "should accept float numbers" do
59
+ expect { 10.2.should_not be_in_the_interval(10.3,12.5) }.to_not raise_error
60
+ expect { 12.6.should_not be_in_the_interval(10.3,12.5) }.to_not raise_error
61
+ end
62
+ end
63
+
64
+ it "should has a friendly description" do
65
+ be_in_the_interval(10.3,12.5).description.should eql("be in the interval from 10.3 to 12.5")
66
+ end
67
+ end
68
+
69
+ context "when defining the 'match_the_pattern' matcher" do
70
+ context "and checking if the text match the pattern" do
71
+ it "should be true when the actual value match the expression" do
72
+ expect { "some text".should match_the_pattern(/EX/i) }.to_not raise_error
73
+ end
74
+
75
+ it "should be false when the actual value does not match the expression" do
76
+ expect { "some text".should match_the_pattern(/EX/) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 'some text' would match the pattern '/EX/'")
77
+ end
78
+ end
79
+
80
+ context "and checking if the text not match the pattern" do
81
+ it "should be true when the actual value match the expression" do
82
+ expect { "some text".should_not match_the_pattern(/EX/i) }.to raise_error(RSpec::Expectations::ExpectationNotMetError, "expected that 'some text' would not match the pattern '/EX/i'")
83
+ end
84
+
85
+ it "should be false when the actual value does not match the expression" do
86
+ expect { "some text".should_not match_the_pattern(/EX/) }.to_not raise_error
87
+ end
88
+ end
89
+
90
+ it "should has a friendly description" do
91
+ match_the_pattern(/EX/i).description.should eql("match the pattern '/EX/i'")
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,320 @@
1
+ require 'spec_helper'
2
+
3
+ describe NginxTestHelper do
4
+ include NginxTestHelper
5
+
6
+ it "should define a method with basic headers" do
7
+ headers.should eql({'accept' => 'text/html'})
8
+ end
9
+
10
+ it "should define a method to calculate the difference in seconds of two dates" do
11
+ time_diff_sec(Time.now, Time.now + 10).should eql(10)
12
+ end
13
+
14
+ it "should define a method to calculate the difference in milliseconds of two dates" do
15
+ time_diff_milli(Time.now, Time.now + 10).should eql(10000)
16
+ end
17
+
18
+ context "when working with sockets" do
19
+ let(:socket) { SocketMock.new }
20
+
21
+ it "should be possible to open a socket to a host and port" do
22
+ TCPSocket.should_receive(:open).with("xpto.com", 100).and_return(socket)
23
+
24
+ open_socket("xpto.com", 100).should eql(socket)
25
+ end
26
+
27
+ it "should be possible to do a GET in an url using the opened socket, and receive header and body response" do
28
+ socket.should_receive(:print).with("GET /index.html HTTP/1.0\r\n\r\n")
29
+
30
+ headers, body = get_in_socket("/index.html", socket)
31
+ headers.should eql("HTTP 200 OK")
32
+ body.should eql("BODY")
33
+ end
34
+
35
+ it "should pass 'wait_for' attribute to 'read_response_on_socket' method when doing a GET in a url" do
36
+ socket.should_receive(:print).with("GET /index.html HTTP/1.0\r\n\r\n")
37
+ self.should_receive(:read_response_on_socket).with(socket, "wait for")
38
+
39
+ get_in_socket("/index.html", socket, "wait for")
40
+ end
41
+
42
+ it "should be possible to do a POST in an url using the opened socket, and receive header and body response" do
43
+ socket.should_receive(:print).with("POST /service HTTP/1.0\r\nContent-Length: 4\r\n\r\nBODY")
44
+
45
+ headers, body = post_in_socket("/service", "BODY", socket)
46
+ headers.should eql("HTTP 200 OK")
47
+ body.should eql("BODY")
48
+ end
49
+
50
+ it "should pass 'wait_for' attribute to 'read_response_on_socket' method when doing a POST in a url" do
51
+ socket.should_receive(:print).with("POST /service HTTP/1.0\r\nContent-Length: 4\r\n\r\nBODY")
52
+ self.should_receive(:read_response_on_socket).with(socket, "wait for")
53
+
54
+ headers, body = post_in_socket("/service", "BODY", socket, "wait for")
55
+ end
56
+
57
+ it "should be possible read a response in a opened socket" do
58
+ headers, body = read_response_on_socket(socket)
59
+ headers.should eql("HTTP 200 OK")
60
+ body.should eql("BODY")
61
+ end
62
+
63
+ it "should concatenate response parts" do
64
+ socket.response1 = "X"
65
+ socket.response2 = "Y"
66
+
67
+ headers, body = read_response_on_socket(socket)
68
+ headers.should eql("HTTP 200 OK")
69
+ body.should eql("BODYXY")
70
+ end
71
+
72
+ it "should raise error if not receive a response" do
73
+ socket.stub!(:readpartial).and_raise(Exception)
74
+
75
+ expect { read_response_on_socket(socket) }.to raise_error("Any response")
76
+ end
77
+
78
+ context "and receive a Errno::EAGAIN" do
79
+ context "and not give a text to wait for" do
80
+ it "should return what was received until the error happens" do
81
+ socket.response1 = "X"
82
+ socket.response3 = "Z"
83
+ socket.exception = Errno::EAGAIN
84
+
85
+ headers, body = read_response_on_socket(socket)
86
+ headers.should eql("HTTP 200 OK")
87
+ body.should eql("BODYX")
88
+ end
89
+ end
90
+
91
+ context "and give a text to wait for" do
92
+ it "should check if a text is present in the response" do
93
+ socket.exception = Errno::EAGAIN
94
+
95
+ headers, body = read_response_on_socket(socket, "OD")
96
+ headers.should eql("HTTP 200 OK")
97
+ body.should eql("BODY")
98
+ end
99
+
100
+ it "should retry if the text is not present in the response" do
101
+ socket.exception = Errno::EAGAIN
102
+ socket.response3 = "Z"
103
+
104
+ IO.should_receive(:select).with([socket]).any_number_of_times
105
+
106
+ headers, body = read_response_on_socket(socket, "Z")
107
+ headers.should eql("HTTP 200 OK")
108
+ body.should include("BODY")
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ context "when testing configuration" do
115
+ before do
116
+ self.stub!(:config_id).and_return("config_id")
117
+ self.stub!(:start_server).and_return("Server started")
118
+ self.stub!(:stop_server).and_return("Server stoped")
119
+ end
120
+
121
+ it "should create an instance of NginxTestHelper::Config with the given configuation" do
122
+ config = NginxTestHelper::Config.new("config_id", {:foo => "bar"})
123
+ NginxTestHelper::Config.should_receive(:new).with("config_id", {:foo => "bar"}).and_return(config)
124
+ nginx_test_configuration({:foo => "bar"})
125
+ end
126
+
127
+ it "should accept test default configuration" do
128
+ config = NginxTestHelper::Config.new("config_id", {})
129
+ NginxTestHelper::Config.should_receive(:new).with("config_id", {}).and_return(config)
130
+ nginx_test_configuration
131
+ end
132
+
133
+ it "should call start_server and stop_server methods" do
134
+ self.should_receive(:start_server).and_return("Server started")
135
+ self.should_receive(:stop_server).and_return("Server stoped")
136
+ nginx_test_configuration({:foo => "bar"})
137
+ end
138
+
139
+ it "should return start command result" do
140
+ nginx_test_configuration({:foo => "bar"}).should eql("Server started\n")
141
+ end
142
+
143
+ it "should return start command result concatenated with error log content if exists" do
144
+ FileUtils.mkdir_p("/tmp/nginx_tests/logs/")
145
+ File.open("/tmp/nginx_tests/logs/error-config_id.log", "w") { |f| f.write("Error log content") }
146
+ nginx_test_configuration({:foo => "bar"}).should eql("Server started\nError log content")
147
+ end
148
+ end
149
+
150
+ context "when starting server to make tests" do
151
+ before do
152
+ self.stub!(:config_id).and_return("config_id")
153
+ self.stub!(:start_server).and_return("Server started")
154
+ self.stub!(:stop_server).and_return("Server stoped")
155
+ end
156
+
157
+ it "should create an instance of NginxTestHelper::Config with the given configuation" do
158
+ config = NginxTestHelper::Config.new("config_id", {:foo => "bar"})
159
+ NginxTestHelper::Config.should_receive(:new).with("config_id", {:foo => "bar"}).and_return(config)
160
+ nginx_run_server({:foo => "bar"}) {}
161
+ end
162
+
163
+ it "should accept test default configuration" do
164
+ config = NginxTestHelper::Config.new("config_id", {})
165
+ NginxTestHelper::Config.should_receive(:new).with("config_id", {}).and_return(config)
166
+ nginx_run_server {}
167
+ end
168
+
169
+ it "should execute the block after start_server and before stop_server methods" do
170
+ obj = {:xyz => 1}
171
+ self.should_receive(:start_server).ordered
172
+ obj.should_receive(:delete).with(:xyz).ordered
173
+ self.should_receive(:stop_server).ordered
174
+
175
+ nginx_run_server({:foo => "bar"}) { obj.delete(:xyz) }
176
+ end
177
+
178
+ it "should execute the block inside a timeout block" do
179
+ expect { nginx_run_server({:foo => "bar"}) { sleep 6 } }.to raise_error(Timeout::Error, "execution expired")
180
+ end
181
+
182
+ it "should accept a custom a timeout" do
183
+ expect { nginx_run_server({:foo => "bar"}, {:timeout => 2}) { sleep 6 } }.to raise_error(Timeout::Error, "execution expired")
184
+ expect { nginx_run_server({:foo => "bar"}, {:timeout => 2}) { sleep 1 } }.to_not raise_error(Timeout::Error, "execution expired")
185
+ end
186
+
187
+ it "should execute stop_server method if an exception was raised" do
188
+ self.should_receive(:stop_server)
189
+ expect { nginx_run_server({:foo => "bar"}) { raise "some error" } }.to raise_error("some error")
190
+ end
191
+ end
192
+
193
+ context "when checking internal behavior" do
194
+ before do
195
+ self.stub!(:start_server).and_return("Server started")
196
+ self.stub!(:stop_server).and_return("Server stoped")
197
+ end
198
+
199
+ context "and check config_id value" do
200
+ it "should use example metadata if available" do
201
+ self.example.stub!(:metadata).and_return(:location => "./spec/test_config_id_spec.rb:100")
202
+ self.send(:config_id).should eql("test_config_id_spec_rb_100")
203
+ end
204
+
205
+ it "should use method_name if example metadata is not available" do
206
+ self.example.stub!(:metadata).and_return(nil)
207
+ def method_name
208
+ "test_config_id_by_method_name"
209
+ end
210
+ self.send(:config_id).should eql("test_config_id_by_method_name")
211
+ end
212
+
213
+ it "should use __name__ if example metadata and method_name are not available" do
214
+ self.example.stub!(:metadata).and_return(nil)
215
+ def __name__
216
+ "test_config_id_by___name__"
217
+ end
218
+ self.send(:config_id).should eql("test_config_id_by___name__")
219
+ end
220
+ end
221
+
222
+ context "and check if the test has passed" do
223
+ context "using example exception if available" do
224
+ it "should be 'true' if exception is 'nil'" do
225
+ self.example.stub!(:exception).and_return(nil)
226
+ self.send(:has_passed?).should be_true
227
+ end
228
+
229
+ it "should be 'false' if exception is not 'nil'" do
230
+ self.example.stub!(:exception).and_return("")
231
+ self.send(:has_passed?).should be_false
232
+ end
233
+ end
234
+
235
+ context "using 'test_passed' attribute if example exception is not available" do
236
+ before { self.example.stub!(:instance_variable_defined?).with(:@exception).and_return(false) }
237
+
238
+ it "should be 'true' if 'test_passed' is 'true'" do
239
+ self.instance_variable_set(:@test_passed, true)
240
+ self.send(:has_passed?).should be_true
241
+ end
242
+
243
+ it "should be 'false' if 'test_passed' is 'false'" do
244
+ self.instance_variable_set(:@test_passed, false)
245
+ self.send(:has_passed?).should be_false
246
+ end
247
+ end
248
+
249
+ context "using 'passed' attribute if example exception and 'test_passed' are not available" do
250
+ before do
251
+ self.example.stub!(:instance_variable_defined?).with(:@exception).and_return(false)
252
+ self.instance_variable_set(:@test_passed, nil)
253
+ end
254
+
255
+ it "should be 'true' if 'passed' is 'true'" do
256
+ self.instance_variable_set(:@passed, true)
257
+ self.send(:has_passed?).should be_true
258
+ end
259
+
260
+ it "should be 'false' if 'passed' is 'false'" do
261
+ self.instance_variable_set(:@passed, false)
262
+ self.send(:has_passed?).should be_false
263
+ end
264
+ end
265
+ end
266
+ end
267
+
268
+ context "when starting the server" do
269
+ let(:config) { NginxTestHelper::Config.new("config_id", {}) }
270
+ let(:status) { Status.new }
271
+
272
+ it "should use POpen4 to execute the command" do
273
+ POpen4.should_receive(:popen4).with("/usr/local/nginx/sbin/nginx -c /tmp/nginx_tests/config_id.conf").and_return(status)
274
+ start_server(config)
275
+ end
276
+
277
+ it "should not start the server if configuration has a key 'disable_start_stop_server' with 'true'" do
278
+ config.configuration[:disable_start_stop_server] = true
279
+ POpen4.should_not_receive(:popen4)
280
+ start_server(config)
281
+ end
282
+
283
+ it "should raise error if 'exitstatus' is not '0'" do
284
+ status.exitstatus = 1
285
+ POpen4.should_receive(:popen4).with("/usr/local/nginx/sbin/nginx -c /tmp/nginx_tests/config_id.conf").and_return(status)
286
+ expect { start_server(config) }.to raise_error("Server doesn't started - ")
287
+ end
288
+
289
+ it "should return error message when the command fail" do
290
+ start_server(config).should eql("nginx: [emerg] unexpected end of file, expecting \";\" or \"}\" in /tmp/nginx_tests/config_id.conf:1")
291
+ end
292
+ end
293
+
294
+ context "when stoping the server" do
295
+ let(:config) { NginxTestHelper::Config.new("config_id", {}) }
296
+ let(:status) { Status.new }
297
+
298
+ it "should use POpen4 to execute the command" do
299
+ POpen4.should_receive(:popen4).with("/usr/local/nginx/sbin/nginx -c /tmp/nginx_tests/config_id.conf -s stop").and_return(status)
300
+ stop_server(config)
301
+ end
302
+
303
+ it "should not start the server if configuration has a key 'disable_start_stop_server' with 'true'" do
304
+ config.configuration[:disable_start_stop_server] = true
305
+ POpen4.should_not_receive(:popen4)
306
+ stop_server(config)
307
+ end
308
+
309
+ it "should raise error if 'exitstatus' is not '0'" do
310
+ status.exitstatus = 1
311
+ POpen4.should_receive(:popen4).with("/usr/local/nginx/sbin/nginx -c /tmp/nginx_tests/config_id.conf -s stop").and_return(status)
312
+ expect { stop_server(config) }.to raise_error("Server doesn't stoped - ")
313
+ end
314
+
315
+ it "should return error message when the command fail" do
316
+ stop_server(config).should eql("nginx: [emerg] unexpected end of file, expecting \";\" or \"}\" in /tmp/nginx_tests/config_id.conf:1")
317
+ end
318
+ end
319
+
320
+ end
@@ -0,0 +1,85 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ config.before do
13
+ FileUtils.rm_rf("/tmp/nginx_tests")
14
+ ENV.each_key do |key|
15
+ ENV.delete(key) if key.start_with?("NGINX_")
16
+ end
17
+ end
18
+ end
19
+
20
+ begin
21
+ require 'simplecov'
22
+ SimpleCov.start do
23
+ add_filter '/spec/'
24
+ end
25
+ SimpleCov.coverage_dir 'coverage'
26
+ rescue LoadError
27
+ # ignore simplecov in ruby < 1.9
28
+ end
29
+
30
+ # Requires lib.
31
+ Dir[File.expand_path('../lib/**/*.rb', File.dirname(__FILE__))].each { |f| require f }
32
+
33
+ module NginxConfiguration
34
+ def self.default_configuration
35
+ {
36
+ :conf_item => "conf_value"
37
+ }
38
+ end
39
+
40
+ def self.template_configuration
41
+ %("template configuration <%= config_id %>")
42
+ end
43
+ end
44
+
45
+ class SocketMock
46
+ attr_accessor :response1, :response2, :response3, :exception
47
+
48
+ def initialize
49
+ @step = 0
50
+ @response1 = ""
51
+ @response2 = ""
52
+ @response3 = ""
53
+ @exception = "just to force go out of the loop"
54
+ end
55
+
56
+ def print(content)
57
+ end
58
+
59
+ def readpartial(count)
60
+ "HTTP 200 OK\r\n\r\nBODY"
61
+ end
62
+
63
+ def read_nonblock(count)
64
+ @step += 1
65
+ if @step == 1
66
+ @response1
67
+ elsif @step == 2
68
+ @response2
69
+ elsif @step == 3
70
+ raise @exception unless @exception.nil?
71
+ elsif @step == 4
72
+ @response3
73
+ else
74
+ raise @exception unless @exception.nil?
75
+ end
76
+ end
77
+ end
78
+
79
+ class Status
80
+ attr_accessor :exitstatus
81
+
82
+ def initialize
83
+ @exitstatus = 0
84
+ end
85
+ end
data/templates/INSTALL ADDED
@@ -0,0 +1,8 @@
1
+ Nginx Test Helper has been installed with example specs.
2
+
3
+ Edit the file 'spec/nginx_configuration.rb'
4
+ setting your module configuration template on 'template_configuration' method
5
+ and your default configuration values on 'default_configuration' method.
6
+
7
+ On 'spec/example_spec.rb' file has some examples of how can you do your tests.
8
+