Ejemplo n.º 1
0
    def test_prep_resize_post_populates_retry(self):
        # Prep resize should add a ('host', 'node') entry to the retry dict.
        sched = fakes.FakeFilterScheduler()

        image = 'image'
        instance = db.instance_create(self.context, {})

        instance_properties = {'project_id': 'fake', 'os_type': 'Linux'}
        instance_type = flavors.get_instance_type_by_name("m1.tiny")
        request_spec = {'instance_properties': instance_properties,
                        'instance_type': instance_type}
        retry = {'hosts': [], 'num_attempts': 1}
        filter_properties = {'retry': retry}
        reservations = None

        host = fakes.FakeHostState('host', 'node', {})
        weighed_host = weights.WeighedHost(host, 1)
        weighed_hosts = [weighed_host]

        self.mox.StubOutWithMock(sched, '_schedule')
        self.mox.StubOutWithMock(sched.compute_rpcapi, 'prep_resize')

        sched._schedule(self.context, request_spec, filter_properties,
                [instance['uuid']]).AndReturn(weighed_hosts)
        sched.compute_rpcapi.prep_resize(self.context, image, instance,
                instance_type, 'host', reservations, request_spec=request_spec,
                filter_properties=filter_properties, node='node')

        self.mox.ReplayAll()
        sched.schedule_prep_resize(self.context, image, request_spec,
                filter_properties, instance, instance_type, reservations)

        self.assertEqual([['host', 'node']],
                         filter_properties['retry']['hosts'])
Ejemplo n.º 2
0
 def test_notify_usage_exists_instance_not_found(self):
     # Ensure 'exists' notification generates appropriate usage data.
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     self.compute.terminate_instance(self.context, instance)
     compute_utils.notify_usage_exists(self.context, instance)
     msg = test_notifier.NOTIFICATIONS[-1]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.exists')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at', 'state',
                  'state_description', 'bandwidth',
                  'audit_period_beginning', 'audit_period_ending',
                  'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'], {})
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
Ejemplo n.º 3
0
 def test_notify_usage_exists_instance_not_found(self):
     # Ensure 'exists' notification generates appropriate usage data.
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     self.compute.terminate_instance(self.context, instance)
     compute_utils.notify_usage_exists(self.context, instance)
     msg = test_notifier.NOTIFICATIONS[-1]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.exists')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at',
                  'state', 'state_description',
                  'bandwidth', 'audit_period_beginning',
                  'audit_period_ending', 'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'], {})
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
Ejemplo n.º 4
0
 def test_notify_usage_exists_deleted_instance(self):
     # Ensure 'exists' notification generates appropriate usage data.
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     # Set some system metadata
     sys_metadata = {'image_md_key1': 'val1',
                     'image_md_key2': 'val2',
                     'other_data': 'meow'}
     db.instance_system_metadata_update(self.context, instance['uuid'],
             sys_metadata, False)
     self.compute.terminate_instance(self.context, instance)
     instance = db.instance_get(self.context.elevated(read_deleted='yes'),
                                instance_id)
     compute_utils.notify_usage_exists(self.context, instance)
     msg = test_notifier.NOTIFICATIONS[-1]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.exists')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at',
                  'state', 'state_description',
                  'bandwidth', 'audit_period_beginning',
                  'audit_period_ending', 'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'],
             {'md_key1': 'val1', 'md_key2': 'val2'})
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
Ejemplo n.º 5
0
    def test_prep_resize_post_populates_retry(self):
        # Prep resize should add a ('host', 'node') entry to the retry dict.
        sched = fakes.FakeFilterScheduler()

        image = 'image'
        instance = db.instance_create(self.context, {})

        instance_properties = {'project_id': 'fake', 'os_type': 'Linux'}
        instance_type = flavors.get_instance_type_by_name("m1.tiny")
        request_spec = {'instance_properties': instance_properties,
                        'instance_type': instance_type}
        retry = {'hosts': [], 'num_attempts': 1}
        filter_properties = {'retry': retry}
        reservations = None

        host = fakes.FakeHostState('host', 'node', {})
        weighed_host = weights.WeighedHost(host, 1)
        weighed_hosts = [weighed_host]

        self.mox.StubOutWithMock(sched, '_schedule')
        self.mox.StubOutWithMock(sched.compute_rpcapi, 'prep_resize')

        sched._schedule(self.context, request_spec, filter_properties,
                [instance['uuid']]).AndReturn(weighed_hosts)
        sched.compute_rpcapi.prep_resize(self.context, image, instance,
                instance_type, 'host', reservations, request_spec=request_spec,
                filter_properties=filter_properties, node='node')

        self.mox.ReplayAll()
        sched.schedule_prep_resize(self.context, image, request_spec,
                filter_properties, instance, instance_type, reservations)

        self.assertEqual([['host', 'node']],
                         filter_properties['retry']['hosts'])
