~ubuntu-branches/ubuntu/raring/nova/raring-proposed

« back to all changes in this revision

Viewing changes to nova/tests/api/ec2/test_cloud.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short, James Page
  • Date: 2013-03-20 12:59:22 UTC
  • mfrom: (1.1.69)
  • Revision ID: package-import@ubuntu.com-20130320125922-ohvfav96lemn9wlz
Tags: 1:2013.1~rc1-0ubuntu1
[ Chuck Short ]
* New upstream release.
* debian/patches/avoid_setuptools_git_dependency.patch: Refreshed.
* debian/control: Clean up dependencies:
  - Dropped python-gflags no longer needed.
  - Dropped python-daemon no longer needed.
  - Dropped python-glance no longer needed.
  - Dropped python-lockfile no longer needed.
  - Dropped python-simplejson no longer needed.
  - Dropped python-tempita no longer needed.
  - Dropped python-xattr no longer needed.
  - Add sqlite3 required for the testsuite.

[ James Page ]
* d/watch: Update uversionmangle to deal with upstream versioning
  changes, remove tarballs.openstack.org. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
from nova.api.ec2 import inst_state
34
34
from nova.api.metadata import password
35
35
from nova.compute import api as compute_api
 
36
from nova.compute import instance_types
36
37
from nova.compute import power_state
37
38
from nova.compute import utils as compute_utils
38
39
from nova.compute import vm_states
41
42
from nova import exception
42
43
from nova.image import s3
43
44
from nova.network import api as network_api
 
45
from nova.network import quantumv2
44
46
from nova.openstack.common import log as logging
45
47
from nova.openstack.common import rpc
46
48
from nova import test
 
49
from nova.tests.api.openstack.compute.contrib import (
 
50
    test_quantum_security_groups as test_quantum)
47
51
from nova.tests import fake_network
48
52
from nova.tests.image import fake
49
53
from nova.tests import matchers
756
760
        self._stub_instance_get_with_fixed_ips('get')
757
761
 
758
762
        image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
 
763
        sys_meta = instance_types.save_instance_type_info(
 
764
            {}, instance_types.get_instance_type(1))
759
765
        inst1 = db.instance_create(self.context, {'reservation_id': 'a',
760
766
                                                  'image_ref': image_uuid,
761
767
                                                  'instance_type_id': 1,
762
768
                                                  'host': 'host1',
763
769
                                                  'hostname': 'server-1234',
764
 
                                                  'vm_state': 'active'})
 
770
                                                  'vm_state': 'active',
 
771
                                                  'system_metadata': sys_meta})
765
772
        inst2 = db.instance_create(self.context, {'reservation_id': 'a',
766
773
                                                  'image_ref': image_uuid,
767
774
                                                  'instance_type_id': 1,
768
775
                                                  'host': 'host2',
769
776
                                                  'hostname': 'server-4321',
770
 
                                                  'vm_state': 'active'})
 
777
                                                  'vm_state': 'active',
 
778
                                                  'system_metadata': sys_meta})
771
779
        comp1 = db.service_create(self.context, {'host': 'host1',
772
780
                                                 'topic': "compute"})
773
781
        agg = db.aggregate_create(self.context,
846
854
        self._stub_instance_get_with_fixed_ips('get')
847
855
 
848
856
        image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
 
857
        sys_meta = instance_types.save_instance_type_info(
 
858
            {}, instance_types.get_instance_type(1))
849
859
        inst_base = {
850
860
                'reservation_id': 'a',
851
861
                'image_ref': image_uuid,
852
862
                'instance_type_id': 1,
853
 
                'vm_state': 'active'
 
863
                'vm_state': 'active',
 
864
                'system_metadata': sys_meta,
854
865
        }
855
866
 
856
867
        inst1_kwargs = {}
898
909
        def test_instance_state(expected_code, expected_name,
899
910
                                power_state_, vm_state_, values=None):
900
911
            image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
 
912
            sys_meta = instance_types.save_instance_type_info(
 
913
                {}, instance_types.get_instance_type(1))
901
914
            values = values or {}
902
915
            values.update({'image_ref': image_uuid, 'instance_type_id': 1,
903
 
                           'power_state': power_state_, 'vm_state': vm_state_})
 
916
                           'power_state': power_state_, 'vm_state': vm_state_,
 
917
                           'system_metadata': sys_meta})
