nginx_utils 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,380 +2,346 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe NginxUtils do
6
- include FakeFS::SpecHelpers
7
-
8
- def create_files
9
- root_dir = "/usr/local/nginx/logs"
10
- FileUtils.mkdir_p root_dir
11
- FileUtils.mkdir_p "/tmp"
12
- not_del = Time.now - (89 * 3600 * 24)
13
- do_del = Time.now - (91 * 3600 * 24)
14
- not_del_file = "access.log.#{not_del.strftime('%Y%m%d%H%M%S')}"
15
- do_del_file = "access.log.#{do_del.strftime('%Y%m%d%H%M%S')}"
16
- [
17
- "access.log",
18
- "error.log",
19
- "nginx.pid",
20
- not_del_file,
21
- do_del_file
22
- ].each{|f| File.open(File.join(root_dir, f), "w").close}
23
- File.utime(not_del, not_del, File.join(root_dir, not_del_file))
24
- File.utime(do_del, do_del, File.join(root_dir, do_del_file))
25
- {
26
- not_del_file: File.join(root_dir, not_del_file),
27
- do_del_file: File.join(root_dir, do_del_file)
28
- }
29
- end
5
+ describe "NginxUtils::Logrotate" do
6
+ let(:rotate) {NginxUtils::Logrotate.new}
30
7
 
31
- def log_lines
32
- File.open("/tmp/nginx_rotate.log").read.split("\n")
8
+ let!(:logger_mock) do
9
+ logger = double("logger mock").as_null_object
10
+ Logger.stub(:new).and_return(logger)
11
+ logger
33
12
  end
34
13
 
35
- before(:each) do
36
- @created = create_files
37
- @script_log = File.open("/tmp/nginx_rotate.log", "a")
14
+ let!(:files) {["access.log", "error.log"]}
15
+ let!(:time_now) {Time.now}
16
+ let!(:default) do
17
+ {
18
+ debug: false,
19
+ script_log: "/tmp/nginx_rotate.log",
20
+ log_level: :debug,
21
+ root_dir: "/usr/local/nginx",
22
+ target_logs: "*.log",
23
+ retention: 90,
24
+ prefix: time_now.strftime("%Y%m%d%H%M%S"),
25
+ pid_file: "/usr/local/nginx/logs/nginx.pid"
26
+ }
38
27
  end
39
28
 
40
- describe "Logrotate" do
41
- describe "#initialize" do
42
- before(:each) do
43
- @time_now = Time.now
44
- Time.stub(:now).and_return(@time_now)
45
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log)
46
- end
29
+ describe "#initialize" do
30
+ context "with default params" do
31
+ let(:params) {rotate.instance_eval{@params}}
32
+ let(:execute) {rotate.instance_eval{@execute}}
47
33
 
48
- context "with default params" do
49
- it "@execute should be true" do
50
- expect(@rotate.instance_eval{@execute}).to eq(true)
51
- end
52
-
53
- it "@logger should be a instance of Logger class" do
54
- expect(@rotate.instance_eval{@logger}.is_a? Logger).to eq(true)
55
- end
56
-
57
- it "@logger.level should be debug(0)" do
58
- expect(@rotate.instance_eval{@logger}.level).to eq(Logger::DEBUG)
59
- end
34
+ it "@execute should be true" do
35
+ expect(execute).to eq(true)
36
+ end
60
37
 
61
- it "@root_dir should be default install prefix of Nginx" do
62
- expect(@rotate.instance_eval{@root_dir}).to eq("/usr/local/nginx")
63
- end
38
+ it "@logger should be created" do
39
+ Logger.should_receive(:new).with(default[:script_log])
40
+ rotate
41
+ end
64
42
 
65
- it "@target logs should be *.log" do
66
- expect(@rotate.instance_eval{@target_logs}).to eq("*.log")
67
- end
43
+ it "@logger.level should be debug" do
44
+ logger_mock.should_receive(:level=).with(Logger::DEBUG)
45
+ rotate
46
+ end
68
47
 
