Beispiel #1
0
    def test_trusted_filter_trusted_and_locale_formated_vtime_passes(
            self, req_mock):
        oat_data = {
            "hosts": [
                {
                    "host_name": "host1",
                    "trust_lvl": "trusted",
                    "vtime": timeutils.strtime(fmt="%c")
                },
                {
                    "host_name": "host2",
                    "trust_lvl": "trusted",
                    "vtime": timeutils.strtime(fmt="%D")
                },
                # This is just a broken date to ensure that
                # we're not just arbitrarily accepting any
                # date format.
            ]
        }
        req_mock.return_value = requests.codes.OK, oat_data
        extra_specs = {'trust:trusted_host': 'trusted'}
        filter_properties = {
            'context': mock.sentinel.ctx,
            'instance_type': {
                'memory_mb': 1024,
                'extra_specs': extra_specs
            }
        }
        host = fakes.FakeHostState('host1', 'host1', {})
        bad_host = fakes.FakeHostState('host2', 'host2', {})

        self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
        self.assertFalse(self.filt_cls.host_passes(bad_host,
                                                   filter_properties))
Beispiel #2
0
    def show(self, req, id):
        """Retrieve tenant_usage for a specified tenant."""
        tenant_id = id
        context = req.environ['nova.context']

        authorize_show(context, {'project_id': tenant_id})

        try:
            (period_start, period_stop, ignore) = self._get_datetime_range(
                req)
        except exception.InvalidStrTime as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())

        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usage = self._tenant_usages_for_period(context,
                                               period_start,
                                               period_stop,
                                               tenant_id=tenant_id,
                                               detailed=True)
        if len(usage):
            usage = usage[0]
        else:
            usage = {}
        return {'tenant_usage': usage}
    def test_shelved_poll_timedout(self):
        active_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, active_instance, {}, {}, [],
                None, None, True, None, False)

        instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance, {}, {}, [], None,
                None, True, None, False)
        sys_meta = utils.metadata_to_dict(instance['system_metadata'])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time + 1)
        sys_meta['shelved_at'] = timeutils.strtime(at=shelved_time)
        (old, instance) = db.instance_update_and_get_original(self.context,
                instance['uuid'], {'vm_state': vm_states.SHELVED,
                                   'system_metadata': sys_meta})

        def fake_destroy(inst, nw_info, bdm):
            # NOTE(alaski) There are too many differences between an instance
            # as returned by instance_update_and_get_original and
            # instance_get_all_by_filters so just compare the uuid.
            self.assertEqual(instance['uuid'], inst['uuid'])

        self.stubs.Set(self.compute.driver, 'destroy', fake_destroy)
        self.compute._poll_shelved_instances(self.context)
Beispiel #4
0
    def test_shelved_poll_timedout(self):
        active_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, active_instance, {}, {}, [],
                                  None, None, True, None, False)

        instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance, {}, {}, [], None,
                                  None, True, None, False)
        sys_meta = utils.metadata_to_dict(instance['system_metadata'])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time + 1)
        sys_meta['shelved_at'] = timeutils.strtime(at=shelved_time)
        (old, instance) = db.instance_update_and_get_original(
            self.context, instance['uuid'], {
                'vm_state': vm_states.SHELVED,
                'system_metadata': sys_meta
            })

        def fake_destroy(inst, nw_info, bdm):
            # NOTE(alaski) There are too many differences between an instance
            # as returned by instance_update_and_get_original and
            # instance_get_all_by_filters so just compare the uuid.
            self.assertEqual(instance['uuid'], inst['uuid'])

        self.stubs.Set(self.compute.driver, 'destroy', fake_destroy)
        self.compute._poll_shelved_instances(self.context)
Beispiel #5
0
    def test_validate_ec2_timestamp_advanced_time(self):

        # EC2 request with Timestamp in advanced time
        timestamp = timeutils.utcnow() + datetime.timedelta(seconds=250)
        params = {"Timestamp": timeutils.strtime(timestamp, "%Y-%m-%dT%H:%M:%SZ")}
        expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
        self.assertFalse(expired)
 def test_validate_ec2_timestamp_advanced_time_expired(self):
     timestamp = timeutils.utcnow() + datetime.timedelta(seconds=350)
     params = {
         'Timestamp': timeutils.strtime(timestamp, "%Y-%m-%dT%H:%M:%SZ")
     }
     expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
     self.assertTrue(expired)
Beispiel #7
0
    def test_unshelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = None
        sys_meta['shelved_host'] = host

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.start')
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'task_state': task_states.SPAWNING},
                update_cells=False,
                columns_to_join=['metadata', 'system_metadata']
                ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance,
                []).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.driver.spawn(self.context, instance, None,
                injected_files=[], admin_password=None,
                network_info=[],
                block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.ACTIVE,
                 'task_state': None,
                 'key_data': None,
                 'auto_disk_config': False,
                 'expected_task_state': task_states.SPAWNING,
                 'launched_at': cur_time_tz},
                 update_cells=False,
                 columns_to_join=['metadata', 'system_metadata']
                 ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.end')
        self.mox.ReplayAll()

        self.compute.unshelve_instance(self.context, instance, image=None)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
Beispiel #8
0
    def test_unshelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = None
        sys_meta['shelved_host'] = host

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.start')
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'task_state': task_states.SPAWNING},
                update_cells=False,
                columns_to_join=['metadata', 'system_metadata']
                ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance,
                []).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.driver.spawn(self.context, instance, None,
                injected_files=[], admin_password=None,
                network_info=[],
                block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.ACTIVE,
                 'task_state': None,
                 'key_data': None,
                 'auto_disk_config': False,
                 'expected_task_state': task_states.SPAWNING,
                 'launched_at': cur_time_tz},
                 update_cells=False,
                 columns_to_join=['metadata', 'system_metadata']
                 ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.end')
        self.mox.ReplayAll()

        self.compute.unshelve_instance(self.context, instance, image=None)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
Beispiel #9
0
    def test_validate_ec2_timestamp_advanced_time(self):

        #EC2 request with Timestamp in advanced time
        timestamp = timeutils.utcnow() + datetime.timedelta(seconds=250)
        params = {'Timestamp': timeutils.strtime(timestamp,
                                           "%Y-%m-%dT%H:%M:%SZ")}
        expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
        self.assertFalse(expired)
