From e1d75b4a7ca88e7b92592db9864e6720ac649452 Mon Sep 17 00:00:00 2001 From: Zhang Yang Date: Fri, 17 Jan 2014 19:07:18 -0800 Subject: [PATCH] add the validation of MaxSize ,MinSize and DesiredCapacity * check whether MinSize is less than MaxSize * check whether MinSize and MaxSize is not less than 0 * if DesiredCapacity is offered,check whether it is between MinSize and MaxSize Change-Id: I79a7b6c0e61f109ac7e71d3fd48d93fb741bbc64 Closes-Bug: #1270070 --- heat/engine/resources/autoscaling.py | 18 +++++++++++ heat/tests/test_autoscaling.py | 46 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/heat/engine/resources/autoscaling.py b/heat/engine/resources/autoscaling.py index 7ba085045..91c8ba7ce 100644 --- a/heat/engine/resources/autoscaling.py +++ b/heat/engine/resources/autoscaling.py @@ -650,6 +650,24 @@ class AutoScalingGroup(InstanceGroup, CooldownMixin): if res: return res + # check validity of group size + min_size = int(self.properties[self.MIN_SIZE]) + max_size = int(self.properties[self.MAX_SIZE]) + + if max_size < min_size: + msg = _("MinSize can not be greater than MaxSize") + raise exception.StackValidationFailed(message=msg) + + if min_size < 0: + msg = _("The size of AutoScalingGroup can not be less than zero") + raise exception.StackValidationFailed(message=msg) + + if self.properties[self.DESIRED_CAPACITY]: + desired_capacity = int(self.properties[self.DESIRED_CAPACITY]) + if desired_capacity < min_size or desired_capacity > max_size: + msg = _("DesiredCapacity must be between MinSize and MaxSize") + raise exception.StackValidationFailed(message=msg) + # TODO(pasquier-s): once Neutron is able to assign subnets to # availability zones, it will be possible to specify multiple subnets. # For now, only one subnet can be specified. The bug #1096017 tracks diff --git a/heat/tests/test_autoscaling.py b/heat/tests/test_autoscaling.py index 38edbf3da..c51df9ff9 100644 --- a/heat/tests/test_autoscaling.py +++ b/heat/tests/test_autoscaling.py @@ -1526,3 +1526,49 @@ class AutoScalingTest(HeatTestCase): self.assertRaises(exception.NotSupported, self.create_scaling_group, t, stack, 'WebServerGroup') + + def test_invalid_min_size(self): + t = template_format.parse(as_template) + properties = t['Resources']['WebServerGroup']['Properties'] + properties['MinSize'] = '-1' + properties['MaxSize'] = '2' + + stack = utils.parse_stack(t, params=self.params) + + e = self.assertRaises(exception.StackValidationFailed, + self.create_scaling_group, t, + stack, 'WebServerGroup') + + expected_msg = "The size of AutoScalingGroup can not be less than zero" + self.assertEqual(expected_msg, str(e)) + + def test_invalid_max_size(self): + t = template_format.parse(as_template) + properties = t['Resources']['WebServerGroup']['Properties'] + properties['MinSize'] = '3' + properties['MaxSize'] = '1' + + stack = utils.parse_stack(t, params=self.params) + + e = self.assertRaises(exception.StackValidationFailed, + self.create_scaling_group, t, + stack, 'WebServerGroup') + + expected_msg = "MinSize can not be greater than MaxSize" + self.assertEqual(expected_msg, str(e)) + + def test_invalid_desiredcapacity(self): + t = template_format.parse(as_template) + properties = t['Resources']['WebServerGroup']['Properties'] + properties['MinSize'] = '1' + properties['MaxSize'] = '3' + properties['DesiredCapacity'] = '4' + + stack = utils.parse_stack(t, params=self.params) + + e = self.assertRaises(exception.StackValidationFailed, + self.create_scaling_group, t, + stack, 'WebServerGroup') + + expected_msg = "DesiredCapacity must be between MinSize and MaxSize" + self.assertEqual(expected_msg, str(e))