serverspec-runner 0.1.8 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec59c8a9e9610ae3bb2e6666409ff2c1c3d04661
4
- data.tar.gz: 28f8e6263c2ec3ac773d6a75de7ea1e57151b15a
3
+ metadata.gz: deb6b6b88775846ef3f209bde2980e2941af4f86
4
+ data.tar.gz: c31f9fa20adbc3e2ead705ef3f2ffb0c45f034ad
5
5
  SHA512:
6
- metadata.gz: 7328503d1082c0f901922514368a75c97554632aa29a25781cc2a565db4e65ca1e00a86587d551523b6055cb7c9a82352dde8bd4da6715857106f4fc7d61a630
7
- data.tar.gz: c8484c391ea579961f4915413f3342de60b129a19eae66e51beca36b203008bafc5c7f3efeb4a2dae2adeb625194bab0c08a19e9f19b578446f66717e08ae8b7
6
+ metadata.gz: 4e774ddd841a66cc479f81b6f9c2973aa0dfe5e75c46a40cc5943ecabce0ec881fca0bd5a7064eb6e372846f9e05bfb07d081aaacb8e4703cc40f0fa0e547603
7
+ data.tar.gz: 26a1ce6ae5f55017bff20eee4ed9c1fc2faf04cd7192a40148dfd362e3c4db941666aba7bce9beea06a5d96cf206e135bed204bf317c162b282fe43badd5fc65
data/README.md CHANGED
@@ -59,66 +59,110 @@ For example. You write serverspec code like this.
59
59
 
60
60
  ```
61
61
  describe "nginx" do
62
- describe "check install" do
63
- describe package('nginx') do
64
- it { should be_installed }
65
- end
66
- end
67
-
62
+
68
63
  describe "check running" do
69
64
  describe process('nginx') do
70
65
  it { should be_running }
71
66
  end
72
67
  end
73
-
74
- describe "check process" do
75
- describe process('nginx') do
76
- it { should be_enabled }
77
- end
78
- end
79
68
 
80
- describe "worker_connection:1024" do
81
- describe file('/etc/nginx/nginx.conf') do
82
- it { should contain "worker_connections 1024;" }
83
- end
84
- end
85
-
86
- describe "logrotate interval" do
87
- describe file('/etc/logrotate.d/nginx') do
88
- it { should contain "rotate 14" }
89
- end
69
+ describe file('/etc/logrotate.d/nginx') do
70
+ it { should be_file }
71
+ it { should contain "rotate 14" }
90
72
  end
91
73
  end
92
74
  ```
93
75
 
94
76
  You can get the following outputs.
95
77
 
78
+ * serverspec-runner -t aa : asci-art table(default)
96
79
  ```
97
- +------------------------------------------+
98
- |description | result |
99
- +------------------------------------------+
100
- |example@anyhost-01(172.30.4.131) | |
101
- | nginx check install | OK |
102
- | nginx check running | NG |
103
- | nginx check process | OK |
104
- | nginx worker_connection:1024 | OK |
105
- | nginx logrotate interval | NG |
106
- |example@anyhost-02(172.30.4.171) | |
107
- | nginx check install | OK |
108
- | nginx check running | NG |
109
- | nginx check process | OK |
110
- | nginx worker_connection:1024 | OK |
111
- | nginx logrotate interval | NG |
112
- +------------------------------------------+
80
+ +-------------------------------------------+
81
+ |description | result |
82
+ +-------------------------------------------+
83
+ |example@anyhost-01(192.168.11.1) | |
84
+ | nginx | |
85
+ | check running | |
86
+ | Process "nginx" | |
87
+ | should be running | OK |
88
+ | File "/etc/logrotate.d/nginx" | |
89
+ | should be file | OK |
90
+ | should contain "rotate 14" | OK |
91
+ |example@anyhost-02(192.168.11.2) | |
92
+ | nginx | |
93
+ | check running | |
94
+ | Process "nginx" | |
95
+ | should be running | OK |
96
+ | File "/etc/logrotate.d/nginx" | |
97
+ | should be file | OK |
98
+ | should contain "rotate 14" | NG |
99
+ +-------------------------------------------+
100
+ ```
101
+
102
+ * serverspec-runner -t mkd : markdown table format
103
+ ```
104
+ |description | result |
105
+ |:---------------------------------|:------:|
106
+ |example@anyhost-01(192.168.11.1) | |
107
+ | nginx | |
108
+ | check running | |
109
+ | Process "nginx" | |
110
+ | should be running | OK |
111
+ | File "/etc/logrotate.d/nginx" | |
112
+ | should be file | OK |
113
+ | should contain "rotate 14" | OK |
114
+ |example@anyhost-02(192.168.11.2) | |
115
+ | nginx | |
116
+ | check running | |
117
+ | Process "nginx" | |
118
+ | should be running | OK |
119
+ | File "/etc/logrotate.d/nginx" | |
120
+ | should be file | OK |
121
+ | should contain "rotate 14" | NG |
122
+ ```
123
+
124
+ this example parsed for markdown to that(use -e long option)
113
125
 
