forked from rubber/rubber
/
instance_test.rb
317 lines (252 loc) · 12.5 KB
/
instance_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require File.expand_path(File.join(__FILE__, '..', 'test_helper'))
class InstanceTest < Test::Unit::TestCase
include Rubber::Configuration
def instance_setup
@instance = Instance.new("file:#{Tempfile.new('testforrole').path}")
@instance.add(@i1 = InstanceItem.new('host1', 'domain.com', [RoleItem.new('role1')], '', 'm1.small', 'ami-7000f019'))
@instance.add(@i2 = InstanceItem.new('host2', 'domain.com', [RoleItem.new('role1')], '', 'm1.small', 'ami-7000f019'))
@instance.add(@i3 = InstanceItem.new('host3', 'domain.com', [RoleItem.new('role2')], '', 'm1.small', 'ami-7000f019'))
@instance.add(@i4 = InstanceItem.new('host4', 'domain.com', [RoleItem.new('role2', 'primary' => true)], '', 'm1.small', 'ami-7000f019'))
end
context "instance" do
setup do
instance_setup
end
context "for_role" do
should "give the right instances for selected role" do
assert_equal 2, @instance.for_role('role1').size, 'not finding correct instances for role'
assert_equal 2, @instance.for_role('role2').size, 'not finding correct instances for role'
assert_equal 1, @instance.for_role('role2', {}).size, 'not finding correct instances for role'
assert_equal @i3, @instance.for_role('role2', {}).first, 'not finding correct instances for role'
assert_equal 1, @instance.for_role('role2', 'primary' => true).size, 'not finding correct instances for role'
assert_equal @i4, @instance.for_role('role2', 'primary' => true).first, 'not finding correct instances for role'
end
end
context "filtering" do
should "not filter for empty FILTER(_ROLES)" do
ENV['FILTER'] = nil
ENV['FILTER_ROLES'] = nil
instance_setup
assert_equal 4, @instance.filtered().size
end
should "filter hosts" do
ENV['FILTER_ROLES'] = nil
ENV['FILTER'] = 'host1'
instance_setup
assert_equal [@i1].sort, @instance.filtered().sort, 'should have only filtered host'
ENV['FILTER'] = 'host2 , host4'
instance_setup
assert_equal [@i2, @i4].sort, @instance.filtered().sort, 'should have only filtered hosts'
ENV['FILTER'] = '-host2'
instance_setup
assert_equal [@i1, @i3, @i4].sort, @instance.filtered().sort, 'should not have negated hosts'
ENV['FILTER'] = 'host1,host2,-host2'
instance_setup
assert_equal [@i1].sort, @instance.filtered().sort, 'should not have negated hosts'
ENV['FILTER'] = 'host1~host3'
instance_setup
assert_equal [@i1, @i2, @i3].sort, @instance.filtered().sort, 'should allow range in filter'
ENV['FILTER'] = '-host1~-host3'
instance_setup
assert_equal [@i4].sort, @instance.filtered().sort, 'should allow negative range in filter'
ENV['FILTER'] = '-host1'
ENV['FILTER_ROLES'] = 'role1'
instance_setup
assert_equal [@i2].sort, @instance.filtered().sort, 'should not have negated roles'
end
should "filter roles" do
ENV['FILTER'] = nil
ENV['FILTER_ROLES'] = 'role1'
instance_setup
assert_equal [@i1, @i2].sort, @instance.filtered().sort, 'should have only filtered roles'
ENV['FILTER_ROLES'] = 'role1 , role2'
instance_setup
assert_equal [@i1, @i2, @i3, @i4].sort, @instance.filtered().sort, 'should have only filtered roles'
ENV['FILTER_ROLES'] = '-role1'
instance_setup
assert_equal [@i3, @i4].sort, @instance.filtered().sort, 'should not have negated roles'
ENV['FILTER_ROLES'] = 'role1~role2'
instance_setup
assert_equal [@i1, @i2, @i3, @i4].sort, @instance.filtered().sort, 'should allow range in filter'
ENV['FILTER_ROLES'] = '-role1~-role2'
instance_setup
assert_equal [].sort, @instance.filtered().sort, 'should allow negative range in filter'
end
should "validate filters" do
ENV['FILTER'] = "nohost"
instance_setup
assert_raises do
@instance.filtered()
end
ENV['FILTER'] = "-nohost"
instance_setup
assert_raises do
@instance.filtered()
end
ENV['FILTER_ROLES'] = "norole"
instance_setup
assert_raises do
@instance.filtered()
end
ENV['FILTER_ROLES'] = "-norole"
instance_setup
assert_raises do
@instance.filtered()
end
end
end
should "be equal" do
assert RoleItem.new('a').eql?(RoleItem.new('a'))
assert RoleItem.new('a') == RoleItem.new('a')
assert_equal RoleItem.new('a').hash, RoleItem.new('a').hash
assert ! RoleItem.new('a').eql?(RoleItem.new('b'))
assert RoleItem.new('a') != RoleItem.new('b')
assert_not_equal RoleItem.new('a').hash, RoleItem.new('b').hash
assert RoleItem.new('a', {'a' => 'b', 1 => true}).eql?(RoleItem.new('a', {'a' => 'b', 1 => true}))
assert RoleItem.new('a', {'a' => 'b', 1 => true}) == RoleItem.new('a', {'a' => 'b', 1 => true})
assert_equal RoleItem.new('a', {'a' => 'b', 1 => true}).hash, RoleItem.new('a', {'a' => 'b', 1 => true}).hash
assert ! RoleItem.new('a', {'a' => 'b', 1 => true}).eql?(RoleItem.new('a', {'a' => 'b', 2 => true}))
assert RoleItem.new('a', {'a' => 'b', 1 => true}) != RoleItem.new('a', {'a' => 'b', 2 => true})
assert_equal RoleItem.new('a', {'a' => 'b', 1 => true}).hash, RoleItem.new('a', {'a' => 'b', 2 => true}).hash
end
should "parse roles" do
assert_equal RoleItem.new('a'), RoleItem.parse("a")
assert_equal RoleItem.new('a', {'b' => 'c'}), RoleItem.parse("a:b=c")
assert_equal RoleItem.new('a', {'b' => 'c', 'd' => 'e'}), RoleItem.parse("a:b=c;d=e")
assert_equal RoleItem.new('a', {'b' => true, 'c' => false}), RoleItem.parse("a:b=true;c=false")
end
should "convert to a string" do
assert_equal "a", RoleItem.new('a').to_s
assert_equal "a:b=c", RoleItem.new('a', {'b' => 'c'}).to_s
str = RoleItem.new('a', {'b' => 'c', 'd' => 'e'}).to_s
assert ["a:b=c;d=e", "a:d=e;b=c"].include?(str)
assert_equal "a:b=true", RoleItem.new('a', {'b' => true}).to_s
assert_equal "a:b=false", RoleItem.new('a', {'b' => false}).to_s
end
context "role dependencies" do
should "expand role dependencies" do
deps = { RoleItem.new('a') => RoleItem.new('b'),
RoleItem.new('b') => RoleItem.new('c'),
RoleItem.new('c') => [RoleItem.new('d'), RoleItem.new('a')]}
roles = [RoleItem.new('a'),RoleItem.new('b'),RoleItem.new('c'),RoleItem.new('d')]
assert_equal roles, RoleItem.expand_role_dependencies(RoleItem.new('a'), deps).sort
assert_equal roles, RoleItem.expand_role_dependencies([RoleItem.new('a'), RoleItem.new('d')], deps).sort
deps = { RoleItem.new('mysql_master') => RoleItem.new('db', {'primary' => true}),
RoleItem.new('mysql_slave') => RoleItem.new('db'),
RoleItem.new('db', {'primary' => true}) => RoleItem.new('mysql_master'),
RoleItem.new('db') => RoleItem.new('mysql_slave')
}
assert_equal [RoleItem.new('db', 'primary' => true), RoleItem.new('mysql_master')],
RoleItem.expand_role_dependencies(RoleItem.new('mysql_master'), deps).sort
assert_equal [RoleItem.new('db', 'primary' => true), RoleItem.new('mysql_master')],
RoleItem.expand_role_dependencies(RoleItem.new('db', 'primary' => true), deps).sort
assert_equal [RoleItem.new('db'), RoleItem.new('mysql_slave')],
RoleItem.expand_role_dependencies(RoleItem.new('mysql_slave'), deps).sort
assert_equal [RoleItem.new('db'), RoleItem.new('mysql_slave')],
RoleItem.expand_role_dependencies(RoleItem.new('db'), deps).sort
end
should "expand dependencie for common role" do
deps = { RoleItem.new('a') => RoleItem.new('b'),
RoleItem.new('common') => [RoleItem.new('c')]}
roles = [RoleItem.new('a'), RoleItem.new('b'), RoleItem.new('c')]
assert_equal roles, RoleItem.expand_role_dependencies(RoleItem.new('a'), deps).sort
end
end
context "cloud storage" do
require 'rubber/cloud/aws'
setup do
env = {'access_key' => "XXX", 'secret_access_key' => "YYY", 'region' => "us-east-1"}
env = Rubber::Configuration::Environment::BoundEnv.new(env, nil, nil, nil)
@cloud = Rubber::Cloud::Aws.new(env, nil)
@cloud.storage_provider.put_bucket('bucket')
Rubber.stubs(:cloud).returns(@cloud)
end
should "fail for invalid instance_storage protocol" do
Instance.new('file:baz')
@cloud.storage('bucket').store('key', '')
Instance.new('storage:bucket/key')
Instance.any_instance.stubs(:load_from_table)
Instance.new('table:bar')
assert_raises { Instance.new('foo:bar') }
end
should "load and save from file when file given" do
location = "file:#{Tempfile.new('instancestorage').path}"
Instance.any_instance.expects(:load_from_file)
Instance.any_instance.expects(:save_to_file)
Instance.new(location).save
end
should "create new instance in filesystem when instance file doesn't exist" do
tempfile = Tempfile.new('instancestorage')
location = "file:#{tempfile.path}"
tempfile.close
tempfile.unlink
Instance.any_instance.expects(:load_from_file).never
Instance.any_instance.expects(:save_to_file)
Instance.new(location).save
end
should "load and save from storage when storage given" do
@cloud.storage('bucket').store('key', '')
Instance.any_instance.expects(:load_from_file)
Instance.any_instance.expects(:save_to_file)
Instance.new('storage:bucket/key').save
end
should "create a new instance in cloud storage when the instance file doesn't exist" do
Instance.any_instance.expects(:load_from_file).never
Instance.any_instance.expects(:save_to_file)
Instance.new('storage:bucket/key').save
end
should "load and save from table when table given" do
Instance.any_instance.expects(:load_from_table)
Instance.any_instance.expects(:save_to_table)
Instance.new('table:foobar').save
end
should "backup on save when desired" do
location_file = Tempfile.new('instancestorage').path
location = "file:#{location_file}"
backup_file = Tempfile.new('instancestoragebackup').path
backup = "file:#{backup_file}"
instance = Instance.new(location, :backup => backup)
instance.add(@i1 = InstanceItem.new('host1', 'domain.com', [RoleItem.new('role1')], '', 'm1.small', 'ami-7000f019'))
instance.save
location_data = File.read(location_file)
backup_data = File.read(backup_file)
assert location_data.size > 0
assert backup_data.size > 0
assert_equal location_data, backup_data
end
end
context "instance items" do
setup do
@hash = {
'name' => 'host1',
'domain' => 'domain.com',
'roles' => ['role1', 'db:primary=true'],
'instance_id' => 'xxxyyy',
'image_type' => 'm1.small',
'image_id' => 'ami-7000f019',
'security_groups' => ['sg1', 'sg2']
}
end
should "create from a hash" do
item = InstanceItem.from_hash(@hash)
assert_equal 'host1', item.name
assert_equal 'domain.com', item.domain
assert_equal [RoleItem.new('role1'), RoleItem.new('db', {'primary' => true})], item.roles
assert_equal 'xxxyyy', item.instance_id
assert_equal 'm1.small', item.image_type
assert_equal 'ami-7000f019', item.image_id
assert_equal ['sg1', 'sg2'], item.security_groups
end
should "output as a hash" do
item = InstanceItem.new('host1',
'domain.com',
[RoleItem.new('role1'), RoleItem.new('db', {'primary' => true})],
'xxxyyy',
'm1.small',
'ami-7000f019',
['sg1', 'sg2'])
assert_equal @hash, item.to_hash()
end
end
end
end