Merge "Copy correct definition to the backup stack"
diff --git a/common/test.py b/common/test.py
index ce825c7..a7aec6c 100644
--- a/common/test.py
+++ b/common/test.py
@@ -308,7 +308,9 @@
 
     def _wait_for_stack_status(self, stack_identifier, status,
                                failure_pattern=None,
-                               success_on_not_found=False):
+                               success_on_not_found=False,
+                               signal_required=False,
+                               resources_to_signal=None):
         """Waits for a Stack to reach a given status.
 
         Note this compares the full $action_$status, e.g
@@ -340,7 +342,8 @@
                 if self._verify_status(stack, stack_identifier, status,
                                        fail_regexp):
                     return
-
+            if signal_required:
+                self.signal_resources(resources_to_signal)
             time.sleep(build_interval)
 
         message = ('Stack %s failed to reach %s status within '
@@ -489,6 +492,28 @@
         resources = self.client.resources.list(stack_identifier)
         return dict((r.resource_name, r.resource_type) for r in resources)
 
+    def get_resource_stack_id(self, r):
+        stack_link = [l for l in r.links if l.get('rel') == 'stack'][0]
+        return stack_link['href'].split("/")[-1]
+
+    def check_input_values(self, group_resources, key, value):
+        # Check inputs for deployment and derived config
+        for r in group_resources:
+            d = self.client.software_deployments.get(
+                r.physical_resource_id)
+            self.assertEqual({key: value}, d.input_values)
+            c = self.client.software_configs.get(
+                d.config_id)
+            foo_input_c = [i for i in c.inputs if i.get('name') == key][0]
+            self.assertEqual(value, foo_input_c.get('value'))
+
+    def signal_resources(self, resources):
+        # Signal all IN_PROGRESS resources
+        for r in resources:
+            if 'IN_PROGRESS' in r.resource_status:
+                stack_id = self.get_resource_stack_id(r)
+                self.client.resources.signal(stack_id, r.resource_name)
+
     def stack_create(self, stack_name=None, template=None, files=None,
                      parameters=None, environment=None, tags=None,
                      expected_status='CREATE_COMPLETE',
diff --git a/functional/test_software_deployment_group.py b/functional/test_software_deployment_group.py
new file mode 100644
index 0000000..4e8b868
--- /dev/null
+++ b/functional/test_software_deployment_group.py
@@ -0,0 +1,142 @@
+#    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_integrationtests.functional import functional_base
+
+
+class SoftwareDeploymentGroupTest(functional_base.FunctionalTestsBase):
+    sd_template = '''
+heat_template_version: 2016-10-14
+
+parameters:
+  input:
+    type: string
+    default: foo_input
+
+resources:
+  config:
+    type: OS::Heat::SoftwareConfig
+    properties:
+      group: script
+      inputs:
+      - name: foo
+
+  deployment:
+    type: OS::Heat::SoftwareDeploymentGroup
+    properties:
+      config: {get_resource: config}
+      input_values:
+        foo: {get_param: input}
+      servers:
+        '0': dummy0
+        '1': dummy1
+        '2': dummy2
+        '3': dummy3
+'''
+
+    sd_template_with_upd_policy = '''
+heat_template_version: 2016-10-14
+
+parameters:
+  input:
+    type: string
+    default: foo_input
+
+resources:
+  config:
+    type: OS::Heat::SoftwareConfig
+    properties:
+      group: script
+      inputs:
+      - name: foo
+
+  deployment:
+    type: OS::Heat::SoftwareDeploymentGroup
+    update_policy:
+      rolling_update:
+        max_batch_size: 2
+        pause_time: 1
+    properties:
+      config: {get_resource: config}
+      input_values:
+        foo: {get_param: input}
+      servers:
+        '0': dummy0
+        '1': dummy1
+        '2': dummy2
+        '3': dummy3
+'''
+    enable_cleanup = True
+
+    def deployment_crud(self, template):
+        stack_identifier = self.stack_create(
+            template=template,
+            enable_cleanup=self.enable_cleanup,
+            expected_status='CREATE_IN_PROGRESS')
+        self._wait_for_resource_status(
+            stack_identifier, 'deployment', 'CREATE_IN_PROGRESS')
+        nested_identifier = self.assert_resource_is_a_stack(
+            stack_identifier, 'deployment')
+        group_resources = self.list_group_resources(
+            stack_identifier, 'deployment', minimal=False)
+
+        self.assertEqual(4, len(group_resources))
+        self._wait_for_stack_status(stack_identifier, 'CREATE_COMPLETE',
+                                    signal_required=True,
+                                    resources_to_signal=group_resources)
+
+        self.check_input_values(group_resources, 'foo', 'foo_input')
+
+        self.update_stack(stack_identifier,
+                          template=template,
+                          environment={'parameters': {'input': 'input2'}},
+                          expected_status='UPDATE_IN_PROGRESS')
+        nested_identifier = self.assert_resource_is_a_stack(
+            stack_identifier, 'deployment')
+        self.assertEqual(4, len(group_resources))
+        self._wait_for_stack_status(stack_identifier, 'UPDATE_COMPLETE',
+                                    signal_required=True,
+                                    resources_to_signal=group_resources)
+
+        self.check_input_values(group_resources, 'foo', 'input2')
+
+        # We explicitly test delete here, vs just via cleanup and check
+        # the nested stack is gone
+        self._stack_delete(stack_identifier)
+        self._wait_for_stack_status(
+            nested_identifier, 'DELETE_COMPLETE',
+            success_on_not_found=True)
+
+    def test_deployment_crud(self):
+        self.deployment_crud(self.sd_template)
+
+    def test_deployment_crud_with_rolling_update(self):
+        self.deployment_crud(self.sd_template_with_upd_policy)
+
+    def test_deployments_create_delete_in_progress(self):
+        stack_identifier = self.stack_create(
+            template=self.sd_template,
+            enable_cleanup=self.enable_cleanup,
+            expected_status='CREATE_IN_PROGRESS')
+        self._wait_for_resource_status(
+            stack_identifier, 'deployment', 'CREATE_IN_PROGRESS')
+        nested_identifier = self.assert_resource_is_a_stack(
+            stack_identifier, 'deployment')
+        group_resources = self.list_group_resources(
+            stack_identifier, 'deployment', minimal=False)
+
+        self.assertEqual(4, len(group_resources))
+        # Now test delete while the stacks are still IN_PROGRESS
+        self._stack_delete(stack_identifier)
+        self._wait_for_stack_status(
+            nested_identifier, 'DELETE_COMPLETE',
+            success_on_not_found=True)