126
+ |description | result |
127
+ |:----------------------------------------------------------------|:------:|
128
+ |example@anyhost-01(192.168.11.1) | |
129
+ | nginx check running Process "nginx" should be running | OK |
130
+ | nginx File "/etc/logrotate.d/nginx" should be file | OK |
131
+ | nginx File "/etc/logrotate.d/nginx" should contain "rotate 14" | OK |
132
+ |example@anyhost-01(192.168.11.2) | |
133
+ | nginx check running Process "nginx" should be running | OK |
134
+ | nginx File "/etc/logrotate.d/nginx" should be file | OK |
135
+ | nginx File "/etc/logrotate.d/nginx" should contain "rotate 14" | NG |
136
+
137
+ * serverspec-runner -t bool : only 'ok' or 'ng' result string.t
138
+ You can use for cluster monitoring system health.
139
+
140
+ ```
141
+ ng
114
142
  ```
115
143
 
116
- You can use -e option and choose following outputs.
144
+ * serverspec-runner -t csv : CSV file format
145
+ You can get result CSV format output and can use redirect to file.
117
146
 
118
- * aa : asci-art table(default)
119
- * mkd : markdown table format
120
- * csv : CSV file format
121
- * bool: only 'ok' or 'ng' result string.
147
+ ```
148
+ description,,,,,result
149
+ example@anyhost-01(192.168.11.1),,,,,
150
+ ,nginx,,,,
151
+ ,,check running,,,
152
+ ,,,Process "nginx",,
153
+ ,,,,should be running,OK
154
+ ,,File "/etc/logrotate.d/nginx",,,
155
+ ,,,should be file,,OK
156
+ ,,,should contain "rotate 14",,OK
157
+ example@anyhost-02(192.168.11.2),,,,,
158
+ ,nginx,,,,
159
+ ,,check running,,,
160
+ ,,,Process "nginx",,
161
+ ,,,,should be running,OK
162
+ ,,File "/etc/logrotate.d/nginx",,,
163
+ ,,,should be file,,OK
164
+ ,,,should contain "rotate 14",,NG
165
+ ```
122
166
 
123
167
  For more detail. You can see from `serverspec-runner -h` command.
124
168
 
data/Rakefile CHANGED
@@ -23,7 +23,7 @@ namespace :spec do
23
23
  ENV['result_csv'] = ENV['result_csv'] || './_serverspec_result.csv'
24
24
  csv_file = ENV['result_csv']
25
25
  CSV.open(csv_file, 'w') { |w| w << ['description', 'result'] }
26
- ENV['explain'] = ENV['explain'] || "long"
26
+ ENV['explain'] = ENV['explain'] || "short"
27
27
  ENV['tableformat'] = ENV['tableformat'] || "aa"
28
28
  ENV['scenario'] = ENV['scenario'] || "./scenario.yml"
29
29
 
@@ -56,10 +56,11 @@ namespace :spec do
56
56
 
57
57
  if abs_node.kind_of?(Hash)
58
58
  abs_node.keys.each do |n|
