Functional test for asg max_size and min_size

It is vital to have functional test for max_size and min_size so that
we dont exceed quota and stay within capacity limits.

Change-Id: Ib9d8c11bcb08b591bd4c5c211218c342eef52b54
Partial-Bug: #1577090
diff --git a/functional/test_heat_autoscaling.py b/functional/test_heat_autoscaling.py
index e87badb..357612c 100644
--- a/functional/test_heat_autoscaling.py
+++ b/functional/test_heat_autoscaling.py
@@ -10,6 +10,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from heat_integrationtests.common import test
 from heat_integrationtests.functional import functional_base
 
 
@@ -21,20 +22,38 @@
   random_group:
     type: OS::Heat::AutoScalingGroup
     properties:
-      max_size: 10
-      min_size: 10
+      cooldown: 0
+      desired_capacity: 3
+      max_size: 5
+      min_size: 2
       resource:
         type: OS::Heat::RandomString
 
+  scale_up_policy:
+    type: OS::Heat::ScalingPolicy
+    properties:
+      adjustment_type: change_in_capacity
+      auto_scaling_group_id: { get_resource: random_group }
+      scaling_adjustment: 1
+
+  scale_down_policy:
+    type: OS::Heat::ScalingPolicy
+    properties:
+      adjustment_type: change_in_capacity
+      auto_scaling_group_id: { get_resource: random_group }
+      scaling_adjustment: -1
+
 outputs:
   all_values:
     value: {get_attr: [random_group, outputs_list, value]}
   value_0:
     value: {get_attr: [random_group, resource.0.value]}
-  value_5:
-    value: {get_attr: [random_group, resource.5.value]}
-  value_9:
-    value: {get_attr: [random_group, resource.9.value]}
+  value_1:
+    value: {get_attr: [random_group, resource.1.value]}
+  value_2:
+    value: {get_attr: [random_group, resource.2.value]}
+  asg_size:
+    value: {get_attr: [random_group, current_size]}
 '''
 
     template_nested = '''
@@ -44,8 +63,9 @@
   random_group:
     type: OS::Heat::AutoScalingGroup
     properties:
-      max_size: 10
-      min_size: 10
+      desired_capacity: 3
+      max_size: 5
+      min_size: 2
       resource:
         type: randomstr.yaml
 
@@ -54,10 +74,10 @@
     value: {get_attr: [random_group, outputs_list, random_str]}
   value_0:
     value: {get_attr: [random_group, resource.0.random_str]}
-  value_5:
-    value: {get_attr: [random_group, resource.5.random_str]}
-  value_9:
-    value: {get_attr: [random_group, resource.9.random_str]}
+  value_1:
+    value: {get_attr: [random_group, resource.1.random_str]}
+  value_2:
+    value: {get_attr: [random_group, resource.2.random_str]}
 '''
 
     template_randomstr = '''
@@ -78,14 +98,54 @@
     def _assert_output_values(self, stack_id):
         stack = self.client.stacks.get(stack_id)
         all_values = self._stack_output(stack, 'all_values')
-        self.assertEqual(10, len(all_values))
+        self.assertEqual(3, len(all_values))
         self.assertEqual(all_values[0], self._stack_output(stack, 'value_0'))
-        self.assertEqual(all_values[5], self._stack_output(stack, 'value_5'))
-        self.assertEqual(all_values[9], self._stack_output(stack, 'value_9'))
+        self.assertEqual(all_values[1], self._stack_output(stack, 'value_1'))
+        self.assertEqual(all_values[2], self._stack_output(stack, 'value_2'))
+
+    def test_asg_scale_up_max_size(self):
+        stack_id = self.stack_create(template=self.template,
+                                     expected_status='CREATE_COMPLETE')
+        stack = self.client.stacks.get(stack_id)
+        asg_size = self._stack_output(stack, 'asg_size')
+        # Ensure that initial desired capacity is met
+        self.assertEqual(3, asg_size)
+
+        # send scale up signals and ensure that asg honors max_size
+        asg = self.client.resources.get(stack_id, 'random_group')
+        max_size = 5
+        for num in range(asg_size+1, max_size+2):
+            expected_resources = num if num <= max_size else max_size
+            self.client.resources.signal(stack_id, 'scale_up_policy')
+            test.call_until_true(self.conf.build_timeout,
+                                 self.conf.build_interval,
+                                 self.check_autoscale_complete,
+                                 asg.physical_resource_id, expected_resources)
+
+    def test_asg_scale_down_min_size(self):
+        stack_id = self.stack_create(template=self.template,
+                                     expected_status='CREATE_COMPLETE')
+        stack = self.client.stacks.get(stack_id)
+        asg_size = self._stack_output(stack, 'asg_size')
+        # Ensure that initial desired capacity is met
+        self.assertEqual(3, asg_size)
+
+        # send scale down signals and ensure that asg honors min_size
+        asg = self.client.resources.get(stack_id, 'random_group')
+        min_size = 2
+        for num in range(asg_size-1, 0, -1):
+            expected_resources = num if num >= min_size else min_size
+            self.client.resources.signal(stack_id, 'scale_down_policy')
+            test.call_until_true(self.conf.build_timeout,
+                                 self.conf.build_interval,
+                                 self.check_autoscale_complete,
+                                 asg.physical_resource_id, expected_resources)
 
     def test_path_attrs(self):
         stack_id = self.stack_create(template=self.template)
-        expected_resources = {'random_group': 'OS::Heat::AutoScalingGroup'}
+        expected_resources = {'random_group': 'OS::Heat::AutoScalingGroup',
+                              'scale_up_policy': 'OS::Heat::ScalingPolicy',
+                              'scale_down_policy': 'OS::Heat::ScalingPolicy'}
         self.assertEqual(expected_resources, self.list_resources(stack_id))
         self._assert_output_values(stack_id)