Beispiel #10
0
    def test_shelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context,
            db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.SHELVING
        instance.save()
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = None
        sys_meta['shelved_host'] = host
        db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute.driver, 'power_off')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'shelve_offload.start')
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'],
            {
                'power_state':
                123,
                'host':
                None,
                'node':
                None,
                'vm_state':
                vm_states.SHELVED_OFFLOADED,
                'task_state':
                None,
                'expected_task_state':
                [task_states.SHELVING, task_states.SHELVING_OFFLOADING]
            },
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata'],
        ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'shelve_offload.end')
        self.mox.ReplayAll()

        self.compute.shelve_offload_instance(self.context, instance)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
Beispiel #11
0
 def fake_get_timestamp(ds_browser, ds_path):
     self.assertEqual('fake-ds-browser', ds_browser)
     self.assertEqual('fake-ds-path', ds_path)
     if not self.exists:
         return
     ts = '%s%s' % (imagecache.TIMESTAMP_PREFIX,
                    timeutils.strtime(at=self._time,
                                      fmt=imagecache.TIMESTAMP_FORMAT))
     return ts
Beispiel #12
0
 def fake_get_timestamp(ds_browser, ds_path):
     self.assertEqual('fake-ds-browser', ds_browser)
     self.assertEqual('fake-ds-path', ds_path)
     if not self.exists:
         return
     ts = '%s%s' % (imagecache.TIMESTAMP_PREFIX,
             timeutils.strtime(at=self._time,
                               fmt=imagecache.TIMESTAMP_FORMAT))
     return ts
Beispiel #13
0
 def fake_get_timestamp(ds_browser, ds_path):
     self.assertEqual("fake-ds-browser", ds_browser)
     self.assertEqual("[fake-ds] fake-path", str(ds_path))
     if not self.exists:
         return
     ts = "%s%s" % (
         imagecache.TIMESTAMP_PREFIX,
         timeutils.strtime(at=self._time, fmt=imagecache.TIMESTAMP_FORMAT),
     )
     return ts
Beispiel #14
0
    def test_shelve(self):
        CONF.shelved_offload_time = -1
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        image_id = 'fake_image_id'
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        instance.task_state = task_states.SHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image_id
        sys_meta['shelved_host'] = host
        db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute.driver, 'snapshot')
        self.mox.StubOutWithMock(self.compute.driver, 'power_off')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                'shelve.start')
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context,
                instance).AndReturn(123)
        self.compute.driver.snapshot(self.context, instance, 'fake_image_id',
                mox.IgnoreArg())

        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.SHELVED,
                 'task_state': None,
                 'expected_task_state': [task_states.SHELVING,
                    task_states.SHELVING_IMAGE_UPLOADING],
                 'system_metadata': sys_meta},
                 update_cells=False,
                 columns_to_join=['metadata', 'system_metadata'],
                ).AndReturn((db_instance,
                                                db_instance))
        self.compute._notify_about_instance_usage(self.context,
                                                  instance, 'shelve.end')
        self.mox.ReplayAll()

        self.compute.shelve_instance(self.context, instance,
                image_id=image_id)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
    def test_shelve(self):
        CONF.shelved_offload_time = -1
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        image_id = 'fake_image_id'
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        instance.task_state = task_states.SHELVING
        instance.save()
        sys_meta = instance.system_metadata
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image_id
        sys_meta['shelved_host'] = host
        db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute.driver, 'snapshot')
        self.mox.StubOutWithMock(self.compute.driver, 'power_off')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                'shelve.start')
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context,
                instance).AndReturn(123)
        self.compute.driver.snapshot(self.context, instance, 'fake_image_id',
                mox.IgnoreArg())

        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.SHELVED,
                 'task_state': None,
                 'expected_task_state': [task_states.SHELVING,
                    task_states.SHELVING_IMAGE_UPLOADING],
                 'system_metadata': sys_meta}).AndReturn((db_instance,
                                                          db_instance))
        self.compute._notify_about_instance_usage(self.context,
                                                  instance, 'shelve.end')
        self.mox.ReplayAll()

        self.compute.shelve_instance(self.context, instance,
                image_id=image_id)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
Beispiel #16
0
    def test_shelved_poll_not_timedout(self):
        instance = jsonutils.to_primitive(self._create_fake_instance())
        sys_meta = utils.metadata_to_dict(instance['system_metadata'])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time - 1)
        sys_meta['shelved_at'] = timeutils.strtime(at=shelved_time)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'vm_state': vm_states.SHELVED, 'system_metadata': sys_meta})

        self.mox.StubOutWithMock(self.compute.driver, 'destroy')
        self.mox.ReplayAll()
        self.compute._poll_shelved_instances(self.context)
Beispiel #17
0
 def to_dict(self):
     return {'user_id': self.user_id,
             'project_id': self.project_id,
             'is_admin': self.is_admin,
             'read_deleted': self.read_deleted,
             'roles': self.roles,
             'remote_address': self.remote_address,
             'timestamp': timeutils.strtime(self.timestamp),
             'request_id': self.request_id,
             'auth_token': self.auth_token,
             'quota_class': self.quota_class,
             'user_name': self.user_name,
             'project_name': self.project_name}
Beispiel #18
0
    def _create_backup(self, context, instance, backups_needed, ts):
        backup_ts = timeutils.strtime(at=ts)
        backup_name = "%s-backup-%s" % (instance["display_name"], backup_ts)
        metadata = {
            meta.BACKUP_AT_KEY: backup_ts,
            meta.BACKUP_FOR_KEY: instance["uuid"],
            meta.BACKUP_SATISFIES_KEY: jsonutils.dumps(backups_needed),
        }

        try:
            backup = self.driver.create_snapshot(context, instance, name=backup_name, metadata=metadata)
            LOG.info(_("Created backup with uuid %s" % backup["uuid"]))
        except:
            LOG.exception(_("Couldn't create backup for instance %s, will retry" % instance["uuid"]))