59
- path << '::' unless path.empty?
60
- path << n.to_s
61
- path = gen_exec_plan(abs_node, n, path, ssh_options, tasks, platform)
59
+ path.push(n.to_s)
60
+ gen_exec_plan(abs_node, n, path, ssh_options, tasks, platform)
62
61
  end
62
+
63
+ path.pop
63
64
  elsif abs_node.kind_of?(Array)
64
65
  abs_node.each do |host_alias|
65
66
  if platform.include?(host_alias.to_sym)
@@ -70,12 +71,11 @@ namespace :spec do
70
71
  end
71
72
 
72
73
  platform[host_alias.to_sym][:ssh_opts].each { |k, v| ssh_options[k.to_sym] = v } if platform[host_alias.to_sym].include?(:ssh_opts)
73
-
74
- tasks << "#{path}::#{host_alias}"
74
+ tasks << "#{path.join('::')}::#{host_alias}"
75
75
  end
76
- end
77
76
 
78
- return path
77
+ path.pop
78
+ end
79
79
  end
80
80
 
81
81
  def exec_tasks(parent, node, real_path, platform)
@@ -85,18 +85,16 @@ namespace :spec do
85
85
  else
86
86
  abs_node = parent[node]
87
87
  end
88
-
88
+
89
89
  if abs_node.kind_of?(Hash)
90
90
  abs_node.keys.each do |n|
91
- real_path << n
92
- real_path = exec_tasks(abs_node, n, real_path, platform)
91
+ real_path.push(n)
92
+ exec_tasks(abs_node, n, real_path, platform)
93
93
  end
94
+
95
+ real_path.pop
94
96
  elsif abs_node.kind_of?(Array)
95
- task_path = ''
96
- real_path.map.each do |p|
97
- task_path << '::' unless task_path.empty?
98
- task_path << p
99
- end
97
+ task_path = "#{real_path.join('::')}"
100
98
 
101
99
  abs_node.each do |host_alias|
102
100
  desc "Run serverspec to #{task_path}@#{host_alias}"
@@ -116,10 +114,8 @@ namespace :spec do
116
114
  end
117
115
  end
118
116
 
119
- return []
117
+ real_path.pop
120
118
  end
121
-
122
- return real_path
123
119
  end
124
120
 
125
121
  if !Dir.exists?(ENV['specpath'])
@@ -171,7 +167,7 @@ namespace :spec do
171
167
  end
172
168
 
173
169
  tasks = []
174
- gen_exec_plan(nil, scenarios, '', ssh_options, tasks, platform)
170
+ gen_exec_plan(nil, scenarios, [], ssh_options, tasks, platform)
175
171
 
176
172
  task :stdout do
177
173
 
@@ -184,8 +180,16 @@ namespace :spec do
184
180
 
185
181
  puts ret
186
182
  elsif ENV['tableformat'] == 'csv'
183
+
184
+ maxrows = 0
187
185
  CSV.foreach(csv_file) do |r|
188
- puts "#{r[0].strip}#{r[1].empty? ? '': ','}#{r[1]}"
186
+ maxrows = r[2].to_i if r[2].to_i > maxrows
187
+ end
188
+ maxrows += 1 # host row
189
+
190
+ CSV.foreach(csv_file) do |r|
191
+ pad_comma = ',' * (maxrows - r[0].split(',').length)
192
+ puts "#{r[0]}#{pad_comma},#{r[1]}"
189
193
  end
190
194
  else
191
195
  maxlen = 0
@@ -193,9 +197,9 @@ namespace :spec do
193
197
  n = r[0].each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
194
198
  maxlen = n if n > maxlen
195
199
  end
196
-
200
+
197
201
  pad_spaces = 4
198
-
202
+
199
203
  spacer = nil
200
204
  if ENV['tableformat'] == 'mkd'
201
205
  spacer = "|:" + ("-" * maxlen) + "|:" + ("-" * "result".length) + ":|"
@@ -207,19 +211,22 @@ namespace :spec do
207
211
  is_header = true
208
212
  CSV.foreach(csv_file) do |r|
209
213
  n = r[0].each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
210
- pad_mid = (" " * (maxlen - n)) + " | "
211
- pad_tail = (" " * ("result".length - r[1].length)) + " |"
212
214
 
