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.
20 from snaps import file_utils
21 from snaps.openstack.tests import openstack_tests
23 from snaps.openstack.tests import validation_utils
24 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
25 from snaps.openstack.utils import glance_utils
27 __author__ = 'spisarski'
30 logger = logging.getLogger('glance_utils_tests')
33 class GlanceSmokeTests(OSComponentTestCase):
35 Tests to ensure that the neutron client can communicate with the cloud
38 def test_glance_connect_success(self):
40 Tests to ensure that the proper credentials can connect.
42 glance = glance_utils.glance_client(self.os_creds, self.os_session)
43 image = glance_utils.get_image(glance, image_name='foo')
44 self.assertIsNone(image)
46 def test_glance_connect_fail(self):
48 Tests to ensure that the improper credentials cannot connect.
50 from snaps.openstack.os_credentials import OSCreds
52 with self.assertRaises(Exception):
53 glance = glance_utils.glance_client(OSCreds(
54 username='user', password='pass', auth_url='url',
55 project_name='project'))
56 glance_utils.get_image(glance, image_name='foo')
59 class GlanceUtilsTests(OSComponentTestCase):
61 Test for the CreateImage class defined in create_image.py
66 Instantiates the CreateImage object that is responsible for downloading
67 and creating an OS image file within OpenStack
70 self.image_name = self.__class__.__name__ + '-' + str(guid)
72 self.glance = glance_utils.glance_client(
73 self.os_creds, self.os_session)
74 if self.image_metadata:
75 self.glance_test_meta = self.image_metadata.get('glance_tests')
77 self.glance_test_meta = dict()
79 self.tmp_dir = 'tmp/' + str(guid)
80 if not os.path.exists(self.tmp_dir):
81 os.makedirs(self.tmp_dir)
85 Cleans the remote OpenStack objects
88 glance_utils.delete_image(self.glance, self.image)
90 if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir):
91 shutil.rmtree(self.tmp_dir)
93 super(self.__class__, self).__clean__()
95 def test_create_image_minimal_url(self):
97 Tests the glance_utils.create_image() function with a URL unless the
98 self.glance_test_meta has configured a file to be used.
100 if 'disk_file' not in self.glance_test_meta:
101 os_image_settings = openstack_tests.cirros_image_settings(
102 name=self.image_name, image_metadata=self.glance_test_meta)
104 self.image = glance_utils.create_image(self.glance,
106 self.assertIsNotNone(self.image)
108 self.assertEqual(self.image_name, self.image.name)
110 image = glance_utils.get_image(self.glance,
111 image_settings=os_image_settings)
112 self.assertIsNotNone(image)
114 validation_utils.objects_equivalent(self.image, image)
116 logger.warn('Test not executed as the image metadata requires '
119 def test_create_image_minimal_file(self):
121 Tests the glance_utils.create_image() function with a file
123 if 'disk_file' not in self.glance_test_meta:
124 url_image_settings = openstack_tests.cirros_image_settings(
125 name='foo', image_metadata=self.glance_test_meta)
126 image_file_name = file_utils.download(
127 url_image_settings.url, self.tmp_dir).name
129 image_file_name = self.glance_test_meta['disk_file']
131 file_image_settings = openstack_tests.file_image_test_settings(
132 name=self.image_name, file_path=image_file_name)
134 self.image = glance_utils.create_image(self.glance,
136 self.assertIsNotNone(self.image)
137 self.assertEqual(self.image_name, self.image.name)
139 image = glance_utils.get_image(
140 self.glance, image_settings=file_image_settings)
141 self.assertIsNotNone(image)
142 validation_utils.objects_equivalent(self.image, image)