Ejemplo n.º 6
0
 def list(self, name=None):
     """Lists all active or specific instance types / flavors."""
     try:
         if name is None:
             inst_types = flavors.get_all_types()
         else:
             inst_types = flavors.get_instance_type_by_name(name)
     except db_exc.DBError as e:
         _db_error(e)
     if isinstance(inst_types.values()[0], dict):
         for k, v in inst_types.iteritems():
             self._print_instance_types(k, v)
     else:
         self._print_instance_types(name, inst_types)
Ejemplo n.º 7
0
    def set_key(self, name, key, value=None):
        """Add key/value pair to specified instance type's extra_specs."""
        try:
            try:
                inst_type = flavors.get_instance_type_by_name(name)
            except exception.InstanceTypeNotFoundByName as e:
                print e
                return 2

            ctxt = context.get_admin_context()
            ext_spec = {key: value}
            db.instance_type_extra_specs_update_or_create(ctxt, inst_type["flavorid"], ext_spec)
            print _("Key %(key)s set to %(value)s on instance" " type %(name)s") % locals()
        except db_exc.DBError as e:
            _db_error(e)
Ejemplo n.º 8
0
 def launch_vpn_instance(self, context):
     LOG.debug(_("Launching VPN for %s") % (context.project_id))
     key_name = self.setup_key_pair(context)
     group_name = self.setup_security_group(context)
     instance_type = flavors.get_instance_type_by_name(
             CONF.vpn_instance_type)
     instance_name = '%s%s' % (context.project_id, CONF.vpn_key_suffix)
     user_data = self.get_encoded_zip(context.project_id)
     return self.compute_api.create(context,
                                    instance_type,
                                    CONF.vpn_image_id,
                                    display_name=instance_name,
                                    user_data=user_data,
                                    key_name=key_name,
                                    security_group=[group_name])
Ejemplo n.º 9
0
 def launch_vpn_instance(self, context):
     LOG.debug(_("Launching VPN for %s") % (context.project_id))
     key_name = self.setup_key_pair(context)
     group_name = self.setup_security_group(context)
     instance_type = flavors.get_instance_type_by_name(
         CONF.vpn_instance_type)
     instance_name = '%s%s' % (context.project_id, CONF.vpn_key_suffix)
     user_data = self.get_encoded_zip(context.project_id)
     return self.compute_api.create(context,
                                    instance_type,
                                    CONF.vpn_image_id,
                                    display_name=instance_name,
                                    user_data=user_data,
                                    key_name=key_name,
                                    security_group=[group_name])
Ejemplo n.º 10
0
    def unset_key(self, name, key):
        """Delete the specified extra spec for instance type."""
        try:
            try:
                inst_type = flavors.get_instance_type_by_name(name)
            except exception.InstanceTypeNotFoundByName as e:
                print e
                return 2

            ctxt = context.get_admin_context()
            db.instance_type_extra_specs_delete(ctxt, inst_type["flavorid"], key)

            print _("Key %(key)s on instance type %(name)s unset") % locals()
        except db_exc.DBError as e:
            _db_error(e)
Ejemplo n.º 11
0
    def _create_fake_instance(self, params=None, type_name='m1.tiny'):
        """Create a test instance."""
        if not params:
            params = {}

        inst = {}
        inst['vm_state'] = vm_states.ACTIVE
        inst['image_ref'] = 1
        inst['reservation_id'] = 'r-fakeres'
        inst['user_id'] = 'fake'
        inst['project_id'] = 'fake'
        type_id = flavors.get_instance_type_by_name(type_name)['id']
        inst['instance_type_id'] = type_id
        inst['ami_launch_index'] = 0
        inst.update(params)
        return db.instance_create(self.context, inst)
