3c32eb72a03c6d02908b55cdd2af36ca890e75f5
[snaps.git] / snaps / openstack / tests / openstack_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 re
17
18 from snaps import file_utils
19 from snaps.config.image import ImageConfig
20 from snaps.config.network import NetworkConfig, SubnetConfig
21 from snaps.config.router import RouterConfig
22 from snaps.openstack.os_credentials import OSCreds, ProxySettings
23
24 __author__ = 'spisarski'
25
26 logger = logging.getLogger('openstack_tests')
27
28 CIRROS_DEFAULT_IMAGE_URL =\
29     'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
30 CIRROS_DEFAULT_KERNEL_IMAGE_URL =\
31     'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-kernel'
32 CIRROS_DEFAULT_RAMDISK_IMAGE_URL =\
33     'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-initramfs'
34 CIRROS_USER = 'cirros'
35
36 CENTOS_DEFAULT_IMAGE_URL =\
37     'http://cloud.centos.org/centos/7/images/' \
38     'CentOS-7-x86_64-GenericCloud.qcow2'
39 CENTOS_USER = 'centos'
40
41 UBUNTU_DEFAULT_IMAGE_URL = \
42     'http://uec-images.ubuntu.com/releases/trusty/14.04/' \
43     'ubuntu-14.04-server-cloudimg-amd64-disk1.img'
44 UBUNTU_USER = 'ubuntu'
45
46 DEFAULT_IMAGE_FORMAT = 'qcow2'
47
48
49 def get_credentials(os_env_file=None, proxy_settings_str=None,
50                     ssh_proxy_cmd=None, dev_os_env_file=None, overrides=None):
51     """
52     Returns the OpenStack credentials object. It first attempts to retrieve
53     them from a standard OpenStack source file. If that file is None, it will
54     attempt to retrieve them with a YAML file.
55     :param os_env_file: the OpenStack source file
56     :param proxy_settings_str: proxy settings string <host>:<port> (optional)
57     :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
58     :param dev_os_env_file: the YAML file to retrieve both the OS credentials
59                             and proxy settings
60     :param overrides: dict() containing values to override the credentials
61                       found and passed in.
62     :return: the SNAPS credentials object
63     """
64     if os_env_file:
65         logger.debug('Reading RC file - ' + os_env_file)
66         config = file_utils.read_os_env_file(os_env_file)
67         proj_name = config.get('OS_PROJECT_NAME')
68         if not proj_name:
69             proj_name = config.get('OS_TENANT_NAME')
70
71         proxy_settings = None
72         if proxy_settings_str:
73             tokens = re.split(':', proxy_settings_str)
74             proxy_settings = ProxySettings(host=tokens[0], port=tokens[1],
75                                            ssh_proxy_cmd=ssh_proxy_cmd)
76
77         https_cacert = None
78         if config.get('OS_CACERT'):
79             https_cacert = config.get('OS_CACERT')
80         elif config.get('OS_INSECURE'):
81             https_cacert = False
82
83         interface = 'public'
84         if config.get('OS_INTERFACE'):
85             interface = config.get('OS_INTERFACE')
86
87         creds_dict = {
88             'username': config['OS_USERNAME'],
89             'password': config['OS_PASSWORD'],
90             'auth_url': config['OS_AUTH_URL'],
91             'project_name': proj_name,
92             'identity_api_version': config.get('OS_IDENTITY_API_VERSION'),
93             'image_api_version': config.get('OS_IMAGE_API_VERSION'),
94             'network_api_version': config.get('OS_NETWORK_API_VERSION'),
95             'compute_api_version': config.get('OS_COMPUTE_API_VERSION'),
96             'heat_api_version': config.get('OS_HEAT_API_VERSION'),
97             'user_domain_id': config.get('OS_USER_DOMAIN_ID'),
98             'user_domain_name': config.get('OS_USER_DOMAIN_NAME'),
99             'project_domain_id': config.get('OS_PROJECT_DOMAIN_ID'),
100             'project_domain_name': config.get('OS_PROJECT_DOMAIN_NAME'),
101             'volume_api_version': config.get('OS_VOLUME_API_VERSION'),
102             'interface': interface,
103             'proxy_settings': proxy_settings,
104             'cacert': https_cacert,
105             'region_name': config.get('OS_REGION_NAME')}
106     else:
107         logger.info('Reading development os_env file - ' + dev_os_env_file)
108         config = file_utils.read_yaml(dev_os_env_file)
109
110         proxy_settings = None
111         proxy_str = config.get('http_proxy')
112         if proxy_str:
113             tokens = re.split(':', proxy_str)
114             proxy_settings = ProxySettings(
115                 host=tokens[0], port=tokens[1],
116                 ssh_proxy_cmd=config.get('ssh_proxy_cmd'))
117
118         creds_dict = {
119             'username': config['username'],
120             'password': config['password'],
121             'auth_url': config['os_auth_url'],
122             'project_name': config['project_name'],
123             'identity_api_version': config.get('identity_api_version'),
124             'image_api_version': config.get('image_api_version'),
125             'network_api_version': config.get('network_api_version'),
126             'compute_api_version': config.get('compute_api_version'),
127             'heat_api_version': config.get('heat_api_version'),
128             'user_domain_id': config.get('user_domain_id'),
129             'user_domain_name': config.get('user_domain_name'),
130             'project_domain_id': config.get('project_domain_id'),
131             'project_domain_name': config.get('project_domain_name'),
132             'volume_api_version': config.get('volume_api_version'),
133             'interface': config.get('interface'),
134             'proxy_settings': proxy_settings,
135             'cacert': config.get('cacert'),
136             'region_name': config.get('region_name')}
137
138     if overrides and isinstance(overrides, dict):
139         creds_dict.update(overrides)
140
141     for key, value in creds_dict.items():
142         if value is not None and isinstance(value, str):
143             creds_dict[key] = value.replace('"', '').replace('\'', '')
144
145     os_creds = OSCreds(**creds_dict)
146     logger.info('OS Credentials = %s', os_creds.__str__)
147     return os_creds
148
149
150 def create_image_settings(image_name, image_user, image_format, metadata,
151                           disk_url=None, default_url=None,
152                           kernel_settings=None, ramdisk_settings=None,
153                           public=False, nic_config_pb_loc=None):
154     """
155     Returns the image settings object
156     :param image_name: the name of the image
157     :param image_user: the image's sudo user
158     :param image_format: the image's format string
159     :param metadata: custom metadata for overriding default behavior for test
160                      image settings
161     :param disk_url: the disk image's URL
162     :param default_url: the default URL for the disk image
163     :param kernel_settings: override to the kernel settings from the
164                             image_metadata
165     :param ramdisk_settings: override to the ramdisk settings from the
166                              image_metadata
167     :param public: True denotes image can be used by other projects where False
168                    indicates the converse (default: False)
169     :param nic_config_pb_loc: The location of the playbook used for configuring
170                               multiple NICs
171     :return:
172     """
173
174     logger.debug('Image metadata - ' + str(metadata))
175
176     if metadata and 'config' in metadata:
177         return ImageConfig(**metadata['config'])
178
179     disk_file = None
180     if metadata and ('disk_url' in metadata or 'disk_file' in metadata):
181         disk_url = metadata.get('disk_url')
182         disk_file = metadata.get('disk_file')
183     elif not disk_url:
184         disk_url = default_url
185
186     if (metadata
187             and ('kernel_file' in metadata or 'kernel_url' in metadata)
188             and kernel_settings is None):
189         kernel_image_settings = ImageConfig(
190             name=image_name + '-kernel', image_user=image_user,
191             img_format=image_format, image_file=metadata.get('kernel_file'),
192             url=metadata.get('kernel_url'), public=public)
193     else:
194         kernel_image_settings = kernel_settings
195
196     if (metadata
197             and ('ramdisk_file' in metadata or 'ramdisk_url' in metadata)
198             and ramdisk_settings is None):
199         ramdisk_image_settings = ImageConfig(
200             name=image_name + '-ramdisk', image_user=image_user,
201             img_format=image_format,
202             image_file=metadata.get('ramdisk_file'),
203             url=metadata.get('ramdisk_url'), public=public)
204     else:
205         ramdisk_image_settings = ramdisk_settings
206
207     extra_properties = None
208     if metadata and 'extra_properties' in metadata:
209         extra_properties = metadata['extra_properties']
210
211     return ImageConfig(name=image_name, image_user=image_user,
212                        img_format=image_format, image_file=disk_file,
213                        url=disk_url, extra_properties=extra_properties,
214                        kernel_image_settings=kernel_image_settings,
215                        ramdisk_image_settings=ramdisk_image_settings,
216                        public=public,
217                        nic_config_pb_loc=nic_config_pb_loc)
218
219
220 def cirros_image_settings(name=None, url=None, image_metadata=None,
221                           kernel_settings=None, ramdisk_settings=None,
222                           public=False):
223     """
224     Returns the image settings for a Cirros QCOW2 image
225     :param name: the name of the image
226     :param url: the image's URL
227     :param image_metadata: dict() values to override URLs for disk, kernel, and
228                            ramdisk
229     :param kernel_settings: override to the kernel settings from the
230                             image_metadata
231     :param ramdisk_settings: override to the ramdisk settings from the
232                              image_metadata
233     :param public: True denotes image can be used by other projects where False
234                    indicates the converse
235     :return:
236     """
237     if image_metadata and 'cirros' in image_metadata:
238         metadata = image_metadata['cirros']
239     else:
240         metadata = image_metadata
241
242     return create_image_settings(
243         image_name=name, image_user=CIRROS_USER,
244         image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
245         default_url=CIRROS_DEFAULT_IMAGE_URL,
246         kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
247         public=public)
248
249
250 def file_image_test_settings(name, file_path, image_user=CIRROS_USER):
251     return ImageConfig(name=name, image_user=image_user,
252                        img_format=DEFAULT_IMAGE_FORMAT, image_file=file_path)
253
254
255 def centos_image_settings(name, url=None, image_metadata=None,
256                           kernel_settings=None, ramdisk_settings=None,
257                           public=False):
258     """
259     Returns the image settings for a Centos QCOW2 image
260     :param name: the name of the image
261     :param url: the image's URL
262     :param image_metadata: dict() values to override URLs for disk, kernel, and
263                            ramdisk
264     :param kernel_settings: override to the kernel settings from the
265                             image_metadata
266     :param ramdisk_settings: override to the ramdisk settings from the
267                              image_metadata
268     :param public: True denotes image can be used by other projects where False
269                    indicates the converse
270     :return:
271     """
272     if image_metadata and 'centos' in image_metadata:
273         metadata = image_metadata['centos']
274     else:
275         metadata = image_metadata
276
277     return create_image_settings(
278         image_name=name, image_user=CENTOS_USER,
279         image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
280         default_url=CENTOS_DEFAULT_IMAGE_URL,
281         kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
282         public=public)
283
284
285 def ubuntu_image_settings(name, url=None, image_metadata=None,
286                           kernel_settings=None, ramdisk_settings=None,
287                           public=False):
288     """
289     Returns the image settings for a Ubuntu QCOW2 image
290     :param name: the name of the image
291     :param url: the image's URL
292     :param image_metadata: dict() values to override URLs for disk, kernel, and
293                            ramdisk
294     :param kernel_settings: override to the kernel settings from the
295                             image_metadata
296     :param ramdisk_settings: override to the ramdisk settings from the
297                              image_metadata
298     :param public: True denotes image can be used by other projects where False
299                    indicates the converse
300     :return:
301     """
302     if image_metadata and 'ubuntu' in image_metadata:
303         metadata = image_metadata['ubuntu']
304     else:
305         metadata = image_metadata
306
307     return create_image_settings(
308         image_name=name, image_user=UBUNTU_USER,
309         image_format=DEFAULT_IMAGE_FORMAT, metadata=metadata, disk_url=url,
310         default_url=UBUNTU_DEFAULT_IMAGE_URL,
311         kernel_settings=kernel_settings, ramdisk_settings=ramdisk_settings,
312         public=public)
313
314
315 def get_priv_net_config(net_name, subnet_name, router_name=None,
316                         cidr='10.55.0.0/24', external_net=None,
317                         netconf_override=None):
318     return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
319                            external_gateway=external_net,
320                            netconf_override=netconf_override)
321
322
323 def get_pub_net_config(net_name, subnet_name=None, router_name=None,
324                        cidr='10.55.1.0/24', external_net=None,
325                        netconf_override=None):
326     return OSNetworkConfig(net_name, subnet_name, cidr, router_name,
327                            external_gateway=external_net,
328                            netconf_override=netconf_override)
329
330
331 class OSNetworkConfig:
332     """
333     Represents the settings required for the creation of a network in OpenStack
334     where netconf_override is used to reconfigure the network_type,
335     physical_network and segmentation_id
336     """
337
338     def __init__(self, net_name, subnet_name=None, subnet_cidr=None,
339                  router_name=None, external_gateway=None,
340                  netconf_override=None):
341         """
342         :param netconf_override: dict() containing the reconfigured
343                                  network_type, physical_network and
344                                  segmentation_id
345         """
346         if subnet_name and subnet_cidr:
347             network_conf = NetworkConfig(
348                 name=net_name, subnet_settings=[
349                     SubnetConfig(cidr=subnet_cidr, name=subnet_name)])
350         else:
351             network_conf = NetworkConfig(name=net_name)
352         if netconf_override:
353             network_conf.network_type = netconf_override.get('network_type')
354             network_conf.physical_network = netconf_override.get(
355                 'physical_network')
356             network_conf.segmentation_id = netconf_override.get(
357                 'segmentation_id')
358         self.network_settings = network_conf
359
360         if router_name:
361             if subnet_name:
362                 self.router_settings = RouterConfig(
363                     name=router_name, external_gateway=external_gateway,
364                     internal_subnets=[subnet_name])
365             else:
366                 self.router_settings = RouterConfig(
367                     name=router_name, external_gateway=external_gateway)