ansible_spec 0.0.1.4 → 0.1

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.
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.0.1.4"
2
+ VERSION = "0.1"
3
3
  end
@@ -0,0 +1,4 @@
1
+ ---
2
+ -
3
+ playbook: site.yml
4
+ inventory: hosts
data/lib/src/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require 'yaml'
4
+ require 'ansible_spec'
5
+
6
+ properties = AnsibleSpec.get_properties
7
+ # {"name"=>"Ansible-Sample-TDD", "hosts"=>["192.168.0.103","192.168.0.103"], "user"=>"root", "roles"=>["nginx", "mariadb"]}
8
+ # {"name"=>"Ansible-Sample-TDD", "hosts"=>[{"name" => "192.168.0.103:22","uri"=>"192.168.0.103","port"=>22, "private_key"=> "~/.ssh/id_rsa"}], "user"=>"root", "roles"=>["nginx", "mariadb"]}
9
+
10
+ namespace :serverspec do
11
+ properties.each do |property|
12
+ property["hosts"].each do |host|
13
+ desc "Run serverspec for #{property["name"]}"
14
+ RSpec::Core::RakeTask.new(property["name"].to_sym) do |t|
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
25
+ else
26
+ ENV['TARGET_HOST'] = host
27
+ ENV['TARGET_PRIVATE_KEY'] = '~/.ssh/id_rsa'
28
+ ENV['TARGET_USER'] = property["user"]
29
+ end
30
+ t.pattern = 'roles/{' + property["roles"].join(',') + '}/spec/*_spec.rb'
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,36 @@
1
+ require 'serverspec'
2
+ require 'net/ssh'
3
+
4
+ set :backend, :ssh
5
+
6
+ if ENV['ASK_SUDO_PASSWORD']
7
+ begin
8
+ require 'highline/import'
9
+ rescue LoadError
10
+ fail "highline is not available. Try installing it."
11
+ end
12
+ set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
13
+ else
14
+ set :sudo_password, ENV['SUDO_PASSWORD']
15
+ end
16
+
17
+ host = ENV['TARGET_HOST']
18
+
19
+ options = Net::SSH::Config.for(host)
20
+
21
+ options[:user] ||= ENV['TARGET_USER']
22
+ options[:port] ||= ENV['TARGET_PORT']
23
+ options[:keys] ||= ENV['TARGET_PRIVATE_KEY']
24
+
25
+ set :host, options[:host_name] || host
26
+ set :ssh_options, options
27
+
28
+ # Disable sudo
29
+ # set :disable_sudo, true
30
+
31
+
32
+ # Set environment variables
33
+ # set :env, :LANG => 'C', :LC_MESSAGES => 'C'
34
+
35
+ # Set PATH
36
+ # set :path, '/sbin:/usr/local/sbin:$PATH'
@@ -1,5 +1,6 @@
1
1
  # coding: utf-8
2
2
  require 'ansible_spec'
3
+ require 'diff/lcs'
3
4
 
4
5
  created_file = [
5
6
  "spec/spec_helper.rb",
@@ -11,20 +12,23 @@ created_dir = [
11
12
  ]
12
13
  test_dir = "tmp"
13
14
 
14
- describe "テスト" do
15
+ describe "モジュールの実行" do
15
16
  # テスト実行前
16
- before(:all) do
17
+ before do
18
+ $stdout = File.open("/dev/null", "w") #テスト実行中は標準出力は/dev/nullにする。
17
19
  FileUtils.mkdir_p(test_dir) unless FileTest.exist?(test_dir)
18
20
  Dir.chdir(test_dir) #tmp/に移動
19
21
  AnsibleSpec.main
20
22
  end
21
23
 
22
24
  # テスト実行後
23
- after(:all) do
25
+ after do
24
26
  created_file.each{|f| File.delete(f) }
25
27
  created_dir.each{|d| Dir.delete(d) }
26
28
  Dir.chdir("../")
27
- Dir.delete(test_dir)
29
+ FileUtils.remove_entry_secure(test_dir)
30
+ #Dir.delete(test_dir)
31
+ $stdout =STDOUT # テスト実行後は元に戻す
28
32
  end
29
33
 
30
34
  it "/tmpにディレクトリが作成されること" do
@@ -39,4 +43,22 @@ describe "テスト" do
39
43
  }
40
44
  end
41
45
 
46
+ it "ファイルがオリジナルと一致すること" do
47
+ created_file.each{|f|
48
+ expect(no_diff("../lib/src/"+f,f)).to be_truthy
49
+ }
50
+ end
51
+
52
+ end
53
+
54
+ # check diff
55
+ # if exists diff, return false
56
+ # if not exist diff, return true
57
+ def no_diff(src_file,dst_file)
58
+ src = File.open(src_file).read
59
+ dst = File.open(dst_file).read
60
+ if Diff::LCS.diff(src,dst).count == 0
61
+ return true
62
+ end
63
+ return false
42
64
  end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+ require 'ansible_spec'
