| 1 |
# Copyright 2018 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# 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, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import os |
| 17 |
import urllib |
| 18 |
|
| 19 |
from tenacity import retry |
| 20 |
from tenacity import retry_if_exception_type |
| 21 |
from tenacity import stop_after_attempt |
| 22 |
from tenacity import wait_exponential |
| 23 |
|
| 24 |
from config_tempest import constants as C |
| 25 |
from config_tempest.services.base import VersionedService |
| 26 |
import json |
| 27 |
|
| 28 |
|
| 29 |
class LoadBalancerService(VersionedService): |
| 30 |
def set_versions(self): |
| 31 |
super(LoadBalancerService, self).set_versions(top_level=False) |
| 32 |
|
| 33 |
def set_default_tempest_options(self, conf): |
| 34 |
conf.set('load_balancer', 'enable_security_groups', 'True') |
| 35 |
conf.set('load_balancer', 'admin_role', 'admin') |
| 36 |
conf.set('load_balancer', 'RBAC_test_type', 'owner_or_admin') |
| 37 |
conf.set('network-feature-enabled', 'port_security', 'True') |
| 38 |
# TOOD(gthiemonge) This is a backward-compatible setting for jobs |
| 39 |
# that haven't yet migrated to load_balancer.test_server_remote_url |
| 40 |
# Remove it once all jobs have been migrated. |
| 41 |
conf.set('load_balancer', 'test_server_path', |
| 42 |
C.DEFAULT_OCTAVIA_COMPAT_TEST_SERVER_FILE) |
| 43 |
|
| 44 |
@staticmethod |
| 45 |
def get_service_type(): |
| 46 |
return ['load-balancer'] |
| 47 |
|
| 48 |
@staticmethod |
| 49 |
def get_codename(): |
| 50 |
return 'octavia' |
| 51 |
|
| 52 |
def list_drivers(self): |
| 53 |
"""List lbaas drivers""" |
| 54 |
body = self.do_get(self.service_url + '/v2/lbaas/providers') |
| 55 |
body = json.loads(body) |
| 56 |
names = [ |
| 57 |
'{p[name]}:{p[description]}'.format(p=i) for i in body['providers'] |
| 58 |
] |
| 59 |
return names |
| 60 |
|
| 61 |
def post_configuration(self, conf, is_service): |
| 62 |
if not conf.has_option('auth', 'tempest_roles') \ |
| 63 |
or conf.get('auth', 'tempest_roles') in ['', None]: |
| 64 |
conf.set('load_balancer', 'member_role', 'member') |
| 65 |
else: |
| 66 |
conf.set('load_balancer', 'member_role', |
| 67 |
conf.get('auth', 'tempest_roles').split(',')[0]) |
| 68 |
conf.set('load_balancer', 'region', conf.get('identity', 'region')) |
| 69 |
conf.set('load_balancer', |
| 70 |
'enabled_provider_drivers', |
| 71 |
','.join(self.list_drivers())) |
| 72 |
|
| 73 |
@retry(retry=retry_if_exception_type(urllib.error.URLError), |
| 74 |
stop=stop_after_attempt(3), |
| 75 |
wait=wait_exponential(multiplier=2, min=3, max=10)) |
| 76 |
def _download_file(self, url, destination): |
| 77 |
"""Downloads a file specified by `url` to `destination`. |
| 78 |
|
| 79 |
:type url: string |
| 80 |
:type destination: string |
| 81 |
""" |
| 82 |
if os.path.exists(destination): |
| 83 |
C.LOG.info("File '%s' already fetched to '%s'.", url, destination) |
| 84 |
return |
| 85 |
C.LOG.info("Downloading '%s' and saving as '%s'", url, destination) |
| 86 |
f = urllib.request.urlopen(url) |
| 87 |
data = f.read() |
| 88 |
with open(destination, "wb") as dest: |
| 89 |
dest.write(data) |
| 90 |
|
| 91 |
def get_test_server_application(self, conf): |
| 92 |
if not conf.has_option("load_balancer", |
| 93 |
"test_server_remote_url"): |
| 94 |
return |
| 95 |
|
| 96 |
remote_url = conf.get("load_balancer", |
| 97 |
"test_server_remote_url") |
| 98 |
if remote_url: |
| 99 |
self._download_file(remote_url, |
| 100 |
C.DEFAULT_OCTAVIA_TEST_SERVER_FILE) |
| 101 |
os.chmod(C.DEFAULT_OCTAVIA_TEST_SERVER_FILE, 0o755) |
| 102 |
|
| 103 |
conf.set("load_balancer", |
| 104 |
"test_server_path", |
| 105 |
C.DEFAULT_OCTAVIA_TEST_SERVER_FILE) |