From 579d0e1437efb32ef1a1c50ddbfca9093cfa3d18 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Wed, 29 Dec 2010 23:30:08 +0300 Subject: added tests for EC2 describe_instances --- nova/api/ec2/cloud.py | 7 +++++-- nova/tests/test_cloud.py | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 0c20ef329..9fa422301 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -139,7 +139,10 @@ class CloudController(object): "args": {"security_group_id": security_group.id}}) def _get_availability_zone_by_host(self, context, hostname): - return db.service_get_all_compute_by_host(context, hostname)[0]['availability_zone'] + services = db.service_get_all_compute_by_host(context, hostname) + if len(services) > 0: + return services[0]['availability_zone'] + raise Exception(_('No service with hostname: %s' % hostname)) def get_metadata(self, address): ctxt = context.get_admin_context() @@ -675,7 +678,7 @@ class CloudController(object): r['groupSet'] = self._convert_to_set([], 'groups') r['instancesSet'] = [] reservations[instance['reservation_id']] = r - availability_zone = self._get_availability_zone_by_host(ctxt, instance['hostname']) + availability_zone = self._get_availability_zone_by_host(context, instance['hostname']) i['placement'] = {'availabilityZone': availability_zone} reservations[instance['reservation_id']]['instancesSet'].append(i) diff --git a/nova/tests/test_cloud.py b/nova/tests/test_cloud.py index d7e99b3c8..3adecb729 100644 --- a/nova/tests/test_cloud.py +++ b/nova/tests/test_cloud.py @@ -62,7 +62,7 @@ class CloudTestCase(test.TestCase): self.cloud = cloud.CloudController() # set up services - self.compute = service.Service.create(binary='nova-compute') + self.compute = service.Service.create(binary='nova-compute', host='host1') self.compute.start() self.network = service.Service.create(binary='nova-network') self.network.start() @@ -228,6 +228,20 @@ class CloudTestCase(test.TestCase): logging.debug("Terminating instance %s" % instance_id) rv = yield self.compute.terminate_instance(instance_id) + def test_describe_instances(self): + """Makes sure describe_instances works.""" + instance1 = db.instance_create(self.context, {'hostname': 'host2'}) + service1 = db.service_create(self.context, {'host': 'host1', + 'availability_zone': 'zone1', + 'topic': "compute"}) + result = self.cloud.describe_instances(self.context) + self.assertEqual(result['reservationSet'][0]\ + ['instancesSet'][0]\ + ['placement']['availabilityZone'], 'zone1') + db.instance_destroy(self.context, instance1['id']) + db.service_destroy(self.context, service1['id']) + + def test_instance_update_state(self): def instance(num): return { -- cgit