ansible_spec 0.2.1 → 0.2.2

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: 97d3791daa7f2224554c8c7a361d4c3a19f6e0f9
4
- data.tar.gz: 31bfe5f3c915b3797917b67ce0d21bad52debf9b
3
+ metadata.gz: e860c7053344a34b1907264a715ae6336dd5f6ac
4
+ data.tar.gz: ef8fac0266f20909ec8aabb7488ff603607fdbe6
5
5
  SHA512:
6
- metadata.gz: 38975c66323cf9b219d7fce75ac7e40582d3573fc4fe145657db1db982727f39ae2bd93c8739fd8e5ffeb57550acd247d39c75adf136311819c15bf1658f4cbb
7
- data.tar.gz: 1d6c2cda3f7dc488c7a6f4e0822bfd2105780b46b338a720513ea5ede2955938c49d9d0b0d47be197f8f4e13db4a30a3979f698994de32a37796f77a0bb4a6fe
6
+ metadata.gz: cf7f29b8d481259c395eff714155b7d1280d37dd3c7c966e85c56fa26f0b7c6349ecad7c14822692c5b6643542580e5365f6ccb6c618a80c5cd25ba6e984388b
7
+ data.tar.gz: a11ff18b9e6dba65082e862d9470dec01e24b553f14d6d0e617d90c77502befa4486f2d9bccec3429aba6b91134403f847a22ac2e738fd08d9d6b3b519166821
data/README.md CHANGED
@@ -1,77 +1,81 @@
1
1
  # AnsibleSpec