Beispiel #19
0
    def test_trusted_filter_trusted_and_locale_formated_vtime_passes(self,
            req_mock):
        oat_data = {"hosts": [{"host_name": "host1",
                                    "trust_lvl": "trusted",
                                    "vtime": timeutils.strtime(fmt="%c")},
                                   {"host_name": "host2",
                                    "trust_lvl": "trusted",
                                    "vtime": timeutils.strtime(fmt="%D")},
                                    # This is just a broken date to ensure that
                                    # we're not just arbitrarily accepting any
                                    # date format.
                        ]}
        req_mock.return_value = requests.codes.OK, oat_data
        extra_specs = {'trust:trusted_host': 'trusted'}
        filter_properties = {'context': mock.sentinel.ctx,
                             'instance_type': {'memory_mb': 1024,
                                               'extra_specs': extra_specs}}
        host = fakes.FakeHostState('host1', 'host1', {})
        bad_host = fakes.FakeHostState('host2', 'host2', {})

        self.assertTrue(self.filt_cls.host_passes(host, filter_properties))
        self.assertFalse(self.filt_cls.host_passes(bad_host,
                                                   filter_properties))
    def test_shelved_poll_not_timedout(self):
        instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=instance)
        sys_meta = utils.metadata_to_dict(instance['system_metadata'])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time - 1)
        sys_meta['shelved_at'] = timeutils.strtime(at=shelved_time)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'vm_state': vm_states.SHELVED, 'system_metadata': sys_meta})

        self.mox.StubOutWithMock(self.compute.driver, 'destroy')
        self.mox.ReplayAll()
        self.compute._poll_shelved_instances(self.context)
Beispiel #21
0
    def index(self, req):
        """Retrieve tenant_usage for all tenants."""
        context = req.environ['nova.context']

        authorize_list(context)

        (period_start, period_stop, detailed) = self._get_datetime_range(req)
        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usages = self._tenant_usages_for_period(context,
                                                period_start,
                                                period_stop,
                                                detailed=detailed)
        return {'tenant_usages': usages}
Beispiel #22
0
 def to_dict(self):
     return {'user_id': self.user_id,
             'project_id': self.project_id,
             'is_admin': self.is_admin,
             'read_deleted': self.read_deleted,
             'roles': self.roles,
             'remote_address': self.remote_address,
             'timestamp': timeutils.strtime(self.timestamp),
             'request_id': self.request_id,
             'auth_token': self.auth_token,
             'quota_class': self.quota_class,
             'user_name': self.user_name,
             'service_catalog': self.service_catalog,
             'project_name': self.project_name,
             'instance_lock_checked': self.instance_lock_checked}
    def index(self, req):
        """Retrieve tenant_usage for all tenants."""
        context = req.environ['nova.context']

        authorize_list(context)

        (period_start, period_stop, detailed) = self._get_datetime_range(req)
        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usages = self._tenant_usages_for_period(context,
                                                period_start,
                                                period_stop,
                                                detailed=detailed)
        return {'tenant_usages': usages}
Beispiel #24
0
    def test_shelved_poll_not_timedout(self):
        instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance, {}, {}, [], None, None, True, None, False)
        sys_meta = utils.metadata_to_dict(instance["system_metadata"])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time - 1)
        sys_meta["shelved_at"] = timeutils.strtime(at=shelved_time)
        db.instance_update_and_get_original(
            self.context, instance["uuid"], {"vm_state": vm_states.SHELVED, "system_metadata": sys_meta}
        )

        self.mox.StubOutWithMock(self.compute.driver, "destroy")
        self.mox.ReplayAll()
        self.compute._poll_shelved_instances(self.context)
Beispiel #25
0
 def to_dict(self):
     return {'user_id': self.user_id,
             'project_id': self.project_id,
             'is_admin': self.is_admin,
             'read_deleted': self.read_deleted,
             'roles': self.roles,
             'remote_address': self.remote_address,
             'timestamp': timeutils.strtime(self.timestamp),
             'request_id': self.request_id,
             'auth_token': self.auth_token,
             'user_name': self.user_name,
             'service_catalog': self.service_catalog,
             'project_name': self.project_name,
             'instance_lock_checked': self.instance_lock_checked,
             'tenant': self.tenant,
             'user': self.user}
