def test_ResourceManagementContext_set_value(self): inst, _client, _ctx, _instances_ctx = self._gen_resource_instance() inst._collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): data.ResourceAvailability(10, 1) } # add new inst.set_value(quota=100, usage=50) new_value = inst._collected_data.get( data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "global")) self.assertEqual(new_value.as_dict(), { 'usage': 50.0, 'quota': 100.0, 'availability': 50.0 }) # update inst.set_value(quota=100, usage=80) new_value = inst._collected_data.get( data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "global")) self.assertEqual(new_value.as_dict(), { 'usage': 80.0, 'quota': 100.0, 'availability': 20.0 })
def test_get_profile_from_string(self): res = profile.ResourcesProfile.get_profile_from_string( Mock(), '{"global": {"system": {"resource": 5.0}}}') self.assertEqual(repr(res), '* [<global>, system, resource]: 5.0\n') self.assertEqual( res.requirements, {data.ResourceKey("global", "system", "resource"): 5.0})
def test_ResourceManagementContext_init(self): inst, _client, _ctx, _instances_ctx = self._gen_resource_instance() self.assertEqual(inst.instance.id, 'id') self.assertEqual(inst.project, 'global') self.assertEqual(inst.result_instances, []) inst.add_result_instance_id() inst.add_result_instance_id('abc') self.assertEqual(inst.result_instances, ['id', 'abc']) self.assertEqual(inst.collected_data_raw, {}) self.assertEqual( inst.dump(), { 'availability': {}, 'instances': { 'current': 1, 'left': OrderedDict([('global', 0)]), 'processed': ['id'], 'to_be_processed': [], 'total': 1 } }) inst.logger.info = Mock() inst.log('info', "message {}/{}", 'arg1', 'arg2') inst.logger.info.assert_called_with('[id] message arg1/arg2') inst.logger.info = Mock() inst.log_state() inst.logger.info.assert_called_with( '\n\nCurrent ResourceManagementContext state: \ncurrent_project: ' 'global \ninstances: \n-- TOTAL: 1 \n-- CURRENT: 1 \n-- LEFT: ' 'global=0 | \ninstance: id\n') self.assertEqual(inst.next_instance(), None) self.assertEqual(inst.reset(), { 'visited': True, 'instance': inst.instance, 'project': 'global' }) # check get_resource_key self.assertEqual( inst.get_resource_key("abc"), data.ResourceKey(data.SCOPE_PROJECT, "system", "abc", "global")) # check set_runtime_properties _client.node_instances.update = Mock() _client.node_instances.get = Mock(return_value={ 'version': 11, 'runtime_properties': { 'a': 'b' } }) current_properties = {} inst.set_runtime_properties(current_properties, update=True) self.assertEqual(current_properties, {'a': 'b'}) _client.node_instances.get.assert_called_with('id') _client.node_instances.update.assert_called_with( 'id', runtime_properties={'a': 'b'}, version=11)
def test_ResourceManagementContext_collected_data(self): _ctx = Mock() _client = Mock() _ctx.node_instances = [] inst = context.ResourceManagementContext(_ctx, _client) _ava = Mock() _ava.availability = None inst._collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): _ava } self.assertEqual(inst.collected_data, {}) _ava.availability = 1.1 self.assertEqual(inst.collected_data, { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): _ava })
def test_ResourceManagementContext_collected_data_dict(self): _ctx = Mock() _client = Mock() _ctx.node_instances = [] inst = context.ResourceManagementContext(_ctx, _client) _ava = Mock() _ava.availability = 1.1 _ava.as_dict = Mock(return_value={"a": "!"}) inst._collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): _ava, data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "b"): _ava, data.ResourceKey(data.SCOPE_PROJECT, "system", "other", "b"): _ava, data.ResourceKey(data.SCOPE_GLOBAL, "system", "other"): _ava } self.assertEqual( inst.collected_data_dict, { 'a': { 'system': { 'resource': { 'a': '!' } } }, 'global': { 'system': { 'other': { 'a': '!' } } }, 'b': { 'system': { 'other': { 'a': '!' }, 'resource': { 'a': '!' } } } })
def test_get_profile_from_dict_global_scope(self): res = profile.ResourcesProfile.get_profile_from_dict( Mock(), {"global": { "system": { "resource": "1" } }}) self.assertEqual( res.requirements, {data.ResourceKey("global", "system", "resource"): 1.0})
def test_validate(self): res_prof = profile.ResourcesProfile.get_profile_from_dict( Mock(), {data.SCOPE_PROJECT: { "system": { "resource": "100" } }}) _ava = Mock() _ava.availability = 10.1 _ctx = Mock() _ctx.collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): _ava } # no such stats errors = res_prof.validate(_ctx, data.SCOPE_GLOBAL) self.assertEqual(len(errors), 1) self.assertTrue( isinstance(errors[0], profile.CannotDetermineAvailabilityError)) # overcommit errors = res_prof.validate(_ctx, "a") self.assertEqual(len(errors), 1) self.assertTrue( isinstance(errors[0], profile.NoAvailableResourcesError)) # enough resources _ava = Mock() _ava.availability = 1000 _ctx = Mock() _ctx.collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): _ava } errors = res_prof.validate(_ctx, "a") self.assertEqual(len(errors), 0)
def test_Engine_validate_profile(self): engine, _client, _ctx, _instances_ctx = self._get_engine() # over limit with self.assertRaises(NonRecoverableError): engine.validate_profile( "abc", '{"global": {"system": {"resource": 5.0}}}', True) # under limit engine.rsm_ctx._collected_data = { data.ResourceKey(data.SCOPE_PROJECT, "system", "resource", "a"): data.ResourceAvailability(100, 1) } res = engine.validate_profile("abc", '{"a": {"system": {"resource": 5.0}}}', True) self.assertEqual(res, [])