Removed current working directory logic retrieving test file resources.
[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'))
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('examples.heat', 'test_heat_template.yaml')
91         self.stack_settings = StackSettings(name=stack_name, template_path=heat_tmplt_path, env_values=env_values)
92         self.stack = None
93         self.heat_client = heat_utils.heat_client(self.os_creds)
94
95     def tearDown(self):
96         """
97         Cleans the image and downloaded image file
98         """
99         if self.stack:
100             try:
101                 heat_utils.delete_stack(self.heat_client, self.stack)
102             except:
103                 pass
104
105         if self.image_creator:
106             try:
107                 self.image_creator.clean()
108             except:
109                 pass
110
111         if self.flavor_creator:
112             try:
113                 self.flavor_creator.clean()
114             except:
115                 pass
116
117     def test_create_stack(self):
118         """
119         Tests the creation of an OpenStack keypair that does not exist.
120         """
121         self.stack = heat_utils.create_stack(self.heat_client, self.stack_settings)
122
123         stack_query_1 = heat_utils.get_stack_by_name(self.heat_client, self.stack_settings.name)
124         self.assertEqual(self.stack.id, stack_query_1.id)
125
126         stack_query_2 = heat_utils.get_stack_by_id(self.heat_client, self.stack.id)
127         self.assertEqual(self.stack.id, stack_query_2.id)
128
129         outputs = heat_utils.get_stack_outputs(self.heat_client, self.stack.id)
130         self.assertIsNotNone(outputs)
131         self.assertEqual(0, len(outputs))
132
133         end_time = time.time() + create_stack.STACK_COMPLETE_TIMEOUT
134
135         is_active = False
136         while time.time() < end_time:
137             status = heat_utils.get_stack_status(self.heat_client, self.stack.id)
138             if status == create_stack.STATUS_CREATE_COMPLETE:
139                 is_active = True
140                 break
141
142             time.sleep(3)
143
144         self.assertTrue(is_active)