def test_set_status_for_update_with_profile(self, mock_update, mock_load): x_profile = mock.Mock() mock_load.return_value = x_profile cluster = cm.Cluster('test-cluster', 0, PROFILE_ID, id=CLUSTER_ID, status='UPDATING') new_profile_id = 'a64f0b03-4b77-49d5-89e0-7bcc77c4ce67' cluster.set_status(self.context, consts.CS_ACTIVE, 'Cluster updated', profile_id=new_profile_id) self.assertEqual(consts.CS_ACTIVE, cluster.status) self.assertEqual('Cluster updated', cluster.status_reason) self.assertIsNotNone(cluster.updated_at) self.assertEqual(x_profile, cluster.rt['profile']) self.assertEqual(new_profile_id, cluster.profile_id) mock_load.assert_called_once_with(self.context, profile_id=new_profile_id) mock_update.assert_called_once_with( self.context, CLUSTER_ID, { 'status': consts.CS_ACTIVE, 'status_reason': 'Cluster updated', 'profile_id': new_profile_id, 'updated_at': mock.ANY, })
def setUp(self): super(TestClusterActionNotification, self).setUp() ctx = utils.dummy_context() cluster_params = { 'id': uuidutils.generate_uuid(), 'init_at': timeutils.utcnow(True), 'min_size': 1, 'max_size': 10, 'timeout': 4, 'status': 'ACTIVE', 'status_reason': 'Good', 'user': '******', 'project': 'project1', } self.cluster = cluster.Cluster('CC', 5, uuidutils.generate_uuid(), **cluster_params) action_params = { 'id': uuidutils.generate_uuid(), 'name': 'fake_name', 'start_time': 1.23, 'status': 'RUNNING', 'status_reason': 'Good', 'user': '******', 'project': 'project1', } self.action = action_base.Action(uuidutils.generate_uuid(), 'CLUSTER_CREATE', ctx, **action_params)
def test_event_init_with_entity(self): timestamp = timeutils.utcnow() x_cluster = cluster_mod.Cluster('fake-cluster', 0, 'fake-profile', id='FAKE_CLUSTER') event = EVENT.Event(timestamp, logging.CRITICAL, x_cluster, action="FAKE_ACTION", status="ACTIVE", status_reason="Recovered just now", user=self.context.user, project=self.context.project) self.assertEqual(timestamp, event.timestamp) self.assertIsNone(event.id) self.assertEqual(logging.CRITICAL, event.level) self.assertEqual(self.context.user, event.user) self.assertEqual(self.context.project, event.project) self.assertEqual('FAKE_ACTION', event.action) self.assertEqual('ACTIVE', event.status) self.assertEqual('Recovered just now', event.status_reason) self.assertEqual('FAKE_CLUSTER', event.obj_id) self.assertEqual('fake-cluster', event.obj_name) self.assertEqual('FAKE_CLUSTER', event.cluster_id) self.assertEqual('CLUSTER', event.obj_type)
def test_store_for_create(self): 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_attach_policy_type_conflict(self, mock_load): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.id = CLUSTER_ID existing = mock.Mock() existing.id = POLICY_ID existing.type = 'POLICY_TYPE_ONE' cluster.rt['policies'] = [existing] policy = mock.Mock() policy.singleton = True policy.type = 'POLICY_TYPE_ONE' mock_load.return_value = policy # do it new_policy_id = '62d52dd6-5f83-4340-b079-349da2f9ffd9' res, reason = cluster.attach_policy(self.context, new_policy_id, {}) # assert self.assertFalse(res) expected = ('Only one instance of policy type (POLICY_TYPE_ONE) can ' 'be attached to a cluster, but another instance ' '(%s) is found attached to the cluster ' '(%s) already.' % (POLICY_ID, CLUSTER_ID)) self.assertEqual(expected, reason) mock_load.assert_called_once_with(self.context, new_policy_id)
def test_init(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) self.assertIsNone(cluster.id) self.assertEqual('test-cluster', cluster.name) self.assertEqual(PROFILE_ID, cluster.profile_id) self.assertEqual('', cluster.user) self.assertEqual('', cluster.project) self.assertEqual('', cluster.domain) self.assertIsNone(cluster.init_at) self.assertIsNone(cluster.created_at) self.assertIsNone(cluster.updated_at) self.assertEqual(0, cluster.min_size) self.assertEqual(-1, cluster.max_size) self.assertEqual(0, cluster.desired_capacity) self.assertEqual(1, cluster.next_index) self.assertEqual(cfg.CONF.default_action_timeout, cluster.timeout) self.assertEqual('INIT', cluster.status) self.assertEqual('Initializing', cluster.status_reason) self.assertEqual({}, cluster.data) self.assertEqual({}, cluster.metadata) self.assertEqual({}, cluster.dependents) self.assertEqual({}, cluster.config) self.assertEqual({ 'profile': None, 'nodes': [], 'policies': [] }, cluster.rt)
def test_get_zone_distribution(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) node1 = mock.Mock() node1.data = {} node1.get_details.return_value = { 'OS-EXT-AZ:availability_zone': 'AZ1', } node2 = mock.Mock() node2.data = {'foobar': 'irrelevant'} node3 = mock.Mock() node3.data = {'placement': {'zone': 'AZ2'}} nodes = [node1, node2, node3] for n in nodes: cluster.add_node(n) result = cluster.get_zone_distribution(self.context, ['AZ1', 'AZ2', 'AZ3']) self.assertEqual(3, len(result)) self.assertEqual(1, result['AZ1']) self.assertEqual(1, result['AZ2']) self.assertEqual(0, result['AZ3']) node1.get_details.assert_called_once_with(self.context)
def test__load_runtime_data(self, mock_nodes, mock_profile, mock_policy, mock_pb): x_binding = mock.Mock() x_binding.policy_id = 'FAKE_POLICY' mock_pb.return_value = [x_binding] x_policy = mock.Mock() mock_policy.return_value = x_policy x_profile = mock.Mock() mock_profile.return_value = x_profile x_node_1 = mock.Mock() x_node_2 = mock.Mock() mock_nodes.return_value = [x_node_1, x_node_2] cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') cluster.id = 'FAKE_CLUSTER' cluster._load_runtime_data(self.context) rt = cluster.rt self.assertEqual(x_profile, rt['profile']) self.assertEqual([x_node_1, x_node_2], rt['nodes']) self.assertEqual([x_policy], rt['policies']) mock_pb.assert_called_once_with(self.context, 'FAKE_CLUSTER', filters=None, sort=None) mock_policy.assert_called_once_with(self.context, 'FAKE_POLICY') mock_profile.assert_called_once_with(self.context, profile_id='PROFILE_ID', project_safe=False) mock_nodes.assert_called_once_with(self.context, cluster_id='FAKE_CLUSTER')
def test_attach_policy_type_conflict(self, mock_load): cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') cluster.id = 'FAKE_CLUSTER' existing = mock.Mock() existing.id = 'PLCY2' existing.type = 'POLICY_TYPE_ONE' cluster.rt['policies'] = [existing] policy = mock.Mock() policy.singleton = True policy.type = 'POLICY_TYPE_ONE' mock_load.return_value = policy # do it res, reason = cluster.attach_policy(self.context, 'PLCY1', {}) # assert self.assertFalse(res) expected = ('Only one instance of policy type (POLICY_TYPE_ONE) can ' 'be attached to a cluster, but another instance ' '(PLCY2) is found attached to the cluster ' '(FAKE_CLUSTER) already.') self.assertEqual(expected, reason) mock_load.assert_called_once_with(self.context, 'PLCY1')
def test_attach_policy_type_conflict_but_ok(self, mock_load, mock_cp): cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') existing = mock.Mock() existing.id = 'FAKE_2' existing.type = 'POLICY_TYPE_ONE' cluster.rt['policies'] = [existing] policy = mock.Mock() policy.singleton = False policy.type = 'POLICY_TYPE_ONE' policy.attach.return_value = (True, None) policy.PRIORITY = 10 mock_load.return_value = policy binding = mock.Mock() mock_cp.return_value = binding values = {'enabled': True} # do it res, reason = cluster.attach_policy(self.context, 'FAKE_1', values) # assert self.assertTrue(res) self.assertEqual('Policy attached.', reason) policy.attach.assert_called_once_with(cluster) mock_load.assert_called_once_with(self.context, 'FAKE_1') mock_cp.assert_called_once_with(cluster.id, 'FAKE_1', priority=10, enabled=True, data=None) binding.store.assert_called_once_with(self.context) self.assertIn(policy, cluster.policies)
def test__load_runtime_data(self, mock_nodes, mock_profile, mock_policy, mock_pb): x_binding = mock.Mock() x_binding.policy_id = POLICY_ID mock_pb.return_value = [x_binding] x_policy = mock.Mock() mock_policy.return_value = x_policy x_profile = mock.Mock() mock_profile.return_value = x_profile x_node_1 = mock.Mock() x_node_2 = mock.Mock() mock_nodes.return_value = [x_node_1, x_node_2] cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.id = CLUSTER_ID cluster._load_runtime_data(self.context) rt = cluster.rt self.assertEqual(x_profile, rt['profile']) self.assertEqual([x_node_1, x_node_2], rt['nodes']) self.assertEqual(2, len(rt['nodes'])) self.assertIsInstance(rt['nodes'], list) self.assertEqual([x_policy], rt['policies']) mock_pb.assert_called_once_with(self.context, CLUSTER_ID) mock_policy.assert_called_once_with(self.context, POLICY_ID, project_safe=False) mock_profile.assert_called_once_with(self.context, profile_id=PROFILE_ID, project_safe=False) mock_nodes.assert_called_once_with(self.context, CLUSTER_ID)
def test_health_check(self, mock_update, mock_check, mock_load): cluster = cm.Cluster('test-cluster', 5, PROFILE_ID, min_size=2, id=CLUSTER_ID) node1 = node_mod.Node('fake1', PROFILE_ID, status='ACTIVE') node2 = node_mod.Node('fake2', PROFILE_ID, status='ACTIVE') nodes = [node1, node2] for node in nodes: cluster.add_node(node) node1.status = 'ERROR' mock_load.return_value = [node1, node2] cluster.health_check(self.context) self.assertEqual(2, len(cluster.nodes)) self.assertEqual([node1, node2], cluster.nodes) mock_update.assert_called_once_with([node1, node2]) mock_check.assert_has_calls( [mock.call(self.context), mock.call(self.context)]) mock_load.assert_called_once_with(self.context, cluster_id=CLUSTER_ID)
def test_set_status_for_update_with_profile(self, mock_update, mock_load): x_profile = mock.Mock() mock_load.return_value = x_profile cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID', id='FAKE_ID', status='UPDATING') cluster.set_status(self.context, cluster.ACTIVE, 'Cluster updated', profile_id='NEW_PROFILE') self.assertEqual(cluster.ACTIVE, cluster.status) self.assertEqual('Cluster updated', cluster.status_reason) self.assertIsNotNone(cluster.updated_at) self.assertEqual(x_profile, cluster.rt['profile']) self.assertEqual('NEW_PROFILE', cluster.profile_id) mock_load.assert_called_once_with(self.context, profile_id='NEW_PROFILE') mock_update.assert_called_once_with( self.context, 'FAKE_ID', { 'status': cluster.ACTIVE, 'status_reason': 'Cluster updated', 'profile_id': 'NEW_PROFILE', 'updated_at': mock.ANY, } )
def test_do_create_wrong_status(self): cluster = cb.Cluster('test-cluster', 0, 'PROFILE_ID') cluster.status = cluster.ACTIVE res = cluster.do_create(self.context) self.assertFalse(res)
def test_do_create_wrong_status(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.status = consts.CS_ACTIVE res = cluster.do_create(self.context) self.assertFalse(res)
def test__load_runtime_data_id_is_none(self): cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') cluster._load_runtime_data(self.context) self.assertIsNone(cluster.rt['profile']) self.assertEqual([], cluster.rt['nodes']) self.assertEqual([], cluster.rt['policies'])
def test_detach_policy_not_attached(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.rt['policies'] = [] res, reason = cluster.detach_policy(self.context, POLICY_ID) self.assertFalse(res) self.assertEqual('Policy not attached.', reason)
def test_do_create(self): cluster = cb.Cluster('test-cluster', 0, 'PROFILE_ID') mock_status = self.patchobject(cluster, 'set_status') res = cluster.do_create(self.context) self.assertTrue(res) mock_status.assert_called_once_with( self.context, cluster.CREATING, reason='Creation in progress')
def test_do_update(self): cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') mock_status = self.patchobject(cluster, 'set_status') res = cluster.do_update(self.context) mock_status.assert_called_once_with(self.context, cluster.UPDATING, reason='Update in progress') self.assertTrue(res)
def test_do_operation(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) mock_status = self.patchobject(cluster, 'set_status') res = cluster.do_operation(self.context, operation='dance') mock_status.assert_called_once_with(self.context, consts.CS_OPERATING, 'Operation dance in progress') self.assertTrue(res)
def test_policies_property(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) self.assertEqual([], cluster.policies) # with policies attached policy1 = mock.Mock() policy2 = mock.Mock() cluster.rt['policies'] = [policy1, policy2] self.assertEqual([policy1, policy2], cluster.policies)
def test_do_recover(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) mock_status = self.patchobject(cluster, 'set_status') res = cluster.do_recover(self.context) mock_status.assert_called_once_with(self.context, consts.CS_RECOVERING, 'Recovery in progress') self.assertTrue(res)
def test_do_check(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) mock_status = self.patchobject(cluster, 'set_status') res = cluster.do_check(self.context) mock_status.assert_called_once_with(self.context, consts.CS_CHECKING, 'Check in progress') self.assertTrue(res)
def test_update_policy_not_attached(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.rt['policies'] = [] # do it values = {'enabled': False} res, reason = cluster.update_policy(self.context, POLICY_ID, **values) self.assertFalse(res) self.assertEqual('Policy not attached.', reason)
def test_set_status_for_resize(self, mock_update): cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID', id='FAKE_ID', status='RESIZING') cluster.set_status(self.context, cluster.ACTIVE, 'Cluster resized') self.assertEqual(cluster.ACTIVE, cluster.status) self.assertEqual('Cluster resized', cluster.status_reason) self.assertIsNotNone(cluster.updated_at)
def test_create_with_obj(self): params = copy.deepcopy(self.params) name = params.pop('name') desired_capacity = params.pop('desired_capacity') profile_id = params.pop('profile_id') c1 = cluster.Cluster(name, desired_capacity, profile_id, **params) sot = base.ClusterPayload.from_cluster(c1) self._verify_equality(sot, self.params)
def test_do_delete(self, mock_delete): mock_delete.return_value = None cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID') cluster.id = 'FAKE_ID' res = cluster.do_delete(self.context) mock_delete.assert_called_once_with(self.context, 'FAKE_ID') self.assertTrue(res)
def test_init_with_none(self): kwargs = { 'min_size': None, 'max_size': None, 'metadata': None } cluster = clusterm.Cluster('test-cluster', 0, 'PROFILE_ID', **kwargs) self.assertEqual(0, cluster.min_size) self.assertEqual(-1, cluster.max_size) self.assertEqual({}, cluster.metadata)
def test_do_delete(self, mock_delete): mock_delete.return_value = None cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) cluster.id = CLUSTER_ID res = cluster.do_delete(self.context) mock_delete.assert_called_once_with(self.context, CLUSTER_ID) self.assertTrue(res)
def test_nodes_property(self): cluster = cm.Cluster('test-cluster', 0, PROFILE_ID) self.assertEqual([], cluster.nodes) # with nodes node1 = mock.Mock() node2 = mock.Mock() cluster.rt['nodes'] = [node1, node2] self.assertEqual([node1, node2], cluster.nodes)