213
215
  if r[1] == 'OK'
214
216
  s_effect = "\e[32m"
215
217
  e_effect = "\e[m"
218
+ r[1] = ' ' + r[1]
216
219
  elsif r[1] == 'NG'
217
220
  s_effect = "\e[31m"
218
221
  e_effect = "\e[m"
222
+ r[1] = ' ' + r[1]
219
223
  end
220
-
224
+
225
+ pad_mid = (" " * (maxlen - n)) + " | "
226
+ pad_tail = (" " * ("result".length - r[1].length)) + " |"
227
+
221
228
  puts "|#{s_effect}#{r[0]}#{e_effect}#{pad_mid}#{s_effect}#{r[1]}#{e_effect}#{pad_tail}"
222
-
229
+
223
230
  if is_header
224
231
  puts spacer
225
232
  is_header = false
@@ -12,12 +12,11 @@ opts = GetoptLong.new(
12
12
  ["--scenario", "-s", GetoptLong::REQUIRED_ARGUMENT],
13
13
  ["--specroot", "-r", GetoptLong::REQUIRED_ARGUMENT],
14
14
  ["--ssh_options", "-o", GetoptLong::REQUIRED_ARGUMENT],
15
- ["--result_csv", "-c", GetoptLong::REQUIRED_ARGUMENT],
16
15
  ["--explain", "-e", GetoptLong::REQUIRED_ARGUMENT],
17
16
  ["--tableformat", "-t", GetoptLong::REQUIRED_ARGUMENT],
18
17
  ["--help", "-h", GetoptLong::NO_ARGUMENT],
19
18
  ["--tmpdir", "-1", GetoptLong::REQUIRED_ARGUMENT],
20
- ["--task", "-2", GetoptLong::REQUIRED_ARGUMENT]
19
+ ["--tasks", "-T", GetoptLong::REQUIRED_ARGUMENT]
21
20
  )
22
21
 
23
22
  opts.each do |opt, arg|
@@ -28,24 +27,22 @@ opts.each do |opt, arg|
28
27
  ENV['specroot'] = arg
29
28
  when '--ssh_options'
30
29
  ENV['ssh_options'] = arg
31
- when '--result_csv'
32
- ENV['result_csv'] = arg
33
30
  when '--explain'
34
31
  ENV['explain'] = arg
35
32
  when '--tableformat'
36
33
  ENV['tableformat'] = arg
37
34
  when '--tmpdir'
38
35
  ENV['tmpdir'] = arg
39
- when '--task'
36
+ when '--tasks'
40
37
  raketask += "::" + arg
41
38
  else
42
39
  puts "Usage: serverspec-runner (options)"
43
40
  puts "-s, --scenario SCENARIO_FILE path to scenario yml file"
44
41
  puts "-r, --specroot SPEC_ROOT path to spec tests root dir"
45
42
  puts "-o, --ssh_options SSH_OPTIONS_FILE path to ssh options yml file"
46
- puts "-c, --result_csv RESULT_CSV_FILE path to result csv file"
47
- puts "-e, --explain (short|long) specify result explain length(default: long)"
43
+ puts "-e, --explain (short|long) specify result explain length(default: short)"
48
44
  puts "-t, --tableformat (aa|mkd|csv|bool) specify result table type(default: aa)"
45
+ puts "-T, --tasks display the tasks with descriptions(exec rake -T)"
49
46
  puts "-h, --help show help"
50
47
  exit 0
51
48
  end
@@ -1,3 +1,3 @@
1
1
  module ServerspecRunner
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2.3"
3
3
  end
@@ -1,10 +1,28 @@
1
1
  require "#{File.dirname(__FILE__)}/../spec_helper"
2
2
 
3
- describe "example" do
3
+ describe package('httpd'), :if => os[:family] == 'redhat' do
4
+ it { should be_installed }
5
+ end
6
+
7
+ describe package('apache2'), :if => os[:family] == 'ubuntu' do
8
+ it { should be_installed }
9
+ end
10
+
11
+ describe service('httpd'), :if => os[:family] == 'redhat' do
12
+ it { should be_enabled }
13
+ it { should be_running }
14
+ end
15
+
16
+ describe service('apache2'), :if => os[:family] == 'ubuntu' do
17
+ it { should be_enabled }
18
+ it { should be_running }
19
+ end
20
+
21
+ describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do
22
+ it { should be_enabled }
23
+ it { should be_running }
24
+ end
4
25
 
