Fixed the Glance connection test routines.
[snaps.git] / snaps / openstack / utils / tests / glance_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 os
16 import shutil
17 import uuid
18
19 from snaps import file_utils
20 from snaps.openstack.tests import openstack_tests
21
22 from snaps.openstack.tests import validation_utils
23 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
24 from snaps.openstack.utils import glance_utils
25
26 __author__ = 'spisarski'
27
28
29 class GlanceSmokeTests(OSComponentTestCase):
30     """
31     Tests to ensure that the neutron client can communicate with the cloud
32     """
33
34     def test_glance_connect_success(self):
35         """
36         Tests to ensure that the proper credentials can connect.
37         """
38         glance = glance_utils.glance_client(self.os_creds)
39         nova = nova_utils.nova_client(self.os_creds)
40         image = glance_utils.get_image(nova, glance, 'foo')
41         self.assertIsNone(image)
42
43     def test_glance_connect_fail(self):
44         """
45         Tests to ensure that the improper credentials cannot connect.
46         """
47         from snaps.openstack.os_credentials import OSCreds
48
49         with self.assertRaises(Exception):
50             glance = glance_utils.glance_client(OSCreds('user', 'pass', 'url', 'project'))
51             nova = nova_utils.nova_client(self.os_creds)
52             glance_utils.get_image(nova, glance, 'foo')
53
54
55 class GlanceUtilsTests(OSComponentTestCase):
56     """
57     Test for the CreateImage class defined in create_image.py
58     """
59
60     def setUp(self):
61         """
62         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
63         within OpenStack
64         """
65         guid = uuid.uuid4()
66         self.image_name = self.__class__.__name__ + '-' + str(guid)
67         self.image = None
68         self.glance = glance_utils.glance_client(self.os_creds)
69
70         self.tmp_dir = 'tmp/' + str(guid)
71         if not os.path.exists(self.tmp_dir):
72             os.makedirs(self.tmp_dir)
73
74     def tearDown(self):
75         """
76         Cleans the remote OpenStack objects
77         """
78         if self.image:
79             glance_utils.delete_image(self.glance, self.image)
80
81         if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir):
82             shutil.rmtree(self.tmp_dir)
83
84     def test_create_image_minimal_url(self):
85         """
86         Tests the glance_utils.create_image() function with a URL
87         """
88         os_image_settings = openstack_tests.cirros_url_image(name=self.image_name)
89
90         self.image = glance_utils.create_image(self.glance, os_image_settings)
91         self.assertIsNotNone(self.image)
92
93         self.assertEqual(self.image_name, self.image.name)
94
95         image = glance_utils.get_image(self.glance, os_image_settings.name)
96         self.assertIsNotNone(image)
97
98         validation_utils.objects_equivalent(self.image, image)
99
100     def test_create_image_minimal_file(self):
101         """
102         Tests the glance_utils.create_image() function with a file
103         """
104         url_image_settings = openstack_tests.cirros_url_image('foo')
105         image_file = file_utils.download(url_image_settings.url, self.tmp_dir)
106         file_image_settings = openstack_tests.file_image_test_settings(name=self.image_name, file_path=image_file.name)
107
108         self.image = glance_utils.create_image(self.glance, file_image_settings)
109         self.assertIsNotNone(self.image)
110         self.assertEqual(self.image_name, self.image.name)
111
112         image = glance_utils.get_image(self.glance, file_image_settings.name)
113         self.assertIsNotNone(image)
114         validation_utils.objects_equivalent(self.image, image)