69
- it "@rename_logs size should be 2" do
70
- expect(@rotate.instance_eval{@rename_logs}.size).to eq(2)
71
- end
48
+ it "@params[:root_dir] should be default install prefix of Nginx" do
49
+ expect(params[:root_dir]).to eq(default[:root_dir])
50
+ end
72
51
 
73
- it "@delete_logs size should be 2" do
74
- expect(@rotate.instance_eval{@delete_logs}.size).to eq(2)
75
- end
52
+ it "@params[:target_logs] should be *.log" do
53
+ expect(params[:target_logs]).to eq(default[:target_logs])
54
+ end
76
55
 
77
- it "@prefix should be a some as @time_now" do
78
- expect(@rotate.instance_eval{@prefix}).to eq(@time_now.strftime("%Y%m%d%H%M%S"))
79
- end
56
+ it "@rename_logs size should be number of target log files" do
57
+ Dir.should_receive(:glob).exactly(2).times.and_return(files)
58
+ expect(rotate.rename_logs).to eq(files)
59
+ end
80
60
 
81
- it "@retention should be 90 days" do
82
- expect(@rotate.instance_eval{@retention}).to eq(@time_now - (90 * 3600 * 24))
83
- end
61
+ it "@delete_logs size should be number of target log files" do
62
+ Dir.should_receive(:glob).exactly(2).times.and_return(files)
63
+ expect(rotate.delete_logs).to eq(files)
64
+ end
84
65
 
85
- it "@pid_file should be /usr/local/nginx/logs/nginx.pid" do
86
- expect(@rotate.instance_eval{@pid_file}).to eq("/usr/local/nginx/logs/nginx.pid")
87
- end
66
+ it "@params[:prefix] should be a some as time_now" do
67
+ Time.stub(:now).and_return(time_now)
68
+ expect(params[:prefix]).to eq(default[:prefix])
69
+ end
88
70
 
89
- it "@rename_logs should be created" do
90
- expect(@rotate.instance_eval{@rename_logs}).not_to eq(nil)
91
- end
71
+ it "@params[:retention] should be 90 days" do
72
+ expect(params[:retention]).to eq(default[:retention])
73
+ end
92
74
 
93
- it "@delete_logs should be created" do
94
- expect(@rotate.instance_eval{@delete_logs}).not_to eq(nil)
95
- end
75
+ it "@params[:pid_file] should be /usr/local/nginx/logs/nginx.pid" do
76
+ expect(params[:pid_file]).to eq(default[:pid_file])
96
77
  end
97
78
 
98
- context "with debug" do
99
- before(:each) do
100
- @rotate = NginxUtils::Logrotate.new(debug: true, script_log: @script_log)
101
- end
102
-
103
- it "@execute should be false" do
104
- expect(@rotate.instance_eval{@execute}).to eq(false)
105
- end
106
-
107
- it "@logger should be a instance of Logger class" do
108
- expect(@rotate.instance_eval{@logger}.is_a? Logger).to eq(true)
109
- end
110
-
111
- it "other parameters should be default" do
112
- expect(@rotate.instance_eval{@logger}.level).to eq(Logger::DEBUG)
113
- expect(@rotate.instance_eval{@root_dir}).to eq("/usr/local/nginx")
114
- expect(@rotate.instance_eval{@target_logs}).to eq("*.log")
115
- expect(@rotate.instance_eval{@prefix}).to eq(@time_now.strftime("%Y%m%d%H%M%S"))
116
- expect(@rotate.instance_eval{@retention}).to eq(@time_now - (90 * 3600 * 24))
117
- expect(@rotate.instance_eval{@pid_file}).to eq("/usr/local/nginx/logs/nginx.pid")
118
- end
79
+ it "@rename_logs should be created" do
80
+ expect(rotate.rename_logs).not_to eq(nil)
119
81
  end
120
82
 
