63
63
QUOTAS = quota.QUOTAS
64
64
LOG = logging.getLogger(__name__)
65
65
FLAGS = flags.FLAGS
66
flags.DECLARE('stub_network', 'nova.compute.manager')
67
66
flags.DECLARE('live_migration_retry_count', 'nova.compute.manager')
70
69
FAKE_IMAGE_REF = 'fake-image-ref'
71
orig_rpc_call = rpc.call
72
orig_rpc_cast = rpc.cast
75
def rpc_call_wrapper(context, topic, msg, do_cast=True):
76
"""Stub out the scheduler creating the instance entry"""
77
if (topic == FLAGS.scheduler_topic and
78
msg['method'] == 'run_instance'):
79
request_spec = msg['args']['request_spec']
80
reservations = msg['args'].get('reservations')
81
scheduler = scheduler_driver.Scheduler
82
num_instances = request_spec.get('num_instances', 1)
84
for num in xrange(num_instances):
85
request_spec['instance_properties']['launch_index'] = num
86
instance = scheduler().create_instance_db_entry(
87
context, request_spec, reservations)
88
encoded = scheduler_driver.encode_instance(instance)
89
instances.append(encoded)
93
orig_rpc_cast(context, topic, msg)
95
return orig_rpc_call(context, topic, msg)
98
def rpc_cast_wrapper(context, topic, msg):
99
"""Stub out the scheduler creating the instance entry in
100
the reservation_id case.
102
rpc_call_wrapper(context, topic, msg, do_cast=True)
105
72
def nop_report_driver_status(self):
117
87
super(BaseTestCase, self).setUp()
118
88
self.flags(compute_driver='nova.virt.fake.FakeDriver',
120
89
notification_driver=['nova.openstack.common.notifier.test_notifier'],
121
90
network_manager='nova.network.manager.FlatManager')
122
91
self.compute = importutils.import_object(FLAGS.compute_manager)
93
# override tracker with a version that doesn't need the database:
94
self.compute.resource_tracker = \
95
fake_resource_tracker.FakeResourceTracker(self.compute.host,
97
self.compute.update_available_resource(
98
context.get_admin_context())
124
100
self.user_id = 'fake'
125
101
self.project_id = 'fake'
126
102
self.context = context.RequestContext(self.user_id,
293
269
db.instance_destroy(self.context, instance['uuid'])
271
def test_create_instance_insufficient_memory(self):
272
params = {"memory_mb": 999999999999}
273
instance = self._create_fake_instance(params)
274
self.assertRaises(exception.ComputeResourcesUnavailable,
275
self.compute.run_instance, self.context, instance=instance)
277
def test_create_instance_insufficient_disk(self):
278
params = {"root_gb": 999999999999,
279
"ephemeral_gb": 99999999999}
280
instance = self._create_fake_instance(params)
281
self.assertRaises(exception.ComputeResourcesUnavailable,
282
self.compute.run_instance, self.context, instance=instance)
284
def test_create_multiple_instances_then_starve(self):
285
params = {"memory_mb": 1024, "root_gb": 128, "ephemeral_gb": 128}
286
instance = self._create_fake_instance(params)
287
self.compute.run_instance(self.context, instance=instance)
288
self.assertEquals(1024,
289
self.compute.resource_tracker.compute_node['memory_mb_used'])
290
self.assertEquals(256,
291
self.compute.resource_tracker.compute_node['local_gb_used'])
293
params = {"memory_mb": 2048, "root_gb": 256, "ephemeral_gb": 256}
294
instance = self._create_fake_instance(params)
295
self.compute.run_instance(self.context, instance=instance)
296
self.assertEquals(3072,
297
self.compute.resource_tracker.compute_node['memory_mb_used'])
298
self.assertEquals(768,
299
self.compute.resource_tracker.compute_node['local_gb_used'])
301
params = {"memory_mb": 8192, "root_gb": 8192, "ephemeral_gb": 8192}
302
instance = self._create_fake_instance(params)
303
self.assertRaises(exception.ComputeResourcesUnavailable,
304
self.compute.run_instance, self.context, instance=instance)
306
def test_create_instance_with_oversubscribed_ram(self):
307
"""Test passing of oversubscribed ram policy from the scheduler."""
309
# get total memory as reported by virt driver:
310
resources = self.compute.driver.get_available_resource()
311
total_mem_mb = resources['memory_mb']
313
oversub_limit_mb = total_mem_mb * 1.5
314
instance_mb = int(total_mem_mb * 1.45)
316
# build an instance, specifying an amount of memory that exceeds
317
# total_mem_mb, but is less than the oversubscribed limit:
318
params = {"memory_mb": instance_mb, "root_gb": 128,
320
instance = self._create_fake_instance(params)
322
filter_properties = dict(memory_mb_limit=oversub_limit_mb)
323
self.compute.run_instance(self.context, instance=instance,
324
filter_properties=filter_properties)
326
self.assertEqual(instance_mb,
327
self.compute.resource_tracker.compute_node['memory_mb_used'])
329
def test_create_instance_with_oversubscribed_ram_fail(self):
330
"""Test passing of oversubscribed ram policy from the scheduler, but
331
with insufficient memory.
333
# get total memory as reported by virt driver:
334
resources = self.compute.driver.get_available_resource()
335
total_mem_mb = resources['memory_mb']
337
oversub_limit_mb = total_mem_mb * 1.5
338
instance_mb = int(total_mem_mb * 1.55)
340
# build an instance, specifying an amount of memory that exceeds
341
# total_mem_mb, but is less than the oversubscribed limit:
342
params = {"memory_mb": instance_mb, "root_gb": 128,
344
instance = self._create_fake_instance(params)
346
filter_properties = dict(memory_mb_limit=oversub_limit_mb)
348
self.assertRaises(exception.ComputeResourcesUnavailable,
349
self.compute.run_instance, self.context, instance=instance,
350
filter_properties=filter_properties)
295
352
def test_default_access_ip(self):
296
self.flags(default_access_ip_network_name='test1', stub_network=False)
353
self.flags(default_access_ip_network_name='test1')
354
fake_network.unset_stub_network_methods(self.stubs)
297
355
instance = jsonutils.to_primitive(self._create_fake_instance())
396
454
LOG.info(_("After terminating instances: %s"), instances)
397
455
self.assertEqual(len(instances), 0)
457
def test_run_terminate_with_vol_attached(self):
458
"""Make sure it is possible to run and terminate instance with volume
461
instance = jsonutils.to_primitive(self._create_fake_instance())
463
self.compute.run_instance(self.context, instance=instance)
465
instances = db.instance_get_all(context.get_admin_context())
466
LOG.info(_("Running instances: %s"), instances)
467
self.assertEqual(len(instances), 1)
469
def fake_check_attach(*args, **kwargs):
472
def fake_reserve_volume(*args, **kwargs):
475
def fake_volume_get(self, context, volume_id):
476
return {'id': volume_id}
478
self.stubs.Set(nova.volume.api.API, 'get', fake_volume_get)
479
self.stubs.Set(nova.volume.api.API, 'check_attach', fake_check_attach)
480
self.stubs.Set(nova.volume.api.API, 'reserve_volume',
483
self.compute_api.attach_volume(self.context, instance, 1,
486
self.compute.terminate_instance(self.context, instance=instance)
488
instances = db.instance_get_all(context.get_admin_context())
489
LOG.info(_("After terminating instances: %s"), instances)
490
self.assertEqual(len(instances), 0)
399
492
def test_terminate_no_network(self):
400
493
# This is as reported in LP bug 1008875
401
494
instance = jsonutils.to_primitive(self._create_fake_instance())
3626
3725
self.assertEqual(metadata, {'key1': 'value1', 'key2': 'value2'})
3627
3726
self.assertEqual(meta_changes, [{'key2': ['+', 'value2']}])
3728
self.assertEquals(len(test_notifier.NOTIFICATIONS), 1)
3729
msg = test_notifier.NOTIFICATIONS[0]
3730
payload = msg['payload']
3731
self.assertTrue('metadata' in payload)
3732
self.assertEquals(payload['metadata'], metadata)
3629
3734
new_metadata = {'key2': 'bah', 'key3': 'value3'}
3630
3735
self.compute_api.update_instance_metadata(_context, instance,
3631
3736
new_metadata, delete=True)
3637
3742
'key3': ['+', 'value3'],
3745
self.assertEquals(len(test_notifier.NOTIFICATIONS), 2)
3746
msg = test_notifier.NOTIFICATIONS[1]
3747
payload = msg['payload']
3748
self.assertTrue('metadata' in payload)
3749
self.assertEquals(payload['metadata'], metadata)
3640
3751
self.compute_api.delete_instance_metadata(_context, instance, 'key2')
3641
3752
metadata = self.compute_api.get_instance_metadata(_context, instance)
3642
3753
self.assertEqual(metadata, {'key3': 'value3'})
3643
3754
self.assertEqual(meta_changes, [{'key2': ['-']}])
3756
self.assertEquals(len(test_notifier.NOTIFICATIONS), 3)
3757
msg = test_notifier.NOTIFICATIONS[2]
3758
payload = msg['payload']
3759
self.assertTrue('metadata' in payload)
3760
self.assertEquals(payload['metadata'], {})
3645
3762
db.instance_destroy(_context, instance['uuid'])
3647
3764
def test_get_instance_faults(self):
3930
4047
fake_instance, fake_console_type)
3931
4048
self.assertEqual(console, {'url': 'fake_console_url'})
4050
def test_get_vnc_console_no_host(self):
4051
instance = self._create_fake_instance(params={'host': ''})
4053
self.assertRaises(exception.InstanceNotReady,
4054
self.compute_api.get_vnc_console,
4055
self.context, instance, 'novnc')
4057
db.instance_destroy(self.context, instance['uuid'])
3933
4059
def test_console_output(self):
3934
4060
fake_instance = {'uuid': 'fake_uuid',
3935
4061
'host': 'fake_compute_host'}