5
- describe "hostname実行" do
6
- describe command("hostname") do
7
- it { eq 0 }
8
- end
9
- end
26
+ describe port(80) do
27
+ it { should be_listening }
10
28
  end
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,17 @@ ssh_opts_default = YAML.load_file(ENV['ssh_options'])
9
9
  csv_path = ENV['result_csv']
10
10
  explains = []
11
11
  results = []
12
+ row_num = []
13
+ spacer_char = ' ' unless ENV['tableformat'] == 'csv'
14
+ spacer_char = ',' if ENV['tableformat'] == 'csv'
15
+
16
+ def get_example_desc(example_group, descriptions)
17
+
18
+ descriptions << example_group[:description]
19
+ return descriptions if example_group[:parent_example_group] == nil
20
+
21
+ get_example_desc(example_group[:parent_example_group], descriptions)
22
+ end
12
23
 
13
24
  RSpec.configure do |c|
14
25
 
@@ -48,6 +59,8 @@ RSpec.configure do |c|
48
59
  set :backend, :exec
49
60
  end
50
61
 
62
+ prev_desc_hierarchy = nil
63
+
51
64
  c.before(:suite) do
52
65
  entity_host = (((ENV['TARGET_HOST'] != ENV['TARGET_SSH_HOST']) && (ENV['TARGET_SSH_HOST'] != nil)) ? "(#{ENV['TARGET_SSH_HOST']})" : "")
53
66
  puts "\e[33m"
@@ -56,32 +69,42 @@ RSpec.configure do |c|
56
69
 
57
70
  explains << "#{role_name}@#{ENV['TARGET_HOST']}#{entity_host}"
58
71
  results << ""
72
+ row_num << 1
59
73
  end
60
74
 
61
75
  c.after(:each) do
76
+
62
77
  if ENV['explain'] == 'long'
63
- explains << " " + example.metadata[:full_description] + (RSpec::Matchers.generated_description || '')
78
+ explains << spacer_char + example.metadata[:full_description] + (RSpec::Matchers.generated_description || '')
79
+ results << (self.example.exception ? 'NG' : 'OK')
80
+ row_num << 1
64
81
  else
65
82
 
66
- second_depth = self.example.metadata.depth - 3
67
- h = self.example.metadata
68
-
69
- second_depth.times do |i|
70
- h = h[:example_group]
83
+ spacer = ''
84
+ desc_hierarchy = get_example_desc(self.example.metadata[:example_group], []).reverse
85
+ desc_hierarchy.each_with_index do |ex, i|
86
+ spacer += spacer_char
87
+
88
+ if prev_desc_hierarchy != nil && prev_desc_hierarchy.length > i && prev_desc_hierarchy[i] == desc_hierarchy[i]
89
+ else
90
+ explains << spacer + ex
91
+ results << ''
92
+ row_num << i + 1
93
+ end
71
94
  end
72
95
 
73
- second_desc = h[:description]
74
- first_desc = h[:example_group][:description]
96
+ explains << spacer + spacer_char + RSpec::Matchers.generated_description
97
+ results << (self.example.exception ? 'NG' : 'OK')
98
+ row_num << desc_hierarchy.length + 1
75
99
 
76
- explains << " " + first_desc + " " + second_desc
100
+ prev_desc_hierarchy = desc_hierarchy
77
101
  end
78
- results << (self.example.exception ? 'NG' : 'OK')
79
102
  end
80
103
 
81
104
  c.after(:suite) do
82
105
  CSV.open(csv_path, 'a') do |writer|
83
106
  explains.each_with_index do |v, i|
84
- writer << [v, results[i]]
107
+ writer << [v, results[i], row_num[i]]
85
108
  end
86
109
  end
87
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serverspec-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - hiracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-10 00:00:00.000000000 Z
11
+ date: 2014-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: serverspec