/
create_vm_spec.rb
412 lines (347 loc) · 16.3 KB
/
create_vm_spec.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# Copyright (c) 2009-2013 VMware, Inc.
# Copyright (c) 2012 Piston Cloud Computing, Inc.
require "spec_helper"
describe Bosh::OpenStackCloud::Cloud, "create_vm" do
def agent_settings(unique_name, network_spec = dynamic_network_spec, ephemeral = "/dev/sdb")
{
"vm" => {
"name" => "vm-#{unique_name}"
},
"agent_id" => "agent-id",
"networks" => { "network_a" => network_spec },
"disks" => {
"system" => "/dev/sda",
"ephemeral" => ephemeral,
"persistent" => {}
},
"env" => {
"test_env" => "value"
},
"foo" => "bar", # Agent env
"baz" => "zaz"
}
end
def openstack_params(unique_name, security_groups = [], nics = [], nameserver = nil, volume_id = nil)
params = {
:name => "vm-#{unique_name}",
:image_ref => "sc-id",
:flavor_ref => "f-test",
:key_name => "test_key",
:security_groups => security_groups,
:nics => nics,
:user_data => Yajl::Encoder.encode(user_data(unique_name, nameserver, false)),
:availability_zone => "foobar-1a"
}
if volume_id
params[:block_device_mapping] = [{ :volume_size => "",
:volume_id => volume_id,
:delete_on_termination => "1",
:device_name => "/dev/vda" }]
end
params
end
def user_data(unique_name, nameserver = nil, openssh = false)
user_data = {
"registry" => {
"endpoint" => "http://registry:3333"
},
"server" => {
"name" => "vm-#{unique_name}"
}
}
user_data["openssh"] = { "public_key" => "public openssh key" } if openssh
user_data["dns"] = { "nameserver" => [nameserver] } if nameserver
user_data
end
let(:unique_name) { SecureRandom.uuid }
let(:server) { double("server", :id => "i-test", :name => "i-test") }
let(:image) { double("image", :id => "sc-id", :name => "sc-id") }
let(:flavor) { double("flavor", :id => "f-test", :name => "m1.tiny", :ram => 1024, :disk => 2, :ephemeral => 2) }
let(:key_pair) { double("key_pair", :id => "k-test", :name => "test_key",
:fingerprint => "00:01:02:03:04", :public_key => "public openssh key") }
before(:each) do
@registry = mock_registry
end
it "creates an OpenStack server and polls until it's ready" do
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => "i-test")
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, %w[default], [])).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).and_return(unique_name)
address.should_receive(:server=).with(nil)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => dynamic_network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
it "passes dns servers in server user data when present" do
network_spec = dynamic_network_spec
network_spec["dns"] = ["1.2.3.4"]
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => "i-test")
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, %w[default], [], "1.2.3.4")).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).and_return(unique_name)
address.should_receive(:server=).with(nil)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name, network_spec))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
it "creates an OpenStack server with security groups" do
security_groups = %w[bar foo]
network_spec = dynamic_network_spec
network_spec["cloud_properties"] ||= {}
network_spec["cloud_properties"]["security_groups"] = security_groups
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => nil)
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, security_groups, [])).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(security_groups)
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).and_return(unique_name)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name, network_spec))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
it "creates an OpenStack server with nic for dynamic network" do
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => nil)
nic = { "net_id" => "foo" }
network_spec = dynamic_network_spec
network_spec["cloud_properties"] ||= {}
network_spec["cloud_properties"]["net_id"] = nic["net_id"]
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, %w[default], [nic])).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).and_return(unique_name)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name, network_spec))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
it "creates an OpenStack server with nic for manual network" do
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => nil)
nic = { "net_id" => "foo", "v4_fixed_ip" => "10.0.0.5" }
network_spec = manual_network_spec
network_spec["ip"] = "10.0.0.5"
network_spec["cloud_properties"] ||= {}
network_spec["cloud_properties"]["net_id"] = nic["net_id"]
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, %w[default], [nic])).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).and_return(unique_name)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name, network_spec))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
it "associates server with floating ip if vip network is provided" do
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => "i-test")
cloud = mock_cloud do |openstack|
openstack.servers.should_receive(:create).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:find).and_return(address)
end
address.should_receive(:server=).with(nil)
address.should_receive(:server=).with(server)
cloud.should_receive(:wait_resource).with(server, :active, :state)
@registry.should_receive(:update_settings)
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
combined_network_spec)
end
it "creates an OpenStack server with a boot volume" do
network_spec = dynamic_network_spec
network_spec["dns"] = ["1.2.3.4"]
address = double("address", :id => "a-test", :ip => "10.0.0.1",
:instance_id => "i-test")
unique_vol_name = SecureRandom.uuid
disk_params = {
:display_name => "volume-#{unique_vol_name}",
:size => 2,
:imageRef => "sc-id"
}
boot_volume = double("volume", :id => "v-foobar")
cloud_options = mock_cloud_options
cloud_options['properties']['openstack']['boot_from_volume'] = true
cloud = mock_cloud(cloud_options['properties']) do |openstack|
openstack.servers.should_receive(:create).
with(openstack_params(unique_name, %w[default], [], "1.2.3.4", "v-foobar")).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.volumes.should_receive(:create).with(disk_params).and_return(boot_volume)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
openstack.addresses.should_receive(:each).and_yield(address)
end
cloud.should_receive(:generate_unique_name).exactly(2).times.and_return(unique_name, unique_vol_name)
address.should_receive(:server=).with(nil)
cloud.should_receive(:wait_resource).with(server, :active, :state)
cloud.should_receive(:wait_resource).with(boot_volume, :available)
@registry.should_receive(:update_settings).
with("i-test", agent_settings(unique_name, network_spec))
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => network_spec },
nil, { "test_env" => "value" })
vm_id.should == "i-test"
end
context "when cannot create an OpenStack server" do
let(:cloud) do
c = mock_cloud do |openstack|
openstack.servers.should_receive(:create).and_return(server)
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
openstack.key_pairs.should_receive(:find).and_return(key_pair)
end
allow(c).to receive(:wait_resource).with(server, :active, :state).and_raise(Bosh::Clouds::CloudError)
c
end
it "raises a Retryable Error" do
allow(server).to receive(:destroy)
expect {
vm_id = cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{ "network_a" => dynamic_network_spec },
nil, { "test_env" => "value" })
}.to raise_error(Bosh::Clouds::VMCreationFailed)
end
it "destroys the server" do
expect(server).to receive(:destroy)
cloud.create_vm("agent-id", "sc-id",
resource_pool_spec,
{"network_a" => dynamic_network_spec},
nil, {"test_env" => "value"}) rescue nil
end
end
it "raises an error when a security group doesn't exist" do
cloud = mock_cloud do |openstack|
openstack.security_groups.should_receive(:collect).and_return(%w[foo])
end
expect {
cloud.create_vm("agent-id", "sc-id", resource_pool_spec, { "network_a" => dynamic_network_spec },
nil, { "test_env" => "value" })
}.to raise_error(Bosh::Clouds::CloudError, "Security group `default' not found")
end
it "raises an error when flavor doesn't have enough ephemeral disk capacity" do
flavor = double("flavor", :id => "f-test", :name => "m1.tiny", :ram => 1024, :ephemeral => 1)
cloud = mock_cloud do |openstack|
openstack.security_groups.should_receive(:collect).and_return(%w[default])
openstack.images.should_receive(:find).and_return(image)
openstack.flavors.should_receive(:find).and_return(flavor)
end
expect {
cloud.create_vm("agent-id", "sc-id", resource_pool_spec, { "network_a" => dynamic_network_spec },
nil, { "test_env" => "value" })
}.to raise_error(Bosh::Clouds::CloudError, "Flavor `m1.tiny' should have at least 2Gb of ephemeral disk")
end
def volume(zone)
vol = double("volume")
vol.stub(:availability_zone).and_return(zone)
vol
end
describe "#select_availability_zone" do
it "should return nil when all values are nil" do
cloud = mock_cloud
cloud.select_availability_zone(nil, nil).should == nil
end
it "should select the resource pool availability_zone when disks are nil" do
cloud = mock_cloud
cloud.select_availability_zone(nil, "foobar-1a").should == "foobar-1a"
end
it "should select the zone from a list of disks" do
cloud = mock_cloud do |openstack|
openstack.volumes.stub(:get).and_return(volume("foo"), volume("foo"))
end
cloud.select_availability_zone(%w[cid1 cid2], nil).should == "foo"
end
it "should select the zone from a list of disks and a default" do
cloud = mock_cloud do |openstack|
openstack.volumes.stub(:get).and_return(volume("foo"), volume("foo"))
end
cloud.select_availability_zone(%w[cid1 cid2], "foo").should == "foo"
end
end
describe "#ensure_same_availability_zone" do
it "should raise an error when the zones differ" do
cloud = mock_cloud
expect {
cloud.ensure_same_availability_zone([volume("foo"), volume("bar")],
nil)
}.to raise_error Bosh::Clouds::CloudError
end
it "should raise an error when the zones differ" do
cloud = mock_cloud
expect {
cloud.ensure_same_availability_zone([volume("foo"), volume("bar")],
"foo")
}.to raise_error Bosh::Clouds::CloudError
end
it "should raise an error when the zones differ" do
cloud = mock_cloud
expect {
cloud.ensure_same_availability_zone([volume("foo"), volume("foo")],
"bar")
}.to raise_error Bosh::Clouds::CloudError
end
end
end