| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import logging |
| 14 |
import os |
| 15 |
import subprocess |
| 16 |
|
| 17 |
from tempest import config |
| 18 |
from tempest import test |
| 19 |
|
| 20 |
import magnum_tempest_plugin |
| 21 |
|
| 22 |
|
| 23 |
CONF = config.CONF |
| 24 |
COPY_LOG_HELPER = "magnum_tempest_plugin/tests/contrib/copy_instance_logs.sh" |
| 25 |
COPY_PODLOG_HELPER = "magnum_tempest_plugin/tests/contrib/copy_pod_logs.sh" |
| 26 |
|
| 27 |
|
| 28 |
class BaseMagnumTest(test.BaseTestCase): |
| 29 |
"""Sets up configuration required for functional tests""" |
| 30 |
|
| 31 |
LOG = logging.getLogger(__name__) |
| 32 |
|
| 33 |
def __init__(self, *args, **kwargs): |
| 34 |
super(BaseMagnumTest, self).__init__(*args, **kwargs) |
| 35 |
|
| 36 |
@classmethod |
| 37 |
def skip_checks(cls): |
| 38 |
super(BaseMagnumTest, cls).skip_checks() |
| 39 |
|
| 40 |
if not CONF.service_available.magnum: |
| 41 |
skip_msg = ("%s skipped as magnum is not available" |
| 42 |
% cls.__name__) |
| 43 |
raise cls.skipException(skip_msg) |
| 44 |
|
| 45 |
@classmethod |
| 46 |
def copy_logs_handler(cls, get_nodes_fn, coe, keypair): |
| 47 |
"""Copy logs closure. |
| 48 |
|
| 49 |
This method will retrieve all running nodes for a specified cluster |
| 50 |
and copy addresses from there locally. |
| 51 |
|
| 52 |
:param get_nodes_fn: function that takes no parameters and returns |
| 53 |
a list of node IPs which are in such form: |
| 54 |
[[master_nodes], [slave_nodes]]. |
| 55 |
:param coe: the COE type of the nodes |
| 56 |
""" |
| 57 |
def int_copy_logs(): |
| 58 |
try: |
| 59 |
func_name = "test" |
| 60 |
msg = ("Failed to copy logs for cluster") |
| 61 |
nodes_addresses = get_nodes_fn() |
| 62 |
master_nodes = nodes_addresses[0] |
| 63 |
slave_nodes = nodes_addresses[1] |
| 64 |
|
| 65 |
base_path = os.path.split(os.path.dirname( |
| 66 |
os.path.abspath(magnum_tempest_plugin.__file__)))[0] |
| 67 |
full_location = os.path.join(base_path, COPY_LOG_HELPER) |
| 68 |
|
| 69 |
def do_copy_logs(prefix, nodes_address): |
| 70 |
if not nodes_address: |
| 71 |
return |
| 72 |
|
| 73 |
msg = "copy logs from : %s" % ','.join(nodes_address) |
| 74 |
cls.LOG.info(msg) |
| 75 |
log_name = prefix + "-" + func_name |
| 76 |
for node_address in nodes_address: |
| 77 |
try: |
| 78 |
cls.LOG.debug("running %s", full_location) |
| 79 |
cls.LOG.debug("keypair: %s", keypair) |
| 80 |
subprocess.check_call([ |
| 81 |
full_location, |
| 82 |
node_address, |
| 83 |
coe, |
| 84 |
log_name, |
| 85 |
str(keypair) |
| 86 |
]) |
| 87 |
except Exception: |
| 88 |
cls.LOG.error(msg) |
| 89 |
msg = ( |
| 90 |
"failed to copy from %(node_address)s " |
| 91 |
"to %(base_path)s%(log_name)s-" |
| 92 |
"%(node_address)s" % |
| 93 |
{'node_address': node_address, |
| 94 |
'base_path': "/opt/stack/logs/magnum-nodes/", |
| 95 |
'log_name': log_name}) |
| 96 |
cls.LOG.exception(msg) |
| 97 |
raise |
| 98 |
|
| 99 |
do_copy_logs('master', master_nodes) |
| 100 |
do_copy_logs('node', slave_nodes) |
| 101 |
except Exception: |
| 102 |
cls.LOG.exception(msg) |
| 103 |
raise |
| 104 |
|
| 105 |
return int_copy_logs() |
| 106 |
|
| 107 |
@classmethod |
| 108 |
def copy_pod_logs(cls): |
| 109 |
"""Copy pod logs |
| 110 |
|
| 111 |
This method will retrieve all pod logs using bash script, |
| 112 |
expects a kube.config file under /tmp/magnum-nodes/ |
| 113 |
""" |
| 114 |
base_path = os.path.split(os.path.dirname( |
| 115 |
os.path.abspath(magnum_tempest_plugin.__file__)))[0] |
| 116 |
full_location = os.path.join(base_path, COPY_PODLOG_HELPER) |
| 117 |
|
| 118 |
try: |
| 119 |
cls.LOG.debug("running %s", full_location) |
| 120 |
subprocess.check_call([full_location]) |
| 121 |
except Exception as e: |
| 122 |
cls.LOG.exception(e) |
| 123 |
raise |