Unit test autoscaling._calculate_new_capacity

This is so we can reduce the complexity in test_autoscaling.py

Change-Id: I96050295f48109bd370168f0a5c66e93e506da3e
part of blueprint decouple-nested
This commit is contained in:
Angus Salkeld 2014-09-18 10:37:57 +10:00
parent 2da1288623
commit 499e9d3d84
2 changed files with 78 additions and 199 deletions

View File

@ -0,0 +1,78 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from heat.engine.resources import autoscaling as asc
from heat.tests import common
class TestCapacityChanges(common.HeatTestCase):
# below:
# n CHANGE_IN_CAPACITY (+up, -down)
# b bounded
# r rounded (+up, -down)
# e EXACT_CAPACITY
# p PERCENT_CHANGE_IN_CAPACITY
scenarios = [
('+n', dict(current=2, adjustment=3,
adjustment_type=asc.CHANGE_IN_CAPACITY,
minimum=0, maximum=10, expected=5)),
('-n', dict(current=6, adjustment=-2,
adjustment_type=asc.CHANGE_IN_CAPACITY,
minimum=0, maximum=5, expected=4)),
('+nb', dict(current=2, adjustment=8,
adjustment_type=asc.CHANGE_IN_CAPACITY,
minimum=0, maximum=5, expected=5)),
('-nb', dict(current=2, adjustment=-10,
adjustment_type=asc.CHANGE_IN_CAPACITY,
minimum=1, maximum=5, expected=1)),
('e', dict(current=2, adjustment=4,
adjustment_type=asc.EXACT_CAPACITY,
minimum=0, maximum=5, expected=4)),
('+eb', dict(current=2, adjustment=11,
adjustment_type=asc.EXACT_CAPACITY,
minimum=0, maximum=5, expected=5)),
('-eb', dict(current=4, adjustment=1,
adjustment_type=asc.EXACT_CAPACITY,
minimum=3, maximum=5, expected=3)),
('+p', dict(current=4, adjustment=50,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=1, maximum=10, expected=6)),
('-p', dict(current=4, adjustment=-25,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=1, maximum=10, expected=3)),
('+pb', dict(current=4, adjustment=100,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=1, maximum=6, expected=6)),
('-pb', dict(current=6, adjustment=-50,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=4, maximum=10, expected=4)),
('-p+r', dict(current=2, adjustment=-33,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=0, maximum=10, expected=1)),
('+p+r', dict(current=1, adjustment=33,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=0, maximum=10, expected=2)),
('-p-r', dict(current=2, adjustment=-66,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=0, maximum=10, expected=1)),
('+p-r', dict(current=1, adjustment=225,
adjustment_type=asc.PERCENT_CHANGE_IN_CAPACITY,
minimum=0, maximum=10, expected=3)),
]
def test_calc(self):
self.assertEqual(self.expected,
asc._calculate_new_capacity(
self.current, self.adjustment,
self.adjustment_type,
self.minimum, self.maximum))

View File

