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
49 def test_nova_connect_fail(self):
51 Tests to ensure that the improper credentials cannot connect.
53 from snaps.openstack.os_credentials import OSCreds
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))
59 # This should throw an exception
60 with self.assertRaises(Exception):
64 class HeatUtilsCreateStackTests(OSComponentTestCase):
66 Test basic nova keypair functionality
71 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
74 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
75 stack_name = self.__class__.__name__ + '-' + str(guid) + '-stack'
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()
83 self.flavor_creator = OpenStackFlavor(
85 FlavorSettings(name=guid + '-flavor', ram=128, disk=10, vcpus=1))
86 self.flavor_creator.create()
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)
94 self.heat_client = heat_utils.heat_client(self.os_creds)
98 Cleans the image and downloaded image file
102 heat_utils.delete_stack(self.heat_client, self.stack)
106 if self.image_creator:
108 self.image_creator.clean()
112 if self.flavor_creator:
114 self.flavor_creator.clean()
118 def test_create_stack(self):
120 Tests the creation of an OpenStack keypair that does not exist.
122 self.stack = heat_utils.create_stack(self.heat_client, self.stack_settings)
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)
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)
130 outputs = heat_utils.get_stack_outputs(self.heat_client, self.stack.id)
131 self.assertIsNotNone(outputs)
132 self.assertEqual(0, len(outputs))
134 end_time = time.time() + create_stack.STACK_COMPLETE_TIMEOUT
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:
145 self.assertTrue(is_active)