121
- context "with custom params" do
122
- it "@logger should be false if script_log is false" do
123
- rotate = NginxUtils::Logrotate.new(script_log: false)
124
- expect(rotate.instance_eval{@logger}).to eq(false)
125
- end
126
-
127
- it "@logger.level should be a specified level" do
128
- rotate = NginxUtils::Logrotate.new(log_level: :warn, script_log: @script_log)
129
- expect(rotate.instance_eval{@logger}.level).to eq(Logger::WARN)
130
- end
131
-
132
- it "@root_dir should be a specified parameter" do
133
- rotate = NginxUtils::Logrotate.new(root_dir: "/var/log/nginx", script_log: @script_log)
134
- expect(rotate.instance_eval{@root_dir}).to eq("/var/log/nginx")
135
- end
136
-
137
- it "@prefix should be a specified parameter" do
138
- rotate = NginxUtils::Logrotate.new(prefix: "20130518", script_log: @script_log)
139
- expect(rotate.instance_eval{@prefix}).to eq("20130518")
140
- end
141
-
142
- it "@retention should be a specified period" do
143
- rotate = NginxUtils::Logrotate.new(retention: 30, script_log: @script_log)
144
- expect(rotate.instance_eval{@retention}).to eq(@time_now - (30 * 3600 * 24))
145
- end
146
-
147
- it "@pid_file should be a specified parameter" do
148
- rotate = NginxUtils::Logrotate.new(pid_file: "/var/run/nginx.pid", script_log: @script_log)
149
- expect(rotate.instance_eval{@pid_file}).to eq("/var/run/nginx.pid")
150
- end
83
+ it "@delete_logs should be created" do
84
+ expect(rotate.delete_logs).not_to eq(nil)
151
85
  end
152
86
  end
153
87
 
154
- describe "#config" do
155
- before(:each) do
156
- @time_now = Time.now
157
- Time.stub(:now).and_return(@time_now)
158
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log)
88
+ context "with debug" do
89
+ let(:debug_rotate) {NginxUtils::Logrotate.new(debug: true)}
90
+ let(:params) {debug_rotate.instance_eval{@params}}
91
+ let(:execute) {debug_rotate.instance_eval{@execute}}
92
+
93
+ it "@execute should be false" do
94
+ expect(execute).to eq(false)
159
95
  end
160
96
 
161
- it "@execute should be a false if debug is true" do
162
- old_execute = @rotate.instance_eval{@execute}
163
- @rotate.config debug: true
164
- expect(@rotate.instance_eval{@execute}).to eq(false)
165
- expect(@rotate.instance_eval{@execute}).not_to eq(old_execute)
97
+ it "@logger should be created with STDOUT" do
98
+ Logger.should_receive(:new).with(STDOUT)
99
+ debug_rotate.logger
166
100
  end
167
101
 
168
- it "@execute should be a true if debug is false" do
169
- rotate = NginxUtils::Logrotate.new(debug: true)
170
- old_execute = rotate.instance_eval{@execute}
171
- rotate.config debug: false
172
- expect(rotate.instance_eval{@execute}).to eq(true)
173
- expect(rotate.instance_eval{@execute}).not_to eq(old_execute)
102
+ it "other parameters should be default" do
103
+ logger_mock.should_receive(:level=).with(Logger::DEBUG)
104
+ Time.stub(:now).and_return(time_now)
105
+ expect(params[:root_dir]).to eq(default[:root_dir])
106
+ expect(params[:target_logs]).to eq(default[:target_logs])
107
+ expect(params[:prefix]).to eq(default[:prefix])
108
+ expect(params[:retention]).to eq(default[:retention])
109
+ expect(params[:pid_file]).to eq(default[:pid_file])
174
110
  end
111
+ end
175
112
 
176
- it "@logger should be a false if script_log is false" do
177
- old_logger = @rotate.instance_eval{@logger}
178
- @rotate.config script_log: false
179
- expect(@rotate.instance_eval{@logger}).to eq(false)
180
- expect(@rotate.instance_eval{@logger}).not_to eq(old_logger)
113
+ context "with custom params" do
114
+ it "@logger should be false if script_log is false" do
115
+ rotate = NginxUtils::Logrotate.new(script_log: false)
116
+ expect(rotate.logger).to eq(false)
181
117
  end
182
118
 
183
119
  it "@logger.level should be a specified level" do