Beispiel #26
0
    def test_shelve(self):
        CONF.shelved_offload_time = -1
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, db_instance, {}, {}, [], None, None, True, None, False)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance["uuid"], expected_attrs=["metadata", "system_metadata"]
        )
        image_id = "fake_image_id"
        host = "fake-mini"
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        instance.task_state = task_states.SHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta["shelved_at"] = timeutils.strtime(at=cur_time)
        sys_meta["shelved_image_id"] = image_id
        sys_meta["shelved_host"] = host
        db_instance["system_metadata"] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, "_notify_about_instance_usage")
        self.mox.StubOutWithMock(self.compute.driver, "snapshot")
        self.mox.StubOutWithMock(self.compute.driver, "power_off")
        self.mox.StubOutWithMock(self.compute, "_get_power_state")
        self.mox.StubOutWithMock(db, "instance_update_and_get_original")

        self.compute._notify_about_instance_usage(self.context, instance, "shelve.start")
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        self.compute.driver.snapshot(self.context, instance, "fake_image_id", mox.IgnoreArg())

        db.instance_update_and_get_original(
            self.context,
            instance["uuid"],
            {
                "power_state": 123,
                "vm_state": vm_states.SHELVED,
                "task_state": None,
                "expected_task_state": [task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING],
                "system_metadata": sys_meta,
            },
            update_cells=False,
            columns_to_join=["metadata", "system_metadata"],
        ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance, "shelve.end")
        self.mox.ReplayAll()

        self.compute.shelve_instance(self.context, instance, image_id=image_id)
    def test_shelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.SHELVING
        instance.save()
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        sys_meta = instance.system_metadata
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = None
        sys_meta['shelved_host'] = host
        db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute.driver, 'power_off')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                'shelve_offload.start')
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context,
                instance).AndReturn(123)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123, 'host': None, 'node': None,
                 'vm_state': vm_states.SHELVED_OFFLOADED,
                 'task_state': None,
                 'expected_task_state': [task_states.SHELVING,
                    task_states.SHELVING_OFFLOADING]}).AndReturn(
                            (db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                'shelve_offload.end')
        self.mox.ReplayAll()

        self.compute.shelve_offload_instance(self.context, instance)

        self.mox.VerifyAll()
        self.mox.UnsetStubs()

        self.compute.terminate_instance(self.context, instance=instance)
Beispiel #28
0
 def to_dict(self):
     return {
         "user_id": self.user_id,
         "project_id": self.project_id,
         "is_admin": self.is_admin,
         "read_deleted": self.read_deleted,
         "roles": self.roles,
         "remote_address": self.remote_address,
         "timestamp": timeutils.strtime(self.timestamp),
         "request_id": self.request_id,
         "auth_token": self.auth_token,
         "quota_class": self.quota_class,
         "user_name": self.user_name,
         "service_catalog": self.service_catalog,
         "project_name": self.project_name,
         "instance_lock_checked": self.instance_lock_checked,
         "tenant": self.tenant,
         "user": self.user,
     }
Beispiel #29
0
    def test_shelved_poll_timedout(self):
        instance = jsonutils.to_primitive(self._create_fake_instance())
        sys_meta = utils.metadata_to_dict(instance["system_metadata"])
        shelved_time = timeutils.utcnow()
        timeutils.set_time_override(shelved_time)
        timeutils.advance_time_seconds(CONF.shelved_offload_time + 1)
        sys_meta["shelved_at"] = timeutils.strtime(at=shelved_time)
        (old, instance) = db.instance_update_and_get_original(
            self.context, instance["uuid"], {"vm_state": vm_states.SHELVED, "system_metadata": sys_meta}
        )

        def fake_destroy(inst, nw_info, bdm):
            # NOTE(alaski) There are too many differences between an instance
            # as returned by instance_update_and_get_original and
            # instance_get_all_by_filters so just compare the uuid.
            self.assertEqual(instance["uuid"], inst["uuid"])

        self.stubs.Set(self.compute.driver, "destroy", fake_destroy)
        self.compute._poll_shelved_instances(self.context)
Beispiel #30
0
    def test_shelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        instance = objects.Instance.get_by_uuid(
            self.context, db_instance["uuid"], expected_attrs=["metadata", "system_metadata"]
        )
        instance.task_state = task_states.SHELVING
        instance.save()
        host = "fake-mini"
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta["shelved_at"] = timeutils.strtime(at=cur_time)
        sys_meta["shelved_image_id"] = None
        sys_meta["shelved_host"] = host
        db_instance["system_metadata"] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, "_notify_about_instance_usage")
        self.mox.StubOutWithMock(self.compute.driver, "power_off")
        self.mox.StubOutWithMock(self.compute, "_get_power_state")
        self.mox.StubOutWithMock(db, "instance_update_and_get_original")

        self.compute._notify_about_instance_usage(self.context, instance, "shelve_offload.start")
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(
            self.context,
            instance["uuid"],
            {
                "power_state": 123,
                "host": None,
                "node": None,
                "vm_state": vm_states.SHELVED_OFFLOADED,
                "task_state": None,
                "expected_task_state": [task_states.SHELVING, task_states.SHELVING_OFFLOADING],
            },
            update_cells=False,
            columns_to_join=["metadata", "system_metadata"],
        ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance, "shelve_offload.end")
        self.mox.ReplayAll()

        self.compute.shelve_offload_instance(self.context, instance)
Beispiel #31
0
    def index(self, req):
        """Retrieve tenant_usage for all tenants."""
        context = req.environ['nova.context']

        authorize_list(context)

        try:
            (period_start, period_stop, detailed) = self._get_datetime_range(
                req)
        except exception.InvalidStrTime as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())

        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usages = self._tenant_usages_for_period(context,
                                                period_start,
                                                period_stop,
                                                detailed=detailed)
        return {'tenant_usages': usages}
Beispiel #32
0
    def show(self, req, id):
        """Retrieve tenant_usage for a specified tenant."""
        tenant_id = id
        context = req.environ['nova.context']

        authorize_show(context, {'project_id': tenant_id})

        (period_start, period_stop, ignore) = self._get_datetime_range(req)
        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usage = self._tenant_usages_for_period(context,
                                               period_start,
                                               period_stop,
                                               tenant_id=tenant_id,
                                               detailed=True)
        if len(usage):
            usage = usage[0]
        else:
            usage = {}
        return {'tenant_usage': usage}
    def show(self, req, id):
        """Retrieve tenant_usage for a specified tenant."""
        tenant_id = id
        context = req.environ['nova.context']

        authorize_show(context, {'project_id': tenant_id})

        (period_start, period_stop, ignore) = self._get_datetime_range(req)
        now = timeutils.parse_isotime(timeutils.strtime())
        if period_stop > now:
            period_stop = now
        usage = self._tenant_usages_for_period(context,
                                               period_start,
                                               period_stop,
                                               tenant_id=tenant_id,
                                               detailed=True)
        if len(usage):
            usage = usage[0]
        else:
            usage = {}
        return {'tenant_usage': usage}
Beispiel #34
0
 def null_safe_isotime(s):
     if isinstance(s, datetime.datetime):
         return timeutils.strtime(s)
     else:
         return str(s) if s else ''
    def test_unshelve(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        instance = objects.Instance.get_by_uuid(
            self.context,
            db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        image = {'id': 'fake_id'}
        host = 'fake-mini'
        node = test_compute.NODENAME
        limits = {}
        filter_properties = {'limits': limits}
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image['id']
        sys_meta['shelved_host'] = host

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(self.rt, 'instance_claim')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')
        self.mox.StubOutWithMock(self.compute.network_api,
                                 'migrate_instance_finish')

        self.deleted_image_id = None

        def fake_delete(self2, ctxt, image_id):
            self.deleted_image_id = image_id

        def fake_claim(context, instance, limits):
            instance.host = self.compute.host
            return claims.Claim(db_instance, self.rt, _fake_resources())

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'delete', fake_delete)

        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'unshelve.start')
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'],
            {
                'task_state': task_states.SPAWNING
            },
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata'],
        ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(
            self.context, instance, mox.IgnoreArg(),
            do_check_attach=False).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.network_api.migrate_instance_finish(
            self.context, instance, {
                'source_compute': '',
                'dest_compute': self.compute.host
            })
        self.compute.driver.spawn(self.context,
                                  instance,
                                  image,
                                  injected_files=[],
                                  admin_password=None,
                                  network_info=[],
                                  block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'],
            {
                'power_state': 123,
                'vm_state': vm_states.ACTIVE,
                'task_state': None,
                'image_ref': instance['image_ref'],
                'key_data': None,
                'host': self.compute.host,  # rt.instance_claim set this
                'auto_disk_config': False,
                'expected_task_state': task_states.SPAWNING,
                'launched_at': cur_time_tz
            },
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata']).AndReturn(
                (db_instance, dict(db_instance, host=self.compute.host)))
        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'unshelve.end')
        self.mox.ReplayAll()

        with mock.patch.object(self.rt,
                               'instance_claim',
                               side_effect=fake_claim):
            self.compute.unshelve_instance(self.context,
                                           instance,
                                           image=image,
                                           filter_properties=filter_properties,
                                           node=node)
        self.assertEqual(image['id'], self.deleted_image_id)
        self.assertEqual(instance.host, self.compute.host)
