nginx_utils 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +22 -2
- data/lib/nginx_utils/logreader.rb +63 -0
- data/lib/nginx_utils/logrotate.rb +9 -7
- data/lib/nginx_utils/version.rb +1 -1
- data/lib/nginx_utils.rb +1 -0
- data/spec/nginx_utils/logreader_spec.rb +126 -0
- data/spec/nginx_utils/logrotate_spec.rb +60 -59
- data/spec/nginx_utils/status_spec.rb +3 -3
- data/spec/spec_helper.rb +0 -1
- metadata +8 -5
data/README.md
CHANGED
@@ -24,7 +24,7 @@ Or install it yourself as:
|
|
24
24
|
require 'nginx_utils'
|
25
25
|
```
|
26
26
|
|
27
|
-
|
27
|
+
Logrotate:
|
28
28
|
|
29
29
|
```
|
30
30
|
params = {
|
@@ -35,13 +35,33 @@ params = {
|
|
35
35
|
target_logs: "*.log",
|
36
36
|
prefix: Time.now.strftime("%Y%m%d%H%M%S"),
|
37
37
|
retention: 90,
|
38
|
-
pid_file: "/usr/local/nginx/logs/nginx.pid"
|
38
|
+
pid_file: "/usr/local/nginx/logs/nginx.pid"
|
39
39
|
}
|
40
40
|
|
41
41
|
rotate = NginxUtils::Logrotate.new(params)
|
42
42
|
rotate.execute
|
43
43
|
```
|
44
44
|
|
45
|
+
Status:
|
46
|
+
|
47
|
+
```
|
48
|
+
p NginxUtils::Status.get # => {active_connection: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
|
49
|
+
```
|
50
|
+
|
51
|
+
Logreader:
|
52
|
+
|
53
|
+
```
|
54
|
+
reader = NginxUtils::Logreader.new("/path/to/nginx/logs/access.log")
|
55
|
+
reader.each do |line|
|
56
|
+
p line # => {time: "2013-05-19T08:13:14+00:00", host: "192.168.1.10", ...}
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Options that can be specified:
|
61
|
+
|
62
|
+
* :format => :ltsv or :combined
|
63
|
+
* :parser => Parse with scan method. Specified in Regexp.
|
64
|
+
|
45
65
|
## Contributing
|
46
66
|
|
47
67
|
1. Fork it
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module NginxUtils
|
4
|
+
class Logreader
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def initialize(log, options={})
|
8
|
+
@log = File.open(log, "r")
|
9
|
+
|
10
|
+
if options[:parser]
|
11
|
+
if options[:parser].is_a? Regexp
|
12
|
+
@format = :custom
|
13
|
+
@parser = options[:parser]
|
14
|
+
else
|
15
|
+
raise ArgumentError, "invalid argument"
|
16
|
+
end
|
17
|
+
else
|
18
|
+
@format = options[:format] || :ltsv
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def each
|
23
|
+
@log.each do |line|
|
24
|
+
yield parse(line.chomp)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def parse(line)
|
30
|
+
case @format
|
31
|
+
when :ltsv then
|
32
|
+
row = line.split("\t").map do |f|
|
33
|
+
c = f.split(":")
|
34
|
+
if c == 2
|
35
|
+
[c[0].to_sym, c[1]]
|
36
|
+
else
|
37
|
+
[c[0].to_sym, c[1..-1].join(":")]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
Hash[row]
|
41
|
+
when :combined then
|
42
|
+
if /([0-9.]+)\s-\s([^\s]+)\s\[(.*?)\]\s"(.*?)"\s([0-9]+)\s([0-9]+)\s"(.*?)"\s"(.*?)".*/ =~ line
|
43
|
+
{
|
44
|
+
remote_addr: $1,
|
45
|
+
remote_user: $2,
|
46
|
+
time_local: $3,
|
47
|
+
request: $4,
|
48
|
+
status: $5,
|
49
|
+
body_bytes_sent: $6,
|
50
|
+
http_referer: $7,
|
51
|
+
http_user_agent: $8
|
52
|
+
}
|
53
|
+
else
|
54
|
+
{unknown: line}
|
55
|
+
end
|
56
|
+
when :custom then
|
57
|
+
line.scan @parser
|
58
|
+
else
|
59
|
+
raise "format error"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
module NginxUtils
|
4
4
|
class Logrotate
|
5
|
-
attr_accessor :logger
|
5
|
+
attr_accessor :logger
|
6
|
+
attr_accessor :rename_logs
|
7
|
+
attr_accessor :delete_logs
|
6
8
|
|
7
9
|
def initialize(options={})
|
8
10
|
# Debug
|
@@ -134,22 +136,22 @@ module NginxUtils
|
|
134
136
|
@logger.debug "Nginx restart command: #{cmd}" if @logger
|
135
137
|
if @execute
|
136
138
|
if system(cmd)
|
137
|
-
@logger.info "Nginx restart is successfully
|
139
|
+
@logger.info "Nginx restart is successfully" if @logger
|
138
140
|
else
|
139
|
-
@logger.error "Nginx restart failed
|
140
|
-
raise "Nginx restart failed
|
141
|
+
@logger.error "Nginx restart failed" if @logger
|
142
|
+
raise "Nginx restart failed" if @logger == false
|
141
143
|
end
|
142
144
|
end
|
143
145
|
else
|
144
|
-
@logger.warn "Pid file is not found. not restart nginx. (#{@pid_file})" if @logger
|
146
|
+
@logger.warn "Pid file is not found. Do not restart nginx. (#{@pid_file})" if @logger
|
145
147
|
end
|
146
148
|
end
|
147
149
|
|
148
150
|
def execute
|
149
|
-
@logger.info "Nginx logrotate
|
151
|
+
@logger.info "Execute Nginx logrotate" if @logger
|
150
152
|
rename
|
151
153
|
delete
|
152
|
-
@logger.info "Nginx logrotate is successfully
|
154
|
+
@logger.info "Nginx logrotate is successfully" if @logger
|
153
155
|
restart
|
154
156
|
end
|
155
157
|
|
data/lib/nginx_utils/version.rb
CHANGED
data/lib/nginx_utils.rb
CHANGED
@@ -0,0 +1,126 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe NginxUtils do
|
6
|
+
include FakeFS::SpecHelpers
|
7
|
+
|
8
|
+
def create_log(format)
|
9
|
+
log_dir = "/tmp"
|
10
|
+
FileUtils.mkdir_p log_dir
|
11
|
+
|
12
|
+
case format
|
13
|
+
when :ltsv then
|
14
|
+
line = "time:2013-05-19T08:13:14+00:00\thost:192.168.1.10\txff:-\tmethod:GET\tpath:/\tstatus:200\tua:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31\treq_size:124\treq_time:0.007\tres_size:239\tbody_size:11\tapp_time:-\n"
|
15
|
+
when :combined then
|
16
|
+
line = "192.168.1.10 - - [19/May/2013:23:14:04 +0900] \"GET / HTTP/1.1\" 200 239 \"-\" \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31\"\n"
|
17
|
+
else raise "format error"
|
18
|
+
end
|
19
|
+
|
20
|
+
File.open(File.join(log_dir, "access.log"), "w") do |f|
|
21
|
+
3.times {f.write line}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
before(:each) do
|
26
|
+
@log_file = "/tmp/access.log"
|
27
|
+
@parser = /([0-9.]+)\s-\s([^\s]+)\s\[(.*?)\]\s"(.*?)"\s([0-9]+)\s([0-9]+)\s"(.*?)"\s"(.*?)"\s"(.*)".*/
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "Logreader" do
|
31
|
+
describe "#initialize" do
|
32
|
+
context "with default parameters" do
|
33
|
+
before(:each) do
|
34
|
+
create_log(:ltsv)
|
35
|
+
@reader = NginxUtils::Logreader.new(@log_file)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "specified file should be opened" do
|
39
|
+
expect(@reader.instance_eval{@log}.path).to eq(@log_file)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "default format should be ltsv" do
|
43
|
+
expect(@reader.instance_eval{@format}).to eq(:ltsv)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "default parser should be nil" do
|
47
|
+
expect(@reader.instance_eval{@parser}).to eq(nil)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with custom parameters" do
|
52
|
+
before(:each) do
|
53
|
+
create_log(:combined)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "format should be specified parameter" do
|
57
|
+
reader = NginxUtils::Logreader.new(@log_file, format: :combined)
|
58
|
+
expect(reader.instance_eval{@format}).to eq(:combined)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "parser should be specified parameter" do
|
62
|
+
reader = NginxUtils::Logreader.new(@log_file, parser: @parser)
|
63
|
+
expect(reader.instance_eval{@parser}).to eq(@parser)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "format should be :custom if specified parser" do
|
67
|
+
reader = NginxUtils::Logreader.new(@log_file, parser: @parser)
|
68
|
+
expect(reader.instance_eval{@format}).to eq(:custom)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should create exception if parser is not Regexp instance" do
|
72
|
+
expect(proc{NginxUtils::Logreader.new(@log_file, parser: "invalid parser")}).to raise_error("invalid argument")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#each" do
|
78
|
+
context "with ltsv log" do
|
79
|
+
it "return ltsv hash" do
|
80
|
+
create_log(:ltsv)
|
81
|
+
reader = NginxUtils::Logreader.new(@log_file)
|
82
|
+
ltsv_hash = {
|
83
|
+
time: "2013-05-19T08:13:14+00:00",
|
84
|
+
host: "192.168.1.10",
|
85
|
+
xff:"-",
|
86
|
+
method: "GET",
|
87
|
+
path: "/",
|
88
|
+
status: "200",
|
89
|
+
ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31",
|
90
|
+
req_size: "124",
|
91
|
+
req_time: "0.007",
|
92
|
+
res_size: "239",
|
93
|
+
body_size: "11",
|
94
|
+
app_time: "-"
|
95
|
+
}
|
96
|
+
reader.each {|line| expect(line).to eq(ltsv_hash)}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
context "with combined log" do
|
101
|
+
it "return combined hash" do
|
102
|
+
create_log(:combined)
|
103
|
+
reader = NginxUtils::Logreader.new(@log_file, format: :combined)
|
104
|
+
combined_hash = {
|
105
|
+
remote_addr: "192.168.1.10",
|
106
|
+
remote_user: "-",
|
107
|
+
time_local: "19/May/2013:23:14:04 +0900",
|
108
|
+
request: "GET / HTTP/1.1",
|
109
|
+
status: "200",
|
110
|
+
body_bytes_sent: "239",
|
111
|
+
http_referer: "-",
|
112
|
+
http_user_agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
|
113
|
+
}
|
114
|
+
reader.each {|line| expect(line).to eq(combined_hash)}
|
115
|
+
end
|
116
|
+
|
117
|
+
it "return unknown log if parse error" do
|
118
|
+
create_log(:combined)
|
119
|
+
File.open(@log_file, "w") {|f| f.write "unknown log"}
|
120
|
+
reader = NginxUtils::Logreader.new(@log_file, format: :combined)
|
121
|
+
reader.each {|line| expect(line).to eq({unknown: "unknown log"})}
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
@@ -5,21 +5,21 @@ require "spec_helper"
|
|
5
5
|
describe NginxUtils do
|
6
6
|
include FakeFS::SpecHelpers
|
7
7
|
|
8
|
-
def create_files
|
9
|
-
root_dir =
|
8
|
+
def create_files
|
9
|
+
root_dir = "/usr/local/nginx/logs"
|
10
10
|
FileUtils.mkdir_p root_dir
|
11
|
-
|
12
|
-
|
11
|
+
FileUtils.mkdir_p "/tmp"
|
12
|
+
not_del = Time.now - (89 * 3600 * 24)
|
13
|
+
do_del = Time.now - (91 * 3600 * 24)
|
13
14
|
not_del_file = "access.log.#{not_del.strftime('%Y%m%d%H%M%S')}"
|
14
15
|
do_del_file = "access.log.#{do_del.strftime('%Y%m%d%H%M%S')}"
|
15
|
-
|
16
|
+
[
|
16
17
|
"access.log",
|
17
18
|
"error.log",
|
18
19
|
"nginx.pid",
|
19
20
|
not_del_file,
|
20
21
|
do_del_file
|
21
|
-
]
|
22
|
-
files.each{|f| File.open(File.join(root_dir, f), "w").close}
|
22
|
+
].each{|f| File.open(File.join(root_dir, f), "w").close}
|
23
23
|
File.utime(not_del, not_del, File.join(root_dir, not_del_file))
|
24
24
|
File.utime(do_del, do_del, File.join(root_dir, do_del_file))
|
25
25
|
{
|
@@ -28,8 +28,12 @@ describe NginxUtils do
|
|
28
28
|
}
|
29
29
|
end
|
30
30
|
|
31
|
+
def log_lines
|
32
|
+
File.open("/tmp/nginx_rotate.log").read.split("\n")
|
33
|
+
end
|
34
|
+
|
31
35
|
before(:each) do
|
32
|
-
|
36
|
+
@created = create_files
|
33
37
|
@script_log = File.open("/tmp/nginx_rotate.log", "a")
|
34
38
|
end
|
35
39
|
|
@@ -37,15 +41,11 @@ describe NginxUtils do
|
|
37
41
|
describe "#initialize" do
|
38
42
|
before(:each) do
|
39
43
|
@time_now = Time.now
|
40
|
-
Time.stub
|
44
|
+
Time.stub(:now).and_return(@time_now)
|
45
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
41
46
|
end
|
42
47
|
|
43
48
|
context "with default params" do
|
44
|
-
before(:each) do
|
45
|
-
create_files
|
46
|
-
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
47
|
-
end
|
48
|
-
|
49
49
|
it "@execute should be true" do
|
50
50
|
expect(@rotate.instance_eval{@execute}).to eq(true)
|
51
51
|
end
|
@@ -154,7 +154,7 @@ describe NginxUtils do
|
|
154
154
|
describe "#config" do
|
155
155
|
before(:each) do
|
156
156
|
@time_now = Time.now
|
157
|
-
Time.stub
|
157
|
+
Time.stub(:now).and_return(@time_now)
|
158
158
|
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
159
159
|
end
|
160
160
|
|
@@ -225,60 +225,49 @@ describe NginxUtils do
|
|
225
225
|
|
226
226
|
describe "#rename" do
|
227
227
|
before(:each) do
|
228
|
-
|
229
|
-
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
228
|
+
@prefix = Time.now.strftime('%Y%m%d%H%M%S')
|
229
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log, prefix: @prefix)
|
230
230
|
end
|
231
231
|
|
232
232
|
it "rename target logs" do
|
233
|
-
prefix = Time.now.strftime('%Y%m%d%H%M%S')
|
234
|
-
@rotate.config prefix: prefix
|
235
233
|
@rotate.rename
|
236
234
|
@rotate.rename_logs.each do |log|
|
237
235
|
expect(File.exists? log).to eq(false)
|
238
|
-
expect(File.exists? "#{log}.#{prefix}").to eq(true)
|
236
|
+
expect(File.exists? "#{log}.#{@prefix}").to eq(true)
|
239
237
|
end
|
240
238
|
end
|
241
239
|
|
242
240
|
it "output log file" do
|
243
241
|
@rotate.rename
|
244
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
245
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)
|
246
244
|
end
|
247
245
|
|
248
246
|
it "do not rename if a file with the same name exists" do
|
249
|
-
prefix
|
250
|
-
@rotate.config prefix: prefix
|
251
|
-
File.open("#{@rotate.rename_logs.first}.#{prefix}", "w").close
|
247
|
+
File.open("#{@rotate.rename_logs.first}.#{@prefix}", "w").close
|
252
248
|
@rotate.rename
|
253
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
254
249
|
expect(File.exists? @rotate.rename_logs.first).to eq(true)
|
255
250
|
expect(log_lines.select{|line| /File already exists/ =~ line}.size).to eq(1)
|
256
251
|
end
|
257
252
|
|
258
253
|
it "do not rename if not executable" do
|
259
|
-
|
260
|
-
@rotate.config debug: true, script_log: false, prefix: prefix
|
254
|
+
@rotate.config debug: true, script_log: false
|
261
255
|
@rotate.rename
|
262
256
|
@rotate.rename_logs.each do |log|
|
263
257
|
expect(File.exists? log).to eq(true)
|
264
|
-
expect(File.exists? "#{log}.#{prefix}").to eq(false)
|
258
|
+
expect(File.exists? "#{log}.#{@prefix}").to eq(false)
|
265
259
|
end
|
266
260
|
end
|
267
261
|
|
268
262
|
it "do not output log if script_log is false" do
|
269
263
|
@rotate.config script_log: false
|
270
264
|
@rotate.rename
|
271
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
272
265
|
expect(log_lines.size).to eq(0)
|
273
266
|
end
|
274
267
|
end
|
275
268
|
|
276
269
|
describe "#delete" do
|
277
270
|
before(:each) do
|
278
|
-
@time_now = Time.now
|
279
|
-
Time.stub!(:now).and_return(@time_now)
|
280
|
-
@retention = @time_now - (90 * 3600 * 24)
|
281
|
-
@created = create_files
|
282
271
|
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
283
272
|
end
|
284
273
|
|
@@ -290,89 +279,101 @@ describe NginxUtils do
|
|
290
279
|
|
291
280
|
it "output log file" do
|
292
281
|
@rotate.delete
|
293
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
294
282
|
expect(log_lines.size).to eq(1)
|
295
283
|
end
|
296
284
|
|
297
285
|
it "do not delete if not executable" do
|
286
|
+
File.should_not_receive(:unlink)
|
298
287
|
@rotate.config debug: true, script_log: false
|
299
288
|
@rotate.delete
|
300
|
-
expect(File.exists? @created[:do_del_file]).to eq(true)
|
301
289
|
end
|
302
290
|
|
303
291
|
it "do not output log if script_log is false" do
|
304
292
|
@rotate.config script_log: false
|
305
293
|
@rotate.delete
|
306
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
307
294
|
expect(log_lines.size).to eq(0)
|
308
295
|
end
|
309
296
|
end
|
310
297
|
|
311
298
|
describe "#restart" do
|
312
299
|
before(:each) do
|
313
|
-
|
314
|
-
create_files
|
300
|
+
Object.any_instance.stub(:system).and_return(true)
|
315
301
|
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
316
302
|
end
|
317
303
|
|
318
304
|
it "should execute command" do
|
319
|
-
Object.any_instance.should_receive(:system).
|
305
|
+
Object.any_instance.should_receive(:system).and_return(true)
|
306
|
+
@rotate.restart
|
307
|
+
end
|
308
|
+
|
309
|
+
it "output success log" do
|
320
310
|
@rotate.restart
|
321
|
-
log_lines
|
322
|
-
expect(log_lines.select{|
|
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)
|
323
313
|
end
|
324
314
|
|
325
315
|
it "do not execute command if not exists pid file" do
|
326
|
-
|
316
|
+
Object.any_instance.should_not_receive(:system)
|
317
|
+
File.stub(:exists?).and_return(false)
|
327
318
|
@rotate.restart
|
328
|
-
|
329
|
-
|
319
|
+
end
|
320
|
+
|
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)
|
330
325
|
end
|
331
326
|
|
332
327
|
it "do not execute commando if not executable" do
|
333
|
-
|
328
|
+
Object.any_instance.should_not_receive(:system)
|
329
|
+
@rotate.config debug: true, script_log: false
|
334
330
|
@rotate.restart
|
335
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
336
|
-
expect(log_lines.size).to eq(1)
|
337
331
|
end
|
338
332
|
|
339
333
|
it "should outputs error log if it fails" do
|
340
|
-
Object.any_instance.should_receive(:system).
|
334
|
+
Object.any_instance.should_receive(:system).and_return(false)
|
341
335
|
@rotate.restart
|
342
|
-
log_lines
|
343
|
-
expect(log_lines.select{|line| /Nginx restart failed!/ =~ line}.size).to eq(1)
|
336
|
+
expect(log_lines.select{|l| /Nginx restart failed/ =~ l}.size).to eq(1)
|
344
337
|
end
|
345
338
|
|
346
339
|
it "should generate an exception if it fails" do
|
340
|
+
Object.any_instance.should_receive(:system).and_return(false)
|
347
341
|
@rotate.config script_log: false
|
348
|
-
|
349
|
-
|
342
|
+
expect(proc{@rotate.restart}).to raise_error("Nginx restart failed")
|
343
|
+
end
|
344
|
+
|
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)
|
350
349
|
end
|
351
350
|
end
|
352
351
|
|
353
352
|
describe "#execute" do
|
354
353
|
before(:each) do
|
355
|
-
NginxUtils::Logrotate.any_instance.
|
356
|
-
NginxUtils::Logrotate.any_instance.
|
357
|
-
NginxUtils::Logrotate.any_instance.
|
358
|
-
create_files
|
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)
|
359
357
|
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
360
358
|
end
|
361
359
|
|
362
|
-
it "should call methods" do
|
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)
|
363
364
|
@rotate.execute
|
364
365
|
end
|
365
366
|
|
366
367
|
it "output log file" do
|
367
368
|
@rotate.execute
|
368
|
-
log_lines
|
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)
|
369
371
|
expect(log_lines.size).to eq(2)
|
370
372
|
end
|
371
373
|
|
372
374
|
it "do not output log if script_log is false" do
|
373
375
|
@rotate.config script_log: false
|
374
376
|
@rotate.execute
|
375
|
-
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
376
377
|
expect(log_lines.size).to eq(0)
|
377
378
|
end
|
378
379
|
end
|
@@ -8,14 +8,14 @@ describe NginxUtils do
|
|
8
8
|
it "should get status" do
|
9
9
|
body = "Active connections: 1 \nserver accepts handled requests\n 4 5 51 \nReading: 1 Writing: 3 Waiting: 2 \n"
|
10
10
|
status = {active_connection: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
|
11
|
-
response =
|
11
|
+
response = double("http response mock", body: body)
|
12
12
|
Net::HTTP.should_receive(:start).and_return(response)
|
13
13
|
expect(NginxUtils::Status.get).to eq(status)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should generate an exception if status get fails" do
|
17
|
-
|
18
|
-
Net::HTTP.should_receive(:start).and_return(
|
17
|
+
response = double("http response mock", body: "invalid content")
|
18
|
+
Net::HTTP.should_receive(:start).and_return(response)
|
19
19
|
expect(proc{NginxUtils::Status.get}).to raise_error("Nginx status get failed")
|
20
20
|
end
|
21
21
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nginx_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -89,10 +89,12 @@ files:
|
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
91
|
- lib/nginx_utils.rb
|
92
|
+
- lib/nginx_utils/logreader.rb
|
92
93
|
- lib/nginx_utils/logrotate.rb
|
93
94
|
- lib/nginx_utils/status.rb
|
94
95
|
- lib/nginx_utils/version.rb
|
95
96
|
- nginx_utils.gemspec
|
97
|
+
- spec/nginx_utils/logreader_spec.rb
|
96
98
|
- spec/nginx_utils/logrotate_spec.rb
|
97
99
|
- spec/nginx_utils/status_spec.rb
|
98
100
|
- spec/spec_helper.rb
|
@@ -111,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
113
|
version: '0'
|
112
114
|
segments:
|
113
115
|
- 0
|
114
|
-
hash:
|
116
|
+
hash: -10429621290292328
|
115
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
118
|
none: false
|
117
119
|
requirements:
|
@@ -120,14 +122,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
122
|
version: '0'
|
121
123
|
segments:
|
122
124
|
- 0
|
123
|
-
hash:
|
125
|
+
hash: -10429621290292328
|
124
126
|
requirements: []
|
125
127
|
rubyforge_project:
|
126
|
-
rubygems_version: 1.8.
|
128
|
+
rubygems_version: 1.8.25
|
127
129
|
signing_key:
|
128
130
|
specification_version: 3
|
129
131
|
summary: Nginx utilities
|
130
132
|
test_files:
|
133
|
+
- spec/nginx_utils/logreader_spec.rb
|
131
134
|
- spec/nginx_utils/logrotate_spec.rb
|
132
135
|
- spec/nginx_utils/status_spec.rb
|
133
136
|
- spec/spec_helper.rb
|