def test_node_update_new_profile(self, mock_update, mock_db): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context, physical_id=uuidutils.generate_uuid()) node.id = node.store(self.context) new_id = uuidutils.generate_uuid() utils.create_profile(self.context, new_id) mock_update.return_value = True res = node.do_update(self.context, {'new_profile_id': new_id}) self.assertTrue(res) mock_update.assert_called_once_with(self.context, node, new_id) self.assertEqual(new_id, node.profile_id) self.assertEqual(new_id, node.rt['profile'].id) mock_db.assert_has_calls([ mock.call( self.context, node.id, { 'status': consts.NS_UPDATING, 'status_reason': 'Update in progress' }), mock.call( self.context, node.id, { 'status': consts.NS_ACTIVE, 'status_reason': 'Update succeeded', 'profile_id': new_id, 'updated_at': mock.ANY }) ])
def test_node_update_EResourceUpdate(self, mock_db, mock_update): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context) node.physical_id = uuidutils.generate_uuid() node.id = uuidutils.generate_uuid() ex = exception.EResourceUpdate(type='PROFILE', id='ID', message='reason') mock_update.side_effect = ex new_id = uuidutils.generate_uuid() utils.create_profile(self.context, new_id) res = node.do_update(self.context, {'new_profile_id': new_id}) self.assertFalse(res) self.assertNotEqual(new_id, node.profile_id) mock_db.assert_has_calls([ mock.call(self.context, node.id, { "status": "UPDATING", "status_reason": "Update in progress" }), mock.call( self.context, node.id, { "status": "ERROR", "status_reason": "Failed in updating PROFILE 'ID': reason.", "updated_at": mock.ANY }) ]) self.assertEqual(1, mock_update.call_count)
def test_cluster_policy_load(self): ex = self.assertRaises(exception.PolicyNotAttached, cpm.ClusterPolicy.load, self.context, 'some-cluster', 'any-policy') self.assertEqual( "The policy 'any-policy' is not attached to the " "specified cluster 'some-cluster'.", str(ex)) utils.create_profile(self.context, PROFILE_ID) cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID) policy = utils.create_policy(self.context, POLICY_ID) values = { 'priority': 12, 'enabled': True, } cp = cpm.ClusterPolicy(cluster.id, policy.id, **values) cp_id = cp.store(self.context) result = cpm.ClusterPolicy.load(self.context, CLUSTER_ID, POLICY_ID) self.assertEqual(cp_id, result.id) self.assertEqual(cluster.id, result.cluster_id) self.assertEqual(policy.id, result.policy_id) self.assertTrue(result.enabled) self.assertEqual(12, result.priority) self.assertEqual({}, result.data) self.assertIsNone(result.last_op) self.assertEqual('test-cluster', result.cluster_name) self.assertEqual('senlin.policy.dummy-1.0', result.policy_type) self.assertEqual('test_policy', result.policy_name)
def test_store_for_update(self): utils.create_profile(self.context, PROFILE_ID) cluster = cm.Cluster('test-cluster', 0, PROFILE_ID, user=self.context.user_id, project=self.context.project_id) mock_load = self.patchobject(cluster, '_load_runtime_data') self.assertIsNone(cluster.id) cluster_id = cluster.store(self.context) self.assertIsNotNone(cluster_id) mock_load.assert_called_once_with(self.context) # do an update cluster.name = 'test-cluster-1' cluster.min_size = 1 cluster.max_size = 3 cluster.desired_capacity = 2 cluster.timeout = 120 cluster.data = {'FOO': 'BAR'} cluster.metadata = {'KEY': 'VALUE'} cluster.config = {'KEY': 'VALUE'} new_id = cluster.store(self.context) self.assertEqual(cluster_id, new_id) result = co.Cluster.get(self.context, cluster_id) self.assertIsNotNone(result) self.assertEqual('test-cluster-1', result.name) self.assertEqual(self.context.user_id, result.user) self.assertEqual(self.context.project_id, result.project) self.assertEqual(1, result.min_size) self.assertEqual(3, result.max_size) self.assertEqual(2, result.desired_capacity) self.assertEqual(120, result.timeout) self.assertEqual({'FOO': 'BAR'}, result.data) self.assertEqual({'KEY': 'VALUE'}, result.metadata) self.assertEqual({'KEY': 'VALUE'}, result.config)
def test_cluster_policy_store(self): utils.create_profile(self.context, PROFILE_ID) cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID) policy = utils.create_policy(self.context, POLICY_ID) values = { 'priority': 12, 'enabled': True, } cp = cpm.ClusterPolicy(cluster.id, policy.id, **values) self.assertIsNone(cp.id) cp_id = cp.store(self.context) self.assertIsNotNone(cp_id) result = cpo.ClusterPolicy.get(self.context, CLUSTER_ID, POLICY_ID) self.assertIsNotNone(result) self.assertEqual(12, result.priority) self.assertTrue(result.enabled) self.assertEqual({}, result.data) self.assertIsNone(result.last_op) # do an update cp.enabled = False cp.priority = 60 cp.data = {'foo': 'bar'} timestamp = timeutils.utcnow(True) cp.last_op = timestamp new_id = cp.store(self.context) self.assertEqual(cp_id, new_id) result = cpo.ClusterPolicy.get(self.context, CLUSTER_ID, POLICY_ID) self.assertIsNotNone(result) self.assertFalse(result.enabled) self.assertEqual(60, result.priority) self.assertEqual({'foo': 'bar'}, result.data) self.assertEqual(common_utils.isotime(timestamp), common_utils.isotime(result.last_op))
def test_node_update_resource_status_error(self, mock_update, mock_status, mock_handle_exception): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context) ex = exception.ResourceStatusError(resource_id='id', status='ERROR', reason='some reason') mock_update.side_effect = ex new_id = '71d8f4dd-1ef9-4308-b7ae-03298b04449e' new_profile = utils.create_profile(self.context, new_id) node.physical_id = 'd94d6333-82e6-4f87-b7ab-b786776df9d1' res = node.do_update(self.context, {'new_profile_id': new_profile.id}) self.assertFalse(res) mock_handle_exception.assert_called_once_with(self.context, 'update', 'ERROR', ex) self.assertNotEqual(new_id, node.profile_id)
def test_store_for_create(self): utils.create_profile(self.context, PROFILE_ID) cluster = cm.Cluster('test-cluster', 0, PROFILE_ID, user=self.context.user_id, project=self.context.project_id) mock_load = self.patchobject(cluster, '_load_runtime_data') self.assertIsNone(cluster.id) cluster_id = cluster.store(self.context) self.assertIsNotNone(cluster_id) mock_load.assert_called_once_with(self.context) result = co.Cluster.get(self.context, cluster_id=cluster_id) self.assertIsNotNone(result) self.assertEqual('test-cluster', result.name) self.assertEqual(PROFILE_ID, result.profile_id) self.assertEqual(self.context.user_id, result.user) self.assertEqual(self.context.project_id, result.project) self.assertEqual(self.context.domain_id, result.domain) self.assertIsNotNone(result.init_at) self.assertIsNone(result.created_at) self.assertIsNone(result.updated_at) self.assertEqual(0, result.min_size) self.assertEqual(-1, result.max_size) self.assertEqual(0, result.desired_capacity) self.assertEqual(1, result.next_index) self.assertEqual(cfg.CONF.default_action_timeout, result.timeout) self.assertEqual('INIT', result.status) self.assertEqual('Initializing', result.status_reason) self.assertEqual({}, result.data) self.assertEqual({}, result.metadata)
def test_node_update_resource_status_error(self, mock_update, mock_status, mock_handle_exception): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context) ex = exception.ResourceStatusError(resource_id='id', status='ERROR', reason='some reason') mock_update.side_effect = ex new_id = '71d8f4dd-1ef9-4308-b7ae-03298b04449e' new_profile = utils.create_profile(self.context, new_id) node.physical_id = 'd94d6333-82e6-4f87-b7ab-b786776df9d1' res = node.do_update(self.context, {'new_profile_id': new_profile.id}) self.assertFalse(res) mock_handle_exception.assert_called_once_with(self.context, 'update', 'ERROR', ex) self.assertNotEqual(new_id, node.profile_id)
def test_node_update_new_profile(self, mock_update, mock_status): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context) new_id = '71d8f4dd-1ef9-4308-b7ae-03298b04449e' new_profile = utils.create_profile(self.context, new_id) node.physical_id = 'd94d6333-82e6-4f87-b7ab-b786776df9d1' res = node.do_update(self.context, {'new_profile_id': new_profile.id}) self.assertTrue(res) mock_update.assert_called_once_with(self.context, node, new_profile.id) self.assertEqual(new_id, node.profile_id) self.assertEqual(new_id, node.rt['profile'].id) mock_status.assert_any_call(self.context, 'UPDATING', reason='Update in progress') mock_status.assert_any_call(self.context, 'ACTIVE', reason='Update succeeded')
def test_node_update_new_profile(self, mock_update, mock_status): node = nodem.Node('node1', PROFILE_ID, CLUSTER_ID, self.context) new_id = '71d8f4dd-1ef9-4308-b7ae-03298b04449e' new_profile = utils.create_profile(self.context, new_id) node.physical_id = 'd94d6333-82e6-4f87-b7ab-b786776df9d1' res = node.do_update(self.context, {'new_profile_id': new_profile.id}) self.assertTrue(res) mock_update.assert_called_once_with(self.context, node, new_profile.id) self.assertEqual(new_id, node.profile_id) self.assertEqual(new_id, node.rt['profile'].id) mock_status.assert_any_call(self.context, 'UPDATING', reason='Update in progress') mock_status.assert_any_call(self.context, 'ACTIVE', reason='Update succeeded')
def setUp(self): super(TestScalingPolicy, self).setUp() self.context = utils.dummy_context() self.spec = { 'type': 'senlin.policy.scaling', 'version': '1.0', 'properties': { 'event': 'CLUSTER_SCALE_IN', 'adjustment': { 'type': 'CHANGE_IN_CAPACITY', 'number': 1, 'min_step': 1, 'best_effort': False, 'cooldown': 3, } } } self.profile = utils.create_profile(self.context, PROFILE_ID) self.cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
def setUp(self): super(TestScalingPolicy, self).setUp() self.context = utils.dummy_context() self.spec = { 'type': 'senlin.policy.scaling', 'version': '1.0', 'properties': { 'event': 'CLUSTER_SCALE_IN', 'adjustment': { 'type': 'CHANGE_IN_CAPACITY', 'number': 1, 'min_step': 1, 'best_effort': False, 'cooldown': 3, } } } self.profile = utils.create_profile(self.context, PROFILE_ID) self.cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
def setUp(self): super(TestNode, self).setUp() self.context = utils.dummy_context(project='node_test_project') self.profile = utils.create_profile(self.context, PROFILE_ID) self.cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)
def test_to_dict(self): PROFILE_ID = '96f4df4b-889e-4184-ba8d-b5ca122f95bb' POLICY1_ID = '2c5139a6-24ba-4a6f-bd53-a268f61536de' POLICY2_ID = '2c5139a6-24ba-4a6f-bd53-a268f61536d3' NODE1_ID = '26f4df4b-889e-4184-ba8d-b5ca122f9566' NODE2_ID = '26f4df4b-889e-4184-ba8d-b5ca122f9567' utils.create_profile(self.ctx, PROFILE_ID) policy_1 = utils.create_policy(self.ctx, POLICY1_ID, 'P1') policy_2 = utils.create_policy(self.ctx, POLICY2_ID, 'P2') values = { 'profile_id': PROFILE_ID, 'name': 'test-cluster', 'desired_capacity': 1, 'status': 'INIT', 'init_at': timeutils.utcnow(True), 'max_size': -1, 'min_size': 0, 'timeout': cfg.CONF.default_action_timeout, 'user': self.ctx.user_id, 'project': self.ctx.project_id, } cluster = co.Cluster.create(self.ctx, values) p1 = cpo.ClusterPolicy(cluster_id=cluster.id, policy_id=policy_1.id, enabled=True, id=uuidutils.generate_uuid(), last_op=None) p2 = cpo.ClusterPolicy(cluster_id=cluster.id, policy_id=policy_2.id, enabled=True, id=uuidutils.generate_uuid(), last_op=None) values = { 'priority': 12, 'enabled': True, } p1.create(self.ctx, cluster.id, POLICY1_ID, values) p2.create(self.ctx, cluster.id, POLICY2_ID, values) utils.create_node(self.ctx, NODE1_ID, PROFILE_ID, cluster.id) utils.create_node(self.ctx, NODE2_ID, PROFILE_ID, cluster.id) cluster = co.Cluster.get(self.ctx, cluster.id) expected = { 'id': cluster.id, 'name': cluster.name, 'profile_id': PROFILE_ID, 'user': cluster.user, 'project': cluster.project, 'domain': cluster.domain, 'init_at': mock.ANY, 'created_at': None, 'updated_at': None, 'min_size': 0, 'max_size': -1, 'desired_capacity': 1, 'timeout': cfg.CONF.default_action_timeout, 'status': six.text_type('INIT'), 'status_reason': None, 'metadata': {}, 'data': {}, 'dependents': {}, 'config': {}, 'nodes': [mock.ANY, mock.ANY], 'policies': [mock.ANY, mock.ANY], 'profile_name': six.text_type('test-profile'), } cluster_dict = cluster.to_dict() self.assertEqual(expected, cluster_dict) self.assertEqual(2, len(cluster_dict['nodes'])) self.assertEqual(2, len(cluster_dict['policies']))
def setUp(self): super(TestNode, self).setUp() self.context = utils.dummy_context(project='node_test_project') self.profile = utils.create_profile(self.context, PROFILE_ID) self.cluster = utils.create_cluster(self.context, CLUSTER_ID, PROFILE_ID)