904
918
            inst = db.instance_create(self.context, values)
905
919
 
906
920
            instance_id = ec2utils.id_to_ec2_inst_id(inst['uuid'])
930
944
        self._stub_instance_get_with_fixed_ips('get')
931
945
 
932
946
        image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
 
947
        sys_meta = instance_types.save_instance_type_info(
 
948
            {}, instance_types.get_instance_type(1))
933
949
        inst1 = db.instance_create(self.context, {'reservation_id': 'a',
934
950
                                                  'image_ref': image_uuid,
935
951
                                                  'instance_type_id': 1,
936
952
                                                  'hostname': 'server-1234',
937
 
                                                  'vm_state': 'active'})
 
953
                                                  'vm_state': 'active',
 
954
                                                  'system_metadata': sys_meta})
938
955
        comp1 = db.service_create(self.context, {'host': 'host1',
939
956
                                                 'topic': "compute"})
940
957
        result = self.cloud.describe_instances(self.context)
954
971
 
955
972
    def test_describe_instances_deleted(self):
956
973
        image_uuid = 'cedef40a-ed67-4d10-800e-17455edce175'
 
974
        sys_meta = instance_types.save_instance_type_info(
 
975
            {}, instance_types.get_instance_type(1))
957
976
        args1 = {'reservation_id': 'a',
958
977
                 'image_ref': image_uuid,
959
978
                 'instance_type_id': 1,
960
979
                 'host': 'host1',
961
 
                 'vm_state': 'active'}
 
980
                 'vm_state': 'active',
 
981
                 'system_metadata': sys_meta}
962
982
        inst1 = db.instance_create(self.context, args1)
963
983
        args2 = {'reservation_id': 'b',
964
984
                 'image_ref': image_uuid,
965
985
                 'instance_type_id': 1,
966
986
                 'host': 'host1',
967
 
                 'vm_state': 'active'}
 
987
                 'vm_state': 'active',
 
988
                 'system_metadata': sys_meta}
968
989
        inst2 = db.instance_create(self.context, args2)
969
990
        db.instance_destroy(self.context, inst1['uuid'])
970
991
        result = self.cloud.describe_instances(self.context)
975
996
 
976
997
    def test_describe_instances_with_image_deleted(self):
977
998
        image_uuid = 'aebef54a-ed67-4d10-912f-14455edce176'
 
999
        sys_meta = instance_types.save_instance_type_info(
 
1000
            {}, instance_types.get_instance_type(1))
978
1001
        args1 = {'reservation_id': 'a',
979
1002
                 'image_ref': image_uuid,
980
1003
                 'instance_type_id': 1,
981
1004
                 'host': 'host1',
982
 
                 'vm_state': 'active'}
 
1005
                 'vm_state': 'active',
 
1006
                 'system_metadata': sys_meta}
983
1007
        inst1 = db.instance_create(self.context, args1)
984
1008
        args2 = {'reservation_id': 'b',
985
1009
                 'image_ref': image_uuid,
986
1010
                 'instance_type_id': 1,
987
1011
                 'host': 'host1',
988
 
                 'vm_state': 'active'}
 
1012
                 'vm_state': 'active',
 
1013
                 'system_metadata': sys_meta}
989
1014
        inst2 = db.instance_create(self.context, args2)
990
1015
        result = self.cloud.describe_instances(self.context)
991
1016
        self.assertEqual(len(result['reservationSet']), 2)
1904
1929
        self.stubs.Set(fake._FakeImageService, 'show', fake_show)
1905
1930
 
1906
1931
        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
1907
 
            class BDM(object):
1908
 
                def __init__(self):
1909
 
                    self.no_device = None