Ejemplo n.º 12
0
 def _create_instance(self, params={}):
     """Create a test instance."""
     instance_type = flavors.get_instance_type_by_name('m1.tiny')
     sys_meta = flavors.save_instance_type_info({}, instance_type)
     inst = {}
     inst['image_ref'] = 1
     inst['reservation_id'] = 'r-fakeres'
     inst['user_id'] = self.user_id
     inst['project_id'] = self.project_id
     inst['instance_type_id'] = instance_type['id']
     inst['system_metadata'] = sys_meta
     inst['ami_launch_index'] = 0
     inst['root_gb'] = 0
     inst['ephemeral_gb'] = 0
     inst.update(params)
     return db.instance_create(self.context, inst)['id']
Ejemplo n.º 13
0
    def _create_fake_instance(self, params=None, type_name='m1.tiny'):
        """Create a test instance."""
        if not params:
            params = {}

        inst = {}
        inst['vm_state'] = vm_states.ACTIVE
        inst['image_ref'] = 1
        inst['reservation_id'] = 'r-fakeres'
        inst['user_id'] = 'fake'
        inst['project_id'] = 'fake'
        type_id = flavors.get_instance_type_by_name(type_name)['id']
        inst['instance_type_id'] = type_id
        inst['ami_launch_index'] = 0
        inst.update(params)
        return db.instance_create(self.context, inst)
Ejemplo n.º 14
0
 def _create_instance(self, params={}):
     """Create a test instance."""
     instance_type = flavors.get_instance_type_by_name('m1.tiny')
     sys_meta = flavors.save_instance_type_info({}, instance_type)
     inst = {}
     inst['image_ref'] = 1
     inst['reservation_id'] = 'r-fakeres'
     inst['user_id'] = self.user_id
     inst['project_id'] = self.project_id
     inst['instance_type_id'] = instance_type['id']
     inst['system_metadata'] = sys_meta
     inst['ami_launch_index'] = 0
     inst['root_gb'] = 0
     inst['ephemeral_gb'] = 0
     inst.update(params)
     return db.instance_create(self.context, inst)['id']
Ejemplo n.º 15
0
 def _wrapped_create(self, params=None):
     instance_type = flavors.get_instance_type_by_name('m1.tiny')
     sys_meta = flavors.save_instance_type_info({}, instance_type)
     inst = {}
     inst['image_ref'] = 1
     inst['user_id'] = self.user_id
     inst['project_id'] = self.project_id
     inst['instance_type_id'] = instance_type['id']
     inst['root_gb'] = 0
     inst['ephemeral_gb'] = 0
     inst['access_ip_v4'] = '1.2.3.4'
     inst['access_ip_v6'] = 'feed:5eed'
     inst['display_name'] = 'test_instance'
     inst['hostname'] = 'test_instance_hostname'
     inst['system_metadata'] = sys_meta
     if params:
         inst.update(params)
     return db.instance_create(self.context, inst)
Ejemplo n.º 16
0
 def _wrapped_create(self, params=None):
     instance_type = flavors.get_instance_type_by_name('m1.tiny')
     sys_meta = flavors.save_instance_type_info({}, instance_type)
     inst = {}
     inst['image_ref'] = 1
     inst['user_id'] = self.user_id
     inst['project_id'] = self.project_id
     inst['instance_type_id'] = instance_type['id']
     inst['root_gb'] = 0
     inst['ephemeral_gb'] = 0
     inst['access_ip_v4'] = '1.2.3.4'
     inst['access_ip_v6'] = 'feed:5eed'
     inst['display_name'] = 'test_instance'
     inst['hostname'] = 'test_instance_hostname'
     inst['system_metadata'] = sys_meta
     if params:
         inst.update(params)
     return db.instance_create(self.context, inst)
