Merge "Add tests for heat template apis"
diff --git a/functional/test_aws_stack.py b/functional/test_aws_stack.py
index 65f2ae8..678ce7e 100644
--- a/functional/test_aws_stack.py
+++ b/functional/test_aws_stack.py
@@ -15,7 +15,7 @@
 import random
 
 from oslo_log import log as logging
-import requests
+
 from six.moves.urllib import parse
 from swiftclient import utils as swiftclient_utils
 import yaml
@@ -76,49 +76,36 @@
 
     def setUp(self):
         super(AwsStackTest, self).setUp()
-        self.object_container_name = AwsStackTest.__name__
+        self.object_container_name = test.rand_name()
         self.project_id = self.identity_client.auth_ref.project_id
-        self.object_client.put_container(self.object_container_name)
-        self.nested_name = '%s.yaml' % test.rand_name()
+        self.swift_key = hashlib.sha224(
+            str(random.getrandbits(256))).hexdigest()[:32]
+        key_header = 'x-container-meta-temp-url-key'
+        self.object_client.put_container(self.object_container_name,
+                                         {key_header: self.swift_key})
+        self.addCleanup(self.object_client.delete_container,
+                        self.object_container_name)
 
-    def publish_template(self, name, contents):
+    def publish_template(self, contents, cleanup=True):
         oc = self.object_client
 
         # post the object
-        oc.put_object(self.object_container_name, name, contents)
-        # TODO(asalkeld) see if this is causing problems.
-        # self.addCleanup(self.object_client.delete_object,
-        #                self.object_container_name, name)
-
-        # make the tempurl
-        key_header = 'x-account-meta-temp-url-key'
-        if key_header not in oc.head_account():
-            swift_key = hashlib.sha224(
-                str(random.getrandbits(256))).hexdigest()[:32]
-            LOG.warning('setting swift key to %s' % swift_key)
-            oc.post_account({key_header: swift_key})
-        key = oc.head_account()[key_header]
+        oc.put_object(self.object_container_name, 'template.yaml', contents)
+        if cleanup:
+            self.addCleanup(oc.delete_object,
+                            self.object_container_name,
+                            'template.yaml')
         path = '/v1/AUTH_%s/%s/%s' % (self.project_id,
-                                      self.object_container_name, name)
+                                      self.object_container_name,
+                                      'template.yaml')
         timeout = self.conf.build_timeout * 10
         tempurl = swiftclient_utils.generate_temp_url(path, timeout,
-                                                      key, 'GET')
+                                                      self.swift_key, 'GET')
         sw_url = parse.urlparse(oc.url)
-        full_url = '%s://%s%s' % (sw_url.scheme, sw_url.netloc, tempurl)
-
-        def download():
-            r = requests.get(full_url)
-            LOG.info('GET: %s -> %s' % (full_url, r.status_code))
-            return r.status_code == requests.codes.ok
-
-        # make sure that the object is available.
-        test.call_until_true(self.conf.build_timeout,
-                             self.conf.build_interval, download)
-
-        return full_url
+        return '%s://%s%s' % (sw_url.scheme, sw_url.netloc, tempurl)
 
     def test_nested_stack_create(self):
-        url = self.publish_template(self.nested_name, self.nested_template)
+        url = self.publish_template(self.nested_template)
         self.template = self.test_template.replace('the.yaml', url)
         stack_identifier = self.stack_create(template=self.template)
         stack = self.client.stacks.get(stack_identifier)
@@ -126,7 +113,7 @@
         self.assertEqual('bar', self._stack_output(stack, 'output_foo'))
 
     def test_nested_stack_create_with_timeout(self):
-        url = self.publish_template(self.nested_name, self.nested_template)
+        url = self.publish_template(self.nested_template)
         self.template = self.test_template.replace('the.yaml', url)
         timeout_template = yaml.load(self.template)
         props = timeout_template['Resources']['the_nested']['Properties']
@@ -139,8 +126,7 @@
         self.assertEqual('bar', self._stack_output(stack, 'output_foo'))
 
     def test_nested_stack_adopt_ok(self):
