Merge "Implement handle_<action>_cancel for SoftwareDeployment"
diff --git a/common/test.py b/common/test.py
index 0eedbe3..d43dece 100644
--- a/common/test.py
+++ b/common/test.py
@@ -529,6 +529,20 @@
         stack_link = [l for l in r.links if l.get('rel') == 'stack'][0]
         return stack_link['href'].split("/")[-1]
 
+    def get_physical_resource_id(self, stack_identifier, resource_name):
+        try:
+            resource = self.client.resources.get(
+                stack_identifier, resource_name)
+            return resource.physical_resource_id
+        except Exception:
+            raise Exception('Resource (%s) not found in stack (%s)!' %
+                            (stack_identifier, resource_name))
+
+    def get_stack_output(self, stack_identifier, output_key,
+                         validate_errors=True):
+        stack = self.client.stacks.get(stack_identifier)
+        return self._stack_output(stack, output_key, validate_errors)
+
     def check_input_values(self, group_resources, key, value):
         # Check inputs for deployment and derived config
         for r in group_resources:
diff --git a/functional/test_create_update_neutron_trunk.py b/functional/test_create_update_neutron_trunk.py
new file mode 100644
index 0000000..b5a108a
--- /dev/null
+++ b/functional/test_create_update_neutron_trunk.py
@@ -0,0 +1,275 @@
+# Copyright (c) 2017 Ericsson.
+#
+#    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.
+
+import copy
+import yaml
+
+from heat_integrationtests.functional import functional_base
+
+
+test_template = '''
+heat_template_version: pike
+description: Test template to create, update, delete trunk.
+resources:
+  parent_net:
+    type: OS::Neutron::Net
+  trunk_net_one:
+    type: OS::Neutron::Net
+  trunk_net_two:
+    type: OS::Neutron::Net
+  parent_subnet:
+    type: OS::Neutron::Subnet
+    properties:
+      network: { get_resource: parent_net }
+      cidr: 10.0.0.0/16
+  trunk_subnet_one:
+    type: OS::Neutron::Subnet
+    properties:
+      network: { get_resource: trunk_net_one }
+      cidr: 10.10.0.0/16
+  trunk_subnet_two:
+    type: OS::Neutron::Subnet
+    properties:
+      network: { get_resource: trunk_net_two }
+      cidr: 10.20.0.0/16
+  parent_port:
+    type: OS::Neutron::Port
+    properties:
+      network: { get_resource: parent_net }
+      name: trunk_parent_port
+  sub_port_one:
+    type: OS::Neutron::Port
+    properties:
+      network: { get_resource: trunk_net_one }
+      name: trunk_sub_port_one
+  sub_port_two:
+    type: OS::Neutron::Port
+    properties:
+      network: { get_resource: trunk_net_two }
+      name: trunk_sub_port_two
+  trunk:
+    type: OS::Neutron::Trunk
+    properties:
+      name: test_trunk
+      port: { get_resource: parent_port }
+      sub_ports:
+outputs:
+  trunk_parent_port:
+    value: { get_attr: [trunk, port_id] }
+'''
+
+
+class UpdateTrunkTest(functional_base.FunctionalTestsBase):
+
+    @staticmethod
+    def _sub_ports_dict_to_set(sub_ports):
+        new_sub_ports = copy.deepcopy(sub_ports)
+
+        # NOTE(lajos katona): In the template we have to give the sub port as
+        # port, but from trunk_details we receive back them with port_id.
+        # As an extra trunk_details contains the mac_address as well which is
+        # useless here.
+        # So here we have to make sure that the dictionary (input from
+        # template or output from trunk_details) have the same keys:
+        if any('mac_address' in d for d in new_sub_ports):
+            for sp in new_sub_ports:
+                sp['port'] = sp['port_id']
+                del sp['port_id']
+                del sp['mac_address']
+
+        # NOTE(lajos katona): We receive lists (trunk_details['sub_ports'] and
+        # the input to the template) and we can't be sure that the order is the
+        # same, so by using sets we can compare them.
+        sub_ports_set = {frozenset(d.items()) for d in new_sub_ports}
+        return sub_ports_set
+
+    def test_add_first_sub_port(self):
+        stack_identifier = self.stack_create(template=test_template)
+
+        parsed_template = yaml.safe_load(test_template)
+        new_sub_port = [{'port': {'get_resource': 'sub_port_one'},
+                         'segmentation_id': 10,
+                         'segmentation_type': 'vlan'}]
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'] = new_sub_port
+        updated_template = yaml.safe_dump(parsed_template)
+        self.update_stack(stack_identifier, updated_template)
+
+        # Fix the port_id in the template for assertion
+        new_sub_port[0]['port'] = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_one')
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_sub_port = parent_port['trunk_details']['sub_ports']
+
+        self.assertEqual(self._sub_ports_dict_to_set(new_sub_port),
+                         self._sub_ports_dict_to_set(trunk_sub_port))
+
+    def test_add_a_second_sub_port(self):
+        parsed_template = yaml.safe_load(test_template)
+        sub_ports = [{'port': {'get_resource': 'sub_port_one'},
+                      'segmentation_type': 'vlan',
+                      'segmentation_id': 10}, ]
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'] = sub_ports
+        template_with_sub_ports = yaml.safe_dump(parsed_template)
+
+        stack_identifier = self.stack_create(template=template_with_sub_ports)
+
+        new_sub_port = {'port': {'get_resource': 'sub_port_two'},
+                        'segmentation_id': 20,
+                        'segmentation_type': 'vlan'}
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'].append(new_sub_port)
+
+        updated_template = yaml.safe_dump(parsed_template)
+
+        self.update_stack(stack_identifier, updated_template)
+
+        # Fix the port_ids in the templates for assertion
+        sub_ports[0]['port'] = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_one')
+        new_sub_port['port'] = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_two')
+        expected_sub_ports = [sub_ports[0], new_sub_port]
+
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_sub_ports = parent_port['trunk_details']['sub_ports']
+
+        self.assertEqual(self._sub_ports_dict_to_set(expected_sub_ports),
+                         self._sub_ports_dict_to_set(trunk_sub_ports))
+
+    def test_remove_sub_port_from_trunk(self):
+        sub_ports = [{'port': {'get_resource': 'sub_port_one'},
+                      'segmentation_type': 'vlan',
+                      'segmentation_id': 10},
+                     {'port': {'get_resource': 'sub_port_two'},
+                      'segmentation_type': 'vlan',
+                      'segmentation_id': 20}]
+        parsed_template = yaml.safe_load(test_template)
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'] = sub_ports
+        template_with_sub_ports = yaml.safe_dump(parsed_template)
+
+        stack_identifier = self.stack_create(template=template_with_sub_ports)
+
+        sub_port_to_be_removed = {'port': {'get_resource': 'sub_port_two'},
+                                  'segmentation_type': 'vlan',
+                                  'segmentation_id': 20}
+        parsed_template['resources']['trunk'][
+            'properties']['sub_ports'].remove(sub_port_to_be_removed)
+        updated_template = yaml.safe_dump(parsed_template)
+
+        self.update_stack(stack_identifier, updated_template)
+
+        # Fix the port_ids in the templates for assertion
+        sub_ports[0]['port'] = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_one')
+        expected_sub_ports = [sub_ports[0]]
+
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_sub_ports = parent_port['trunk_details']['sub_ports']
+
+        self.assertEqual(self._sub_ports_dict_to_set(expected_sub_ports),
+                         self._sub_ports_dict_to_set(trunk_sub_ports))
+
+    def test_remove_last_sub_port_from_trunk(self):
+        sub_ports = [{'port': {'get_resource': 'sub_port_one'},
+                      'segmentation_type': 'vlan',
+                      'segmentation_id': 10}]
+        parsed_template = yaml.safe_load(test_template)
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'] = sub_ports
+
+        template_with_sub_ports = yaml.safe_dump(parsed_template)
+        stack_identifier = self.stack_create(template=template_with_sub_ports)
+
+        sub_port_to_be_removed = {'port': {'get_resource': 'sub_port_one'},
+                                  'segmentation_type': 'vlan',
+                                  'segmentation_id': 10}
+
+        parsed_template['resources']['trunk'][
+            'properties']['sub_ports'] = []
+        updated_template = yaml.safe_dump(parsed_template)
+
+        self.update_stack(stack_identifier, updated_template)
+
+        sub_port_to_be_removed['port'] = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_one')
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_sub_ports = parent_port['trunk_details']['sub_ports']
+
+        self.assertNotEqual(
+            self._sub_ports_dict_to_set([sub_port_to_be_removed]),
+            self._sub_ports_dict_to_set(trunk_sub_ports))
+        self.assertFalse(trunk_sub_ports,
+                         'The returned sub ports (%s) in trunk_details is '
+                         'not empty!' % trunk_sub_ports)
+
+    def test_update_existing_sub_port_on_trunk(self):
+        sub_ports = [{'port': {'get_resource': 'sub_port_one'},
+                      'segmentation_type': 'vlan',
+                      'segmentation_id': 10}]
+        parsed_template = yaml.safe_load(test_template)
+        parsed_template['resources']['trunk']['properties'][
+            'sub_ports'] = sub_ports
+
+        template_with_sub_ports = yaml.safe_dump(parsed_template)
+        stack_identifier = self.stack_create(template=template_with_sub_ports)
+
+        sub_port_id = self.get_physical_resource_id(
+            stack_identifier, 'sub_port_one')
+        parsed_template['resources']['trunk']['properties']['sub_ports'][0][
+            'segmentation_id'] = 99
+        updated_template = yaml.safe_dump(parsed_template)
+
+        self.update_stack(stack_identifier, updated_template)
+        updated_sub_port = {'port': sub_port_id,
+                            'segmentation_type': 'vlan',
+                            'segmentation_id': 99}
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_sub_ports = parent_port['trunk_details']['sub_ports']
+
+        self.assertEqual(self._sub_ports_dict_to_set([updated_sub_port]),
+                         self._sub_ports_dict_to_set(trunk_sub_ports))
+
+    def test_update_trunk_name_and_description(self):
+        new_name = 'pineapple'
+        new_description = 'This is a test trunk'
+
+        stack_identifier = self.stack_create(template=test_template)
+        parsed_template = yaml.safe_load(test_template)
+        parsed_template['resources']['trunk']['properties']['name'] = new_name
+        parsed_template['resources']['trunk']['properties'][
+            'description'] = new_description
+        updated_template = yaml.safe_dump(parsed_template)
+        self.update_stack(stack_identifier, template=updated_template)
+
+        parent_id = self.get_stack_output(
+            stack_identifier, 'trunk_parent_port')
+        parent_port = self.network_client.show_port(parent_id)['port']
+        trunk_id = parent_port['trunk_details']['trunk_id']
+
+        trunk = self.network_client.show_trunk(trunk_id)['trunk']
+        self.assertEqual(new_name, trunk['name'])
+        self.assertEqual(new_description, trunk['description'])
diff --git a/functional/test_resources_list.py b/functional/test_resources_list.py
index 257afc5..f57cf67 100644
--- a/functional/test_resources_list.py
+++ b/functional/test_resources_list.py
@@ -41,3 +41,10 @@
                                              filters={'name': 'test2'})
 
         self.assertEqual('CREATE_COMPLETE', test2.resource_status)