2
+
2
3
  [![Gem Version](https://badge.fury.io/rb/ansible_spec.svg)](http://badge.fury.io/rb/ansible_spec)
3
- [![Build Status](https://travis-ci.org/volanja/ansible_spec.svg?branch=master)](https://travis-ci.org/volanja/ansible_spec)
4
+ [![Build Status](https://travis-ci.org/volanja/ansible_spec.svg?branch=master)](https://travis-ci.org/volanja/ansible_spec)
4
5
 
5
- This gem is Ansible Config Parser for Serverspec.
6
- Serverspec RakeTask uses Ansible Config(InventoryFile and Playbook).
7
- Support Run Multi Role and Multi Host of Ansible.
6
+ This is a Ruby gem that implements an Ansible Config Parser for Serverspec.
7
+ It creates a Rake task that can run tests, using Ansible inventory files
8
+ and playbooks. You can test multiple roles and multiple hosts.
8
9
 
9
- # feature
10
+ # Features
10
11
 
11
- - Support Serverspec v2
12
- - Support InventoryParameters
13
- - ansible_ssh_port
14
- - ansible_ssh_user
15
- - ansible_ssh_host
16
- - ansible_ssh_private_key_file
17
- - Support [hostlist expressions](http://docs.ansible.com/intro_inventory.html#hosts-and-groups)
18
- - Support DynamicInventory
12
+ - Supports [Serverspec](http://serverspec.org/) v2
13
+ - Supports special host variables
14
+ - `ansible_ssh_port`
15
+ - `ansible_ssh_user`
16
+ - `ansible_ssh_host`
17
+ - `ansible_ssh_private_key_file`
18
+ - Supports [host patterns/ranges](http://docs.ansible.com/intro_inventory.html#hosts-and-groups) -- e.g.: `www[01:50].example.com`
19
+ - Supports Ansible [dynamic inventory sources](http://docs.ansible.com/ansible/intro_dynamic_inventory.html)
19
20
 
20
21
  # Installation
21
22
 
22
- install it yourself as:
23
-
24
23
  ```
25
24
  $ gem install ansible_spec
26
25
  ```
27
26
 
28
27
  # Usage
29
- ## Create Rakafile & spec/spec_helper.rb
28
+
29
+ ## Create `Rakefile` & `spec/spec_helper.rb`
30
30
 
31
31
  ```
32
- $ ansiblespec-init
32
+ $ ansiblespec-init
33
33
  create spec
34
34
  create spec/spec_helper.rb
35
35
  create Rakefile
36
36
  create .ansiblespec
37
37
  ```
38
38
 
39
- ## [option] .ansiblespec
40
- if `.ansiblespec` is exist, use variables(playbook and inventory).
41
- so, if you don't use `site.yml` and `hosts`, you change this file.
42
- if `.ansiblespec` not found, use `site.yml` as playbook and `hosts` as inventory.
39
+ ## [Optional] `.ansiblespec`
40
+
41
+ By default, `site.yml` will be used as the playbook and `hosts` as the
42
+ inventory file. You can either follow these conventions or you can
43
+ customize the playbook and inventory using an `.ansiblespec` file.
43
44
 
44
45
  ```.ansiblespec
45
- ---
46
- -
46
+ ---
47
+ -
47
48
  playbook: site.yml
48
49
  inventory: hosts
49
50
  ```
50
51
 
51
- ## [option] Environment variable
52
- You can use Environment variable, when RakeTask command. It listed below.
52
+ ## [Optional] Environment variables
53
53
 
54
- - PLAYBOOK playbook name (e.g. site.yml)
55
- - INVENTORY inventory file name (e.g. hosts)
56
- I prioritize ENV more than `.ansiblespec`
54
+ You can use environment variables with the `rake` command. They are listed below.
55
+
56
+ - `PLAYBOOK` -- playbook name (e.g. `site.yml`)
57
+ - `INVENTORY` -- inventory file name (e.g. `hosts`)
58
+
59
+ Environment variables take precedence over the `.ansiblespec` file.
57
60
 
58
- ```
59
61
  Example:
62
+
63
+ ```
60
64
  $ PLAYBOOK=site.yml INVENTORY=hosts rake serverspec:Ansible-Sample-TDD
61
65
  ```
62
66
 
63
67
  ## Inventory
64
- Serverspec use Ansible Inventory. (Rakefile understand Notation of Ansible.)
65
-
66
- Inventory file can sue this:
67
- - InventoryParameters
68
- - ansible_ssh_port
69
- - ansible_ssh_user
70
- - ansible_ssh_private_key
71
- - ansible_ssh_host
68
+
69
+ Inventory files can:
70
+
71
+ - use standard ansible parameters
72
+ - `ansible_ssh_port`
73
+ - `ansible_ssh_user`
74
+ - `ansible_ssh_private_key`
75
+ - `ansible_ssh_host`
72
76
  - define hosts as expressions. `host-[1:3]` would expand into `host-1`,`host-2`,`host-3`
73
77
  - Group Children
74
- - [DynamicInventory](http://docs.ansible.com/intro_dynamic_inventory.html)
78
+ - Use [dynamic inventory sources](http://docs.ansible.com/intro_dynamic_inventory.html)
75
79
 
76
80
  ### Sample
77
81
 
@@ -109,7 +113,9 @@ server
109
113
  databases
110
114
  ```
111
115
 
112
- ## DynamicInventory(need execute permission)
116
+ ## Dynamic Inventory Sources
117
+
118
+ (Note: These files need to have execute permission)
113
119
 
114
120
  ```
115
121
  #!/bin/bash
@@ -123,16 +129,16 @@ sample is [here](https://github.com/volanja/ansible-sample-tdd)
123
129
 
124
130
  ```
125
131
  .
126
- ├── .ansiblespec #Create file (use Serverspec). read above section.
132
+ ├── .ansiblespec # Create file (use Serverspec). read above section.
127
133
  ├── README.md
128
- ├── hosts #use Ansible and Serverspec if .ansiblespec is not exist.
129
- ├── site.yml #use Ansible and Serverspec if .ansiblespec is not exist.
130
- ├── nginx.yml #(comment-out) incluted by site.yml
134
+ ├── hosts # use Ansible and Serverspec if .ansiblespec is not exist.
135
+ ├── site.yml # use Ansible and Serverspec if .ansiblespec is not exist.
136
+ ├── nginx.yml # (comment-out) incluted by site.yml
131
137
  ├── roles
132
138
  │   └── nginx
133
139
  │   ├── handlers
134
140
  │   │   └── main.yml
135
- │   ├── spec #use Serverspec
141
+ │   ├── spec # use Serverspec
136
142
  │   │   └── nginx_spec.rb
137
143
  │   ├── tasks
138
144
  │   │   └── main.yml
@@ -140,13 +146,14 @@ sample is [here](https://github.com/volanja/ansible-sample-tdd)
140
146
  │   │   └── nginx.repo
141
147
  │   └── vars
142
148
  │   └── main.yml
143
- ├── Rakefile #Create file (use Serverspec)
144
- └── spec #Create file (use Serverspec)
149
+ ├── Rakefile # Create file (use Serverspec)
150
+ └── spec # Create file (use Serverspec)
145
151
  └── spec_helper.rb
146
152
  ```
147
153
 
148
154
  ## Playbook
149
- playbook can use `include`
155
+
156
+ playbook can use `include`
150
157
 
151
158
  ```site.yml
152
159
  - name: Ansible-Sample-TDD
@@ -179,8 +186,22 @@ Finished in 0.34306 seconds
179
186
 
180
187
  # Contributing
181
188
 
189
+ To set up a development environment:
190
+
191
+ ```
192
+ $ bundle install
193
+ ```
194
+
195
+ To run the tests:
196
+
197
+ ```
198
+ $ bundle exec rspec
199
+ ```
200
+
201
+ To contribute your change, create a GitHub pull request as follows:
202
+
182
203
  1. Fork it
183
204
  2. Create your feature branch (`git checkout -b my-new-feature`)
184
205
  3. Commit your changes (`git commit -am 'Add some feature'`)
185
206
  4. Push to the branch (`git push origin my-new-feature`)
186
- 5. Create new Pull Request
207
+ 5. Create new Pull Request at https://github.com/volanja/ansible_spec
@@ -82,11 +82,23 @@ module AnsibleSpec
82
82
  # {"databases":{"hosts":["aaa.com","bbb.com"],"vars":{"a":true}}}
83
83
  # return {"databases"=>["aaa.com", "bbb.com"]}
84
84
  def self.get_dynamic_inventory(file)
85
- so, se, st = Open3.capture3("./#{file}")
85
+ if file[0] == "/"
86
+ file_path = file
87
+ else
88
+ file_path = "./#{file}"
89
+ end
86
90
  res = Hash.new
87
- Oj.load(so.to_s).each{|k,v|
88
- res["#{k.to_s}"] = v['hosts']
89
- }
91
+ so, se, st = Open3.capture3(file_path)
92
+ dyn_inv = Oj.load(so.to_s)
93
+
94
+ if dyn_inv.key?('_meta')
95
+ # assume we have an ec2.py created dynamic inventory
96
+ res = dyn_inv.tap{ |h| h.delete("_meta") }
97
+ else
98
+ dyn_inv.each{|k,v|
99
+ res["#{k.to_s}"] = v['hosts']
100
+ }
101
+ end
90
102
  return res
91
103
  end
92
104
 
@@ -121,15 +133,22 @@ module AnsibleSpec
121
133
  # return: playbook, inventoryfile
122
134
  def self.load_ansiblespec()
123
135
  f = '.ansiblespec'
124
- if ENV["PLAYBOOK"] && ENV["INVENTORY"]
125
- playbook = ENV["PLAYBOOK"]
126
- inventoryfile = ENV["INVENTORY"]
127
- elsif File.exist?(f)
136
+ y = nil
137
+ if File.exist?(f)
128
138
  y = YAML.load_file(f)
139
+ end
140
+ if ENV["PLAYBOOK"]
141
+ playbook = ENV["PLAYBOOK"]
142
+ elsif y.is_a?(Array) && y[0]['playbook']
129
143
  playbook = y[0]['playbook']
130
- inventoryfile = y[0]['inventory']
131
144
  else
132
145
  playbook = 'site.yml'
146
+ end
147
+ if ENV["INVENTORY"]
148
+ inventoryfile = ENV["INVENTORY"]
149
+ elsif y.is_a?(Array) && y[0]['inventory']
150
+ inventoryfile = y[0]['inventory']
151
+ else
133
152
  inventoryfile = 'hosts'
134
153
  end
135
154
 
@@ -162,6 +181,9 @@ module AnsibleSpec
162
181
  properties.push site
163
182
  end
164
183
  end
184
+ properties.each do |property|
185
+ property["roles"] = flatten_role(property["roles"])
186
+ end
165
187
  if name_exist?(properties)
166
188
  return properties
167
189
  else
@@ -169,6 +191,26 @@ module AnsibleSpec
169
191
  end
170
192
  end
171
193
 
194
+ # flatten roles (Issue 29)
195
+ # param: Array
196
+ # e.g. ["nginx"]
197
+ # e.g. [{"roles"=>"nginx"}]
198
+ # e.g. [{"role"=>"nginx", "dir"=>"/opt/b", "port"=>5001}]
199
+ # return: Array
200
+ # e.g.["nginx"]
201
+ def self.flatten_role(roles)
202
+ ret = Array.new
203
+ roles.each do |role|
204
+ if role.is_a?(String)
205
+ ret << role
206
+ elsif role.is_a?(Hash)
207
+ ret << role["role"] if role.has_key?("role")
208
+ end
209
+ end
210
+ return ret
211
+ end
212
+
213
+
172
214
  # Issue 27
173
215
  # param: array
174
216
  # return: boolean
@@ -190,8 +232,12 @@ module AnsibleSpec
190
232
  hosts = load_targets(inventoryfile)
191
233
  properties = load_playbook(playbook)
192
234
  properties.each do |var|
193
- if hosts.has_key?("#{var["hosts"]}")
235
+ if var["hosts"].to_s == "all"
236
+ var["hosts"] = hosts.values.flatten
237
+ elsif hosts.has_key?("#{var["hosts"]}")
194
238
  var["hosts"] = hosts["#{var["hosts"]}"]
239
+ else
240
+ fail "no hosts matched"
195
241
  end
196
242
  end
197
243
  return properties
@@ -1,3 +1,3 @@
1
1
  module AnsibleSpec
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ require 'ansible_spec'
3
+
4
+ describe "load_targets_ec2" do
5
+ context 'EC2_DynamicInventory' do
6
+ tmp_hosts = 'hosts'
7
+ before do
8
+ content_h = <<'EOF'
9
+ #!/bin/bash
10
+ echo '{ "_meta": { "hostvars": { "some_key1": { "ec2_something_something": false }, "some_key2": { "ec2_something_something": true }}}, "tag_some_other_key1": [ "host-1", "host-2" ], "some_other_key2": [ "host-1" ]}'
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 'contains 2 groups' do
22
+ expect(@res.length).to eq 2
23
+ end
24
+
25
+ it 'contains key tag_some_other_key1' do
26
+ expect(@res.key?('tag_some_other_key1')).to be_truthy
27
+ end
28
+
29
+ it 'contains key some_other_key2' do
30
+ expect(@res.key?('some_other_key2')).to be_truthy
31
+ end
32
+
33
+ it 'tag_some_other_key1 contains host-1 and host-2' do
34
+ obj = @res['tag_some_other_key1'][0]
35
+ expect(obj.instance_of?(String)).to be_truthy
36
+ expect(obj).to eq 'host-1'
37
+ obj = @res['tag_some_other_key1'][1]
38
+ expect(obj.instance_of?(String)).to be_truthy
39
+ expect(obj).to eq 'host-2'
40
+ end
41
+
42
+ it 'some_other_key2 contains host-1' do
43
+ obj = @res['some_other_key2'][0]
44
+ expect(obj.instance_of?(String)).to be_truthy
45
+ expect(obj).to eq 'host-1'
46
+ end
47
+
48
+ after do
49
+ File.delete(tmp_hosts)
50
+ end
51
+ end
52
+ end
53
+
@@ -412,6 +412,118 @@ EOF
412
412
  end
413
413
  end
414
414
 
415
+ context '正常系(nest roles)' do
416
+ require 'yaml'
417
+ tmp_pb = 'playbook'
418
+ before do
419
+ content = <<'EOF'
420
+ - name: Ansible-Sample-TDD
421
+ hosts: server
422
+ user: root
423
+ roles:
424
+ - role: nginx
425
+ - role: mariadb
426
+ EOF
427
+ create_file(tmp_pb,content)
428
+ @res = AnsibleSpec.load_playbook(tmp_pb)
429
+ end
430
+
431
+ it 'res is array' do
432
+ expect(@res.instance_of?(Array)).to be_truthy
433
+ end
434
+
435
+ it 'res[0] is hash' do
436
+ expect(@res[0].instance_of?(Hash)).to be_truthy
437
+ end
438
+
439
+ it 'check 1 group' do
440
+ expect(@res[0].length).to eq 4
441
+ end
442
+
443
+ it 'exist name' do
444
+ expect(@res[0].key?('name')).to be_truthy
445
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
446
+ end
447
+
448
+ it 'exist hosts' do
449
+ expect(@res[0].key?('hosts')).to be_truthy
450
+ expect(@res[0]['hosts']).to eq 'server'
451
+ end
452
+
453
+ it 'exist user' do
454
+ expect(@res[0].key?('user')).to be_truthy
455
+ expect(@res[0]['user']).to eq 'root'
456
+ end
457
+
458
+ it 'exist roles' do
459
+ expect(@res[0].key?('roles')).to be_truthy
460
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
461
+ expect(@res[0]['roles'][0]).to eq 'nginx'
462
+ expect(@res[0]['roles'][1]).to eq 'mariadb'
463
+ end
464
+
465
+ after do
466
+ File.delete(tmp_pb)
467
+ end
468
+ end
469
+
470
+ context '正常系(nest roles)' do
471
+ require 'yaml'
472
+ tmp_pb = 'playbook'
473
+ before do
474
+ content = <<'EOF'
475
+ - name: Ansible-Sample-TDD
476
+ hosts: server
477
+ user: root
478
+ roles:
479
+ - common
480
+ - { role: nginx, dir: '/opt/a', port: 5001 }
481
+ - { role: mariadb, dir: '/opt/b', port: 5002 }
482
+ EOF
483
+ create_file(tmp_pb,content)
484
+ @res = AnsibleSpec.load_playbook(tmp_pb)
485
+ end
486
+
487
+ it 'res is array' do
488
+ expect(@res.instance_of?(Array)).to be_truthy
489
+ end
490
+
491
+ it 'res[0] is hash' do
492
+ expect(@res[0].instance_of?(Hash)).to be_truthy
493
+ end
494
+
495
+ it 'check 1 group' do
496
+ expect(@res[0].length).to eq 4
497
+ end
498
+
499
+ it 'exist name' do
500
+ expect(@res[0].key?('name')).to be_truthy
501
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
502
+ end
503
+
504
+ it 'exist hosts' do
505
+ expect(@res[0].key?('hosts')).to be_truthy
506
+ expect(@res[0]['hosts']).to eq 'server'
507
+ end
508
+
509
+ it 'exist user' do
510
+ expect(@res[0].key?('user')).to be_truthy
511
+ expect(@res[0]['user']).to eq 'root'
512
+ end
513
+
514
+ it 'exist roles' do
515
+ expect(@res[0].key?('roles')).to be_truthy
516
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
517
+ expect(@res[0]['roles'][0]).to eq 'common'
518
+ expect(@res[0]['roles'][1]).to eq 'nginx'
519
+ expect(@res[0]['roles'][2]).to eq 'mariadb'
520
+ end
521
+
522
+ after do
523
+ File.delete(tmp_pb)
524
+ end
525
+ end
526
+
415
527
  context '正常系(include)' do
416
528
  require 'yaml'
417
529
  tmp_pb = 'site.yml'
@@ -583,13 +695,40 @@ describe "name_exist?の実行" do
583
695
  end
584
696
 
585
697
  describe "load_ansiblespecの実行" do
586
- context '正常系(環境変数)' do
698
+ context '正常系(環境変数PLAYBOOK)' do
587
699
  require 'yaml'
588
700
  tmp_playbook = 'site_env.yml'
589
- tmp_hosts = 'hosts_env'
701
+ tmp_hosts = 'hosts'
590
702
 
591
703
  before do
592
704
  ENV['PLAYBOOK'] = tmp_playbook
705
+ create_file(tmp_playbook,'')
706
+ create_file(tmp_hosts,'')
707
+ @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
708
+ end
709
+
710
+ it "playbook is #{tmp_playbook}" do
711
+ expect(@playbook).to eq tmp_playbook
712
+ end
713
+
714
+ it "inventoryfile is #{tmp_hosts}" do
715
+ expect(@inventoryfile).to eq tmp_hosts
716
+ end
717
+
718
+ after do
719
+ ENV.delete('PLAYBOOK')
720
+ ENV.delete('INVENTORY')
721
+ File.delete(tmp_playbook)
722
+ File.delete(tmp_hosts)
723
+ end
724
+ end
725
+
726
+ context '正常系(環境変数INVENTORY)' do
727
+ require 'yaml'
728
+ tmp_playbook = 'site.yml'
729
+ tmp_hosts = 'hosts_env'
730
+
731
+ before do
593
732
  ENV['INVENTORY'] = tmp_hosts
594
733
  create_file(tmp_playbook,'')
595
734
  create_file(tmp_hosts,'')
@@ -710,14 +849,14 @@ EOF
710
849
  end
711
850
  end
712
851
 
713
- context '異常系(環境変数PLAYBOOKがないので初期値を使う)' do
852
+ context '正常系(環境変数PLAYBOOKがないので初期値を使う)' do
714
853
  require 'yaml'
715
854
  tmp_playbook = 'site.yml'
716
- tmp_hosts = 'hosts'
855
+ tmp_hosts = 'hosts_env'
717
856
  env_hosts = 'hosts_env'
718
857
 
719
858
  before do
720
- ENV['INVENTORY'] = tmp_hosts
859
+ ENV['INVENTORY'] = env_hosts
721
860
  create_file(tmp_playbook,'')
722
861
  create_file(tmp_hosts,'')
723
862
  @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
@@ -727,8 +866,8 @@ EOF
727
866
  expect(@playbook).to eq tmp_playbook
728
867
  end
729
868
 
730
- it "inventoryfile is #{tmp_hosts}" do
731
- expect(@inventoryfile).to eq tmp_hosts
869
+ it "inventoryfile is #{env_hosts}" do
870
+ expect(@inventoryfile).to eq env_hosts
732
871
  end
733
872
 
734
873
  after do
@@ -738,21 +877,21 @@ EOF
738
877
  end
739
878
  end
740
879
 
741
- context '異常系(環境変数INVENTORYがないので初期値を使う)' do
880
+ context '正常系(環境変数INVENTORYがないので初期値を使う)' do
742
881
  require 'yaml'
743
- tmp_playbook = 'site.yml'
882
+ tmp_playbook = 'site_env.yml'
744
883
  tmp_hosts = 'hosts'
745
884
  env_playbook = 'site_env.yml'
746
885
 
747
886
  before do
748
- ENV['PLAYBOOK'] = tmp_playbook
887
+ ENV['PLAYBOOK'] = env_playbook
749
888
  create_file(tmp_playbook,'')
750
889
  create_file(tmp_hosts,'')
751
890
  @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
752
891
  end
753
892
 
754
- it "playbook is #{tmp_playbook}" do
755
- expect(@playbook).to eq tmp_playbook
893
+ it "playbook is #{env_playbook}" do
894
+ expect(@playbook).to eq env_playbook
756
895
  end
757
896
 
758
897
  it "inventoryfile is #{tmp_hosts}" do
@@ -766,7 +905,7 @@ EOF
766
905
  end
767
906
  end
768
907
 
769
- context '異常系(環境変数INVENTORYがないので.ansiblespecを使う)' do
908
+ context '正常系(環境変数PLAYBOOKがないのでplaybookは.ansiblespecを使う)' do
770
909
  require 'yaml'
771
910
  tmp_ansiblespec = '.ansiblespec'
772
911
  tmp_playbook = 'site_spec.yml'
@@ -775,12 +914,11 @@ EOF
775
914
  env_hosts = 'hosts_env'
776
915
 
777
916
  before do
778
- ENV['PLAYBOOK'] = env_playbook
917
+ ENV['INVENTORY'] = env_hosts
779
918
  content = <<'EOF'
780
919
  ---
781
920
  -
782
921
  playbook: site_spec.yml
783
- inventory: hosts_spec
784
922
  EOF
785
923
  create_file(tmp_ansiblespec,content)
786
924
  create_file(tmp_playbook,'')
@@ -794,6 +932,47 @@ EOF
794
932
  expect(@playbook).to eq tmp_playbook
795
933
  end
796
934
 
935
+ it "inventoryfile is #{env_hosts}" do
936
+ expect(@inventoryfile).to eq env_hosts
937
+ end
938
+
939
+ after do
940
+ ENV.delete('INVENTORY')
941
+ File.delete(tmp_ansiblespec)
942
+ File.delete(tmp_playbook)
943
+ File.delete(tmp_hosts)
944
+ File.delete(env_playbook)
945
+ File.delete(env_hosts)
946
+ end
947
+ end
948
+
949
+ context '正常系(環境変数INVENTORYがないのでinventoryfileは.ansiblespecを使う)' do
950
+ require 'yaml'
951
+ tmp_ansiblespec = '.ansiblespec'
952
+ tmp_playbook = 'site_spec.yml'
953
+ tmp_hosts = 'hosts_spec'
954
+ env_playbook = 'site_env.yml'
955
+ env_hosts = 'hosts_env'
956
+
957
+ before do
958
+ ENV['PLAYBOOK'] = env_playbook
959
+ content = <<'EOF'
960
+ ---
961
+ -
962
+ inventory: hosts_spec
963
+ EOF
964
+ create_file(tmp_ansiblespec,content)
965
+ create_file(tmp_playbook,'')
966
+ create_file(tmp_hosts,'')
967
+ create_file(env_playbook,'')
968
+ create_file(env_hosts,'')
969
+ @playbook, @inventoryfile = AnsibleSpec.load_ansiblespec()
970
+ end
971
+
972
+ it "playbook is #{env_playbook}" do
973
+ expect(@playbook).to eq env_playbook
974
+ end
975
+
797
976
  it "inventoryfile is #{tmp_hosts}" do
798
977
  expect(@inventoryfile).to eq tmp_hosts
799
978
  end
@@ -940,4 +1119,131 @@ EOF
940
1119
  File.delete(tmp_hosts)
941
1120
  end
942
1121
  end
1122
+
1123
+ context '正常系 (hosts is all)' do
1124
+ require 'yaml'
1125
+ tmp_ansiblespec = '.ansiblespec'
1126
+ tmp_playbook = 'site.yml'
1127
+ tmp_hosts = 'hosts'
1128
+
1129
+ before do
1130
+
1131
+ content = <<'EOF'
1132
+ ---
1133
+ -
1134
+ playbook: site.yml
1135
+ inventory: hosts
1136
+ EOF
1137
+
1138
+ content_p = <<'EOF'
1139
+ - name: Ansible-Sample-TDD
1140
+ hosts: all
1141
+ user: root
1142
+ roles:
1143
+ - nginx
1144
+ - mariadb
1145
+ EOF
1146
+
1147
+ content_h = <<'EOF'
1148
+ [server]
1149
+ 192.168.0.103
1150
+ 192.168.0.104
1151
+ [server2]
1152
+ 192.168.0.105
1153
+ 192.168.0.106
1154
+ EOF
1155
+
1156
+ create_file(tmp_ansiblespec,content)
1157
+ create_file(tmp_playbook,content_p)
1158
+ create_file(tmp_hosts,content_h)
1159
+ @res = AnsibleSpec.get_properties
1160
+ end
1161
+
1162
+ it 'res is array' do
1163
+ expect(@res.instance_of?(Array)).to be_truthy
1164
+ end
1165
+
1166
+ it 'res[0] is hash' do
1167
+ expect(@res[0].instance_of?(Hash)).to be_truthy
1168
+ end
1169
+
1170
+ it 'check 1 group' do
1171
+ expect(@res[0].length).to eq 4
1172
+ end
1173
+
1174
+ it 'exist name' do
1175
+ expect(@res[0].key?('name')).to be_truthy
1176
+ expect(@res[0]['name']).to eq 'Ansible-Sample-TDD'
1177
+ end
1178
+
1179
+ it 'exist hosts' do
1180
+ expect(@res[0]['hosts'].instance_of?(Array)).to be_truthy
1181
+ expect(['192.168.0.103','192.168.0.104','192.168.0.105','192.168.0.106']).to match_array(@res[0]['hosts'])
1182
+ end
1183
+
1184
+ it 'exist user' do
1185
+ expect(@res[0].key?('user')).to be_truthy
1186
+ expect(@res[0]['user']).to eq 'root'
1187
+ end
1188
+
1189
+ it 'exist roles' do
1190
+ expect(@res[0].key?('roles')).to be_truthy
1191
+ expect(@res[0]['roles'].instance_of?(Array)).to be_truthy
1192
+ expect(@res[0]['roles'][0]).to eq 'nginx'
1193
+ expect(@res[0]['roles'][1]).to eq 'mariadb'
1194
+ end
1195
+
1196
+ after do
1197
+ File.delete(tmp_ansiblespec)
1198
+ File.delete(tmp_playbook)
1199
+ File.delete(tmp_hosts)
1200
+ end
1201
+ end
1202
+
1203
+ context '異常系 (hosts is not match)' do
1204
+ require 'yaml'
1205
+ tmp_ansiblespec = '.ansiblespec'
1206
+ tmp_playbook = 'site.yml'
1207
+ tmp_hosts = 'hosts'
1208
+
1209
+ before do
1210
+
1211
+ content = <<'EOF'
1212
+ ---
1213
+ -
1214
+ playbook: site.yml
1215
+ inventory: hosts
1216
+ EOF
1217
+
1218
+ content_p = <<'EOF'
1219
+ - name: Ansible-Sample-TDD
1220
+ hosts: match
1221
+ user: root
1222
+ roles:
1223
+ - nginx
1224
+ - mariadb
1225
+ EOF
1226
+
1227
+ content_h = <<'EOF'
1228
+ [server]
1229
+ 192.168.0.103
1230
+ 192.168.0.104
1231
+ EOF
1232
+
1233
+ create_file(tmp_ansiblespec,content)
1234
+ create_file(tmp_playbook,content_p)
1235
+ create_file(tmp_hosts,content_h)
1236
+ end
1237
+
1238
+ it 'exitする' do
1239
+ expect{ AnsibleSpec.get_properties }.to raise_error("no hosts matched")
1240
+ end
1241
+
1242
+ after do
1243
+ File.delete(tmp_ansiblespec)
1244
+ File.delete(tmp_playbook)
1245
+ File.delete(tmp_hosts)
1246
+ end
1247
+ end
1248
+
943
1249
  end
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.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - volanja
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-06 00:00:00.000000000 Z
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -135,6 +135,7 @@ files:
135
135
  - lib/src/spec/spec_helper.rb
136
136
  - spec/commands_spec.rb
137
137
  - spec/dynamic_inventory_spec.rb
138
+ - spec/ec2_dynamic_inventory_spec.rb
138
139
  - spec/inventory_parameters_spec.rb
139
140
  - spec/load_ansible_spec.rb
140
141
  - spec/spec_helper.rb
@@ -167,6 +168,7 @@ summary: Ansible Config Parser for Serverspec. Run test Multi Role and Multi Hos
167
168
  test_files:
168
169
  - spec/commands_spec.rb
169
170
  - spec/dynamic_inventory_spec.rb
171
+ - spec/ec2_dynamic_inventory_spec.rb
170
172
  - spec/inventory_parameters_spec.rb
171
173
  - spec/load_ansible_spec.rb
172
174
  - spec/spec_helper.rb