Use neutron to create floating IPs.
[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 logging
16 import os
17 import shutil
18 import uuid
19
20 from snaps import file_utils
21 from snaps.openstack.tests import openstack_tests
22
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
26
27 __author__ = 'spisarski'
28
29
30 logger = logging.getLogger('glance_utils_tests')
31
32
33 class GlanceSmokeTests(OSComponentTestCase):
34     """
35     Tests to ensure that the neutron client can communicate with the cloud
36     """
37
38     def test_glance_connect_success(self):
39         """
40         Tests to ensure that the proper credentials can connect.
41         """
42         glance = glance_utils.glance_client(self.os_creds)
43         image = glance_utils.get_image(glance, 'foo')
44         self.assertIsNone(image)
45
46     def test_glance_connect_fail(self):
47         """
48         Tests to ensure that the improper credentials cannot connect.
49         """
50         from snaps.openstack.os_credentials import OSCreds
51
52         with self.assertRaises(Exception):
53             glance = glance_utils.glance_client(OSCreds('user', 'pass', 'url', 'project'))
54             glance_utils.get_image(glance, 'foo')
55
56
57 class GlanceUtilsTests(OSComponentTestCase):
58     """
59     Test for the CreateImage class defined in create_image.py
60     """
61
62     def setUp(self):
63         """
64         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
65         within OpenStack
66         """
67         guid = uuid.uuid4()
68         self.image_name = self.__class__.__name__ + '-' + str(guid)
69         self.image = None
70         self.glance = glance_utils.glance_client(self.os_creds)
71         if self.image_metadata:
72             self.glance_test_meta = self.image_metadata.get('glance_tests')
73         else:
74             self.glance_test_meta = dict()
75
76         self.tmp_dir = 'tmp/' + str(guid)
77         if not os.path.exists(self.tmp_dir):
78             os.makedirs(self.tmp_dir)
79
80     def tearDown(self):
81         """
82         Cleans the remote OpenStack objects
83         """
84         if self.image:
85             glance_utils.delete_image(self.glance, self.image)
86
87         if os.path.exists(self.tmp_dir) and os.path.isdir(self.tmp_dir):
88             shutil.rmtree(self.tmp_dir)
89
90     def test_create_image_minimal_url(self):
91         """
92         Tests the glance_utils.create_image() function with a URL unless the self.glance_test_meta has configured a
93         file to be used.
94         """
95         if 'disk_file' not in self.glance_test_meta:
96             os_image_settings = openstack_tests.cirros_image_settings(name=self.image_name,
97                                                                       image_metadata=self.glance_test_meta)
98
99             self.image = glance_utils.create_image(self.glance, os_image_settings)
100             self.assertIsNotNone(self.image)
101
102             self.assertEqual(self.image_name, self.image.name)
103
104             image = glance_utils.get_image(self.glance, os_image_settings.name)
105             self.assertIsNotNone(image)
106
107             validation_utils.objects_equivalent(self.image, image)
108         else:
109             logger.warn('Test not executed as the image metadata requires image files')
110
111     def test_create_image_minimal_file(self):
112         """
113         Tests the glance_utils.create_image() function with a file
114         """
115         if 'disk_file' not in self.glance_test_meta:
116             url_image_settings = openstack_tests.cirros_image_settings(name='foo', image_metadata=self.glance_test_meta)
117             image_file_name = file_utils.download(url_image_settings.url, self.tmp_dir).name
118         else:
119             image_file_name = self.glance_test_meta['disk_file']
120
121         file_image_settings = openstack_tests.file_image_test_settings(name=self.image_name, file_path=image_file_name)
122
123         self.image = glance_utils.create_image(self.glance, file_image_settings)
124         self.assertIsNotNone(self.image)
125         self.assertEqual(self.image_name, self.image.name)
126
127         image = glance_utils.get_image(self.glance, file_image_settings.name)
128         self.assertIsNotNone(image)
129         validation_utils.objects_equivalent(self.image, image)