dop_common 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +176 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE.txt +177 -0
  8. data/README.md +48 -0
  9. data/Rakefile +49 -0
  10. data/Vagrantfile +25 -0
  11. data/bin/dop-puppet-autosign +56 -0
  12. data/doc/examples/example_deploment_plan_v0.0.1.yaml +302 -0
  13. data/doc/plan_format_v0.0.1.md +919 -0
  14. data/doc/plan_format_v0.0.2_snippets.md +56 -0
  15. data/dop_common.gemspec +44 -0
  16. data/lib/dop_common/affinity_group.rb +57 -0
  17. data/lib/dop_common/cli/global_options.rb +37 -0
  18. data/lib/dop_common/cli/log.rb +51 -0
  19. data/lib/dop_common/cli/node_selection.rb +62 -0
  20. data/lib/dop_common/command.rb +125 -0
  21. data/lib/dop_common/config/helper.rb +39 -0
  22. data/lib/dop_common/config.rb +66 -0
  23. data/lib/dop_common/configuration.rb +37 -0
  24. data/lib/dop_common/credential.rb +152 -0
  25. data/lib/dop_common/data_disk.rb +62 -0
  26. data/lib/dop_common/dns.rb +55 -0
  27. data/lib/dop_common/hash_parser.rb +241 -0
  28. data/lib/dop_common/hooks.rb +81 -0
  29. data/lib/dop_common/infrastructure.rb +160 -0
  30. data/lib/dop_common/infrastructure_properties.rb +185 -0
  31. data/lib/dop_common/interface.rb +113 -0
  32. data/lib/dop_common/log.rb +78 -0
  33. data/lib/dop_common/network.rb +85 -0
  34. data/lib/dop_common/node/config.rb +159 -0
  35. data/lib/dop_common/node.rb +442 -0
  36. data/lib/dop_common/node_filter.rb +74 -0
  37. data/lib/dop_common/plan.rb +188 -0
  38. data/lib/dop_common/plan_cache.rb +83 -0
  39. data/lib/dop_common/plan_store.rb +263 -0
  40. data/lib/dop_common/pre_processor.rb +73 -0
  41. data/lib/dop_common/run_options.rb +56 -0
  42. data/lib/dop_common/signal_handler.rb +58 -0
  43. data/lib/dop_common/state_store.rb +95 -0
  44. data/lib/dop_common/step.rb +200 -0
  45. data/lib/dop_common/step_set.rb +41 -0
  46. data/lib/dop_common/thread_context_logger.rb +77 -0
  47. data/lib/dop_common/utils.rb +106 -0
  48. data/lib/dop_common/validator.rb +53 -0
  49. data/lib/dop_common/version.rb +3 -0
  50. data/lib/dop_common.rb +32 -0
  51. data/lib/hiera/backend/dop_backend.rb +94 -0
  52. data/lib/hiera/dop_logger.rb +20 -0
  53. data/spec/data/fake_hook_file_invalid +1 -0
  54. data/spec/data/fake_hook_file_valid +5 -0
  55. data/spec/data/fake_keyfile +1 -0
  56. data/spec/dop-puppet-autosign_spec_disable.rb +33 -0
  57. data/spec/dop_common/affinity_group_spec.rb +41 -0
  58. data/spec/dop_common/command_spec.rb +83 -0
  59. data/spec/dop_common/credential_spec.rb +73 -0
  60. data/spec/dop_common/data_disk_spec.rb +165 -0
  61. data/spec/dop_common/dns_spec.rb +33 -0
  62. data/spec/dop_common/hash_parser_spec.rb +181 -0
  63. data/spec/dop_common/hooks_spec.rb +33 -0
  64. data/spec/dop_common/infrastructure_properties_spec.rb +224 -0
  65. data/spec/dop_common/infrastructure_spec.rb +77 -0
  66. data/spec/dop_common/interface_spec.rb +192 -0
  67. data/spec/dop_common/network_spec.rb +92 -0
  68. data/spec/dop_common/node_filter_spec.rb +70 -0
  69. data/spec/dop_common/node_spec.rb +623 -0
  70. data/spec/dop_common/plan_cache_spec.rb +46 -0
  71. data/spec/dop_common/plan_spec.rb +136 -0
  72. data/spec/dop_common/plan_store_spec.rb +194 -0
  73. data/spec/dop_common/pre_processor_spec.rb +27 -0
  74. data/spec/dop_common/run_options_spec.rb +65 -0
  75. data/spec/dop_common/signal_handler_spec.rb +31 -0
  76. data/spec/dop_common/step_set_spec.rb +21 -0
  77. data/spec/dop_common/step_spec.rb +175 -0
  78. data/spec/dop_common/utils_spec.rb +27 -0
  79. data/spec/dop_common/validator_spec.rb +47 -0
  80. data/spec/example_plans_spec.rb +16 -0
  81. data/spec/fixtures/example_ssh_key +27 -0
  82. data/spec/fixtures/example_ssh_key.pub +1 -0
  83. data/spec/fixtures/incl/root_part.yaml +1 -0
  84. data/spec/fixtures/incl/some_list.yaml +2 -0
  85. data/spec/fixtures/other_plan_same_nodes.yaml +19 -0
  86. data/spec/fixtures/simple_include.yaml +6 -0
  87. data/spec/fixtures/simple_include_with_errors.yaml +4 -0
  88. data/spec/fixtures/simple_plan.yaml +19 -0
  89. data/spec/fixtures/simple_plan_invalid.yaml +18 -0
  90. data/spec/fixtures/simple_plan_modified.yaml +21 -0
  91. data/spec/spec_helper.rb +106 -0
  92. metadata +381 -0
