def test_calculate_adjustment_count(self): adjustment = self.spec['properties']['adjustment'] # adjustment_type as EXACT_CAPACITY and event as cluster_scale_in current_size = 3 adjustment['type'] = consts.EXACT_CAPACITY adjustment['number'] = 1 policy = sp.ScalingPolicy('test-policy', self.spec) policy.event = consts.CLUSTER_SCALE_IN count = policy._calculate_adjustment_count(current_size) self.assertEqual(2, count) # adjustment_type as EXACT_CAPACITY and event as cluster_scale_out current_size = 3 adjustment['type'] = consts.EXACT_CAPACITY adjustment['number'] = 1 policy = sp.ScalingPolicy('test-policy', self.spec) policy.event = consts.CLUSTER_SCALE_OUT count = policy._calculate_adjustment_count(current_size) self.assertEqual(-2, count) # adjustment_type is CHANGE_IN_CAPACITY adjustment['type'] = consts.CHANGE_IN_CAPACITY adjustment['number'] = 1 policy = sp.ScalingPolicy('test-policy', self.spec) count = policy._calculate_adjustment_count(current_size) self.assertEqual(1, count) # adjustment_type is CHANGE_IN_PERCENTAGE current_size = 10 adjustment['type'] = consts.CHANGE_IN_PERCENTAGE adjustment['number'] = 50 policy = sp.ScalingPolicy('test-policy', self.spec) count = policy._calculate_adjustment_count(current_size) self.assertEqual(5, count) # adjustment_type is CHANGE_IN_PERCENTAGE and min_step is 2 adjustment['type'] = consts.CHANGE_IN_PERCENTAGE adjustment['number'] = 1 adjustment['min_step'] = 2 policy = sp.ScalingPolicy('test-policy', self.spec) count = policy._calculate_adjustment_count(current_size) self.assertEqual(2, count)
def test_policy_init_default_value(self): self.spec['properties']['adjustment'] = {} policy = sp.ScalingPolicy('p1', self.spec) self.assertIsNone(policy.id) self.assertEqual('senlin.policy.scaling-1.0', policy.type) self.assertEqual('p1', policy.name) self.assertEqual(consts.CHANGE_IN_CAPACITY, policy.adjustment_type) self.assertEqual(1, policy.adjustment_number) self.assertEqual(1, policy.adjustment_min_step) self.assertEqual(False, policy.best_effort)
def test_policy_init(self): policy = sp.ScalingPolicy('p1', self.spec) self.assertEqual(False, policy.singleton) self.assertIsNone(policy.id) self.assertEqual('p1', policy.name) self.assertEqual('senlin.policy.scaling-1.0', policy.type) self.assertEqual('CLUSTER_SCALE_IN', policy.event) adjustment = self.spec['properties']['adjustment'] self.assertEqual(adjustment['type'], policy.adjustment_type) self.assertEqual(adjustment['number'], policy.adjustment_number) self.assertEqual(adjustment['min_step'], policy.adjustment_min_step) self.assertEqual(adjustment['best_effort'], policy.best_effort)
def test_pre_op_action_not_in_event(self): action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_OUT action.data = {} policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) expected = { 'reason': 'Scaling request validated.', 'status': policy_base.CHECK_OK, } self.assertEqual(expected, action.data) action.store.assert_called_with(self.context)
def test_post_op(self, mock_time, mock_cluster_policy): action = mock.Mock() action.context = self.context mock_time.return_value = 'FAKE_TIME' kwargs = {'id': 'FAKE_POLICY_ID'} policy = sp.ScalingPolicy('test-policy', self.spec, **kwargs) policy.post_op('FAKE_CLUSTER_ID', action) mock_cluster_policy.assert_called_once_with(action.context, 'FAKE_CLUSTER_ID', 'FAKE_POLICY_ID', {'last_op': 'FAKE_TIME'})
def test_pre_op_fail_negative_count(self): action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_IN action.inputs = {} adjustment = self.spec['properties']['adjustment'] adjustment['type'] = consts.EXACT_CAPACITY adjustment['number'] = 5 policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) pd = { 'status': pb.CHECK_ERROR, 'reason': "Invalid count (-2) for action 'CLUSTER_SCALE_IN'.", } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_fail_below_min_size(self): action = mock.Mock() action.action = consts.CLUSTER_SCALE_IN action.context = self.context action.inputs = {} adjustment = self.spec['properties']['adjustment'] adjustment['type'] = consts.CHANGE_IN_CAPACITY adjustment['number'] = 3 policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) reason = _('Attempted scaling below minimum size.') pd = { 'reason': reason, 'status': policy_base.CHECK_ERROR, } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_within_cooldown(self): action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_IN action.inputs = {'last_op': timeutils.utcnow(True)} action.entity = self.cluster adjustment = self.spec['properties']['adjustment'] adjustment['cooldown'] = 300 kwargs = {'id': "FAKE_ID"} policy = sp.ScalingPolicy('p1', self.spec, **kwargs) policy.pre_op('FAKE_CLUSTER_ID', action) pd = { 'status': pb.CHECK_ERROR, 'reason': "Policy FAKE_ID cooldown is still in progress.", } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_fail_below_min_size(self): self._create_nodes(3) action = mock.Mock() action.action = consts.CLUSTER_SCALE_IN action.context = self.context action.inputs = {} adjustment = self.spec['properties']['adjustment'] adjustment['type'] = consts.CHANGE_IN_CAPACITY adjustment['number'] = 3 policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) pd = { 'status': pb.CHECK_ERROR, 'reason': ("The target capacity (0) is less than the cluster's " "min_size (1)."), } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_pass_without_input(self): action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_IN action.inputs = {} adjustment = self.spec['properties']['adjustment'] adjustment['type'] = consts.EXACT_CAPACITY adjustment['number'] = 1 policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) pd = { 'deletion': { 'count': 2, }, 'reason': 'Scaling request validated.', 'status': pb.CHECK_OK, } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_pass_with_input(self): nodes = self._create_nodes(3) self.cluster.nodes = nodes action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_IN action.inputs = {'count': 1, 'last_op': timeutils.utcnow(True)} action.entity = self.cluster adjustment = self.spec['properties']['adjustment'] adjustment['type'] = consts.CHANGE_IN_CAPACITY adjustment['number'] = 2 adjustment['cooldown'] = 1 policy = sp.ScalingPolicy('p1', self.spec) time.sleep(1) policy.pre_op(self.cluster['id'], action) pd = { 'deletion': { 'count': 1, }, 'reason': 'Scaling request validated.', 'status': pb.CHECK_OK, } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context) # count value is string rather than integer action.inputs = {'count': '1'} policy.pre_op(self.cluster['id'], action) pd = { 'deletion': { 'count': 1, }, 'reason': 'Scaling request validated.', 'status': pb.CHECK_OK, } action.data.update.assert_called_with(pd)
def test_pre_op_pass_check_effort(self, mock_currentcount, mock_adjustmentcount): # Cluster with maxsize and best_effert is False action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_OUT action.inputs = {} mock_adjustmentcount.return_value = 1 mock_currentcount.return_value = 2 policy = sp.ScalingPolicy('test-policy', self.spec) policy.event = consts.CLUSTER_SCALE_OUT policy.best_effort = True policy.pre_op(self.cluster_no_maxsize['id'], action) pd = { 'creation': { 'count': 1, }, 'reason': 'Scaling request validated.', 'status': pb.CHECK_OK, } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_pre_op_pass_best_effort(self): action = mock.Mock() action.context = self.context action.action = consts.CLUSTER_SCALE_IN action.inputs = {} adjustment = self.spec['properties']['adjustment'] adjustment['best_effort'] = True adjustment['type'] = consts.CHANGE_IN_CAPACITY adjustment['number'] = 3 policy = sp.ScalingPolicy('test-policy', self.spec) policy.pre_op(self.cluster['id'], action) reason = _('Do best effort scaling.') pd = { 'deletion': { 'count': 2, }, 'reason': reason, 'status': policy_base.CHECK_OK, } action.data.update.assert_called_with(pd) action.store.assert_called_with(self.context)
def test_validate(self): self.spec['properties']['adjustment'] = {} policy = sp.ScalingPolicy('p1', self.spec) policy.validate(self.context)