184
- old_level = @rotate.instance_eval{@logger}.level
185
- @rotate.config log_level: :warn
186
- expect(@rotate.instance_eval{@logger}.level).to eq(Logger::WARN)
187
- expect(@rotate.instance_eval{@logger}.level).not_to eq(old_level)
120
+ logger_mock.should_receive(:level=).with(Logger::WARN)
121
+ NginxUtils::Logrotate.new(log_level: :warn)
188
122
  end
189
123
 
190
- it "@root_dir should be a specified parameter" do
191
- old_root_dir = @rotate.instance_eval{@root_dir}
192
- @rotate.config root_dir: "/var/log/nginx"
193
- expect(@rotate.instance_eval{@root_dir}).to eq("/var/log/nginx")
194
- expect(@rotate.instance_eval{@root_dir}).not_to eq(old_root_dir)
124
+ it "@params[:root_dir] should be a specified parameter" do
125
+ root_dir = "/var/log/nginx"
126
+ rotate = NginxUtils::Logrotate.new(root_dir: root_dir)
127
+ expect(rotate.instance_eval{@params[:root_dir]}).to eq(root_dir)
195
128
  end
196
129
 
197
- it "@target_logs should be a specified parameter" do
198
- old_target_logs = @rotate.instance_eval{@target_logs}
199
- @rotate.config target_logs: "*_log"
200
- expect(@rotate.instance_eval{@target_logs}).to eq("*_log")
201
- expect(@rotate.instance_eval{@target_logs}).not_to eq(old_target_logs)
130
+ it "@params[:prefix] should be a specified parameter" do
131
+ prefix = Time.now.strftime("%Y%m%d")
132
+ rotate = NginxUtils::Logrotate.new(prefix: prefix)
133
+ expect(rotate.instance_eval{@params[:prefix]}).to eq(prefix)
202
134
  end
203
135
 
204
- it "@prefix should be a specified parameter" do
205
- old_prefix = @rotate.instance_eval{@prefix}
206
- @rotate.config prefix: "20130518"
207
- expect(@rotate.instance_eval{@prefix}).to eq("20130518")
208
- expect(@rotate.instance_eval{@prefix}).not_to eq(old_prefix)
136
+ it "@params[:retention] should be a specified period" do
137
+ retention = 30
138
+ rotate = NginxUtils::Logrotate.new(retention: retention)
139
+ expect(rotate.instance_eval{@params[:retention]}).to eq(retention)
209
140
  end
210
141
 
211
- it "@retention should be a specified period" do
212
- old_retention = @rotate.instance_eval{@retention}
213
- @rotate.config retention: 30
214
- expect(@rotate.instance_eval{@retention}).to eq(@time_now - (30 * 3600 * 24))
215
- expect(@rotate.instance_eval{@retention}).not_to eq(old_retention)
142
+ it "@params[:pid_file] should be a specified parameter" do
143
+ pid_file = "/var/run/nginx.pid"
144
+ rotate = NginxUtils::Logrotate.new(pid_file: pid_file)
145
+ expect(rotate.instance_eval{@params[:pid_file]}).to eq(pid_file)
216
146
  end
147
+ end
148
+ end
217
149
 
218
- it "@pid_file should be a specified parameter" do
219
- old_pid_file = @rotate.instance_eval{@pid_file}
220
- @rotate.config pid_file: "/var/run/nginx.pid"
221
- expect(@rotate.instance_eval{@pid_file}).to eq("/var/run/nginx.pid")
222
- expect(@rotate.instance_eval{@pid_file}).not_to eq(old_pid_file)
223
- end
150
+ describe "#config" do
151
+ let(:params) {rotate.instance_eval{@params}}
152
+ let(:execute) {rotate.instance_eval{@execute}}
153
+
154
+ it "@execute should be a false if debug is true" do
155
+ Logger.should_receive(:new).with(STDOUT)
156
+ rotate.config debug: true
157
+ expect(execute).to eq(false)
224
158
  end
225
159
 
