def test_pre_op_no_feasible_plan(self, mock_load): # test pre_op method whether returns the correct action.data policy = rp.RegionPlacementPolicy('p1', self.spec) regions = policy.regions kc = mock.Mock() kc.validate_regions.return_value = regions.keys() policy._keystoneclient = kc self.patchobject(policy, '_create_plan', return_value=None) action = mock.Mock() action.action = 'CLUSTER_SCALE_OUT' action.context = self.context action.inputs = {} action.data = {'creation': {'count': 3}} cluster = mock.Mock() current_dist = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0} cluster.get_region_distribution.return_value = current_dist mock_load.return_value = cluster res = policy.pre_op('FAKE_CLUSTER', action) self.assertIsNone(res) self.assertEqual('ERROR', action.data['status']) self.assertEqual('There is no feasible plan to handle all nodes.', action.data['reason']) mock_load.assert_called_once_with(action.context, 'FAKE_CLUSTER') kc.validate_regions.assert_called_once_with(regions.keys()) cluster.get_region_distribution.assert_called_once_with(regions.keys()) policy._create_plan.assert_called_once_with(current_dist, regions, 3, True)
def test_pre_op_count_from_inputs(self, mock_load): # test pre_op method whether returns the correct action.data policy = rp.RegionPlacementPolicy('p1', self.spec) regions = policy.regions kc = mock.Mock() kc.validate_regions.return_value = regions.keys() policy._keystoneclient = kc cluster = mock.Mock() current_dist = {'R1': 0, 'R2': 0, 'R3': 0, 'R4': 0} cluster.get_region_distribution.return_value = current_dist mock_load.return_value = cluster plan = {'R1': 1, 'R3': 2} self.patchobject(policy, '_create_plan', return_value=plan) action = mock.Mock() action.context = self.context action.action = 'CLUSTER_SCALE_OUT' action.inputs = {'count': 3} action.data = {} res = policy.pre_op('FAKE_CLUSTER', action) self.assertIsNone(res) self.assertEqual(3, action.data['creation']['count']) dist = action.data['creation']['regions'] self.assertEqual(2, len(dist)) self.assertEqual(1, dist['R1']) self.assertEqual(2, dist['R3'])
def test_policy_init(self): policy = rp.RegionPlacementPolicy('test-policy', self.spec) self.assertIsNone(policy.id) self.assertIsNone(policy._keystoneclient) self.assertEqual('test-policy', policy.name) self.assertEqual('senlin.policy.region_placement-1.0', policy.type) expected = { 'R1': { 'weight': 100, 'cap': 50 }, 'R2': { 'weight': 50, 'cap': 50, }, 'R3': { 'weight': 30, 'cap': -1, }, 'R4': { 'weight': 20, 'cap': -1, } } self.assertEqual(expected, policy.regions)
def test_get_count_scale_in_with_inputs(self): action = mock.Mock(action=consts.CLUSTER_SCALE_IN, data={}, inputs={'count': 3}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(-3, res)
def test__get_count_scale_out_with_incorrect_inputs(self): action = mock.Mock(action=consts.CLUSTER_SCALE_OUT, data={}, inputs={'num': 3}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(1, res)
def test_validate_no_validate_props(self, mock_base_validate): policy = rp.RegionPlacementPolicy('test-policy', self.spec) ctx = mock.Mock(user='******', project='P1') res = policy.validate(ctx, False) self.assertTrue(res) mock_base_validate.assert_called_once_with(ctx, False)
def test__get_count_node_create_no_region(self): x_profile = mock.Mock(CONTEXT='context', properties={'context': {}}) x_node = mock.Mock(rt={'profile': x_profile}) action = mock.Mock(action=consts.NODE_CREATE, node=x_node) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(1, res)
def test__get_count_scale_out_with_data(self): action = mock.Mock(action=consts.CLUSTER_SCALE_OUT, data={'creation': { 'count': 3 }}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(3, res)
def test__get_count_resize_creation(self): action = mock.Mock(action=consts.CLUSTER_RESIZE, data={'creation': { 'count': 3 }}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(3, res)
def test__get_count_scale_in_with_no_data(self): action = mock.Mock(action=consts.CLUSTER_SCALE_IN, data={'deletion': { 'num': 3 }}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(-1, res)
def test__get_count_resize_parse_error(self, mock_cluster, mock_parse): x_cluster = mock.Mock() mock_cluster.return_value = x_cluster mock_parse.return_value = (pb.CHECK_ERROR, 'Something wrong.') action = mock.Mock(action=consts.CLUSTER_RESIZE, data={}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(0, res) self.assertEqual(pb.CHECK_ERROR, action.data['status']) self.assertEqual('Something wrong.', action.data['reason'])
def test_get_count_node_create_region_specified(self): x_profile = mock.Mock(CONTEXT='context', properties={'context': { 'region_name': 'foo' }}) x_node = mock.Mock(rt={'profile': x_profile}) action = mock.Mock(action=consts.NODE_CREATE, entity=x_node) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(0, res)
def test_validate_okay(self, mock_base_validate): policy = rp.RegionPlacementPolicy('test-policy', self.spec) kc = mock.Mock() kc.validate_regions.return_value = ['R1', 'R2', 'R3', 'R4'] policy._keystoneclient = kc ctx = mock.Mock(user='******', project='P1') res = policy.validate(ctx, True) self.assertTrue(res) mock_base_validate.assert_called_once_with(ctx, True) kc.validate_regions.assert_called_once_with(['R1', 'R2', 'R3', 'R4'])
def test__get_count_resize_parse_deletion(self, mock_cluster, mock_parse): def fake_parse(action, cluster): action.data = {'deletion': {'count': 3}} return pb.CHECK_OK, '' x_cluster = mock.Mock() mock_cluster.return_value = x_cluster mock_parse.side_effect = fake_parse action = mock.Mock(action=consts.CLUSTER_RESIZE, data={}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(-3, res)
def test_validate_region_not_found(self, mock_base_validate): policy = rp.RegionPlacementPolicy('test-policy', self.spec) kc = mock.Mock() kc.validate_regions.return_value = ['R2', 'R4'] policy._keystoneclient = kc ctx = mock.Mock(user='******', project='P1') ex = self.assertRaises(exc.InvalidSpec, policy.validate, ctx, True) mock_base_validate.assert_called_once_with(ctx, True) kc.validate_regions.assert_called_once_with(['R1', 'R2', 'R3', 'R4']) self.assertEqual( "The specified regions '['R1', 'R3']' could not " "be found.", six.text_type(ex))
def test__get_count_resize_parse_deletion(self, mock_parse): def fake_parse(action, cluster, current): action.data = {'deletion': {'count': 3}} return pb.CHECK_OK, '' x_cluster = mock.Mock() x_cluster.nodes = [mock.Mock(), mock.Mock(), mock.Mock()] action = mock.Mock(action=consts.CLUSTER_RESIZE, data={}) action.entity = x_cluster mock_parse.side_effect = fake_parse policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(-3, res) mock_parse.assert_called_once_with(action, x_cluster, 3)
def test__keystone(self, mock_sd, mock_conn): params = mock.Mock() mock_conn.return_value = params kc = mock.Mock() driver = mock.Mock() driver.identity.return_value = kc mock_sd.return_value = driver cluster = mock.Mock() policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._keystone(cluster) self.assertEqual(kc, res) self.assertEqual(kc, policy._keystoneclient) mock_conn.assert_called_once_with(cluster) mock_sd.assert_called_once_with() driver.identity.assert_called_once_with(params)
def test__get_count_resize_parse_creation(self, mock_cluster, mock_parse, mock_count): def fake_parse(action, cluster, current): action.data = {'creation': {'count': 3}} return pb.CHECK_OK, '' x_cluster = mock.Mock() mock_cluster.return_value = x_cluster mock_count.return_value = 0 mock_parse.side_effect = fake_parse action = mock.Mock(action=consts.CLUSTER_RESIZE, data={}) policy = rp.RegionPlacementPolicy('p1', self.spec) res = policy._get_count('FOO', action) self.assertEqual(3, res) mock_count.assert_called_once_with(action.context, 'FOO') mock_parse.assert_called_once_with(action, x_cluster, 0) mock_cluster.assert_called_once_with(action.context, 'FOO')
def test_pre_op_no_regions(self, mock_load): # test pre_op method whether returns the correct action.data policy = rp.RegionPlacementPolicy('p1', self.spec) kc = mock.Mock() kc.validate_regions.return_value = [] policy._keystoneclient = kc action = mock.Mock() action.action = 'CLUSTER_SCALE_OUT' action.context = self.context action.data = {'creation': {'count': 3}} cluster = mock.Mock() mock_load.return_value = cluster res = policy.pre_op('FAKE_CLUSTER', action) self.assertIsNone(res) self.assertEqual('ERROR', action.data['status']) self.assertEqual('No region is found usable.', action.data['reason'])
def test__create_plan(self): policy = rp.RegionPlacementPolicy('p1', self.spec) regions = policy.regions current = {'R1': 2, 'R2': 2, 'R3': 2, 'R4': 1} result = policy._create_plan(current, regions, 5, True) expected = {'R1': 4, 'R2': 1} self.assertEqual(expected, result) current = {'R1': 2, 'R2': 2, 'R3': 0, 'R4': 1} plan = policy._create_plan(current, regions, 5, True) answer = {'R1': 3, 'R2': 1, 'R3': 1} self.assertEqual(answer, plan) current = {'R1': 2, 'R2': 2, 'R3': 0, 'R4': 1} plan = policy._create_plan(current, regions, 3, False) answer = {'R2': 2, 'R4': 1} self.assertEqual(answer, plan) current = {'R1': 4, 'R2': 2, 'R3': 1, 'R4': 1} plan = policy._create_plan(current, regions, 3, False) answer = {'R2': 1, 'R3': 1, 'R4': 1} self.assertEqual(answer, plan)