1910
 
                    self.values = dict(id=1,
1911
 
                                       snapshot_id=snapshots[0],
1912
 
                                       volume_id=volumes[0],
1913
 
                                       virtual_name=None,
1914
 
                                       volume_size=1,
1915
 
                                       device_name='sda1',
1916
 
                                       delete_on_termination=False,
1917
 
                                       connection_info='{"foo":"bar"}')
1918
 
 
1919
 
                def __getattr__(self, name):
1920
 
                    """Properly delegate dotted lookups."""
1921
 
                    if name in self.__dict__['values']:
1922
 
                        return self.values.get(name)
1923
 
                    try:
1924
 
                        return self.__dict__[name]
1925
 
                    except KeyError:
1926
 
                        raise AttributeError
1927
 
 
1928
 
                def __getitem__(self, key):
1929
 
                    return self.values.get(key)
1930
 
 
1931
 
                def iteritems(self):
1932
 
                    return self.values.iteritems()
1933
 
 
1934
 
            return [BDM()]
 
1932
            return [dict(id=1,
 
1933
                         snapshot_id=snapshots[0],
 
1934
                         volume_id=volumes[0],
 
1935
                         virtual_name=None,
 
1936
                         volume_size=1,
 
1937
                         device_name='sda1',
 
1938
                         delete_on_termination=False,
 
1939
                         no_device=None,
 
1940
                         connection_info='{"foo":"bar"}')]
1935
1941
 
1936
1942
        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
1937
1943
                       fake_block_device_mapping_get_all_by_instance)
1995
2001
        ec2_instance_id = self._run_instance(**kwargs)
1996
2002
 
1997
2003
        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
1998
 
            class BDM(object):
1999
 
                def __init__(self):
2000
 
                    self.no_device = None
2001
 
                    self.values = dict(snapshot_id=snapshots[0],
2002
 
                                       volume_id=volumes[0],
2003
 
                                       virtual_name=None,
2004
 
                                       volume_size=1,
2005
 
                                       device_name='vda',
2006
 
                                       delete_on_termination=False)
2007
 
 
2008
 
                def __getattr__(self, name):
2009
 
                    """Properly delegate dotted lookups."""
2010
 
                    if name in self.__dict__['values']:
2011
 
                        return self.values.get(name)
2012
 
                    try:
2013
 
                        return self.__dict__[name]
2014
 
                    except KeyError:
2015
 
                        raise AttributeError
2016
 
 
2017
 
                def __getitem__(self, key):
2018
 
                    return self.values.get(key)
2019
 
 
2020
 
                def iteritems(self):
2021
 
                    return self.values.iteritems()
2022
 
 
2023
 
            return [BDM()]
 
2004
            return [dict(snapshot_id=snapshots[0],
 
2005
                         volume_id=volumes[0],
 
2006
                         virtual_name=None,
 
2007
                         volume_size=1,
 
2008
                         device_name='vda',
 
2009
                         delete_on_termination=False,
 
2010
                         no_device=None)]
2024
2011
 
2025
2012
        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
2026
2013
                       fake_block_device_mapping_get_all_by_instance)
2083
2070
                       self._fake_bdm_get)
2084
2071
 
2085
2072
        def fake_get(ctxt, instance_id):
 
2073
            inst_type = instance_types.get_default_instance_type()
 
2074
            inst_type['name'] = 'fake_type'
 
2075
            sys_meta = instance_types.save_instance_type_info({}, inst_type)
 
2076
            sys_meta = utils.dict_to_metadata(sys_meta)
2086
2077
            return {
2087
2078
                'id': 0,
2088
2079
                'uuid': 'e5fe5518-0288-4fa3-b0c4-c79764101b85',
2089
2080
                'root_device_name': '/dev/sdh',
2090
2081
                'security_groups': [{'name': 'fake0'}, {'name': 'fake1'}],
2091
2082
                'vm_state': vm_states.STOPPED,
2092
 
                'instance_type': {'name': 'fake_type'},
2093
2083
                'kernel_id': 'cedef40a-ed67-4d10-800e-17455edce175',
2094
2084
                'ramdisk_id': '76fa36fc-c930-4bf3-8c8a-ea2a2420deb6',
2095
2085
                'user_data': 'fake-user data',
2096
2086
                'shutdown_terminate': False,
2097
2087
                'disable_terminate': False,
 
2088
                'system_metadata': sys_meta,
2098
2089
                }