Beispiel #36
0
def to_primitive(value, convert_instances=False, level=0):
    """Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, convert_instances=True is lossy ... be aware.

    """
    nasty = [inspect.ismodule, inspect.isclass, inspect.ismethod,
             inspect.isfunction, inspect.isgeneratorfunction,
             inspect.isgenerator, inspect.istraceback, inspect.isframe,
             inspect.iscode, inspect.isbuiltin, inspect.isroutine,
             inspect.isabstract]
    for test in nasty:
        if test(value):
            return unicode(value)

    # value of itertools.count doesn't get caught by inspects
    # above and results in infinite loop when list(value) is called.
    if type(value) == itertools.count:
        return unicode(value)

    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
    #              tests that raise an exception in a mocked method that
    #              has a @wrap_exception with a notifier will fail. If
    #              we up the dependency to 0.5.4 (when it is released) we
    #              can remove this workaround.
    if getattr(value, '__module__', None) == 'mox':
        return 'mock'

    if level > 3:
        return '?'

    # The try block may not be necessary after the class check above,
    # but just in case ...
    try:
        # It's not clear why xmlrpclib created their own DateTime type, but
        # for our purposes, make it a datetime type which is explicitly
        # handled
        if isinstance(value, xmlrpclib.DateTime):
            value = datetime.datetime(*tuple(value.timetuple())[:6])

        if isinstance(value, (list, tuple)):
            o = []
            for v in value:
                o.append(to_primitive(v, convert_instances=convert_instances,
                                      level=level))
            return o
        elif isinstance(value, dict):
            o = {}
            for k, v in value.iteritems():
                o[k] = to_primitive(v, convert_instances=convert_instances,
                                    level=level)
            return o
        elif isinstance(value, datetime.datetime):
            return timeutils.strtime(value)
        elif hasattr(value, 'iteritems'):
            return to_primitive(dict(value.iteritems()),
                                convert_instances=convert_instances,
                                level=level + 1)
        elif hasattr(value, '__iter__'):
            return to_primitive(list(value),
                                convert_instances=convert_instances,
                                level=level)
        elif convert_instances and hasattr(value, '__dict__'):
            # Likely an instance of something. Watch for cycles.
            # Ignore class member vars.
            return to_primitive(value.__dict__,
                                convert_instances=convert_instances,
                                level=level + 1)
        else:
            return value
    except TypeError, e:
        # Class objects are tricky since they may define something like
        # __iter__ defined but it isn't callable as list().
        return unicode(value)
Beispiel #37
0
def to_primitive(value, convert_instances=False, convert_datetime=True,
                 level=0, max_depth=3):
    """Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, convert_instances=True is lossy ... be aware.

    """
    # handle obvious types first - order of basic types determined by running
    # full tests on nova project, resulting in the following counts:
    # 572754 <type 'NoneType'>
    # 460353 <type 'int'>
    # 379632 <type 'unicode'>
    # 274610 <type 'str'>
    # 199918 <type 'dict'>
    # 114200 <type 'datetime.datetime'>
    #  51817 <type 'bool'>
    #  26164 <type 'list'>
    #   6491 <type 'float'>
    #    283 <type 'tuple'>
    #     19 <type 'long'>
    if isinstance(value, _simple_types):
        return value

    if isinstance(value, datetime.datetime):
        if convert_datetime:
            return timeutils.strtime(value)
        else:
            return value

    # value of itertools.count doesn't get caught by nasty_type_tests
    # and results in infinite loop when list(value) is called.
    if type(value) == itertools.count:
        return six.text_type(value)

    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
    #              tests that raise an exception in a mocked method that
    #              has a @wrap_exception with a notifier will fail. If
    #              we up the dependency to 0.5.4 (when it is released) we
    #              can remove this workaround.
    if getattr(value, '__module__', None) == 'mox':
        return 'mock'

    if level > max_depth:
        return '?'

    # The try block may not be necessary after the class check above,
    # but just in case ...
    try:
        recursive = functools.partial(to_primitive,
                                      convert_instances=convert_instances,
                                      convert_datetime=convert_datetime,
                                      level=level,
                                      max_depth=max_depth)
        if isinstance(value, dict):
            return dict((k, recursive(v)) for k, v in six.iteritems(value))
        elif isinstance(value, (list, tuple)):
            return [recursive(lv) for lv in value]

        # It's not clear why xmlrpclib created their own DateTime type, but
        # for our purposes, make it a datetime type which is explicitly
        # handled
        if isinstance(value, xmlrpclib.DateTime):
            value = datetime.datetime(*tuple(value.timetuple())[:6])

        if convert_datetime and isinstance(value, datetime.datetime):
            return timeutils.strtime(value)
        elif isinstance(value, gettextutils.Message):
            return value.data
        elif hasattr(value, 'iteritems'):
            return recursive(dict(value.iteritems()), level=level + 1)
        elif hasattr(value, '__iter__'):
            return recursive(list(value))
        elif convert_instances and hasattr(value, '__dict__'):
            # Likely an instance of something. Watch for cycles.
            # Ignore class member vars.
            return recursive(value.__dict__, level=level + 1)
        elif netaddr and isinstance(value, netaddr.IPAddress):
            return six.text_type(value)
        else:
            if any(test(value) for test in _nasty_type_tests):
                return six.text_type(value)
            return value
    except TypeError:
        # Class objects are tricky since they may define something like
        # __iter__ defined but it isn't callable as list().
        return six.text_type(value)
Beispiel #38
0
    def test_unshelve(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        instance = objects.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        image = {'id': 'fake_id'}
        host = 'fake-mini'
        node = test_compute.NODENAME
        limits = {}
        filter_properties = {'limits': limits}
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image['id']
        sys_meta['shelved_host'] = host

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(self.rt, 'instance_claim')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')
        self.mox.StubOutWithMock(self.compute.network_api,
                                 'migrate_instance_finish')

        self.deleted_image_id = None

        def fake_delete(self2, ctxt, image_id):
            self.deleted_image_id = image_id

        def fake_claim(context, instance, limits):
            instance.host = self.compute.host
            return claims.Claim(db_instance, self.rt, _fake_resources())

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'delete', fake_delete)

        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.start')
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'task_state': task_states.SPAWNING},
                update_cells=False,
                columns_to_join=['metadata', 'system_metadata'],
                ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance,
                mox.IgnoreArg(), do_check_attach=False).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.network_api.migrate_instance_finish(
                self.context, instance, {'source_compute': '',
                                         'dest_compute': self.compute.host})
        self.compute.driver.spawn(self.context, instance, image,
                injected_files=[], admin_password=None,
                network_info=[],
                block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.ACTIVE,
                 'task_state': None,
                 'image_ref': instance['image_ref'],
                 'key_data': None,
                 'host': self.compute.host,  # rt.instance_claim set this
                 'auto_disk_config': False,
                 'expected_task_state': task_states.SPAWNING,
                 'launched_at': cur_time_tz},
                 update_cells=False,
                 columns_to_join=['metadata', 'system_metadata']
                 ).AndReturn((db_instance,
                              dict(db_instance,
                                   host=self.compute.host)))
        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.end')
        self.mox.ReplayAll()

        with mock.patch.object(self.rt, 'instance_claim',
                               side_effect=fake_claim):
            self.compute.unshelve_instance(self.context, instance, image=image,
                    filter_properties=filter_properties, node=node)
        self.assertEqual(image['id'], self.deleted_image_id)
        self.assertEqual(instance.host, self.compute.host)
Beispiel #39
0
    def test_unshelve_volume_backed(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        host = "fake-mini"
        node = test_compute.NODENAME
        limits = {}
        filter_properties = {"limits": limits}
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        self.compute.run_instance(self.context, db_instance, {}, {}, [], None, None, True, None, False)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance["uuid"], expected_attrs=["metadata", "system_metadata"]
        )
        instance.task_state = task_states.UNSHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta["shelved_at"] = timeutils.strtime(at=cur_time)
        sys_meta["shelved_image_id"] = None
        sys_meta["shelved_host"] = host

        self.mox.StubOutWithMock(self.compute, "_notify_about_instance_usage")
        self.mox.StubOutWithMock(self.compute, "_prep_block_device")
        self.mox.StubOutWithMock(self.compute.driver, "spawn")
        self.mox.StubOutWithMock(self.compute, "_get_power_state")
        self.mox.StubOutWithMock(self.rt, "instance_claim")
        self.mox.StubOutWithMock(db, "instance_update_and_get_original")

        self.compute._notify_about_instance_usage(self.context, instance, "unshelve.start")
        db.instance_update_and_get_original(
            self.context,
            instance["uuid"],
            {"task_state": task_states.SPAWNING},
            update_cells=False,
            columns_to_join=["metadata", "system_metadata"],
        ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance, mox.IgnoreArg()).AndReturn("fake_bdm")
        db_instance["key_data"] = None
        db_instance["auto_disk_config"] = None
        self.rt.instance_claim(self.context, instance, limits).AndReturn(
            claims.Claim(db_instance, self.rt, _fake_resources())
        )
        self.compute.driver.spawn(
            self.context,
            instance,
            None,
            injected_files=[],
            admin_password=None,
            network_info=[],
            block_device_info="fake_bdm",
        )
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(
            self.context,
            instance["uuid"],
            {
                "power_state": 123,
                "vm_state": vm_states.ACTIVE,
                "task_state": None,
                "key_data": None,
                "auto_disk_config": False,
                "expected_task_state": task_states.SPAWNING,
                "launched_at": cur_time_tz,
            },
            update_cells=False,
            columns_to_join=["metadata", "system_metadata"],
        ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance, "unshelve.end")
        self.mox.ReplayAll()

        self.compute.unshelve_instance(
            self.context, instance, image=None, filter_properties=filter_properties, node=node
        )
 def test_validate_ec2_timestamp_advanced_time_expired(self):
     timestamp = timeutils.utcnow() + datetime.timedelta(seconds=350)
     params = {'Timestamp': timeutils.strtime(timestamp,
                                        "%Y-%m-%dT%H:%M:%SZ")}
     expired = ec2utils.is_ec2_timestamp_expired(params, expires=300)
     self.assertTrue(expired)
Beispiel #41
0
    def test_unshelve(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, db_instance, {}, {}, [], None,
                                  None, True, None, False)
        instance = instance_obj.Instance.get_by_uuid(
            self.context,
            db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        image = {'id': 'fake_id'}
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image['id']
        sys_meta['shelved_host'] = host
        hypervisor_hostname = 'fake_hypervisor_hostname'
        fake_compute_info = {'hypervisor_hostname': hypervisor_hostname}

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(self.compute, '_get_compute_info')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.deleted_image_id = None

        def fake_delete(self2, ctxt, image_id):
            self.deleted_image_id = image_id

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'delete', fake_delete)

        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'unshelve.start')
        self.compute._get_compute_info(
            mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(fake_compute_info)
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'],
            {
                'task_state': task_states.SPAWNING,
                'host': host,
                'node': hypervisor_hostname
            },
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata'],
        ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance,
                                        []).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.driver.spawn(self.context,
                                  instance,
                                  image,
                                  injected_files=[],
                                  admin_password=None,
                                  network_info=[],
                                  block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'], {
                'power_state': 123,
                'vm_state': vm_states.ACTIVE,
                'task_state': None,
                'key_data': None,
                'auto_disk_config': False,
                'expected_task_state': task_states.SPAWNING,
                'launched_at': cur_time_tz
            },
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata']).AndReturn(
                (db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'unshelve.end')
        self.mox.ReplayAll()

        self.compute.unshelve_instance(self.context, instance, image=image)
        self.assertEqual(image['id'], self.deleted_image_id)
        self.assertEqual(instance.host, self.compute.host)
Beispiel #42
0
 def null_safe_isotime(s):
     if isinstance(s, datetime.datetime):
         return timeutils.strtime(s)
     else:
         return str(s) if s else ''
Beispiel #43
0
def to_primitive(value, convert_instances=False, convert_datetime=True, level=0, max_depth=3):
    """Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, convert_instances=True is lossy ... be aware.

    """
    nasty = [
        inspect.ismodule,
        inspect.isclass,
        inspect.ismethod,
        inspect.isfunction,
        inspect.isgeneratorfunction,
        inspect.isgenerator,
        inspect.istraceback,
        inspect.isframe,
        inspect.iscode,
        inspect.isbuiltin,
        inspect.isroutine,
        inspect.isabstract,
    ]
    for test in nasty:
        if test(value):
            return unicode(value)

    # value of itertools.count doesn't get caught by inspects
    # above and results in infinite loop when list(value) is called.
    if type(value) == itertools.count:
        return unicode(value)

    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
    #              tests that raise an exception in a mocked method that
    #              has a @wrap_exception with a notifier will fail. If
    #              we up the dependency to 0.5.4 (when it is released) we
    #              can remove this workaround.
    if getattr(value, "__module__", None) == "mox":
        return "mock"

    if level > max_depth:
        return "?"

    # The try block may not be necessary after the class check above,
    # but just in case ...
    try:
        recursive = functools.partial(
            to_primitive,
            convert_instances=convert_instances,
            convert_datetime=convert_datetime,
            level=level,
            max_depth=max_depth,
        )
        # It's not clear why xmlrpclib created their own DateTime type, but
        # for our purposes, make it a datetime type which is explicitly
        # handled
        if isinstance(value, xmlrpclib.DateTime):
            value = datetime.datetime(*tuple(value.timetuple())[:6])

        if isinstance(value, (list, tuple)):
            return [recursive(v) for v in value]
        elif isinstance(value, dict):
            return dict((k, recursive(v)) for k, v in value.iteritems())
        elif convert_datetime and isinstance(value, datetime.datetime):
            return timeutils.strtime(value)
        elif hasattr(value, "iteritems"):
            return recursive(dict(value.iteritems()), level=level + 1)
        elif hasattr(value, "__iter__"):
            return recursive(list(value))
        elif convert_instances and hasattr(value, "__dict__"):
            # Likely an instance of something. Watch for cycles.
            # Ignore class member vars.
            return recursive(value.__dict__, level=level + 1)
        else:
            return value
    except TypeError:
        # Class objects are tricky since they may define something like
        # __iter__ defined but it isn't callable as list().
        return unicode(value)
 def test_validate_ec2_req_not_expired(self):
     expire = timeutils.utcnow() + datetime.timedelta(seconds=350)
     params = {'Expires': timeutils.strtime(expire, "%Y-%m-%dT%H:%M:%SZ")}
     expired = ec2utils.is_ec2_timestamp_expired(params)
     self.assertFalse(expired)
Beispiel #45
0
    def test_unshelve(self):
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, instance=db_instance)
        instance = instance_obj.Instance.get_by_uuid(
            self.context, db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        instance.task_state = task_states.UNSHELVING
        instance.save()
        image = {'id': 'fake_id'}
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        cur_time_tz = cur_time.replace(tzinfo=iso8601.iso8601.Utc())
        timeutils.set_time_override(cur_time)
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image['id']
        sys_meta['shelved_host'] = host
        hypervisor_hostname = 'fake_hypervisor_hostname'
        fake_compute_info = {'hypervisor_hostname': hypervisor_hostname}

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute, '_prep_block_device')
        self.mox.StubOutWithMock(self.compute.driver, 'spawn')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(self.compute, '_get_compute_info')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.deleted_image_id = None

        def fake_delete(self2, ctxt, image_id):
            self.deleted_image_id = image_id

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'delete', fake_delete)

        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.start')
        self.compute._get_compute_info(mox.IgnoreArg(),
                                       mox.IgnoreArg()).AndReturn(
                                                        fake_compute_info)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'task_state': task_states.SPAWNING, 'host': host,
                 'node': hypervisor_hostname},
                update_cells=False,
                columns_to_join=['metadata', 'system_metadata'],
                ).AndReturn((db_instance, db_instance))
        self.compute._prep_block_device(self.context, instance,
                []).AndReturn('fake_bdm')
        db_instance['key_data'] = None
        db_instance['auto_disk_config'] = None
        self.compute.driver.spawn(self.context, instance, image,
                injected_files=[], admin_password=None,
                network_info=[],
                block_device_info='fake_bdm')
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        db.instance_update_and_get_original(self.context, instance['uuid'],
                {'power_state': 123,
                 'vm_state': vm_states.ACTIVE,
                 'task_state': None,
                 'key_data': None,
                 'auto_disk_config': False,
                 'expected_task_state': task_states.SPAWNING,
                 'launched_at': cur_time_tz},
                 update_cells=False,
                 columns_to_join=['metadata', 'system_metadata']
                 ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                'unshelve.end')
        self.mox.ReplayAll()

        self.compute.unshelve_instance(self.context, instance,
                image=image)
        self.assertEqual(image['id'], self.deleted_image_id)
        self.assertEqual(instance.host, self.compute.host)
Beispiel #46
0
 def _get_timestamp_filename(self):
     return '%s%s' % (TIMESTAMP_PREFIX,
                      timeutils.strtime(fmt=TIMESTAMP_FORMAT))
Beispiel #47
0
    def _shelve_instance(self, shelved_offload_time):
        CONF.set_override('shelved_offload_time', shelved_offload_time)
        db_instance = jsonutils.to_primitive(self._create_fake_instance())
        self.compute.run_instance(self.context, db_instance, {}, {}, [], None,
                                  None, True, None, False)
        instance = instance_obj.Instance.get_by_uuid(
            self.context,
            db_instance['uuid'],
            expected_attrs=['metadata', 'system_metadata'])
        image_id = 'fake_image_id'
        host = 'fake-mini'
        cur_time = timeutils.utcnow()
        timeutils.set_time_override(cur_time)
        instance.task_state = task_states.SHELVING
        instance.save()
        sys_meta = dict(instance.system_metadata)
        sys_meta['shelved_at'] = timeutils.strtime(at=cur_time)
        sys_meta['shelved_image_id'] = image_id
        sys_meta['shelved_host'] = host
        db_instance['system_metadata'] = utils.dict_to_metadata(sys_meta)

        self.mox.StubOutWithMock(self.compute, '_notify_about_instance_usage')
        self.mox.StubOutWithMock(self.compute.driver, 'snapshot')
        self.mox.StubOutWithMock(self.compute.driver, 'power_off')
        self.mox.StubOutWithMock(self.compute, '_get_power_state')
        self.mox.StubOutWithMock(db, 'instance_update_and_get_original')

        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'shelve.start')
        self.compute.driver.power_off(instance)
        self.compute._get_power_state(self.context, instance).AndReturn(123)
        self.compute.driver.snapshot(self.context, instance, 'fake_image_id',
                                     mox.IgnoreArg())

        update_values = {
            'power_state':
            123,
            'vm_state':
            vm_states.SHELVED,
            'task_state':
            None,
            'expected_task_state':
            [task_states.SHELVING, task_states.SHELVING_IMAGE_UPLOADING],
            'system_metadata':
            sys_meta
        }
        if CONF.shelved_offload_time == 0:
            update_values['task_state'] = task_states.SHELVING_OFFLOADING
        db.instance_update_and_get_original(
            self.context,
            instance['uuid'],
            update_values,
            update_cells=False,
            columns_to_join=['metadata', 'system_metadata'],
        ).AndReturn((db_instance, db_instance))
        self.compute._notify_about_instance_usage(self.context, instance,
                                                  'shelve.end')
        if CONF.shelved_offload_time == 0:
            self.compute._notify_about_instance_usage(self.context, instance,
                                                      'shelve_offload.start')
            self.compute.driver.power_off(instance)
            self.compute._get_power_state(self.context,
                                          instance).AndReturn(123)
            db.instance_update_and_get_original(
                self.context,
                instance['uuid'],
                {
                    'power_state':
                    123,
                    'host':
                    None,
                    'node':
                    None,
                    'vm_state':
                    vm_states.SHELVED_OFFLOADED,
                    'task_state':
                    None,
                    'expected_task_state':
                    [task_states.SHELVING, task_states.SHELVING_OFFLOADING]
                },
                update_cells=False,
                columns_to_join=['metadata', 'system_metadata'],
            ).AndReturn((db_instance, db_instance))
            self.compute._notify_about_instance_usage(self.context, instance,
                                                      'shelve_offload.end')
        self.mox.ReplayAll()

        self.compute.shelve_instance(self.context, instance, image_id=image_id)
Beispiel #48
0
def to_primitive(value, convert_instances=False, convert_datetime=True,
                 level=0, max_depth=3):
    """Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, convert_instances=True is lossy ... be aware.

    """
    # handle obvious types first - order of basic types determined by running
    # full tests on nova project, resulting in the following counts:
    # 572754 <type 'NoneType'>
    # 460353 <type 'int'>
    # 379632 <type 'unicode'>
    # 274610 <type 'str'>
    # 199918 <type 'dict'>
    # 114200 <type 'datetime.datetime'>
    #  51817 <type 'bool'>
    #  26164 <type 'list'>
    #   6491 <type 'float'>
    #    283 <type 'tuple'>
    #     19 <type 'long'>
    if isinstance(value, _simple_types):
        return value

    if isinstance(value, datetime.datetime):
        if convert_datetime:
            return timeutils.strtime(value)
        else:
            return value

    # value of itertools.count doesn't get caught by nasty_type_tests
    # and results in infinite loop when list(value) is called.
    if type(value) == itertools.count:
        return six.text_type(value)

    # FIXME(vish): Workaround for LP bug 852095. Without this workaround,
    #              tests that raise an exception in a mocked method that
    #              has a @wrap_exception with a notifier will fail. If
    #              we up the dependency to 0.5.4 (when it is released) we
    #              can remove this workaround.
    if getattr(value, '__module__', None) == 'mox':
        return 'mock'

    if level > max_depth:
        return '?'

    # The try block may not be necessary after the class check above,
    # but just in case ...
    try:
        recursive = functools.partial(to_primitive,
                                      convert_instances=convert_instances,
                                      convert_datetime=convert_datetime,
                                      level=level,
                                      max_depth=max_depth)
        if isinstance(value, dict):
            return dict((k, recursive(v)) for k, v in six.iteritems(value))
        elif isinstance(value, (list, tuple)):
            return [recursive(lv) for lv in value]

        # It's not clear why xmlrpclib created their own DateTime type, but
        # for our purposes, make it a datetime type which is explicitly
        # handled
        if isinstance(value, xmlrpclib.DateTime):
            value = datetime.datetime(*tuple(value.timetuple())[:6])

        if convert_datetime and isinstance(value, datetime.datetime):
            return timeutils.strtime(value)
        elif isinstance(value, gettextutils.Message):
            return value.data
        elif hasattr(value, 'iteritems'):
            return recursive(dict(value.iteritems()), level=level + 1)
        elif hasattr(value, '__iter__'):
            return recursive(list(value))
        elif convert_instances and hasattr(value, '__dict__'):
            # Likely an instance of something. Watch for cycles.
            # Ignore class member vars.
            return recursive(value.__dict__, level=level + 1)
        elif netaddr and isinstance(value, netaddr.IPAddress):
            return six.text_type(value)
        else:
            if any(test(value) for test in _nasty_type_tests):
                return six.text_type(value)
            return value
    except TypeError:
        # Class objects are tricky since they may define something like
        # __iter__ defined but it isn't callable as list().
        return six.text_type(value)
 def test_validate_ec2_req_not_expired(self):
     expire = timeutils.utcnow() + datetime.timedelta(seconds=350)
     params = {'Expires': timeutils.strtime(expire, "%Y-%m-%dT%H:%M:%SZ")}
     expired = ec2utils.is_ec2_timestamp_expired(params)
     self.assertFalse(expired)