226
- describe "#rename" do
227
- before(:each) do
228
- @prefix = Time.now.strftime('%Y%m%d%H%M%S')
229
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log, prefix: @prefix)
230
- end
160
+ it "@execute should be a true if debug is false" do
161
+ rotate = NginxUtils::Logrotate.new(debug: true)
162
+ rotate.config debug: false
163
+ expect(rotate.instance_eval{@execute}).to eq(true)
164
+ end
231
165
 
232
- it "rename target logs" do
233
- @rotate.rename
234
- @rotate.rename_logs.each do |log|
235
- expect(File.exists? log).to eq(false)
236
- expect(File.exists? "#{log}.#{@prefix}").to eq(true)
237
- end
238
- end
166
+ it "@logger should be a false if script_log is false" do
167
+ rotate.config script_log: false
168
+ expect(rotate.logger).to eq(false)
169
+ end
239
170
 
240
- it "output log file" do
241
- @rotate.rename
242
- expect(log_lines.size).to eq(@rotate.rename_logs.size)
243
- expect(log_lines.select{|l| /Rename log file/ =~ l}.size).to eq(@rotate.rename_logs.size)
244
- end
171
+ it "@logger.level should be a specified level" do
172
+ logger_mock.should_receive(:level=).with(Logger::WARN)
173
+ rotate.config log_level: :warn
174
+ end
245
175
 
246
- it "do not rename if a file with the same name exists" do
247
- File.open("#{@rotate.rename_logs.first}.#{@prefix}", "w").close
248
- @rotate.rename
249
- expect(File.exists? @rotate.rename_logs.first).to eq(true)
250
- expect(log_lines.select{|line| /File already exists/ =~ line}.size).to eq(1)
251
- end
176
+ it "@params[:root_dir] should be a specified parameter" do
177
+ root_dir = "/var/log/nginx"
178
+ rotate.config root_dir: root_dir
179
+ expect(params[:root_dir]).to eq(root_dir)
180
+ end
252
181
 
253
- it "do not rename if not executable" do
254
- @rotate.config debug: true, script_log: false
255
- @rotate.rename
256
- @rotate.rename_logs.each do |log|
257
- expect(File.exists? log).to eq(true)
258
- expect(File.exists? "#{log}.#{@prefix}").to eq(false)
259
- end
260
- end
182
+ it "@params[:target_logs] should be a specified parameter" do
183
+ target_logs = "*_log"
184
+ rotate.config target_logs: target_logs
185
+ expect(params[:target_logs]).to eq(target_logs)
186
+ end
261
187
 
262
- it "do not output log if script_log is false" do
263
- @rotate.config script_log: false
264
- @rotate.rename
265
- expect(log_lines.size).to eq(0)
266
- end
188
+ it "@params[:prefix] should be a specified parameter" do
189
+ prefix = Time.now.strftime("%Y%m%d")
190
+ rotate.config prefix: prefix
191
+ expect(params[:prefix]).to eq(prefix)
267
192
  end
268
193
 
269
- describe "#delete" do
270
- before(:each) do
271
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log)
272
- end
194
+ it "@params[:retention] should be a specified period" do
195
+ retention = 30
196
+ rotate.config retention: retention
197
+ expect(params[:retention]).to eq(retention)
198
+ end
273
199
 
274
- it "delete target logs" do
275
- @rotate.delete
276
- expect(File.exists? @created[:do_del_file]).to eq(false)
277
- expect(File.exists? @created[:not_del_file]).to eq(true)
278
- end
200
+ it "@params[:pid_file] should be a specified parameter" do
201
+ pid_file = "/var/run/nginx.pid"
202
+ rotate.config pid_file: pid_file
203
+ expect(params[:pid_file]).to eq(pid_file)
204
+ end
205
+ end
279
206
 
280
- it "output log file" do
281
- @rotate.delete
282
- expect(log_lines.size).to eq(1)
283
- end
207
+ describe "#rename" do
208
+ before(:each) do
209
+ Dir.should_receive(:glob).exactly(2).times.and_return(files)
210
+ File.stub(:rename).and_return(true)
211
+ end
284
212
 
285
- it "do not delete if not executable" do
286
- File.should_not_receive(:unlink)
287
- @rotate.config debug: true, script_log: false
288
- @rotate.delete
289
- end
213
+ it "rename target logs" do
214
+ File.should_receive(:rename).exactly(2).times
215
+ rotate.rename
216
+ end
290
217
 