@@ -0,0 +1,181 @@
1
+ require 'spec_helper'
2
+
3
+ describe DopCommon::HashParser do
4
+
5
+ describe '#key_aliases' do
6
+ it 'should set the proper keys' do
7
+ hash = {'my_key' => 'test'}
8
+ aliases = ['my_key', :my_keys, 'my_keys']
9
+ DopCommon::HashParser.key_aliases(hash, :my_key, aliases)
10
+ expect(hash[:my_key]).to eq 'test'
11
+ # Make sure we can execute it again
12
+ DopCommon::HashParser.key_aliases(hash, :my_key, aliases)
13
+ expect(hash[:my_key]).to eq 'test'
14
+ end
15
+ it 'raises an exception if more than one alias/key is already set' do
16
+ hash = {'my_key' => 'test', :my_key => 'test2'}
17
+ aliases = ['my_key', :my_keys, 'my_keys']
18
+ expect{DopCommon::HashParser.key_aliases(hash, :my_key, aliases)}.to raise_error DopCommon::PlanParsingError
19
+ end
20
+ end
21
+
22
+ describe '#symbolize_keys' do
23
+ subject { DopCommon::HashParser.symbolize_keys(
24
+ hash = {'a' => [{'aa' => 1, 'bb' => 2}], 'b' => 3}
25
+ )}
26
+ it { is_expected.to eq({:a => [{'aa' => 1, 'bb' => 2}], :b => 3}) }
27
+ end
28
+
29
+ describe '#deep_symbolize_keys' do
30
+ context 'simple nested hash' do
31
+ subject { DopCommon::HashParser.deep_symbolize_keys(
32
+ hash = {'a' => [{'aa' => '1', 'bb' => 2}], 'b' => 3}
33
+ )}
34
+ it { is_expected.to eq({:a => [{:aa => '1', :bb => 2}], :b => 3}) }
35
+ end
36
+ context 'nested hash with loops' do
37
+ subject {
38
+ hash = {'a' => [{'aa' => 1, 'bb' => 2}], 'b' => 3}
39
+ hash['a'] << hash
40
+ DopCommon::HashParser.deep_symbolize_keys(hash)
41
+ }
42
+ it { is_expected.to be_a Hash }
43
+ end
44
+ end
45
+
46
+ describe '#represents_regexp?' do
47
+ context 'valid regexp' do
48
+ subject {DopCommon::HashParser.represents_regexp?('/valid/')}
49
+ it { is_expected.to be true }
50
+ end
51
+ context 'invalid regexp' do
52
+ subject {DopCommon::HashParser.represents_regexp?('noregex')}
53
+ it { is_expected.to be false }
54
+ end
55
+ end
56
+
57
+ describe '#is_valid_regexp?' do
58
+ context 'valid regexp' do
59
+ subject {DopCommon::HashParser.is_valid_regexp?('/valid/')}
60
+ it { is_expected.to be true }
61
+ end
62
+ context 'invalid regexp' do
63
+ subject {DopCommon::HashParser.is_valid_regexp?('/][/')}
64
+ it { is_expected.to be false }
65
+ end
66
+ end
67
+
68
+ describe '#pattern_list_valid?' do
69
+ it 'returns true if the hash is correctly specified' do
70
+ hash = {'my_key' => :all}
71
+ expect(DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')).to be(true)
72
+ hash = {'my_key' => 'foo'}
73
+ expect(DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')).to be(true)
74
+ hash = {'my_key' => ['foo', '/foo/']}
75
+ expect(DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')).to be(true)
76
+ end
77
+ it 'returns false if the key is missing but optional' do
78
+ hash = {}
79
+ expect(DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')).to be(false)
80
+ end
81
+ it 'raises an error if the key is missing and not optional' do
82
+ hash = {}
83
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key', false)}.to raise_error DopCommon::PlanParsingError
84
+ end
85
+ it 'raises an error if a value in the array is not a string' do
86
+ hash = {'my_key' => [2]}
87
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')}.to raise_error DopCommon::PlanParsingError
88
+ end
89
+ it 'raises an error if a regexp in the array is not valid' do
90
+ hash = {'my_key' => ['/][/']}
91
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')}.to raise_error DopCommon::PlanParsingError
92
+ end
93
+ end
94
+
95
+ describe '#hash_of_pattern_lists_valid?' do
96
+ it 'returns true if the hash is correctly specified' do
97
+ hash = {'my_key' => {'list_1' => '/foo/', 'list_2' => ['foo', '/foo/']}}
98
+ expect(DopCommon::HashParser.hash_of_pattern_lists_valid?(hash, 'my_key')).to be(true)
99
+ end
100
+ it 'returns false if the key is missing but optional' do
101
+ hash = {}
102
+ expect(DopCommon::HashParser.hash_of_pattern_lists_valid?(hash, 'my_key')).to be(false)
103
+ end
104
+ it 'raises an error if the key is missing and not optional' do
105
+ hash = {}
106
+ expect{DopCommon::HashParser.hash_of_pattern_lists_valid?(hash, 'my_key', false)}.to raise_error DopCommon::PlanParsingError
107
+ end
108
+ it 'raises an error if the value is not a hash' do
109
+ hash = {'my_key' => 2}
110
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')}.to raise_error DopCommon::PlanParsingError
111
+ end
112
+ it 'raises an error if a key is not a string' do
113
+ hash = {'my_key' => { 2 => 'foo' }}
114
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')}.to raise_error DopCommon::PlanParsingError
115
+ end
116
+ it 'raises an error if a list is not valid' do
117
+ hash = {'my_key' => { 'list_1' => 2 }}
118
+ expect{DopCommon::HashParser.pattern_list_valid?(hash, 'my_key')}.to raise_error DopCommon::PlanParsingError
119
+ end
120
+ end
121
+
122
+ describe '#parse_pattern_list' do
123
+ pending
124
+ end
125
+
126
+ describe '#parse_hash_of_pattern_lists' do
127
+ pending
128
+ end
129
+
130
+ describe 'load_content' do
131
+ it 'successfully returns the content from a string' do
132
+ value = 'Some simple string'
133
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to_not raise_error
134
+ expect(DopCommon::HashParser.load_content(value)).to eq('Some simple string')
135
+ end
136
+
137
+ it 'successfully retrieve content from a file' do
138
+ secret = SecureRandom.hex
139
+ key_file = Tempfile.new('secret_file', ENV['HOME'])
140
+ key_file.write(secret)
141
+ key_file.close
142
+ value = {:file => key_file.path}
143
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to_not raise_error
144
+ expect(DopCommon::HashParser.load_content(value)).to eq(secret)
145
+ key_file.delete
146
+ end
147
+
148
+ it 'successfully retrieve content from an executable' do
149
+ secret = SecureRandom.hex
150
+ key_exec = Tempfile.new('secret_exec', ENV['HOME'])
151
+ key_exec.write("#!/bin/sh\necho \"#{secret}\"")
152
+ key_exec.close
153
+ FileUtils.chmod(0700, key_exec.path)
154
+ value = {:exec => [key_exec.path]}
155
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to_not raise_error
156
+ expect(DopCommon::HashParser.load_content(value)).to eq(secret)
157
+ key_exec.delete
158
+ end
159
+
160
+ it 'successfully retrieve content from an executable with parameters' do
161
+ secret = SecureRandom.hex
162
+ key_exec = Tempfile.new('secret_exec', ENV['HOME'])
163
+ key_exec.write("#!/bin/sh\necho $2")
164
+ key_exec.close
165
+ FileUtils.chmod(0700, key_exec.path)
166
+ value = {:exec => [key_exec.path, '--secret', secret]}
167
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to_not raise_error
168
+ expect(DopCommon::HashParser.load_content(value)).to eq(secret)
169
+ key_exec.delete
170
+ end
171
+
172
+ it "raises an exeption if the file does not exist" do
173
+ file_name = SecureRandom.hex
174
+ value = {:file => File.join('/tmp', file_name)}
175
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to raise_error DopCommon::PlanParsingError
176
+ value = {:exec => File.join('/tmp', file_name)}
177
+ expect{DopCommon::HashParser.load_content_valid?(value)}.to raise_error DopCommon::PlanParsingError
178
+ end
179
+ end
180
+
181
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe DopCommon::Hooks do
4
+ %w(create update).each do |a|
5
+ %w(pre post).each do |p|
6
+ hook = :"#{p}_#{a}_vm"
7
+ describe "##{hook}" do
8
+ it "will return an empty array if given hook isn't specified" do
9
+ hooks = ::DopCommon::Hooks.new({})
10
+ expect(hooks.send(hook)).to eq([])
11
+ end
12
+ it "will return an array of hooks if specified correctly" do
13
+ hooks = ::DopCommon::Hooks.new({ hook => ['spec/data/fake_hook_file_valid'] })
14
+ expect(hooks.send(hook)).to eq(['spec/data/fake_hook_file_valid'])
15
+ end
16
+ [nil, {}, [], 1].each do |v|
17
+ it "will raise an error if hook isn't specified properly" do
18
+ hooks = ::DopCommon::Hooks.new({ hook => v })
19
+ expect { hooks.send(hook) }.to raise_error ::DopCommon::PlanParsingError
20
+ end
21
+ end
22
+ it "will raise an error if hook isn't a file" do
23
+ hooks = ::DopCommon::Hooks.new({ hook => '/' })
24
+ expect { hooks.send(hook) }.to raise_error ::DopCommon::PlanParsingError
25
+ end
26
+ it "will raise an error if hook isn't an executable file" do
27
+ hooks = ::DopCommon::Hooks.new({ hook => 'spec/data/fake_hook_file_invalid' })
28
+ expect { hooks.send(hook) }.to raise_error ::DopCommon::PlanParsingError
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,224 @@
1
+ require 'spec_helper'
2
+
3
+ describe DopCommon::InfrastructureProperties do
4
+
5
+ providers = %w(ovirt vsphere openstack)
6
+ infrastructure = Hash[
7
+ providers.collect { |p| [p, DopCommon::Infrastructure.new(p, {'type' => p})] }
8
+ ]
9
+
10
+ describe '#affinity_groups' do
11
+ it 'will return an array of affinity group if input is valid' do
12
+ ags = ['ag1', 'ag2', 'ag3']
13
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, nil)
14
+ expect(infrastructure_properties.affinity_groups).to eq []
15
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'affinity_groups' => ags}, nil)
16
+ expect(infrastructure_properties.affinity_groups).to eq ags
17
+ end
18
+
19
+ it 'will raise an error if input affinity groups point to a non-empty array' do
20
+ [[], 'invalid'].each do |val|
21
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'affinity_groups' => val}, nil)
22
+ expect { infrastructure_properties.affinity_groups }.to raise_error DopCommon::PlanParsingError
23
+ end
24
+ end
25
+
26
+ it 'will raise an error if input affinity groups array contains invalid entries' do
27
+ [[:ag1, "ag2", 2], ["ag1", 2, "ag3"], ["ag1", "ag2", ""]].each do |ag|
28
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'affinity_groups' => ag}, nil)
29
+ expect { infrastructure_properties.affinity_groups }.to raise_error DopCommon::PlanParsingError
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#keep_ha?' do
35
+ it "will return 'true' if not specified in input hash" do
36
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, infrastructure['ovirt'])
37
+ expect(infrastructure_properties.keep_ha?).to eq true
38
+ end
39
+
40
+ it "will return 'true' or 'false' if specified in input hash properly" do
41
+ [true, false].each do |val|
42
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'keep_ha' => val}, infrastructure['ovirt'])
43
+ expect(infrastructure_properties.keep_ha?).to eq val
44
+ end
45
+ end
46
+
47
+ it 'will raise an exception if used for non-openstack provider' do
48
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'keep_ha' => true}, infrastructure['vsphere'])
49
+ expect { infrastructure_properties.keep_ha? }.to raise_error DopCommon::PlanParsingError
50
+ end
51
+ it 'will raise an exception if not specified properly in input hash' do
52
+ ['true', 'false', 1, 0, :invalid, {}].each do |val|
53
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'keep_ha' => val}, infrastructure['ovirt'])
54
+ expect { infrastructure_properties.keep_ha? }.to raise_error DopCommon::PlanParsingError
55
+ end
56
+ end
57
+ end
58
+
59
+ %w(datacenter cluster).each do |prop_name|
60
+ describe "##{prop_name}" do
61
+ providers.each do |p|
62
+ it "will return the '#{prop_name}' name if specified correctly" do
63
+ prop_val = "prop-val-#{p}"
64
+ infrastructure_properties = DopCommon::InfrastructureProperties.new(
65
+ {prop_name => prop_val},
66
+ infrastructure[p]
67
+ )
68
+ expect(infrastructure_properties.send(prop_name.to_sym)).to eq(p == 'openstack' ? nil : prop_val)
69
+ end
70
+
71
+ it "will raise an exception if '#{prop_name}' is not specified properly" do
72
+ unless p == 'openstack'
73
+ [{}, :invalid, ""].each do |prop_val|
74
+ infrastructure_properties = DopCommon::InfrastructureProperties.new(
75
+ {prop_name => prop_val},
76
+ infrastructure[p]
77
+ )
78
+ expect { infrastructure_properties.send(prop_name.to_sym) }.to raise_error DopCommon::PlanParsingError
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ %w(default_pool dest_folder).each do |method_name|
87
+ describe "##{method_name}" do
88
+ it "will return 'nil' if not specified in input hash" do
89
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, nil)
90
+ expect(infrastructure_properties.send(method_name.to_sym)).to eq nil
91
+ end
92
+
93
+ it "will return '#{method_name}' if specified properly in input hash" do
94
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({method_name => "foo-#{method_name}"}, nil)
95
+ expect(infrastructure_properties.send(method_name.to_sym)).to eq "foo-#{method_name}"
96
+ end
97
+
98
+ it "will raise an exception if not specified properly in input hash" do
99
+ ["", :invalid].each do |val|
100
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({method_name => val}, nil)
101
+ expect { infrastructure_properties.send(method_name.to_sym) }.to raise_error DopCommon::PlanParsingError
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ describe '#tenant' do
108
+ providers.each do |p|
109
+ it "will return the 'tenant' name if specified properly" do
110
+ tenant = "tenant_#{p}"
111
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'tenant' => tenant}, infrastructure[p])
112
+ expect(infrastructure_properties.tenant).to eq(p == 'openstack' ? tenant : nil)
113
+ end
114
+ end
115
+
116
+ it "will raise an exception if provider is 'openstack' and 'tenant' is undefined" do
117
+ [nil, :invalid, ""].each do |prop_val|
118
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'tenant' => prop_val}, infrastructure['openstack'])
119
+ expect { infrastructure_properties.tenant }.to raise_error DopCommon::PlanParsingError
120
+ end
121
+ end
122
+ end
123
+
124
+ describe '#use_config_drive?' do
125
+ it "will return false if not specified in input hash" do
126
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, nil)
127
+ expect(infrastructure_properties.use_config_drive?).to eq false
128
+ end
129
+
130
+ it "will return true or false if specified in input hash properly" do
131
+ [true, false].each do |val|
132
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'use_config_drive' => val}, infrastructure['openstack'])
133
+ expect(infrastructure_properties.use_config_drive?).to eq val
134
+ end
135
+ end
136
+
137
+ it 'will raise an exception if used for non-openstack provider' do
138
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'use_config_drive' => true}, infrastructure['ovirt'])
139
+ expect { infrastructure_properties.use_config_drive? }.to raise_error DopCommon::PlanParsingError
140
+ end
141
+ it 'will raise an exception if not specified properly in input hash' do
142
+ ['true', 'false', 1, 0, :invalid, {}].each do |val|
143
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'use_config_drive' => val}, infrastructure['openstack'])
144
+ expect { infrastructure_properties.use_config_drive? }.to raise_error DopCommon::PlanParsingError
145
+ end
146
+ end
147
+ end
148
+
149
+ describe '#security_groups' do
150
+ infra = DopCommon::Infrastructure.new('openstack', {'type' => 'openstack', 'default_security_groups' => ['sg1', 'sg2']})
151
+ it 'will return the default_security_groups if nothing is specified' do
152
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, infra)
153
+ expect(infrastructure_properties.security_groups).to eq(['sg1', 'sg2'])
154
+ end
155
+ it 'will return the security_groups array if security_groups is specified' do
156
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'security_groups' => ['sg3']}, infra)
157
+ expect(infrastructure_properties.security_groups).to eq(['sg3'])
158
+ end
159
+ it 'will return a merged array of defaults and additional_security_groups if additional_security_groups is defined' do
160
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'additional_security_groups' => ['sg3']}, infra)
161
+ expect(infrastructure_properties.security_groups).to eq(['sg1', 'sg2', 'sg3'])
162
+ end
163
+ it 'will raise an error if security_groups is not properly specified' do
164
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'security_groups' => 'sg3'}, infra)
165
+ expect { infrastructure_properties.security_groups }.to raise_error DopCommon::PlanParsingError
166
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'security_groups' => [1, 2]}, infra)
167
+ expect { infrastructure_properties.security_groups }.to raise_error DopCommon::PlanParsingError
168
+ end
169
+ it 'will raise an error if additional_security_groups is not properly specified' do
170
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'additional_security_groups' => 'sg3'}, infra)
171
+ expect { infrastructure_properties.security_groups }.to raise_error DopCommon::PlanParsingError
172
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'additional_security_groups' => [1, 2]}, infra)
173
+ expect { infrastructure_properties.security_groups }.to raise_error DopCommon::PlanParsingError
174
+ end
175
+ it 'will raise an error if both security_groups and additional_security_groups are specified' do
176
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'additional_security_groups' => ['sg3'], 'security_groups' => ['sg3']}, infra)
177
+ expect { infrastructure_properties.security_groups }.to raise_error DopCommon::PlanParsingError
178
+ end
179
+ end
180
+
181
+ describe '#domain_id' do
182
+ providers.each do |p|
183
+ it "will return the 'domain_id' name if specified properly" do
184
+ domain_id = "domain_id_#{p}"
185
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'domain_id' => domain_id}, infrastructure[p])
186
+ expect(infrastructure_properties.domain_id).to eq(p == 'openstack' ? domain_id : nil)
187
+ end
188
+ end
189
+ it "will return the 'default' if domain_id name isn't specified " do
190
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, infrastructure['openstack'])
191
+ expect(infrastructure_properties.domain_id).to eq 'default'
192
+ end
193
+
194
+ it "will raise an exception if provider is 'openstack' and 'domain_id' is not a non-empty string" do
195
+ [1, :invalid, "", {}, []].each do |prop_val|
196
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'domain_id' => prop_val}, infrastructure['openstack'])
197
+ expect { infrastructure_properties.domain_id }.to raise_error DopCommon::PlanParsingError
198
+ end
199
+ end
200
+ end
201
+
202
+ describe '#endpoint_type' do
203
+ providers.each do |p|
204
+ %w(publicURL internalURL adminURL).each do |endpoint_type|
205
+ it "will return the 'endpoint_type' name if specified properly" do
206
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'endpoint_type' => endpoint_type}, infrastructure[p])
207
+ expect(infrastructure_properties.endpoint_type).to eq(p == 'openstack' ? endpoint_type : nil)
208
+ end
209
+ end
210
+ end
211
+ it "will return the 'publicURL' if endpoint_type name isn't specified " do
212
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({}, infrastructure['openstack'])
213
+ expect(infrastructure_properties.endpoint_type).to eq 'publicURL'
214
+ end
215
+
216
+ it "will raise an exception if provider is 'openstack' and 'endpoint_type' is not publicURL, internalURL or adminURL literal " do
217
+ [1, :invalid, "", {}, [], 'foo'].each do |prop_val|
218
+ infrastructure_properties = DopCommon::InfrastructureProperties.new({'endpoint_type' => prop_val}, infrastructure['openstack'])
219
+ expect { infrastructure_properties.endpoint_type }.to raise_error DopCommon::PlanParsingError
220
+ end
221
+ end
222
+ end
223
+ end
224
+
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe DopCommon::Infrastructure do
4
+
5
+ describe '#provider' do
6
+ it 'will set and return the infrastructure type of infrastructure if specified correctly' do
7
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev'})
8
+ expect(infrastructure.provider).to eq(:rhev)
9
+ end
10
+ it 'will raise an error if the type is missing' do
11
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {})
12
+ expect { infrastructure.provider }.to raise_error ::DopCommon::PlanParsingError
13
+ end
14
+ it 'will raise an error ig the infrastructure provider type is invalid' do
15
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'invalid'})
16
+ expect { infrastructure.provider }.to raise_error ::DopCommon::PlanParsingError
17
+ end
18
+ end
19
+
20
+ describe '#endpoint' do
21
+ it 'will set and return an URL object of infrastructure endpoint if specified correctly' do
22
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'baremetal'})
23
+ expect(infrastructure.endpoint.to_s).to eq('')
24
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'endpoint' => 'https://foo.bar/baz'})
25
+ expect(infrastructure.endpoint.to_s).to eq('https://foo.bar/baz')
26
+ end
27
+ it 'will raise an error if endpoint is unspecified and the cloud provider is not of baremetal type' do
28
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev'})
29
+ expect { infrastructure.endpoint }.to raise_error ::DopCommon::PlanParsingError
30
+ end
31
+ it 'will raise an error if the endpoint specification is invalid' do
32
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'endpoint' => nil})
33
+ expect { infrastructure.endpoint }.to raise_error ::DopCommon::PlanParsingError
34
+ end
35
+ end
36
+
37
+ describe '#networks' do
38
+ it 'will set and return networks if specified correctly' do
39
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'baremetal'})
40
+ expect(infrastructure.networks).to eq([])
41
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'networks' => {'net1' => {}}})
42
+ expect(infrastructure.networks.find { |n| n.name == 'net1' }).to be_a ::DopCommon::Network
43
+ end
44
+ it 'will raise an error if network specification is invalid' do
45
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev'})
46
+ expect { infrastructure.networks }.to raise_error ::DopCommon::PlanParsingError
47
+ end
48
+ end
49
+
50
+ describe '#affinity_groups' do
51
+ it 'will set and return affinity groups if specified correctly' do
52
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'affinity_groups' => {}})
53
+ expect(infrastructure.affinity_groups).to eq([])
54
+ end
55
+ it 'will raise an error in case of invalid specification of affinity groups' do
56
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'affinity_groups' => :invalid})
57
+ expect { infrastructure.affinity_groups }.to raise_error ::DopCommon::PlanParsingError
58
+ end
59
+ end
60
+
61
+ describe '#default_security_groups' do
62
+ it 'will return an empty array if not defined' do
63
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev'})
64
+ expect(infrastructure.default_security_groups).to eq([])
65
+ end
66
+ it 'will return an array of security groups if specified correctly' do
67
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'default_security_groups' => ['sg1', 'sg2']})
68
+ expect(infrastructure.default_security_groups).to eq(['sg1', 'sg2'])
69
+ end
70
+ it 'will raise an error if the security groups is not specified correctly' do
71
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'default_security_groups' => 1})
72
+ expect { infrastructure.default_security_groups }.to raise_error ::DopCommon::PlanParsingError
73
+ infrastructure = ::DopCommon::Infrastructure.new('dummy', {'type' => 'rhev', 'default_security_groups' => [ 1, 2 ]})
74
+ expect { infrastructure.default_security_groups }.to raise_error ::DopCommon::PlanParsingError
75
+ end
76
+ end
77
+ end