Added region support.
[snaps.git] / snaps / openstack / utils / tests / heat_utils_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import logging
16 import pkg_resources
17 import uuid
18
19 import time
20
21 from snaps.openstack import create_stack
22 from snaps.openstack.create_flavor import OpenStackFlavor, FlavorSettings
23
24 from snaps.openstack.create_image import OpenStackImage
25 from snaps.openstack.create_stack import StackSettings
26 from snaps.openstack.tests import openstack_tests
27 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
28 from snaps.openstack.utils import heat_utils
29
30 __author__ = 'spisarski'
31
32 logger = logging.getLogger('nova_utils_tests')
33
34
35 class HeatSmokeTests(OSComponentTestCase):
36     """
37     Tests to ensure that the nova client can communicate with the cloud
38     """
39
40     def test_nova_connect_success(self):
41         """
42         Tests to ensure that the proper credentials can connect.
43         """
44         heat = heat_utils.heat_client(self.os_creds)
45
46         # This should not throw an exception
47         stacks = heat.stacks.list()
48         for stack in stacks:
49             print stack
50
51     def test_nova_connect_fail(self):
52         """
53         Tests to ensure that the improper credentials cannot connect.
54         """
55         from snaps.openstack.os_credentials import OSCreds
56
57         heat = heat_utils.heat_client(
58             OSCreds(username='user', password='pass',
59                     auth_url=self.os_creds.auth_url,
60                     project_name=self.os_creds.project_name,
61                     proxy_settings=self.os_creds.proxy_settings))
62         stacks = heat.stacks.list()
63
64         # This should throw an exception
65         with self.assertRaises(Exception):
66             for stack in stacks:
67                 print stack
68
69
70 class HeatUtilsCreateStackTests(OSComponentTestCase):
71     """
72     Test basic nova keypair functionality
73     """
74
75     def setUp(self):
76         """
77         Instantiates the CreateImage object that is responsible for downloading
78         and creating an OS image file within OpenStack
79         """
80         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
81         stack_name = self.__class__.__name__ + '-' + str(guid) + '-stack'
82
83         self.image_creator = OpenStackImage(
84             self.os_creds, openstack_tests.cirros_image_settings(
85                 name=self.__class__.__name__ + '-' + str(guid) + '-image',
86                 image_metadata=self.image_metadata))
87         self.image_creator.create()
88
89         # Create Flavor
90         self.flavor_creator = OpenStackFlavor(
91             self.os_creds,
92             FlavorSettings(name=guid + '-flavor', ram=128, disk=10, vcpus=1))
93         self.flavor_creator.create()
94
95         env_values = {'image_name': self.image_creator.image_settings.name,
96                       'flavor_name': self.flavor_creator.flavor_settings.name}
97         heat_tmplt_path = pkg_resources.resource_filename(
98             'snaps.openstack.tests.heat', 'test_heat_template.yaml')
99         self.stack_settings = StackSettings(
100             name=stack_name, template_path=heat_tmplt_path,
101             env_values=env_values)
102         self.stack = None
103         self.heat_client = heat_utils.heat_client(self.os_creds)
104
105     def tearDown(self):
106         """
107         Cleans the image and downloaded image file
108         """
109         if self.stack:
110             try:
111                 heat_utils.delete_stack(self.heat_client, self.stack)
112             except:
113                 pass
114
115         if self.image_creator:
116             try:
117                 self.image_creator.clean()
118             except:
119                 pass
120
121         if self.flavor_creator:
122             try:
123                 self.flavor_creator.clean()
124             except:
125                 pass
126
127     def test_create_stack(self):
128         """
129         Tests the creation of an OpenStack keypair that does not exist.
130         """
131         self.stack = heat_utils.create_stack(self.heat_client,
132                                              self.stack_settings)
133
134         stack_query_1 = heat_utils.get_stack_by_name(self.heat_client,
135                                                      self.stack_settings.name)
136         self.assertEqual(self.stack.id, stack_query_1.id)
137
138         stack_query_2 = heat_utils.get_stack_by_id(self.heat_client,
139                                                    self.stack.id)
140         self.assertEqual(self.stack.id, stack_query_2.id)
141
142         outputs = heat_utils.get_stack_outputs(self.heat_client, self.stack.id)
143         self.assertIsNotNone(outputs)
144         self.assertEqual(0, len(outputs))
145
146         end_time = time.time() + create_stack.STACK_COMPLETE_TIMEOUT
147
148         is_active = False
149         while time.time() < end_time:
150             status = heat_utils.get_stack_status(self.heat_client,
151                                                  self.stack.id)
152             if status == create_stack.STATUS_CREATE_COMPLETE:
153                 is_active = True
154                 break
155             elif status == create_stack.STATUS_CREATE_FAILED:
156                 is_active = False
157                 break
158
159             time.sleep(3)
160
161         self.assertTrue(is_active)