Merge "Add test for basic resources as heat define test"
diff --git a/api/gabbits/resources.yaml b/api/gabbits/resources.yaml
index 164f4cb..41da444 100644
--- a/api/gabbits/resources.yaml
+++ b/api/gabbits/resources.yaml
@@ -82,7 +82,7 @@
 
 - name: signal resource
   POST: $LAST_URL/signal
-  status: 200
+  status: 400
 
 - name: delete stack with resources
   DELETE: /stacks/$ENVIRON['PREFIX']-rsrcstack
diff --git a/common/test.py b/common/test.py
index 9944bd1..4587770 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_nested_get_attr.py b/functional/test_nested_get_attr.py
new file mode 100644
index 0000000..fff89a4
--- /dev/null
+++ b/functional/test_nested_get_attr.py
@@ -0,0 +1,165 @@
+#    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.
+
+# Using nested get_attr functions isn't a good idea - in particular, this
+# actually working depends on correct dependencies between the two resources
+# whose attributes are being fetched, and these dependencies are non-local to
+# where the get_attr calls are used. Nevertheless, it did sort-of work, and
+# this test will help keep it that way.
+
+from heat_integrationtests.functional import functional_base
+
+
+initial_template = '''
+heat_template_version: ocata
+resources:
+  dict_resource:
+    type: OS::Heat::Value
+    properties:
+      value:
+        blarg: wibble
+        foo: bar
+        baz: quux
+        fred: barney
+    # These dependencies are required because we only want to read the
+    # attribute values for a given resource once, and therefore we do so in
+    # dependency order. This is necessarily true for a convergence traversal,
+    # but also happens when we're fetching the resource attributes e.g. to show
+    # the output values. The key1/key2 attribute values must be stored before
+    # we attempt to calculate the dep_attrs for dict_resource in order to
+    # correctly determine which attributes of dict_resource are used.
+    depends_on:
+      - key1
+      - key2
+      - indirect_key3_dep
+  key1:
+    type: OS::Heat::Value
+    properties:
+      value: blarg
+  key2:
+    type: OS::Heat::Value
+    properties:
+      value: foo
+  key3:
+    type: OS::Heat::Value
+    properties:
+      value: fred
+  value1:
+    type: OS::Heat::Value
+    properties:
+      value:
+        get_attr:
+          - dict_resource
+          - value
+          - {get_attr: [key1, value]}
+  indirect_key3_dep:
+    type: OS::Heat::Value
+    properties:
+      value: ignored
+    depends_on: key3
+outputs:
+  value1:
+    value: {get_attr: [value1, value]}
+  value2:
+    value: {get_attr: [dict_resource, value, {get_attr: [key2, value]}]}
+  value3:
+    value: {get_attr: [dict_resource, value, {get_attr: [key3, value]}]}
+'''
+
+update_template = '''
+heat_template_version: ocata
+resources:
+  dict_resource:
+    type: OS::Heat::Value
+    properties:
+      value:
+        blarg: wibble
+        foo: bar
+        baz: quux
+        fred: barney
+    depends_on:
+      - key1
+      - key2
+      - indirect_key3_dep
+      - key4
+  key1:
+    type: OS::Heat::Value
+    properties:
+      value: foo
+  key2:
+    type: OS::Heat::Value
+    properties:
+      value: fred
+  key3:
+    type: OS::Heat::Value
+    properties:
+      value: blarg
+  key4:
+    type: OS::Heat::Value
+    properties:
+      value: baz
+  value1:
+    type: OS::Heat::Value
+    properties:
+      value:
+        get_attr:
+          - dict_resource
+          - value
+          - {get_attr: [key1, value]}
+  value4:
+    type: OS::Heat::Value
+    properties:
+      value:
+        get_attr:
+          - dict_resource
+          - value
+          - {get_attr: [key4, value]}
+  indirect_key3_dep:
+    type: OS::Heat::Value
+    properties:
+      value: ignored
+    depends_on: key3
+outputs:
+  value1:
+    value: {get_attr: [value1, value]}
+  value2:
+    value: {get_attr: [dict_resource, value, {get_attr: [key2, value]}]}
+  value3:
+    value: {get_attr: [dict_resource, value, {get_attr: [key3, value]}]}
+  value4:
+    value: {get_attr: [value4, value]}
+'''
+
+
+class NestedGetAttrTest(functional_base.FunctionalTestsBase):
+    def assertOutput(self, value, stack_identifier, key):
+        op = self.client.stacks.output_show(stack_identifier, key)['output']
+        self.assertEqual(key, op['output_key'])
+        if 'output_error' in op:
+            raise Exception(op['output_error'])
+        self.assertEqual(value, op['output_value'])
+
+    def test_nested_get_attr_create(self):
+        stack_identifier = self.stack_create(template=initial_template)
+
+        self.assertOutput('wibble', stack_identifier, 'value1')
+        self.assertOutput('bar', stack_identifier, 'value2')
+        self.assertOutput('barney', stack_identifier, 'value3')
+
+    def test_nested_get_attr_update(self):
+        stack_identifier = self.stack_create(template=initial_template)
+        self.update_stack(stack_identifier, template=update_template)
+
+        self.assertOutput('bar', stack_identifier, 'value1')
+        self.assertOutput('barney', stack_identifier, 'value2')
+        self.assertOutput('wibble', stack_identifier, 'value3')
+        self.assertOutput('quux', stack_identifier, 'value4')
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)