+
+    def test_required_by(self):
+        stack_identifier = self.stack_create(template=test_template_depend)
+        [test1] = self.client.resources.list(stack_identifier,
+                                             filters={'name': 'test1'})
+
+        self.assertEqual(['test2'], test1.required_by)
diff --git a/functional/test_stack_outputs.py b/functional/test_stack_outputs.py
index 536e589..161e0b3 100644
--- a/functional/test_stack_outputs.py
+++ b/functional/test_stack_outputs.py
@@ -61,3 +61,41 @@
             stack_identifier, 'resource_output_b')['output']
         self.assertEqual(expected_output_a, actual_output_a)
         self.assertEqual(expected_output_b, actual_output_b)
+
+    before_template = '''
+heat_template_version: 2015-10-15
+resources:
+  test_resource_a:
+    type: OS::Heat::TestResource
+    properties:
+      value: 'foo'
+outputs:
+'''
+
+    after_template = '''
+heat_template_version: 2015-10-15
+resources:
+  test_resource_a:
+    type: OS::Heat::TestResource
+    properties:
+      value: 'foo'
+  test_resource_b:
+    type: OS::Heat::TestResource
+    properties:
+      value: {get_attr: [test_resource_a, output]}
+outputs:
+  output_value:
+    description: 'Output of resource b'
+    value: {get_attr: [test_resource_b, output]}
+'''
+
+    def test_outputs_update_new_resource(self):
+        stack_identifier = self.stack_create(template=self.before_template)
+        self.update_stack(stack_identifier, template=self.after_template)
+
+        expected_output_value = {
+            u'output_value': u'foo', u'output_key': u'output_value',
+            u'description': u'Output of resource b'}
+        actual_output_value = self.client.stacks.output_show(
+            stack_identifier, 'output_value')['output']
+        self.assertEqual(expected_output_value, actual_output_value)
diff --git a/scenario/templates/test_base_resources.yaml b/scenario/templates/test_base_resources.yaml
new file mode 100644
index 0000000..bff6185
--- /dev/null
+++ b/scenario/templates/test_base_resources.yaml
@@ -0,0 +1,110 @@
+heat_template_version: 2014-10-16
+
+description: >
+  This HOT template that just defines a single server.
+  Contains just base features to verify base heat support.
+
+parameters:
+  key_name:
+    type: string
+    default: key-01
+    description: Name of an existing key pair to use for the server
+  flavor:
+    type: string
+    description: Flavor for the server to be created
+    default: m1.small
+    constraints:
+      - custom_constraint: nova.flavor
+  image:
+    type: string
+    description: Image ID or image name to use for the server
+    constraints:
+      - custom_constraint: glance.image
+  vol_size:
+    type: number
+    description: The size of the Cinder volume
+    default: 1
+  private_net_name:
+    type: string
+    default: private-net-01
+    description: Name of private network to be created
+  private_net_cidr:
+    type: string
+    default: 192.168.101.0/24
+    description: Private network address (CIDR notation)
+  private_net_gateway:
+    type: string
+    default: 192.168.101.1
+    description: Private network gateway address
+  private_net_pool_start:
+    type: string
+    default: 192.168.101.2
+    description: Start of private network IP address allocation pool
+  private_net_pool_end:
+    type: string
+    default: 192.168.101.127
+    description: End of private network IP address allocation pool
+  echo_foo:
+    default: fooooo
+    type: string
+
+resources:
+  private_net:
+    type: OS::Neutron::Net
+    properties:
+      name: { get_param: private_net_name }
+
+  private_subnet:
+    type: OS::Neutron::Subnet
+    properties:
+      network_id: { get_resource: private_net }
+      cidr: { get_param: private_net_cidr }
+      gateway_ip: { get_param: private_net_gateway }
+      allocation_pools:
+        - start: { get_param: private_net_pool_start }
+          end: { get_param: private_net_pool_end }
+
+  server_port:
+    type: OS::Neutron::Port
+    properties:
+      network_id: { get_resource: private_net }
+      fixed_ips:
+        - subnet_id: { get_resource: private_subnet }
+
+  key:
+    type: OS::Nova::KeyPair
+    properties:
+      name: { get_param: key_name }
+
+  server:
+    type: OS::Nova::Server
+    properties:
+      key_name: { get_resource: key }
+      image: { get_param: image }
+      flavor: { get_param: flavor }
+      networks:
+        - port: { get_resource: server_port }
+      user_data:
+        str_replace:
+          template: |
+            #!/bin/bash
+            echo echo_foo
+          params:
+            echo_foo: { get_param: echo_foo }
+
+  vol:
+    type: OS::Cinder::Volume
+    properties:
+      size: { get_param: vol_size }
+
+  vol_att:
+    type: OS::Cinder::VolumeAttachment
+    properties:
+      instance_uuid: { get_resource: server }
+      volume_id: { get_resource: vol }
+      mountpoint: /dev/vdb
+
+outputs:
+  server_networks:
+    description: The networks of the deployed server
+    value: { get_attr: [server, networks] }
diff --git a/scenario/test_base_resources.py b/scenario/test_base_resources.py
new file mode 100644
index 0000000..80194a0
--- /dev/null
+++ b/scenario/test_base_resources.py
@@ -0,0 +1,73 @@
+#    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.common import test
+from heat_integrationtests.scenario import scenario_base
+from heatclient.common import template_utils
+
+
+class BasicResourcesTest(scenario_base.ScenarioTestsBase):
+
+    def setUp(self):
+        super(BasicResourcesTest, self).setUp()
+        if not self.conf.image_ref:
+            raise self.skipException("No image configured to test")
+        if not self.conf.instance_type:
+            raise self.skipException("No flavor configured to test")
+
+    def check_stack(self):
+        sid = self.stack_identifier
+        # Check that stack were created
+        self._wait_for_stack_status(sid, 'CREATE_COMPLETE')
+        server_resource = self.client.resources.get(sid, 'server')
+        server_id = server_resource.physical_resource_id
+        server = self.compute_client.servers.get(server_id)
+        self.assertEqual(server.id, server_id)
+
+        stack = self.client.stacks.get(sid)
+
+        server_networks = self._stack_output(stack, 'server_networks')
+        self.assertIn(self.private_net_name, server_networks)
+
+    def test_base_resources_integration(self):
+        """Define test for base resources interation from core porjects
+
+        The alternative scenario is the following:
+            1. Create a stack with basic resources from core projects.
+            2. Check that all stack resources are created successfully.
+            3. Wait for deployment.
+            4. Check that stack was created.
+            5. Check stack outputs.
+        """
+
+        self.private_net_name = test.rand_name('heat-net')
+        parameters = {
+            'key_name': test.rand_name('heat-key'),
+            'flavor': self.conf.instance_type,
+            'image': self.conf.image_ref,
+            'vol_size': self.conf.volume_size,
+            'private_net_name': self.private_net_name
+        }
+
+        env_files, env = template_utils.process_environment_and_files(
+            self.conf.boot_config_env)
+
+        # Launch stack
+        self.stack_identifier = self.launch_stack(
+            template_name='test_base_resources.yaml',
+            parameters=parameters,
+            expected_status=None,
+            environment=env
+        )
+
+        # Check stack
+        self.check_stack()