Changes required for running CI tests (Pike pod).
[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         heat.stacks.list()
48
49     def test_nova_connect_fail(self):
50         """
51         Tests to ensure that the improper credentials cannot connect.
52         """
53         from snaps.openstack.os_credentials import OSCreds
54
55         nova = heat_utils.heat_client(
56             OSCreds(username='user', password='pass', auth_url=self.os_creds.auth_url,
57                     project_name=self.os_creds.project_name, proxy_settings=self.os_creds.proxy_settings))
58
59         # This should throw an exception
60         with self.assertRaises(Exception):
61             nova.flavors.list()
62
63
64 class HeatUtilsCreateStackTests(OSComponentTestCase):
65     """
66     Test basic nova keypair functionality
67     """
68
69     def setUp(self):
70         """
71         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
72         within OpenStack
73         """
74         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
75         stack_name = self.__class__.__name__ + '-' + str(guid) + '-stack'
76
77         self.image_creator = OpenStackImage(
78             self.os_creds, openstack_tests.cirros_image_settings(
79                 name=self.__class__.__name__ + '-' + str(guid) + '-image', image_metadata=self.image_metadata))
80         self.image_creator.create()
81
82         # Create Flavor
83         self.flavor_creator = OpenStackFlavor(
84             self.os_creds,
85             FlavorSettings(name=guid + '-flavor', ram=128, disk=10, vcpus=1))
86         self.flavor_creator.create()
87
88         env_values = {'image_name': self.image_creator.image_settings.name,
89                       'flavor_name': self.flavor_creator.flavor_settings.name}
90         heat_tmplt_path = pkg_resources.resource_filename(
91             'snaps.openstack.tests.heat', 'test_heat_template.yaml')
92         self.stack_settings = StackSettings(name=stack_name, template_path=heat_tmplt_path, env_values=env_values)
93         self.stack = None
94         self.heat_client = heat_utils.heat_client(self.os_creds)
95
96     def tearDown(self):
97         """
98         Cleans the image and downloaded image file
99         """
100         if self.stack:
101             try:
102                 heat_utils.delete_stack(self.heat_client, self.stack)
103             except:
104                 pass
105
106         if self.image_creator:
107             try:
108                 self.image_creator.clean()
109             except:
110                 pass
111
112         if self.flavor_creator:
113             try:
114                 self.flavor_creator.clean()
115             except:
116                 pass
117
118     def test_create_stack(self):
119         """
120         Tests the creation of an OpenStack keypair that does not exist.
121         """
122         self.stack = heat_utils.create_stack(self.heat_client, self.stack_settings)
123
124         stack_query_1 = heat_utils.get_stack_by_name(self.heat_client, self.stack_settings.name)
125         self.assertEqual(self.stack.id, stack_query_1.id)
126
127         stack_query_2 = heat_utils.get_stack_by_id(self.heat_client, self.stack.id)
128         self.assertEqual(self.stack.id, stack_query_2.id)
129
130         outputs = heat_utils.get_stack_outputs(self.heat_client, self.stack.id)
131         self.assertIsNotNone(outputs)
132         self.assertEqual(0, len(outputs))
133
134         end_time = time.time() + create_stack.STACK_COMPLETE_TIMEOUT
135
136         is_active = False
137         while time.time() < end_time:
138             status = heat_utils.get_stack_status(self.heat_client, self.stack.id)
139             if status == create_stack.STATUS_CREATE_COMPLETE:
140                 is_active = True
141                 break
142             elif status == create_stack.STATUS_CREATE_FAILED:
143                 is_active = False
144                 break
145
146             time.sleep(3)
147
148         self.assertTrue(is_active)