ansible_spec 0.2.4 → 0.2.5

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: c4883e779389ae7096bdce09a7e572a699f86f1c
4
- data.tar.gz: 144e14d67f8d46b48fb0f80b29bb79ea32510d27
3
+ metadata.gz: 4e5c10ea9ab4741a2c8f99e20261cbbad47af7fe
4
+ data.tar.gz: 572f3ddf20328f19eccd4e1107162b00cf881c3b
5
5
  SHA512:
6
- metadata.gz: 8f91185a22486befc9314296258734fad2848e2ee533ffe4800eb3147eeca70524e4bbbcc513f02429a16bbb9436e8545311f4289759a418f9af79fdba84dae1
7
- data.tar.gz: ec415b24ec8dd9c9f97e0c3c89a88382c223b3f0754c224ef92b692bf329fc643710435111a2559ba1c77c094ba91cb17d48fb0945a7d5b158c8b62f537a075e
6
+ metadata.gz: b266c6100fac87326945d7c1c6c0f49a9dd45092b5bb6d6d02e619088a6f278d29a43078250c9a998393a94b5a644b8f4f8ac9f5e183660fdda70816bbe87d99
7
+ data.tar.gz: 34fc3ec00c8d71de98c10eb6328d0cac4607c2fa82782c612d0e115e873584ae722aa0d4c68a1a7b878eef6a02e782572e79d117a5759ef1d3b595833aa1f43e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ # v0.2.5
2
+ - Fix [#51 Handle hosts which are not assigned to any group] by [Gerrrr](https://github.com/Gerrrr)
3
+
1
4
  # v0.2.4
2
5
  - Fix [#50 do not fail on no hosts matched](https://github.com/volanja/ansible_spec/pull/50) by [phiche](https://github.com/phiche)
3
6
 
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ansible_spec.gemspec
4
4
  gemspec
5
+
6
+ if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0')
7
+ # net-ssh 3.x dropped Ruby 1.8 and 1.9 support.
8
+ gem 'net-ssh', '~> 2.7'
9
+ end
@@ -11,8 +11,10 @@ module AnsibleSpec
11
11
  return get_dynamic_inventory(file)
12
12
  end
13
13
  f = File.open(file).read
14
- res = Hash.new
14
+ groups = Hash.new
15
15
  group = ''
16
+ hosts = Hash.new
17
+ hosts.default = Hash.new
16
18
  f.each_line{|line|
17
19
  line = line.chomp
18
20
  # skip
@@ -22,45 +24,56 @@ module AnsibleSpec
22
24
  # get group
23
25
  if line.start_with?('[') && line.end_with?(']')
24
26
  group = line.gsub('[','').gsub(']','')
25
- res["#{group}"] = Array.new
27
+ groups["#{group}"] = Array.new
26
28
  next
27
29
  end
28
30
 
29
- #get host
31
+ # get host
32
+ host_name = line.split[0]
30
33
  if group.empty? == false
31
- host = Hash.new
32
- # 1つのみ、かつ:を含まない場合
33
- if line.split.count == 1 && !line.include?(":")
34
- # 192.168.0.1
35
- res["#{group}"] << line
34
+ if groups.has_key?(line)
35
+ groups["#{group}"] << line
36
36
  next
37
- elsif line.split.count == 1 && line.include?("[") && line.include?("]")
37
+ elsif host_name.include?("[") && host_name.include?("]")
38
38
  # www[01:50].example.com
39
39
  # db-[a:f].example.com
40
40
  hostlist_expression(line,":").each{|h|
41
- res["#{group}"] << h
41
+ host = hosts[h.split[0]]
42
+ groups["#{group}"] << get_inventory_param(h).merge(host)
42
43
  }
43
44
  next
44
45
  else
45
- res["#{group}"] << get_inventory_param(line)
46
+ # 1つのみ、かつ:を含まない場合
47
+ # 192.168.0.1
48
+ # 192.168.0.1 ansible_ssh_host=127.0.0.1 ...
49
+ host = hosts[host_name]
50
+ groups["#{group}"] << get_inventory_param(line).merge(host)
46
51
  next
47
52
  end
53
+ else
54
+ if host_name.include?("[") && host_name.include?("]")
55
+ hostlist_expression(line, ":").each{|h|
56
+ hosts[h.split[0]] = get_inventory_param(h)
57
+ }
58
+ else
59
+ hosts[host_name] = get_inventory_param(line)
60
+ end
48
61
  end
49
62
  }
50
63
 
51
64
  # parse children [group:children]
52
65
  search = Regexp.new(":children".to_s)
53
- res.keys.each{|k|
66
+ groups.keys.each{|k|
54
67
  unless (k =~ search).nil?
55
68
  # get group parent & merge parent
56
- res.merge!(get_parent(res,search,k))
69
+ groups.merge!(get_parent(groups,search,k))
57
70
  # delete group children
58
- if res.has_key?("#{k}") && res.has_key?("#{k.gsub(search,'')}")
59
- res.delete("#{k}")
71
+ if groups.has_key?("#{k}") && groups.has_key?("#{k.gsub(search,'')}")
72
+ groups.delete("#{k}")
60
73
  end
61
74
  end
62
75
  }
63
- return res
76
+ return groups
64
77
  end
65
78
 
66
79
  # param hash {"server"=>["192.168.0.103"], "databases"=>["192.168.0.104"], "pg:children"=>["server", "databases"]}
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
data/lib/src/Rakefile CHANGED
@@ -13,18 +13,12 @@ namespace :serverspec do
13
13
  desc "Run serverspec for #{property["name"]}"
14
14
  RSpec::Core::RakeTask.new(property["name"].to_sym) do |t|
15
15
  puts "Run serverspec for #{property["name"]} to #{host}"
16
- if host.instance_of?(Hash)
17
- ENV['TARGET_HOST'] = host["uri"]
18
- ENV['TARGET_PORT'] = host["port"].to_s
19
- ENV['TARGET_PRIVATE_KEY'] = host["private_key"]
20
- unless host["user"].nil?
21
- ENV['TARGET_USER'] = host["user"]
22
- else
23
- ENV['TARGET_USER'] = property["user"]
24
- end
16
+ ENV['TARGET_HOST'] = host["uri"]
17
+ ENV['TARGET_PORT'] = host["port"].to_s
18
+ ENV['TARGET_PRIVATE_KEY'] = host["private_key"]
19
+ unless host["user"].nil?
20
+ ENV['TARGET_USER'] = host["user"]
25
21
  else
26
- ENV['TARGET_HOST'] = host
27
- ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
28
22
  ENV['TARGET_USER'] = property["user"]
29
23
  end
30
24
  t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
@@ -29,6 +29,8 @@ EOF
29
29
  EOF
30
30
 
31
31
  content_h = <<'EOF'
32
+ node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55
33
+
32
34
  [normal]
33
35
  192.168.0.1
34
36
  192.168.0.2 ansible_ssh_port=22
@@ -36,8 +38,9 @@ EOF
36
38
  192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa
37
39
  192.168.0.5 ansible_ssh_user=git
38
40
  jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
39
-
41
+ node
40
42
  EOF
43
+
41
44
  create_file(tmp_ansiblespec,content)
42
45
  create_file(tmp_playbook,content_p)
43
46
  create_file(tmp_hosts,content_h)
@@ -65,8 +68,9 @@ describe "load_targetsの実行" do
65
68
 
66
69
  it 'normal 192.168.0.1' do
67
70
  obj = @res['normal'][0]
68
- expect(obj.instance_of?(String)).to be_truthy
69
- expect(obj).to eq '192.168.0.1'
71
+ expect(obj.instance_of?(Hash)).to be_truthy
72
+ expect(obj['uri']).to eq '192.168.0.1'
73
+ expect(obj['port']).to eq 22
70
74
  end
71
75
  it 'normal 192.168.0.2 ansible_ssh_port=22' do
72
76
  obj = @res['normal'][1]
@@ -104,6 +108,14 @@ describe "load_targetsの実行" do
104
108
  expect(obj['port']).to eq 5555
105
109
  end
106
110
 
111
+ it 'node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55' do
112
+ obj = @res['normal'][6]
113
+ expect(obj.instance_of?(Hash)).to be_truthy
114
+ expect(obj['name']).to eq 'node ansible_ssh_port=4444 ansible_ssh_host=192.168.1.55'
115
+ expect(obj['uri']).to eq '192.168.1.55'
116
+ expect(obj['port']).to eq 4444
117
+ end
118
+
107
119
  after do
108
120
  File.delete(tmp_hosts)
109
121
  end
@@ -141,7 +153,9 @@ describe "get_propertiesの実行" do
141
153
 
142
154
  it 'normal 192.168.0.1' do
143
155
  expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
144
- expect(@res[0]['hosts'][0]).to eq '192.168.0.1'
156
+ expect(@res[0]['hosts'][0]).to include({'name' => '192.168.0.1',
157
+ 'uri' => '192.168.0.1',
158
+ 'port' => 22})
145
159
  end
146
160
 
147
161
  it 'normal 192.168.0.2 ansible_ssh_port=22' do
@@ -35,13 +35,19 @@ EOF
35
35
  end
36
36
 
37
37
  it 'exist 1st server' do
38
- expect(@res['server'][0]).to eq '192.168.0.1'
38
+ expect(@res['server'][0]).to include({'uri' => '192.168.0.1',
39
+ 'name' => '192.168.0.1',
40
+ 'port' => 22})
39
41
  end
40
42
  it 'exist 2nd server' do
41
- expect(@res['server'][1]).to eq '192.168.0.2'
43
+ expect(@res['server'][1]).to include({'uri' => '192.168.0.2',
44
+ 'name' => '192.168.0.2',
45
+ 'port' => 22})
42
46
  end
43
47
  it 'exist 3rd server' do
44
- expect(@res['server'][2]).to eq 'example.com'
48
+ expect(@res['server'][2]).to include({'uri' => 'example.com',
49
+ 'name' => 'example.com',
50
+ 'port' => 22})
45
51
  end
46
52
  it 'not exist 4th server' do
47
53
  expect(@res['server'][3]).to eq nil
@@ -82,10 +88,14 @@ EOF
82
88
  expect(@res.key?('web')).to be_truthy
83
89
  end
84
90
  it 'exist 1st web' do
85
- expect(@res['web'][0]).to eq '192.168.0.3'
91
+ expect(@res['web'][0]).to include({'name' => '192.168.0.3',
92
+ 'uri' => '192.168.0.3',
93
+ 'port' => 22})
86
94
  end
87
95
  it 'exist 2nd web' do
88
- expect(@res['web'][1]).to eq '192.168.0.4'
96
+ expect(@res['web'][1]).to include({'name' => '192.168.0.4',
97
+ 'uri' => '192.168.0.4',
98
+ 'port' => 22})
89
99
  end
90
100
  it 'not exist 3rd web' do
91
101
  expect(@res['web'][2]).to eq nil
@@ -96,10 +106,14 @@ EOF
96
106
  expect(@res.key?('db')).to be_truthy
97
107
  end
98
108
  it 'exist 1st db' do
99
- expect(@res['db'][0]).to eq '192.168.0.5'
109
+ expect(@res['db'][0]).to include({'name' => '192.168.0.5',
110
+ 'uri' => '192.168.0.5',
111
+ 'port' => 22})
100
112
  end
101
113
  it 'exist 2nd db' do
102
- expect(@res['db'][1]).to eq '192.168.0.6'
114
+ expect(@res['db'][1]).to include({'name' => '192.168.0.6',
115
+ 'uri' => '192.168.0.6',
116
+ 'port' => 22})
103
117
  end
104
118
  it 'not exist 3rd db' do
105
119
  expect(@res['db'][2]).to eq nil
@@ -114,10 +128,14 @@ EOF
114
128
  tmp_hosts = 'hosts'
115
129
  before do
116
130
  content = <<'EOF'
131
+ node[01:20] ansible_ssh_port=2222
132
+
117
133
  [web]
118
134
  www[01:50].example.com
119
135
  [databases]
120
136
  db-[a:f].example.com
137
+ [nodes]
138
+ node[01:20]
121
139
  EOF
122
140
  create_file(tmp_hosts,content)
123
141
  @res = AnsibleSpec.load_targets(tmp_hosts)
@@ -128,7 +146,7 @@ EOF
128
146
  end
129
147
 
130
148
  it 'check 1 group' do
131
- expect(@res.length).to eq 2
149
+ expect(@res.length).to eq 3
132
150
  end
133
151
 
134
152
  it 'check group name' do
@@ -139,14 +157,27 @@ EOF
139
157
  it 'www[01:50].example.com' do
140
158
  1.upto(50){|n|
141
159
  leading_zero = n.to_s.rjust(2, '0')
142
- expect(@res['web']["#{n - 1}".to_i]).to eq "www#{leading_zero}.example.com"
160
+ expect(@res['web']["#{n - 1}".to_i]).to include({'name' => "www#{leading_zero}.example.com",
161
+ 'uri' => "www#{leading_zero}.example.com",
162
+ 'port' => 22})
143
163
  }
144
164
  end
145
165
 
146
166
  it 'db-[a:f].example.com' do
147
167
  alphabet = [*'a'..'f'] # Array splat
148
168
  alphabet.each_with_index {|word, i|
149
- expect(@res['databases'][i]).to eq "db-#{word}.example.com"
169
+ expect(@res['databases'][i]).to include ({'name' => "db-#{word}.example.com",
170
+ 'uri' => "db-#{word}.example.com",
171
+ 'port' => 22})
172
+ }
173
+ end
174
+
175
+ it 'node[01:20]' do
176
+ 1.upto(20){|n|
177
+ leading_zero = n.to_s.rjust(2, '0')
178
+ expect(@res['nodes']["#{n - 1}".to_i]).to include({'name' => "node#{leading_zero} ansible_ssh_port=2222",
179
+ 'uri' => "node#{leading_zero}",
180
+ 'port' => 2222})
150
181
  }
151
182
  end
152
183
 
@@ -241,7 +272,9 @@ EOF
241
272
  expect(@res['server'][0]).not_to eq '192.168.0.10'
242
273
  end
243
274
  it 'exist 2nd server' do
244
- expect(@res['server'][0]).to eq '192.168.0.11'
275
+ expect(@res['server'][0]).to include({'name' => '192.168.0.11',
276
+ 'uri' => '192.168.0.11',
277
+ 'port' => 22})
245
278
  end
246
279
 
247
280
  after do
@@ -275,10 +308,14 @@ EOF
275
308
  expect(@res.key?('web')).to be_truthy
276
309
  end
277
310
  it 'exist 1st web' do
278
- expect(@res['web'][0]).to eq '192.168.0.3'
311
+ expect(@res['web'][0]).to include ({'name' => '192.168.0.3',
312
+ 'uri' => '192.168.0.3',
313
+ 'port' => 22})
279
314
  end
280
315
  it 'exist 2nd web' do
281
- expect(@res['web'][1]).to eq '192.168.0.4'
316
+ expect(@res['web'][1]).to include ({'name' => '192.168.0.4',
317
+ 'uri' => '192.168.0.4',
318
+ 'port' => 22})
282
319
  end
283
320
 
284
321
  after do
@@ -323,8 +360,10 @@ EOF
323
360
 
324
361
  it 'pg 192.168.0.103' do
325
362
  obj = @res['pg'][0]
326
- expect(obj.instance_of?(String)).to be_truthy
327
- expect(obj).to eq '192.168.0.103'
363
+ expect(obj.instance_of?(Hash)).to be_truthy
364
+ expect(obj).to include({'name' => '192.168.0.103',
365
+ 'uri' => '192.168.0.103',
366
+ 'port' => 22})
328
367
  end
329
368
 
330
369
  it 'pg 192.168.0.104 ansible_ssh_port=22' do
@@ -337,8 +376,10 @@ EOF
337
376
 
338
377
  it 'pg 192.168.0.105' do
339
378
  obj = @res['pg'][2]
340
- expect(obj.instance_of?(String)).to be_truthy
341
- expect(obj).to eq '192.168.0.105'
379
+ expect(obj.instance_of?(Hash)).to be_truthy
380
+ expect(obj).to include({'name' => '192.168.0.105',
381
+ 'uri' => '192.168.0.105',
382
+ 'port' => 22})
342
383
  end
343
384
 
344
385
  it 'pg 192.168.0.106 ansible_ssh_port=5555' do
@@ -1156,8 +1197,12 @@ EOF
1156
1197
 
1157
1198
  it 'exist hosts' do
1158
1199
  expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
1159
- expect(@res[0]['hosts'][0]).to eq '192.168.0.103'
1160
- expect(@res[0]['hosts'][1]).to eq '192.168.0.104'
1200
+ expect(@res[0]['hosts'][0]).to include({'name' => '192.168.0.103',
1201
+ 'uri' => '192.168.0.103',
1202
+ 'port' => 22})
1203
+ expect(@res[0]['hosts'][1]).to include({'name' => '192.168.0.104',
1204
+ 'uri' => '192.168.0.104',
1205
+ 'port' => 22})
1161
1206
  end
1162
1207
 
1163
1208
  it 'exist user' do
@@ -1237,7 +1282,18 @@ EOF
1237
1282
 
1238
1283
  it 'exist hosts' do
1239
1284
  expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
1240
- expect(['192.168.0.103','192.168.0.104','192.168.0.105','192.168.0.106']).to match_array(@res[0]['hosts'])
1285
+ expect([{'name' => '192.168.0.103',
1286
+ 'uri' => '192.168.0.103',
1287
+ 'port' => 22},
1288
+ {'name' => '192.168.0.104',
1289
+ 'uri' => '192.168.0.104',
1290
+ 'port' => 22},
1291
+ {'name' => '192.168.0.105',
1292
+ 'uri' => '192.168.0.105',
1293
+ 'port' => 22},
1294
+ {'name' => '192.168.0.106',
1295
+ 'uri' => '192.168.0.106',
1296
+ 'port' => 22}]).to match_array(@res[0]['hosts'])
1241
1297
  end
1242
1298
 
1243
1299
  it 'exist user' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - volanja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-05 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler