def test_multiple_keys(self): self._send_bad_attempts('test1', CONF.lockout_attempts) self.assertTrue(self._is_locked_out('test1')) self.assertFalse(self._is_locked_out('test2')) timeutils.advance_time_seconds(CONF.lockout_minutes * 60) self.assertFalse(self._is_locked_out('test1')) self.assertFalse(self._is_locked_out('test2'))
def test_trusted_filter_update_cache(self, req_mock): oat_data = { "hosts": [{ "host_name": "node1", "trust_lvl": "untrusted", "vtime": timeutils.isotime() }] } req_mock.return_value = requests.codes.OK, oat_data extra_specs = {'trust:trusted_host': 'untrusted'} filter_properties = { 'context': mock.sentinel.ctx, 'instance_type': { 'memory_mb': 1024, 'extra_specs': extra_specs } } host = fakes.FakeHostState('host1', 'node1', {}) self.filt_cls.host_passes(host, filter_properties) # Fill the caches req_mock.reset_mock() self.filt_cls.host_passes(host, filter_properties) self.assertFalse(req_mock.called) req_mock.reset_mock() timeutils.set_time_override(timeutils.utcnow()) timeutils.advance_time_seconds( CONF.trusted_computing.attestation_auth_timeout + 80) self.filt_cls.host_passes(host, filter_properties) self.assertTrue(req_mock.called) timeutils.clear_time_override()
def test_memcached_driver(self): serv = self.useFixture( ServiceFixture(self._host, self._binary, self._topic)).serv serv.start() service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) hostkey = str("%s:%s" % (self._topic, self._host)) self.servicegroup_api._driver.mc.set(hostkey, timeutils.utcnow(), time=self.down_time) self.assertTrue(self.servicegroup_api.service_is_up(service_ref)) self.useFixture(test.TimeOverride()) timeutils.advance_time_seconds(self.down_time + 1) self.servicegroup_api._driver._report_state(serv) service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) self.assertTrue(self.servicegroup_api.service_is_up(service_ref)) serv.stop() timeutils.advance_time_seconds(self.down_time + 1) service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) self.assertFalse(self.servicegroup_api.service_is_up(service_ref))
def test_trusted_filter_update_cache(self, req_mock): oat_data = {"hosts": [{"host_name": "node1", "trust_lvl": "untrusted", "vtime": timeutils.isotime()}]} req_mock.return_value = requests.codes.OK, oat_data extra_specs = {'trust:trusted_host': 'untrusted'} filter_properties = {'context': mock.sentinel.ctx, 'instance_type': {'memory_mb': 1024, 'extra_specs': extra_specs}} host = fakes.FakeHostState('host1', 'node1', {}) self.filt_cls.host_passes(host, filter_properties) # Fill the caches req_mock.reset_mock() self.filt_cls.host_passes(host, filter_properties) self.assertFalse(req_mock.called) req_mock.reset_mock() timeutils.set_time_override(timeutils.utcnow()) timeutils.advance_time_seconds( CONF.trusted_computing.attestation_auth_timeout + 80) self.filt_cls.host_passes(host, filter_properties) self.assertTrue(req_mock.called) timeutils.clear_time_override()
def test_backwards(self): watch = tt.StopWatch(0.1) watch.start() timeutils.advance_time_seconds(0.5) self.assertTrue(watch.expired()) timeutils.advance_time_seconds(-1.0) self.assertFalse(watch.expired()) self.assertEqual(0.0, watch.elapsed())
def test_pause_resume(self): watch = tt.StopWatch() watch.start() timeutils.advance_time_seconds(0.05) watch.stop() elapsed = watch.elapsed() self.assertAlmostEqual(elapsed, watch.elapsed()) watch.resume() timeutils.advance_time_seconds(0.05) self.assertNotEqual(elapsed, watch.elapsed())
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)
def test_shelved_poll_not_timedout(self): instance = self._create_fake_instance_obj() sys_meta = 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)
def test_tokens_expire(self): # Test that tokens expire correctly. self.useFixture(test.TimeOverride()) token = u'mytok' self.flags(console_token_ttl=1) self._stub_validate_console_port(True) self.manager_api.authorize_console(self.context, token, 'novnc', '127.0.0.1', '8080', 'host', self.instance['uuid']) self.assertTrue(self.manager_api.check_token(self.context, token)) timeutils.advance_time_seconds(1) self.assertFalse(self.manager_api.check_token(self.context, token))
def test_on_wait_task_expired(self): now = timeutils.utcnow() self.request_inst_mock.expired = True self.request_inst_mock.created_on = now timeutils.set_time_override(now) self.addCleanup(timeutils.clear_time_override) timeutils.advance_time_seconds(120) ex = self.executor() ex._requests_cache[self.task_uuid] = self.request_inst_mock self.assertEqual(len(ex._requests_cache), 1) ex._on_wait() self.assertEqual(len(ex._requests_cache), 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)
def test_shelved_poll_timedout(self): instance = self._create_fake_instance_obj() sys_meta = 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)
def test_DB_driver(self): serv = self.useFixture( ServiceFixture(self._host, self._binary, self._topic)).serv serv.start() service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) self.assertTrue(self.servicegroup_api.service_is_up(service_ref)) self.useFixture(test.TimeOverride()) timeutils.advance_time_seconds(self.down_time + 1) self.servicegroup_api._driver._report_state(serv) service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) self.assertTrue(self.servicegroup_api.service_is_up(service_ref)) serv.stop() timeutils.advance_time_seconds(self.down_time + 1) service_ref = db.service_get_by_args(self._ctx, self._host, self._binary) self.assertFalse(self.servicegroup_api.service_is_up(service_ref))
def test_delete_expired_tokens(self): self.useFixture(test.TimeOverride()) token = u'mytok' self.flags(console_token_ttl=1) self._stub_validate_console_port(True) self.manager_api.authorize_console(self.context, token, 'novnc', '127.0.0.1', '8080', 'host', self.instance['uuid']) timeutils.advance_time_seconds(1) self.assertFalse(self.manager_api.check_token(self.context, token)) token1 = u'mytok2' self.manager_api.authorize_console(self.context, token1, 'novnc', '127.0.0.1', '8080', 'host', self.instance['uuid']) stored_tokens = self.manager._get_tokens_for_instance( self.instance['uuid']) # when trying to store token1, expired token is removed fist. self.assertEqual(len(stored_tokens), 1) self.assertEqual(stored_tokens[0], token1)
def test_pending_expired(self): req = self.request() timeutils.advance_time_seconds(self.timeout + 1) self.assertTrue(req.expired)
def test_pending_not_expired(self): req = self.request() timeutils.advance_time_seconds(self.timeout - 1) self.assertFalse(req.expired)
def test_expiry(self): watch = tt.StopWatch(0.1) watch.start() timeutils.advance_time_seconds(0.2) self.assertTrue(watch.expired())
def test_not_expired(self): watch = tt.StopWatch(0.1) watch.start() timeutils.advance_time_seconds(0.05) self.assertFalse(watch.expired())
def test_advance_time_seconds(self): timeutils.set_time_override(self.one_minute_before) timeutils.advance_time_seconds(60) self.assertEqual(timeutils.utcnow(), self.skynet_self_aware_time)
def test_context_manager(self): with tt.StopWatch() as watch: timeutils.advance_time_seconds(0.05) self.assertGreater(0.01, watch.elapsed())
def test_running_not_expired(self): request = self.request() request.transition(pr.PENDING) request.transition(pr.RUNNING) timeutils.advance_time_seconds(self.timeout + 1) self.assertFalse(request.expired)
def test_elapsed(self): watch = tt.StopWatch() watch.start() timeutils.advance_time_seconds(0.2) # NOTE(harlowja): Allow for a slight variation by using 0.19. self.assertGreaterEqual(0.19, watch.elapsed())
def test_window_timeout(self): self._send_bad_attempts('test', CONF.lockout_attempts - 1) self.assertFalse(self._is_locked_out('test')) timeutils.advance_time_seconds(CONF.lockout_window * 60) self._send_bad_attempts('test', CONF.lockout_attempts - 1) self.assertFalse(self._is_locked_out('test'))