2099
2090
        self.stubs.Set(self.cloud.compute_api, 'get', fake_get)
2100
2091
 
2226
2217
        test_dia_iisb('stop', image_id='ami-4')
2227
2218
        test_dia_iisb('stop', image_id='ami-5')
2228
2219
        test_dia_iisb('stop', image_id='ami-6')
 
2220
 
 
2221
 
 
2222
class CloudTestCaseQuantumProxy(test.TestCase):
 
2223
    def setUp(self):
 
2224
        cfg.CONF.set_override('security_group_api', 'quantum')
 
2225
        self.cloud = cloud.CloudController()
 
2226
        self.original_client = quantumv2.get_client
 
2227
        quantumv2.get_client = test_quantum.get_client
 
2228
        self.user_id = 'fake'
 
2229
        self.project_id = 'fake'
 
2230
        self.context = context.RequestContext(self.user_id,
 
2231
                                              self.project_id,
 
2232
                                              is_admin=True)
 
2233
        super(CloudTestCaseQuantumProxy, self).setUp()
 
2234
 
 
2235
    def tearDown(self):
 
2236
        quantumv2.get_client = self.original_client
 
2237
        test_quantum.get_client()._reset()
 
2238
        super(CloudTestCaseQuantumProxy, self).tearDown()
 
2239
 
 
2240
    def test_describe_security_groups(self):
 
2241
        # Makes sure describe_security_groups works and filters results.
 
2242
        group_name = 'test'
 
2243
        description = 'test'
 
2244
        self.cloud.create_security_group(self.context, group_name,
 
2245
                                         description)
 
2246
        result = self.cloud.describe_security_groups(self.context)
 
2247
        # NOTE(vish): should have the default group as well
 
2248
        self.assertEqual(len(result['securityGroupInfo']), 2)
 
2249
        result = self.cloud.describe_security_groups(self.context,
 
2250
                      group_name=[group_name])
 
2251
        self.assertEqual(len(result['securityGroupInfo']), 1)
 
2252
        self.assertEqual(result['securityGroupInfo'][0]['groupName'],
 
2253
                         group_name)
 
2254
        self.cloud.delete_security_group(self.context, group_name)
 
2255
 
 
2256
    def test_describe_security_groups_by_id(self):
 
2257
        group_name = 'test'
 
2258
        description = 'test'
 
2259
        self.cloud.create_security_group(self.context, group_name,
 
2260
                                         description)
 
2261
        quantum = test_quantum.get_client()
 
2262
        # Get id from quantum since cloud.create_security_group
 
2263
        # does not expose it.
 
2264
        search_opts = {'name': group_name}
 
2265
        groups = quantum.list_security_groups(
 
2266
            **search_opts)['security_groups']
 
2267
        result = self.cloud.describe_security_groups(self.context,
 
2268
                      group_id=[groups[0]['id']])
 
2269
        self.assertEqual(len(result['securityGroupInfo']), 1)
 
2270
        self.assertEqual(
 
2271
                result['securityGroupInfo'][0]['groupName'],
 
2272
                group_name)
 
2273
        self.cloud.delete_security_group(self.context, group_name)
 
2274
 
 
2275
    def test_create_delete_security_group(self):
 
2276
        descript = 'test description'
 
2277
        create = self.cloud.create_security_group
 
2278
        result = create(self.context, 'testgrp', descript)
 
2279
        group_descript = result['securityGroupSet'][0]['groupDescription']
 
2280
        self.assertEqual(descript, group_descript)
 
2281
        delete = self.cloud.delete_security_group
 
2282
        self.assertTrue(delete(self.context, 'testgrp'))