@ -878,55 +878,6 @@ class AutoScalingTest(common.HeatTestCase):
self.m.VerifyAll() self.m.VerifyAll()
def test_scaling_group_adjust(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params)
# start with 3
properties = t['Resources']['WebServerGroup']['Properties']
properties['DesiredCapacity'] = '3'
self._stub_lb_reload(3)
now = timeutils.utcnow()
self._stub_meta_expected(now, 'ExactCapacity : 3')
self._stub_create(3)
self.m.ReplayAll()
rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
self.assertEqual(3, len(rsrc.get_instance_names()))
# reduce to 1
self._stub_lb_reload(1)
self._stub_delete(2)
self.stub_ImageConstraint_validate(num=1)
self._stub_meta_expected(now, 'ChangeInCapacity : -2')
self._stub_scale_notification(adjust=-2, groupname=rsrc.FnGetRefId(),
start_capacity=3, end_capacity=1)
self.m.ReplayAll()
rsrc.adjust(-2)
self.assertEqual(1, len(rsrc.get_instance_names()))
# raise to 3
self._stub_lb_reload(3)
self._stub_meta_expected(now, 'ChangeInCapacity : 2')
self._stub_create(2)
self._stub_scale_notification(adjust=2, groupname=rsrc.FnGetRefId(),
start_capacity=1, end_capacity=3)
self.m.ReplayAll()
rsrc.adjust(2)
self.assertEqual(3, len(rsrc.get_instance_names()))
# set to 2
self._stub_lb_reload(2)
self._stub_delete(1)
self.stub_ImageConstraint_validate(num=2)
self._stub_meta_expected(now, 'ExactCapacity : 2')
self._stub_scale_notification(adjust=2, groupname=rsrc.FnGetRefId(),
adjust_type='ExactCapacity',
start_capacity=3, end_capacity=2)
self.m.ReplayAll()
rsrc.adjust(2, 'ExactCapacity')
self.assertEqual(2, len(rsrc.get_instance_names()))
self.m.VerifyAll()
def test_scaling_group_scale_up_failure(self): def test_scaling_group_scale_up_failure(self):
t = template_format.parse(as_template) t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params) stack = utils.parse_stack(t, params=self.params)
@ -956,93 +907,6 @@ class AutoScalingTest(common.HeatTestCase):
self.m.VerifyAll() self.m.VerifyAll()
def test_scaling_group_truncate_adjustment(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params)
# Create initial group, 2 instances
properties = t['Resources']['WebServerGroup']['Properties']
properties['DesiredCapacity'] = '2'
self._stub_lb_reload(2)
now = timeutils.utcnow()
self._stub_meta_expected(now, 'ExactCapacity : 2')
self._stub_create(2)
self.m.ReplayAll()
rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
stack.resources['WebServerGroup'] = rsrc
self.assertEqual(2, len(rsrc.get_instance_names()))
# raise above the max
self._stub_lb_reload(5)
self._stub_meta_expected(now, 'ChangeInCapacity : 4')
self._stub_create(3)
self.m.ReplayAll()
rsrc.adjust(4)
self.assertEqual(5, len(rsrc.get_instance_names()))
# lower below the min
self._stub_lb_reload(1)
self._stub_delete(4)
self.stub_ImageConstraint_validate(num=1)
self._stub_meta_expected(now, 'ChangeInCapacity : -5')
self.m.ReplayAll()
rsrc.adjust(-5)
self.assertEqual(1, len(rsrc.get_instance_names()))
# no change
rsrc.adjust(0)
self.assertEqual(1, len(rsrc.get_instance_names()))
rsrc.delete()
self.m.VerifyAll()
def _do_test_scaling_group_percent(self, decrease, lowest,
increase, create, highest):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params)
# Create initial group, 2 instances
properties = t['Resources']['WebServerGroup']['Properties']
properties['DesiredCapacity'] = '2'
self._stub_lb_reload(2)
self._stub_create(2)
now = timeutils.utcnow()
self._stub_meta_expected(now, 'ExactCapacity : 2')
self.m.ReplayAll()
rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
stack.resources['WebServerGroup'] = rsrc
self.assertEqual(2, len(rsrc.get_instance_names()))
# reduce by decrease %
self._stub_lb_reload(lowest)
adjust = 'PercentChangeInCapacity : %d' % decrease
self._stub_meta_expected(now, adjust)
self._stub_delete(2 - lowest)
self.stub_ImageConstraint_validate(num=1)
self.m.ReplayAll()
rsrc.adjust(decrease, 'PercentChangeInCapacity')
self.assertEqual(lowest, len(rsrc.get_instance_names()))
# raise by increase %
self._stub_lb_reload(highest)
adjust = 'PercentChangeInCapacity : %d' % increase
self._stub_meta_expected(now, adjust)
self._stub_create(create)
self.m.ReplayAll()
rsrc.adjust(increase, 'PercentChangeInCapacity')
self.assertEqual(highest, len(rsrc.get_instance_names()))
rsrc.delete()
def test_scaling_group_percent(self):
self._do_test_scaling_group_percent(-50, 1, 200, 2, 3)
def test_scaling_group_percent_round_up(self):
self._do_test_scaling_group_percent(-33, 1, 33, 1, 2)
def test_scaling_group_percent_round_down(self):
self._do_test_scaling_group_percent(-66, 1, 225, 2, 3)
def test_scaling_group_cooldown_toosoon(self): def test_scaling_group_cooldown_toosoon(self):
t = template_format.parse(as_template) t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params) stack = utils.parse_stack(t, params=self.params)
@ -1243,38 +1107,6 @@ class AutoScalingTest(common.HeatTestCase):
self.m.VerifyAll() self.m.VerifyAll()
def test_scaling_policy_up(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params)
# Create initial group
self._stub_lb_reload(1)
now = timeutils.utcnow()
self._stub_meta_expected(now, 'ExactCapacity : 1')
self._stub_create(1)
self.m.ReplayAll()
rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
stack.resources['WebServerGroup'] = rsrc
self.assertEqual(1, len(rsrc.get_instance_names()))
# Scale up one
self._stub_lb_reload(2)
self._stub_meta_expected(now, 'ChangeInCapacity : 1', 2)
self._stub_create(1)
self.m.ReplayAll()
up_policy = self.create_scaling_policy(t, stack,
'WebServerScaleUpPolicy')
alarm_url = up_policy.FnGetAtt('AlarmUrl')
self.assertIsNotNone(alarm_url)
up_policy.signal()
self.assertEqual(2, len(rsrc.get_instance_names()))
rsrc.delete()
self.m.VerifyAll()
def test_scaling_up_meta_update(self): def test_scaling_up_meta_update(self):
t = template_format.parse(as_template) t = template_format.parse(as_template)
@ -1326,37 +1158,6 @@ class AutoScalingTest(common.HeatTestCase):
rsrc.delete() rsrc.delete()
self.m.VerifyAll() self.m.VerifyAll()
def test_scaling_policy_down(self):
t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params)
# Create initial group, 2 instances
properties = t['Resources']['WebServerGroup']['Properties']
properties['DesiredCapacity'] = '2'
self._stub_lb_reload(2)
now = timeutils.utcnow()
self._stub_meta_expected(now, 'ExactCapacity : 2')
self._stub_create(2)
self.m.ReplayAll()
rsrc = self.create_scaling_group(t, stack, 'WebServerGroup')
stack.resources['WebServerGroup'] = rsrc
self.assertEqual(2, len(rsrc.get_instance_names()))
# Scale down one
self._stub_lb_reload(1)
self._stub_delete(1)
self.stub_ImageConstraint_validate(num=1)
self._stub_meta_expected(now, 'ChangeInCapacity : -1', 2)
self.m.ReplayAll()
down_policy = self.create_scaling_policy(t, stack,
'WebServerScaleDownPolicy')
down_policy.signal()
self.assertEqual(1, len(rsrc.get_instance_names()))
rsrc.delete()
self.m.VerifyAll()
def test_scaling_policy_cooldown_toosoon(self): def test_scaling_policy_cooldown_toosoon(self):
t = template_format.parse(as_template) t = template_format.parse(as_template)
stack = utils.parse_stack(t, params=self.params) stack = utils.parse_stack(t, params=self.params)