Ejemplo n.º 17
0
 def test_notify_about_instance_usage(self):
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     # Set some system metadata
     sys_metadata = {
         'image_md_key1': 'val1',
         'image_md_key2': 'val2',
         'other_data': 'meow'
     }
     extra_usage_info = {'image_name': 'fake_name'}
     db.instance_system_metadata_update(self.context, instance['uuid'],
                                        sys_metadata, False)
     # NOTE(russellb) Make sure our instance has the latest system_metadata
     # in it.
     instance = db.instance_get(self.context, instance_id)
     compute_utils.notify_about_instance_usage(
         self.context,
         instance,
         'create.start',
         extra_usage_info=extra_usage_info)
     self.assertEquals(len(test_notifier.NOTIFICATIONS), 1)
     msg = test_notifier.NOTIFICATIONS[0]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.create.start')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at', 'state',
                  'state_description', 'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'], {
         'md_key1': 'val1',
         'md_key2': 'val2'
     })
     self.assertEquals(payload['image_name'], 'fake_name')
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
     self.compute.terminate_instance(self.context, instance)
Ejemplo n.º 18
0
 def test_notify_usage_exists_deleted_instance(self):
     # Ensure 'exists' notification generates appropriate usage data.
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     # Set some system metadata
     sys_metadata = {
         'image_md_key1': 'val1',
         'image_md_key2': 'val2',
         'other_data': 'meow'
     }
     db.instance_system_metadata_update(self.context, instance['uuid'],
                                        sys_metadata, False)
     self.compute.terminate_instance(self.context, instance)
     instance = db.instance_get(self.context.elevated(read_deleted='yes'),
                                instance_id)
     compute_utils.notify_usage_exists(self.context, instance)
     msg = test_notifier.NOTIFICATIONS[-1]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.exists')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at', 'state',
                  'state_description', 'bandwidth',
                  'audit_period_beginning', 'audit_period_ending',
                  'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'], {
         'md_key1': 'val1',
         'md_key2': 'val2'
     })
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
Ejemplo n.º 19
0
 def test_notify_about_instance_usage(self):
     instance_id = self._create_instance()
     instance = db.instance_get(self.context, instance_id)
     # Set some system metadata
     sys_metadata = {'image_md_key1': 'val1',
                     'image_md_key2': 'val2',
                     'other_data': 'meow'}
     extra_usage_info = {'image_name': 'fake_name'}
     db.instance_system_metadata_update(self.context, instance['uuid'],
             sys_metadata, False)
     # NOTE(russellb) Make sure our instance has the latest system_metadata
     # in it.
     instance = db.instance_get(self.context, instance_id)
     compute_utils.notify_about_instance_usage(self.context, instance,
     'create.start', extra_usage_info=extra_usage_info)
     self.assertEquals(len(test_notifier.NOTIFICATIONS), 1)
     msg = test_notifier.NOTIFICATIONS[0]
     self.assertEquals(msg['priority'], 'INFO')
     self.assertEquals(msg['event_type'], 'compute.instance.create.start')
     payload = msg['payload']
     self.assertEquals(payload['tenant_id'], self.project_id)
     self.assertEquals(payload['user_id'], self.user_id)
     self.assertEquals(payload['instance_id'], instance['uuid'])
     self.assertEquals(payload['instance_type'], 'm1.tiny')
     type_id = flavors.get_instance_type_by_name('m1.tiny')['id']
     self.assertEquals(str(payload['instance_type_id']), str(type_id))
     for attr in ('display_name', 'created_at', 'launched_at',
                  'state', 'state_description', 'image_meta'):
         self.assertTrue(attr in payload,
                         msg="Key %s not in payload" % attr)
     self.assertEquals(payload['image_meta'],
             {'md_key1': 'val1', 'md_key2': 'val2'})
     self.assertEquals(payload['image_name'], 'fake_name')
     image_ref_url = "%s/images/1" % glance.generate_glance_url()
     self.assertEquals(payload['image_ref_url'], image_ref_url)
     self.compute.terminate_instance(self.context, instance)
Ejemplo n.º 20
0
 def test_instance_type_get_by_None_name_returns_default(self):
     # Ensure get by name returns default flavor with no name.
     default = flavors.get_default_instance_type()
     actual = flavors.get_instance_type_by_name(None)
     self.assertEqual(default, actual)
Ejemplo n.º 21
0
 def test_instance_type_get_by_None_name_returns_default(self):
     # Ensure get by name returns default flavor with no name.
     default = flavors.get_default_instance_type()
     actual = flavors.get_instance_type_by_name(None)
     self.assertEqual(default, actual)