291
- it "do not output log if script_log is false" do
292
- @rotate.config script_log: false
293
- @rotate.delete
294
- expect(log_lines.size).to eq(0)
295
- end
218
+ it "output log file" do
219
+ logger_mock.should_receive(:debug).exactly(2).times
220
+ rotate.rename
296
221
  end
297
222
 
298
- describe "#restart" do
299
- before(:each) do
300
- Object.any_instance.stub(:system).and_return(true)
301
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log)
302
- end
223
+ it "do not rename if a file with the same name exists" do
224
+ File.stub(:exists?).and_return(true)
225
+ File.should_not_receive(:rename)
226
+ logger_mock.should_receive(:warn).exactly(2).times
227
+ rotate.rename
228
+ end
303
229
 
304
- it "should execute command" do
305
- Object.any_instance.should_receive(:system).and_return(true)
306
- @rotate.restart
307
- end
230
+ it "do not rename if not executable" do
231
+ File.should_not_receive(:rename)
232
+ rotate.config debug: true
233
+ rotate.rename
234
+ end
308
235
 
309
- it "output success log" do
310
- @rotate.restart
311
- expect(log_lines.select{|l| /Nginx restart command/ =~ l}.size).to eq(1)
312
- expect(log_lines.select{|l| /Nginx restart is successfully/ =~ l}.size).to eq(1)
313
- end
236
+ it "do not output log if script_log is false" do
237
+ logger_mock.should_not_receive(:debug)
238
+ rotate.config script_log: false
239
+ rotate.rename
240
+ end
241
+ end
314
242
 
315
- it "do not execute command if not exists pid file" do
316
- Object.any_instance.should_not_receive(:system)
317
- File.stub(:exists?).and_return(false)
318
- @rotate.restart
319
- end
243
+ describe "#delete" do
244
+ before(:each) do
245
+ Dir.should_receive(:glob).exactly(2).times.and_return(files)
246
+ delete_time = Time.now - ((default[:retention] + 1) * 3600 * 24)
247
+ File.stub(:stat).and_return(double("time mock", mtime: delete_time))
248
+ File.stub(:unlink).and_return(true)
249
+ end
320
250
 
321
- it "should output log if not exists pid file" do
322
- File.stub(:exists?).and_return(false)
323
- @rotate.restart
324
- expect(log_lines.select{|l| /Pid file is not found/ =~ l}.size).to eq(1)
325
- end
251
+ it "delete target logs" do
252
+ File.should_receive(:unlink).exactly(2).times
253
+ rotate.delete
254
+ end
326
255
 
327
- it "do not execute commando if not executable" do
328
- Object.any_instance.should_not_receive(:system)
329
- @rotate.config debug: true, script_log: false
330
- @rotate.restart
331
- end
256
+ it "output log file" do
257
+ logger_mock.should_receive(:debug).exactly(2).times
258
+ rotate.delete
259
+ end
332
260
 
333
- it "should outputs error log if it fails" do
334
- Object.any_instance.should_receive(:system).and_return(false)
335
- @rotate.restart
336
- expect(log_lines.select{|l| /Nginx restart failed/ =~ l}.size).to eq(1)
337
- end
261
+ it "do not delete if not executable" do
262
+ File.should_not_receive(:unlink)
263
+ rotate.config debug: true
264
+ rotate.delete
265
+ end
338
266
 
339
- it "should generate an exception if it fails" do
340
- Object.any_instance.should_receive(:system).and_return(false)
341
- @rotate.config script_log: false
342
- expect(proc{@rotate.restart}).to raise_error("Nginx restart failed")
343
- end
267
+ it "do not output log if script_log is false" do
268
+ logger_mock.should_not_receive(:debug)
269
+ rotate.config script_log: false
270
+ rotate.delete
271
+ end
272
+ end
344
273
 
