1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 from snaps.openstack import create_stack
22 from snaps.openstack.create_flavor import OpenStackFlavor, FlavorSettings
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
30 __author__ = 'spisarski'
32 logger = logging.getLogger('nova_utils_tests')
35 class HeatSmokeTests(OSComponentTestCase):
37 Tests to ensure that the nova client can communicate with the cloud
40 def test_nova_connect_success(self):
42 Tests to ensure that the proper credentials can connect.
44 heat = heat_utils.heat_client(self.os_creds)
46 # This should not throw an exception
47 stacks = heat.stacks.list()
51 def test_nova_connect_fail(self):
53 Tests to ensure that the improper credentials cannot connect.
55 from snaps.openstack.os_credentials import OSCreds
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()
64 # This should throw an exception
65 with self.assertRaises(Exception):
70 class HeatUtilsCreateStackTests(OSComponentTestCase):
72 Test basic nova keypair functionality
77 Instantiates the CreateImage object that is responsible for downloading
78 and creating an OS image file within OpenStack
80 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
81 stack_name = self.__class__.__name__ + '-' + str(guid) + '-stack'
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()
90 self.flavor_creator = OpenStackFlavor(
92 FlavorSettings(name=guid + '-flavor', ram=128, disk=10, vcpus=1))
93 self.flavor_creator.create()
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)
103 self.heat_client = heat_utils.heat_client(self.os_creds)
107 Cleans the image and downloaded image file
111 heat_utils.delete_stack(self.heat_client, self.stack)
115 if self.image_creator:
117 self.image_creator.clean()
121 if self.flavor_creator:
123 self.flavor_creator.clean()
127 def test_create_stack(self):
129 Tests the creation of an OpenStack keypair that does not exist.
131 self.stack = heat_utils.create_stack(self.heat_client,
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)
138 stack_query_2 = heat_utils.get_stack_by_id(self.heat_client,
140 self.assertEqual(self.stack.id, stack_query_2.id)
142 outputs = heat_utils.get_stack_outputs(self.heat_client, self.stack.id)
143 self.assertIsNotNone(outputs)
144 self.assertEqual(0, len(outputs))
146 end_time = time.time() + create_stack.STACK_COMPLETE_TIMEOUT
149 while time.time() < end_time:
150 status = heat_utils.get_stack_status(self.heat_client,
152 if status == create_stack.STATUS_CREATE_COMPLETE:
155 elif status == create_stack.STATUS_CREATE_FAILED:
161 self.assertTrue(is_active)