-        url = self.publish_template(self.nested_name,
-                                    self.nested_with_res_template)
+        url = self.publish_template(self.nested_with_res_template)
         self.template = self.test_template.replace('the.yaml', url)
         adopt_data = {
             "resources": {
@@ -166,8 +152,7 @@
         self.assertEqual('goopie', self._stack_output(stack, 'output_foo'))
 
     def test_nested_stack_adopt_fail(self):
-        url = self.publish_template(self.nested_name,
-                                    self.nested_with_res_template)
+        url = self.publish_template(self.nested_with_res_template)
         self.template = self.test_template.replace('the.yaml', url)
         adopt_data = {
             "resources": {
@@ -187,7 +172,7 @@
         self.assertEqual('ADOPT_FAILED', rsrc.resource_status)
 
     def test_nested_stack_update(self):
-        url = self.publish_template(self.nested_name, self.nested_template)
+        url = self.publish_template(self.nested_template)
         self.template = self.test_template.replace('the.yaml', url)
         stack_identifier = self.stack_create(template=self.template)
         original_nested_id = self.assert_resource_is_a_stack(
@@ -197,8 +182,8 @@
 
         new_template = yaml.load(self.template)
         props = new_template['Resources']['the_nested']['Properties']
-        props['TemplateURL'] = self.publish_template(self.nested_name,
-                                                     self.update_template)
+        props['TemplateURL'] = self.publish_template(self.update_template,
+                                                     cleanup=False)
 
         self.update_stack(stack_identifier, new_template)
 
@@ -211,7 +196,7 @@
         self.assertEqual('foo', self._stack_output(updt_stack, 'output_foo'))
 
     def test_nested_stack_suspend_resume(self):
-        url = self.publish_template(self.nested_name, self.nested_template)
+        url = self.publish_template(self.nested_template)
         self.template = self.test_template.replace('the.yaml', url)
         stack_identifier = self.stack_create(template=self.template)
         self.stack_suspend(stack_identifier)
diff --git a/functional/test_create_update_neutron_subnet.py b/functional/test_create_update_neutron_subnet.py
index 6dad7f2..ceb74a9 100644
--- a/functional/test_create_update_neutron_subnet.py
+++ b/functional/test_create_update_neutron_subnet.py
@@ -24,10 +24,13 @@
     properties:
       network: { get_resource: net }
       cidr: 11.11.11.0/24
+      gateway_ip: 11.11.11.5
       allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]
 outputs:
   alloc_pools:
     value: {get_attr: [subnet, allocation_pools]}
+  gateway_ip:
+    value: {get_attr: [subnet, gateway_ip]}
 '''
 
 
@@ -36,14 +39,14 @@
     def setUp(self):
         super(UpdateSubnetTest, self).setUp()
 
-    def get_alloc_pools(self, stack_identifier):
+    def get_outputs(self, stack_identifier, output_key):
         stack = self.client.stacks.get(stack_identifier)
-        alloc_pools = self._stack_output(stack, 'alloc_pools')
-        return alloc_pools
+        output = self._stack_output(stack, output_key)
+        return output
 
     def test_update_allocation_pools(self):
         stack_identifier = self.stack_create(template=test_template)
-        alloc_pools = self.get_alloc_pools(stack_identifier)
+        alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
                          alloc_pools)
 
@@ -52,14 +55,14 @@
             'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
             'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.100}]')
         self.update_stack(stack_identifier, templ_other_pool)
-        new_alloc_pools = self.get_alloc_pools(stack_identifier)
+        new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         # the new pools should be the new range
         self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.100'}],
                          new_alloc_pools)
 
     def test_update_allocation_pools_to_empty(self):
         stack_identifier = self.stack_create(template=test_template)
-        alloc_pools = self.get_alloc_pools(stack_identifier)
+        alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
                          alloc_pools)
 
@@ -68,13 +71,13 @@
             'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
             'allocation_pools: []')
         self.update_stack(stack_identifier, templ_empty_pools)
-        new_alloc_pools = self.get_alloc_pools(stack_identifier)
+        new_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         # new_alloc_pools should be []
         self.assertEqual([], new_alloc_pools)
 
     def test_update_to_no_allocation_pools(self):
         stack_identifier = self.stack_create(template=test_template)
-        alloc_pools = self.get_alloc_pools(stack_identifier)
+        alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         self.assertEqual([{'start': '11.11.11.10', 'end': '11.11.11.250'}],
                          alloc_pools)
 
@@ -83,6 +86,45 @@
             'allocation_pools: [{start: 11.11.11.10, end: 11.11.11.250}]',
             '')
         self.update_stack(stack_identifier, templ_no_pools)
-        last_alloc_pools = self.get_alloc_pools(stack_identifier)
+        last_alloc_pools = self.get_outputs(stack_identifier, 'alloc_pools')
         # last_alloc_pools should be []
         self.assertEqual([], last_alloc_pools)
+
+    def test_update_gateway_ip(self):
+        stack_identifier = self.stack_create(template=test_template)
+        gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        self.assertEqual('11.11.11.5', gw_ip)
+
+        # Update gateway_ip
+        templ_other_gw_ip = test_template.replace(
+            'gateway_ip: 11.11.11.5', 'gateway_ip: 11.11.11.9')
+        self.update_stack(stack_identifier, templ_other_gw_ip)
+        new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        # the gateway_ip should be the new one
+        self.assertEqual('11.11.11.9', new_gw_ip)
+
+    def test_update_gateway_ip_to_empty(self):
+        stack_identifier = self.stack_create(template=test_template)
+        gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        self.assertEqual('11.11.11.5', gw_ip)
+
+        # Update gateway_ip to null(resolve to '')
+        templ_empty_gw_ip = test_template.replace(
+            'gateway_ip: 11.11.11.5', 'gateway_ip: null')
+        self.update_stack(stack_identifier, templ_empty_gw_ip)
+        new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        # new gateway_ip should be None
+        self.assertIsNone(new_gw_ip)
+
+    def test_update_to_no_gateway_ip(self):
+        stack_identifier = self.stack_create(template=test_template)
+        gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        self.assertEqual('11.11.11.5', gw_ip)
+
+        # Remove the gateway from template
+        templ_no_gw_ip = test_template.replace(
+            'gateway_ip: 11.11.11.5', '')
+        self.update_stack(stack_identifier, templ_no_gw_ip)
+        new_gw_ip = self.get_outputs(stack_identifier, 'gateway_ip')
+        # new gateway_ip should be None
+        self.assertIsNone(new_gw_ip)