3
+
4
+ describe "load_targetsの実行" do
5
+ context '正常系:単グループ:DynamicInventory' do
6
+ tmp_hosts = 'hosts'
7
+ before do
8
+ content_h = <<'EOF'
9
+ #!/bin/bash
10
+ echo '{"databases": {"hosts": ["host1.example.com", "host2.example.com"],"vars":{"a": true}}}'
11
+ EOF
12
+ create_file(tmp_hosts,content_h)
13
+ File.chmod(0755,tmp_hosts)
14
+ @res = AnsibleSpec.load_targets(tmp_hosts)
15
+ end
16
+
17
+ it 'res is hash' do
18
+ expect(@res.instance_of?(Hash)).to be_truthy
19
+ end
20
+
21
+ it 'exist 1 group' do
22
+ expect(@res.length).to eq 1
23
+ end
24
+
25
+ it 'exist group' do
26
+ expect(@res.key?('databases')).to be_truthy
27
+ end
28
+
29
+ it 'databases aaa.com' do
30
+ obj = @res['databases'][0]
31
+ expect(obj.instance_of?(String)).to be_truthy
32
+ expect(obj).to eq 'host1.example.com'
33
+ end
34
+
35
+ it 'databases bbb.com' do
36
+ obj = @res['databases'][1]
37
+ expect(obj.instance_of?(String)).to be_truthy
38
+ expect(obj).to eq 'host2.example.com'
39
+ end
40
+
41
+ after do
42
+ File.delete(tmp_hosts)
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,202 @@
1
+ # coding: utf-8
2
+ require 'ansible_spec'
3
+
4
+ def create_file(name,content)
5
+ File.open(name, 'w') do |f|
6
+ f.puts content
7
+ end
8
+ end
9
+
10
+ def ready_test
11
+ tmp_hosts = 'hosts'
12
+ tmp_ansiblespec = '.ansiblespec'
13
+ tmp_playbook = 'site.yml'
14
+
15
+ content = <<'EOF'
16
+ ---
17
+ -
18
+ playbook: site.yml
19
+ inventory: hosts
20
+ EOF
21
+
22
+ content_p = <<'EOF'
23
+ - name: Ansible-Sample-TDD
24
+ hosts: normal
25
+ user: root
26
+ roles:
27
+ - nginx
28
+ - mariadb
29
+ EOF
30
+
31
+ content_h = <<'EOF'
32
+ [normal]
33
+ 192.168.0.1
34
+ 192.168.0.2 ansible_ssh_port=22
35
+ 192.168.0.3:5309
36
+ 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa
37
+ 192.168.0.5 ansible_ssh_user=git
38
+ jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50
39
+
40
+ EOF
41
+ create_file(tmp_ansiblespec,content)
42
+ create_file(tmp_playbook,content_p)
43
+ create_file(tmp_hosts,content_h)
44
+ end
45
+
46
+ describe "load_targetsの実行" do
47
+ context '正常系:複数グループ:変数' do
48
+ tmp_hosts = 'hosts'
49
+ before do
50
+ ready_test
51
+ @res = AnsibleSpec.load_targets(tmp_hosts)
52
+ end
53
+
54
+ it 'res is hash' do
55
+ expect(@res.instance_of?(Hash)).to be_truthy
56
+ end
57
+
58
+ it 'exist 1 group' do
59
+ expect(@res.length).to eq 1
60
+ end
61
+
62
+ it 'exist group' do
63
+ expect(@res.key?('normal')).to be_truthy
64
+ end
65
+
66
+ it 'normal 192.168.0.1' do
67
+ obj = @res['normal'][0]
68
+ expect(obj.instance_of?(String)).to be_truthy
69
+ expect(obj).to eq '192.168.0.1'
70
+ end
71
+ it 'normal 192.168.0.2 ansible_ssh_port=22' do
72
+ obj = @res['normal'][1]
73
+ expect(obj.instance_of?(Hash)).to be_truthy
74
+ expect(obj['uri']).to eq '192.168.0.2'
75
+ expect(obj['port']).to eq 22
76
+ end
77
+ it 'normal 192.168.0.3:5309' do
78
+ obj = @res['normal'][2]
79
+ expect(obj.instance_of?(Hash)).to be_truthy
80
+ expect(obj['uri']).to eq '192.168.0.3'
81
+ expect(obj['port']).to eq 5309
82
+ end
83
+ it '192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do
84
+ obj = @res['normal'][3]
85
+ expect(obj.instance_of?(Hash)).to be_truthy
86
+ expect(obj['uri']).to eq '192.168.0.4'
87
+ expect(obj['port']).to eq 22
88
+ expect(obj['private_key']).to eq '~/.ssh/id_rsa'
89
+ end
90
+
91
+ it '192.168.0.5 ansible_ssh_user=git' do
92
+ obj = @res['normal'][4]
93
+ expect(obj.instance_of?(Hash)).to be_truthy
94
+ expect(obj['uri']).to eq '192.168.0.5'
95
+ expect(obj['port']).to eq 22
96
+ expect(obj['user']).to eq 'git'
97
+ end
98
+
99
+ it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
100
+ obj = @res['normal'][5]
101
+ expect(obj.instance_of?(Hash)).to be_truthy
102
+ expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50'
103
+ expect(obj['uri']).to eq '192.168.1.50'
104
+ expect(obj['port']).to eq 5555
105
+ end
106
+
107
+ after do
108
+ File.delete(tmp_hosts)
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "get_propertiesの実行" do
114
+ require 'yaml'
115
+ tmp_ansiblespec = '.ansiblespec'
116
+ tmp_playbook = 'site.yml'
117
+ tmp_hosts = 'hosts'
118
+
119
+ before do
120
+ ready_test
121
+ @res = AnsibleSpec.get_properties
122
+ end
123
+
124
+ context '正常系' do
125
+ it 'res is array' do
126
+ expect(@res.instance_of?(Array)).to be_truthy
127
+ end
128
+
129
+ it 'res[0] is hash' do
130
+ expect(@res[0].instance_of?(Hash)).to be_truthy
131
+ end
132
+
133
+ it 'check 4 group' do
134
+ expect(@res[0].length).to eq 4
135
+ end
136
+
137
+ it 'exist name' do
138
+ expect(@res[0].key?('name')).to be_truthy
139
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
140
+ end
141
+
142
+ it 'normal 192.168.0.1' do
143
+ expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
144
+ expect(@res[0]['hosts'][0]).to eq '192.168.0.1'
145
+ end
146
+
147
+ it 'normal 192.168.0.2 ansible_ssh_port=22' do
148
+ obj = @res[0]['hosts'][1]
149
+ expect(obj.instance_of?(Hash)).to be_truthy
150
+ expect(obj['uri']).to eq '192.168.0.2'
151
+ expect(obj['port']).to eq 22
152
+ end
153
+
154
+ it 'normal 192.168.0.3:5309' do
155
+ obj = @res[0]['hosts'][2]
156
+ expect(obj.instance_of?(Hash)).to be_truthy
157
+ expect(obj['uri']).to eq '192.168.0.3'
158
+ expect(obj['port']).to eq 5309
159
+ end
160
+ it 'normal 192.168.0.4 ansible_ssh_private_key_file=~/.ssh/id_rsa' do
161
+ obj = @res[0]['hosts'][3]
162
+ expect(obj.instance_of?(Hash)).to be_truthy
163
+ expect(obj['uri']).to eq '192.168.0.4'
164
+ expect(obj['port']).to eq 22
165
+ expect(obj['private_key']).to eq '~/.ssh/id_rsa'
166
+ end
167
+
168
+ it '192.168.0.5 ansible_ssh_user=git' do
169
+ obj = @res[0]['hosts'][4]
170
+ expect(obj.instance_of?(Hash)).to be_truthy
171
+ expect(obj['uri']).to eq '192.168.0.5'
172
+ expect(obj['port']).to eq 22
173
+ expect(obj['user']).to eq 'git'
174
+ end
175
+
176
+ it 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50' do
177
+ obj = @res[0]['hosts'][5]
178
+ expect(obj.instance_of?(Hash)).to be_truthy
179
+ expect(obj['name']).to eq 'jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50'
180
+ expect(obj['uri']).to eq '192.168.1.50'
181
+ expect(obj['port']).to eq 5555
182
+ end
183
+
184
+ it 'exist user' do
185
+ expect(@res[0].key?('user')).to be_truthy
186
+ expect(@res[0]['user']).to eq 'root'
187
+ end
188
+
189
+ it 'exist roles' do
190
+ expect(@res[0].key?('roles')).to be_truthy
191
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
192
+ expect(@res[0]['roles'][0]).to eq 'nginx'
193
+ expect(@res[0]['roles'][1]).to eq 'mariadb'
194
+ end
195
+
196
+ after do
197
+ File.delete(tmp_ansiblespec)
198
+ File.delete(tmp_playbook)
199
+ File.delete(tmp_hosts)
200
+ end
201
+ end
202
+ end
@@ -0,0 +1,752 @@
1
+ # coding: utf-8
2
+ require 'ansible_spec'
3
+
4
+ def create_file(name,content)
5
+ File.open(name, 'w') do |f|
6
+ f.puts content
7
+ end
8
+ end
9
+
10
+ describe "load_targetsの実行" do
11
+ context '正常系:1グループ' do
12
+ tmp_hosts = 'hosts'
13
+ before do
14
+ content = <<'EOF'
15
+ [server]
16
+ 192.168.0.1
17
+ 192.168.0.2
18
+ example.com
19
+
20
+ EOF
21
+ create_file(tmp_hosts,content)
22
+ @res = AnsibleSpec.load_targets(tmp_hosts)
23
+ end
24
+
25
+ it 'res is hash' do
26
+ expect(@res.instance_of?(Hash)).to be_truthy
27
+ end
28
+
29
+ it 'exist 1 group' do
30
+ expect(@res.length).to eq 1
31
+ end
32
+
33
+ it 'exist [server]' do
34
+ expect(@res.key?('server')).to be_truthy
35
+ end
36
+
37
+ it 'exist 1st server' do
38
+ expect(@res['server'][0]).to eq '192.168.0.1'
39
+ end
40
+ it 'exist 2nd server' do
41
+ expect(@res['server'][1]).to eq '192.168.0.2'
42
+ end
43
+ it 'exist 3rd server' do
44
+ expect(@res['server'][2]).to eq 'example.com'
45
+ end
46
+ it 'not exist 4th server' do
47
+ expect(@res['server'][3]).to eq nil
48
+ end
49
+
50
+ after do
51
+ File.delete(tmp_hosts)
52
+ end
53
+ end
54
+
55
+ context '正常系:2グループ' do
56
+ tmp_hosts = 'hosts'
57
+ before do
58
+ content = <<'EOF'
59
+ [web]
60
+ 192.168.0.3
61
+ 192.168.0.4
62
+
63
+ [db]
64
+ 192.168.0.5
65
+ 192.168.0.6
66
+
67
+ EOF
68
+ create_file(tmp_hosts,content)
69
+ @res = AnsibleSpec.load_targets(tmp_hosts)
70
+ end
71
+
72
+ it 'res is hash' do
73
+ expect(@res.instance_of?(Hash)).to be_truthy
74
+ end
75
+
76
+ it 'check 2 group' do
77
+ expect(@res.length).to eq 2
78
+ end
79
+
80
+ #[web]のチェック
81
+ it 'exist [web]' do
82
+ expect(@res.key?('web')).to be_truthy
83
+ end
84
+ it 'exist 1st web' do
85
+ expect(@res['web'][0]).to eq '192.168.0.3'
86
+ end
87
+ it 'exist 2nd web' do
88
+ expect(@res['web'][1]).to eq '192.168.0.4'
89
+ end
90
+ it 'not exist 3rd web' do
91
+ expect(@res['web'][2]).to eq nil
92
+ end
93
+
94
+ #[db]のチェック
95
+ it 'exist [db]' do
96
+ expect(@res.key?('db')).to be_truthy
97
+ end
98
+ it 'exist 1st db' do
99
+ expect(@res['db'][0]).to eq '192.168.0.5'
100
+ end
101
+ it 'exist 2nd db' do
102
+ expect(@res['db'][1]).to eq '192.168.0.6'
103
+ end
104
+ it 'not exist 3rd db' do
105
+ expect(@res['db'][2]).to eq nil
106
+ end
107
+
108
+ after do
109
+ File.delete(tmp_hosts)
110
+ end
111
+ end
112
+
113
+ context '正常系:1グループ www[01:50].example.com' do
114
+ tmp_hosts = 'hosts'
115
+ before do
116
+ content = <<'EOF'
117
+ [web]
118
+ www[01:50].example.com
119
+ [databases]
120
+ db-[a:f].example.com
121
+ EOF
122
+ create_file(tmp_hosts,content)
123
+ @res = AnsibleSpec.load_targets(tmp_hosts)
124
+ end
125
+
126
+ it 'res is hash' do
127
+ expect(@res.instance_of?(Hash)).to be_truthy
128
+ end
129
+
130
+ it 'check 1 group' do
131
+ expect(@res.length).to eq 2
132
+ end
133
+
134
+ it 'check group name' do
135
+ expect(@res.key?('web')).to be_truthy
136
+ expect(@res.key?('databases')).to be_truthy
137
+ end
138
+
139
+ it 'www[01:50].example.com' do
140
+ 1.upto(50){|n|
141
+ leading_zero = n.to_s.rjust(2, '0')
142
+ expect(@res['web']["#{n - 1}".to_i]).to eq "www#{leading_zero}.example.com"
143
+ }
144
+ end
145
+
146
+ it 'db-[a:f].example.com' do
147
+ alphabet = [*'a'..'f'] # Array splat
148
+ alphabet.each_with_index {|word, i|
149
+ expect(@res['databases'][i]).to eq "db-#{word}.example.com"
150
+ }
151
+ end
152
+
153
+ after do
154
+ File.delete(tmp_hosts)
155
+ end
156
+ end
157
+
158
+ context '異常系:全てコメントアウトされている状態' do
159
+ tmp_hosts = 'hosts'
160
+ before do
161
+ content = <<'EOF'
162
+ #[server]
163
+ #192.168.0.1
164
+
165
+ EOF
166
+ create_file(tmp_hosts,content)
167
+ @res = AnsibleSpec.load_targets(tmp_hosts)
168
+ end
169
+
170
+ it 'res is hash' do
171
+ expect(@res.instance_of?(Hash)).to be_truthy
172
+ end
173
+
174
+ it 'check 0 group' do
175
+ expect(@res.length).to eq 0
176
+ end
177
+
178
+ it 'not exist [server]' do
179
+ expect(@res.key?('server')).not_to be_truthy
180
+ end
181
+
182
+ after do
183
+ File.delete(tmp_hosts)
184
+ end
185
+ end
186
+
187
+ context '異常系:ファイル内が空の状態' do
188
+ tmp_hosts = 'hosts'
189
+ before do
190
+ content = <<'EOF'
191
+
192
+ EOF
193
+ File.open(tmp_hosts, 'w') do |f|
194
+ f.puts content
195
+ end
196
+ @res = AnsibleSpec.load_targets(tmp_hosts)
197
+ end
198
+
199
+ it 'res is hash' do
200
+ expect(@res.instance_of?(Hash)).to be_truthy
201
+ end
202
+
203
+ it 'check 0 group' do
204
+ expect(@res.length).to eq 0
205
+ end
206
+
207
+ it 'not exist [server]' do
208
+ expect(@res.key?('server')).not_to be_truthy
209
+ end
210
+
211
+ after do
212
+ File.delete(tmp_hosts)
213
+ end
214
+ end
215
+
216
+ context '異常系:1行だけコメントアウトされている状態' do
217
+ tmp_hosts = 'hosts'
218
+ before do
219
+ content = <<'EOF'
220
+ [server]
221
+ #192.168.0.10
222
+ 192.168.0.11
223
+
224
+ EOF
225
+ create_file(tmp_hosts,content)
226
+ @res = AnsibleSpec.load_targets(tmp_hosts)
227
+ end
228
+
229
+ it 'res is hash' do
230
+ expect(@res.instance_of?(Hash)).to be_truthy
231
+ end
232
+
233
+ it 'check 1 group' do
234
+ expect(@res.length).to eq 1
235
+ end
236
+
237
+ it 'not exist [server]' do
238
+ expect(@res.key?('server')).to be_truthy
239
+ end
240
+ it 'not exist 1st server' do
241
+ expect(@res['server'][0]).not_to eq '192.168.0.10'
242
+ end
243
+ it 'exist 2nd server' do
244
+ expect(@res['server'][0]).to eq '192.168.0.11'
245
+ end
246
+
247
+ after do
248
+ File.delete(tmp_hosts)
249
+ end
250
+ end
251
+
252
+ context '異常系:グループ名のみコメントアウトされている状態' do
253
+ tmp_hosts = 'hosts'
254
+ before do
255
+ content = <<'EOF'
256
+ [web]
257
+ 192.168.0.3
258
+ #[server]
259
+ 192.168.0.4
260
+
261
+ EOF
262
+ create_file(tmp_hosts,content)
263
+ @res = AnsibleSpec.load_targets(tmp_hosts)
264
+ end
265
+
266
+ it 'res is hash' do
267
+ expect(@res.instance_of?(Hash)).to be_truthy
268
+ end
269
+
270
+ it 'check 1 group' do
271
+ expect(@res.length).to eq 1
272
+ end
273
+
274
+ it 'exist [web]' do
275
+ expect(@res.key?('web')).to be_truthy
276
+ end
277
+ it 'exist 1st web' do
278
+ expect(@res['web'][0]).to eq '192.168.0.3'
279
+ end
280
+ it 'exist 2nd web' do
281
+ expect(@res['web'][1]).to eq '192.168.0.4'
282
+ end
283
+
284
+ after do
285
+ File.delete(tmp_hosts)
286
+ end
287
+ end
288
+
289
+ context '正常系:複数グループ:Children in Group' do
290
+ tmp_hosts = 'hosts'
291
+ before do
292
+ content_h = <<'EOF'
293
+ [server]
294
+ 192.168.0.103
295
+ 192.168.0.104 ansible_ssh_port=22
296
+
297
+ [databases]
298
+ 192.168.0.105
299
+ 192.168.0.106 ansible_ssh_port=5555
300
+
301
+ [pg:children]
302
+ server
303
+ databases
304
+ EOF
305
+ create_file(tmp_hosts,content_h)
306
+ @res = AnsibleSpec.load_targets(tmp_hosts)
307
+ end
308
+
309
+ it 'res is hash' do
310
+ expect(@res.instance_of?(Hash)).to be_truthy
311
+ end
312
+
313
+ it 'exist 1 group' do
314
+ expect(@res.length).to eq 3
315
+ end
316
+
317
+ it 'exist group' do
318
+ expect(@res.key?('server')).to be_truthy
319
+ expect(@res.key?('databases')).to be_truthy
320
+ expect(@res.key?('pg')).to be_truthy
321
+ expect(@res.key?('pg:children')).not_to be_truthy
322
+ end
323
+
324
+ it 'pg 192.168.0.103' do
325
+ obj = @res['pg'][0]
326
+ expect(obj.instance_of?(String)).to be_truthy
327
+ expect(obj).to eq '192.168.0.103'
328
+ end
329
+
330
+ it 'pg 192.168.0.104 ansible_ssh_port=22' do
331
+ obj = @res['pg'][1]
332
+ expect(obj.instance_of?(Hash)).to be_truthy
333
+ expect(obj['name']).to eq '192.168.0.104 ansible_ssh_port=22'
334
+ expect(obj['uri']).to eq '192.168.0.104'
335
+ expect(obj['port']).to eq 22
336
+ end
337
+
338
+ it 'pg 192.168.0.105' do
339
+ obj = @res['pg'][2]
340
+ expect(obj.instance_of?(String)).to be_truthy
341
+ expect(obj).to eq '192.168.0.105'
342
+ end
343
+
344
+ it 'pg 192.168.0.106 ansible_ssh_port=5555' do
345
+ obj = @res['pg'][3]
346
+ expect(obj.instance_of?(Hash)).to be_truthy
347
+ expect(obj['name']).to eq '192.168.0.106 ansible_ssh_port=5555'
348
+ expect(obj['uri']).to eq '192.168.0.106'
349
+ expect(obj['port']).to eq 5555
350
+ end
351
+
352
+ after do
353
+ File.delete(tmp_hosts)
354
+ end
355
+ end
356
+
357
+ end
358
+
359
+ describe "load_playbookの実行" do
360
+ context '正常系' do
361
+ require 'yaml'
362
+ tmp_pb = 'playbook'
363
+ before do
364
+ content = <<'EOF'
365
+ - name: Ansible-Sample-TDD
366
+ hosts: server
367
+ user: root
368
+ roles:
369
+ - nginx
370
+ - mariadb
371
+ EOF
372
+ create_file(tmp_pb,content)
373
+ @res = AnsibleSpec.load_playbook(tmp_pb)
374
+ end
375
+
376
+ it 'res is array' do
377
+ expect(@res.instance_of?(Array)).to be_truthy
378
+ end
379
+
380
+ it 'res[0] is hash' do
381
+ expect(@res[0].instance_of?(Hash)).to be_truthy
382
+ end
383
+
384
+ it 'check 1 group' do
385
+ expect(@res[0].length).to eq 4
386
+ end
387
+
388
+ it 'exist name' do
389
+ expect(@res[0].key?('name')).to be_truthy
390
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
391
+ end
392
+
393
+ it 'exist hosts' do
394
+ expect(@res[0].key?('hosts')).to be_truthy
395
+ expect(@res[0]['hosts']).to eq 'server'
396
+ end
397
+
398
+ it 'exist user' do
399
+ expect(@res[0].key?('user')).to be_truthy
400
+ expect(@res[0]['user']).to eq 'root'
401
+ end
402
+
403
+ it 'exist roles' do
404
+ expect(@res[0].key?('roles')).to be_truthy
405
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
406
+ expect(@res[0]['roles'][0]).to eq 'nginx'
407
+ expect(@res[0]['roles'][1]).to eq 'mariadb'
408
+ end
409
+
410
+ after do
411
+ File.delete(tmp_pb)
412
+ end
413
+ end
414
+
415
+ context '正常系(include)' do
416
+ require 'yaml'
417
+ tmp_pb = 'site.yml'
418
+ tmp_inc = 'nginx.yml'
419
+ before do
420
+ content_pb = <<'EOF'
421
+ - name: Ansible-Sample-TDD
422
+ hosts: server
423
+ user: root
424
+ roles:
425
+ - mariadb
426
+ - include: nginx.yml
427
+ EOF
428
+ create_file(tmp_pb,content_pb)
429
+
430
+ content_inc = <<'EOF'
431
+ - name: Ansible-Nginx
432
+ hosts: web
433
+ user: nginx
434
+ roles:
435
+ - nginx
436
+ EOF
437
+ create_file(tmp_inc,content_inc)
438
+
439
+ @res = AnsibleSpec.load_playbook(tmp_pb)
440
+ end
441
+
442
+ it 'res is array' do
443
+ expect(@res.instance_of?(Array)).to be_truthy
444
+ end
445
+
446
+ it 'res is hash' do
447
+ expect(@res[0].instance_of?(Hash)).to be_truthy
448
+ end
449
+
450
+ it 'check 1 group' do
451
+ expect(@res[0].length).to eq 4
452
+ end
453
+
454
+ it 'exist name' do
455
+ expect(@res[0].key?('name')).to be_truthy
456
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
457
+ end
458
+
459
+ it 'exist hosts' do
460
+ expect(@res[0].key?('hosts')).to be_truthy
461
+ expect(@res[0]['hosts']).to eq 'server'
462
+ end
463
+
464
+ it 'exist user' do
465
+ expect(@res[0].key?('user')).to be_truthy
466
+ expect(@res[0]['user']).to eq 'root'
467
+ end
468
+
469
+ it 'exist roles' do
470
+ expect(@res[0].key?('roles')).to be_truthy
471
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
472
+ expect(@res[0]['roles'][0]).to eq 'mariadb'
473
+ end
474
+
475
+ # - include: nginx.yml
476
+ it 'res is hash' do
477
+ expect(@res[1].instance_of?(Hash)).to be_truthy
478
+ end
479
+
480
+ it 'check 1 group' do
481
+ expect(@res[1].length).to eq 4
482
+ end
483
+
484
+ it 'exist name' do
485
+ expect(@res[1].key?('name')).to be_truthy
486
+ expect(@res[1]['name']).to eq 'Ansible-Nginx'
487
+ end
488
+
489
+ it 'exist hosts' do
490
+ expect(@res[1].key?('hosts')).to be_truthy
491
+ expect(@res[1]['hosts']).to eq 'web'
492
+ end
493
+
494
+ it 'exist user' do
495
+ expect(@res[1].key?('user')).to be_truthy
496
+ expect(@res[1]['user']).to eq 'nginx'
497
+ end
498
+
499
+ it 'exist roles' do
500
+ expect(@res[1].key?('roles')).to be_truthy
501
+ expect(@res[1]['roles'].instance_of?(Array)).to be_truthy
502
+ expect(@res[1]['roles'][0]).to eq 'nginx'
503
+ end
504
+ after do
505
+ File.delete(tmp_pb)
506
+ File.delete(tmp_inc)
507
+ end
508
+ end
509
+
510
+ context '異常系(playbookファイルの中身がない場合)' do
511
+ require 'yaml'
512
+ tmp_pb = 'playbook'
513
+ before do
514
+ content = <<'EOF'
515
+ EOF
516
+ create_file(tmp_pb,content)
517
+ end
518
+
519
+ it 'exitする' do
520
+ expect{ AnsibleSpec.load_playbook(tmp_pb) }.to raise_error(SystemExit)
521
+ # TODO
522
+ # 標準出力のメッセージまでテストしたいが、exitしてしまう。
523
+ #expect{ AnsibleSpec.load_playbook(tmp_pb) }.to output("Error: No data in site.yml").to_stdout
524
+ end
525
+
526
+ after do
527
+ File.delete(tmp_pb)
528
+ end
529
+ end
530
+ end
531
+
532
+ describe "load_ansiblespecの実行" do
533
+ context '正常系' do
534
+ require 'yaml'
535
+ tmp_ansiblespec = '.ansiblespec'
536
+ tmp_playbook = 'site.yml'
537
+ tmp_hosts = 'hosts'
538
+
539
+ before do
540
+
541
+ content = <<'EOF'
542
+ ---
543
+ -
544
+ playbook: site.yml
545
+ inventory: hosts
546
+ EOF
547
+
548
+ content_p = <<'EOF'
549
+ - name: Ansible-Sample-TDD
550
+ hosts: server
551
+ user: root
552
+ roles:
553
+ - nginx
554
+ - mariadb
555
+ EOF
556
+
557
+ content_h = <<'EOF'
558
+ [server]
559
+ 192.168.0.103
560
+ 192.168.0.104
561
+ EOF
562
+ create_file(tmp_ansiblespec,content)
563
+ create_file(tmp_playbook,content_p)
564
+ create_file(tmp_hosts,content_h)
565
+ @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
566
+ end
567
+
568
+ it 'playbook is site.yml' do
569
+ expect(@playbook).to eq 'site.yml'
570
+ end
571
+
572
+ it 'inventoryfile is hosts' do
573
+ expect(@inventoryfile).to eq 'hosts'
574
+ end
575
+
576
+ after do
577
+ File.delete(tmp_ansiblespec)
578
+ File.delete(tmp_playbook)
579
+ File.delete(tmp_hosts)
580
+ end
581
+ end
582
+
583
+ context '異常系(.ansiblespecがないが、site.ymlとhostsがある場合)' do
584
+ require 'yaml'
585
+ tmp_playbook = 'site.yml'
586
+ tmp_hosts = 'hosts'
587
+
588
+ before do
589
+
590
+ content_p = <<'EOF'
591
+ - name: Ansible-Sample-TDD
592
+ hosts: server
593
+ user: root
594
+ roles:
595
+ - nginx
596
+ - mariadb
597
+ EOF
598
+
599
+ content_h = <<'EOF'
600
+ [server]
601
+ 192.168.0.103
602
+ 192.168.0.104
603
+ EOF
604
+ create_file(tmp_playbook,content_p)
605
+ create_file(tmp_hosts,content_h)
606
+ @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
607
+ end
608
+
609
+ it 'playbook is site.yml' do
610
+ expect(@playbook).to eq 'site.yml'
611
+ end
612
+
613
+ it 'inventoryfile is hosts' do
614
+ expect(@inventoryfile).to eq 'hosts'
615
+ end
616
+
617
+ after do
618
+ File.delete(tmp_playbook)
619
+ File.delete(tmp_hosts)
620
+ end
621
+ end
622
+
623
+ context '異常系(.ansiblespecとsite.ymlがないが、hostsがある場合)' do
624
+ require 'yaml'
625
+ tmp_hosts = 'hosts'
626
+
627
+ before do
628
+
629
+ content_h = <<'EOF'
630
+ [server]
631
+ 192.168.0.103
632
+ 192.168.0.104
633
+ EOF
634
+ create_file(tmp_hosts,content_h)
635
+ end
636
+
637
+ it 'exitする' do
638
+ expect{ AnsibleSpec.load_ansiblespec }.to raise_error(SystemExit)
639
+ end
640
+
641
+ after do
642
+ File.delete(tmp_hosts)
643
+ end
644
+ end
645
+
646
+ context '異常系(.ansiblespecとhostsがないが、site.ymlがある場合)' do
647
+ require 'yaml'
648
+ tmp_playbook = 'site.yml'
649
+
650
+ before do
651
+
652
+ content_p = <<'EOF'
653
+ - name: Ansible-Sample-TDD
654
+ hosts: server
655
+ user: root
656
+ roles:
657
+ - nginx
658
+ - mariadb
659
+ EOF
660
+ create_file(tmp_playbook,content_p)
661
+ end
662
+
663
+ it 'exitする' do
664
+ expect{ AnsibleSpec.load_ansiblespec }.to raise_error(SystemExit)
665
+ end
666
+
667
+ after do
668
+ File.delete(tmp_playbook)
669
+ end
670
+ end
671
+
672
+ end
673
+
674
+ describe "get_propertiesの実行" do
675
+ context '正常系' do
676
+ require 'yaml'
677
+ tmp_ansiblespec = '.ansiblespec'
678
+ tmp_playbook = 'site.yml'
679
+ tmp_hosts = 'hosts'
680
+
681
+ before do
682
+
683
+ content = <<'EOF'
684
+ ---
685
+ -
686
+ playbook: site.yml
687
+ inventory: hosts
688
+ EOF
689
+
690
+ content_p = <<'EOF'
691
+ - name: Ansible-Sample-TDD
692
+ hosts: server
693
+ user: root
694
+ roles:
695
+ - nginx
696
+ - mariadb
697
+ EOF
698
+
699
+ content_h = <<'EOF'
700
+ [server]
701
+ 192.168.0.103
702
+ 192.168.0.104
703
+ EOF
704
+
705
+ create_file(tmp_ansiblespec,content)
706
+ create_file(tmp_playbook,content_p)
707
+ create_file(tmp_hosts,content_h)
708
+ @res = AnsibleSpec.get_properties
709
+ end
710
+
711
+ it 'res is array' do
712
+ expect(@res.instance_of?(Array)).to be_truthy
713
+ end
714
+
715
+ it 'res[0] is hash' do
716
+ expect(@res[0].instance_of?(Hash)).to be_truthy
717
+ end
718
+
719
+ it 'check 1 group' do
720
+ expect(@res[0].length).to eq 4
721
+ end
722
+
723
+ it 'exist name' do
724
+ expect(@res[0].key?('name')).to be_truthy
725
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
726
+ end
727
+
728
+ it 'exist hosts' do
729
+ expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
730
+ expect(@res[0]['hosts'][0]).to eq '192.168.0.103'
731
+ expect(@res[0]['hosts'][1]).to eq '192.168.0.104'
732
+ end
733
+
734
+ it 'exist user' do
735
+ expect(@res[0].key?('user')).to be_truthy
736
+ expect(@res[0]['user']).to eq 'root'
737
+ end
738
+
739
+ it 'exist roles' do
740
+ expect(@res[0].key?('roles')).to be_truthy
741
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
742
+ expect(@res[0]['roles'][0]).to eq 'nginx'
743
+ expect(@res[0]['roles'][1]).to eq 'mariadb'
744
+ end
745
+
746
+ after do
747
+ File.delete(tmp_ansiblespec)
748
+ File.delete(tmp_playbook)
749
+ File.delete(tmp_hosts)
750
+ end
751
+ end
752
+ end