345
- it "do not output log if script_log is false" do
346
- @rotate.config script_log: false
347
- @rotate.restart
348
- expect(log_lines.size).to eq(0)
349
- end
274
+ describe "#restart" do
275
+ before(:each) do
276
+ File.stub(:exists?).and_return(true)
277
+ File.stub(:read).and_return("2000")
278
+ Process.stub(:kill).and_return(1)
350
279
  end
351
280
 
352
- describe "#execute" do
353
- before(:each) do
354
- NginxUtils::Logrotate.any_instance.stub(:rename).and_return(true)
355
- NginxUtils::Logrotate.any_instance.stub(:delete).and_return(true)
356
- NginxUtils::Logrotate.any_instance.stub(:restart).and_return(true)
357
- @rotate = NginxUtils::Logrotate.new(script_log: @script_log)
358
- end
281
+ it "should execute reopen log" do
282
+ Process.should_receive(:kill).and_return(1)
283
+ rotate.restart
284
+ end
359
285
 
360
- it "should call rename and delete and restart methods" do
361
- NginxUtils::Logrotate.any_instance.should_receive(:rename).and_return(true)
362
- NginxUtils::Logrotate.any_instance.should_receive(:delete).and_return(true)
363
- NginxUtils::Logrotate.any_instance.should_receive(:restart).and_return(true)
364
- @rotate.execute
365
- end
286
+ it "output success log" do
287
+ logger_mock.should_receive(:info)
288
+ rotate.restart
289
+ end
366
290
 
367
- it "output log file" do
368
- @rotate.execute
369
- expect(log_lines.select{|l| /Execute Nginx logrotate/ =~ l}.size).to eq(1)
370
- expect(log_lines.select{|l| /Nginx logrotate is successfully/ =~ l}.size).to eq(1)
371
- expect(log_lines.size).to eq(2)
372
- end
291
+ it "do not execute reopen log if not exists pid file" do
292
+ File.stub(:exists?).and_return(false)
293
+ Process.should_not_receive(:kill)
294
+ rotate.restart
295
+ end
373
296
 
374
- it "do not output log if script_log is false" do
375
- @rotate.config script_log: false
376
- @rotate.execute
377
- expect(log_lines.size).to eq(0)
378
- end
297
+ it "should output log if not exists pid file" do
298
+ File.stub(:exists?).and_return(false)
299
+ logger_mock.should_receive(:warn)
300
+ rotate.restart
301
+ end
302
+
303
+ it "do not execute reopen log if not executable" do
304
+ Process.should_not_receive(:kill)
305
+ rotate.config debug: true
306
+ rotate.restart
307
+ end
308
+
309
+ it "should generate an exception" do
310
+ Process.should_receive(:kill).and_raise("error")
311
+ logger_mock.should_receive(:error).exactly(2).times
312
+ expect(proc{rotate.restart}).to raise_error("Nginx restart failed")
313
+ end
314
+
315
+ it "do not output log if script_log is false" do
316
+ logger_mock.should_not_receive(:info)
317
+ rotate.config script_log: false
318
+ rotate.restart
379
319
  end
380
320
  end
381
- end
321
+
322
+ describe "#execute" do
323
+ before(:each) do
324
+ NginxUtils::Logrotate.any_instance.stub(:rename).and_return(true)
325
+ NginxUtils::Logrotate.any_instance.stub(:delete).and_return(true)
326
+ NginxUtils::Logrotate.any_instance.stub(:restart).and_return(true)
327
+ end
328
+
329
+ it "should call rename and delete and restart methods" do
330
+ NginxUtils::Logrotate.any_instance.should_receive(:rename).and_return(true)
331
+ NginxUtils::Logrotate.any_instance.should_receive(:delete).and_return(true)
332
+ NginxUtils::Logrotate.any_instance.should_receive(:restart).and_return(true)
333
+ rotate.execute
334
+ end
335
+
336
+ it "output log file" do
337
+ logger_mock.should_receive(:info).exactly(2).times
338
+ rotate.execute
339
+ end
340
+
341
+ it "do not output log if script_log is false" do
342
+ logger_mock.should_not_receive(:info)
343
+ rotate.config script_log: false
344
+ rotate.execute
345
+ end
346
+ end
347
+ end