# Copyright 2014 Mirantis, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import inspect import time import traceback from proboscis import asserts from fuelweb_test import logger from fuelweb_test import logwrap from fuelweb_test import settings @logwrap def get_yaml_to_json(node_ssh, file): cmd = ("python -c 'import sys, yaml, json; json.dump(" "yaml.load(sys.stdin)," " sys.stdout)' < {0}").format(file) err_res = '' res = node_ssh.execute(cmd) err_res.join(res['stderr']) asserts.assert_equal( res['exit_code'], 0, 'Command {0} execution failed ' 'with message {1}'.format(cmd, err_res)) return res['stdout'] @logwrap def nova_service_get_pid(node_ssh, nova_services=None): pid_dict = {} for el in nova_services: cmd = "pgrep {0}".format(el) pid_dict[el] = node_ssh.execute(cmd)['stdout'] logger.debug('current dict is {0}'. format(pid_dict)) return pid_dict @logwrap def check_if_service_restarted(node_ssh, services_list=None, pattern='(re)?start', skip=0): if services_list: # from the log file {2}, scan all lines after line {0} with the # pattern {1} to find restarted services, print their names to stdout cmd = ("awk 'NR >= {0} && /{1}/ {{print $11}}' {2}" .format(skip, pattern, '/var/log/puppet.log')) res = ''.join(node_ssh.execute(cmd)['stdout']) logger.debug('Next services were restarted {0}'.format(res)) for service in services_list: asserts.assert_true( any(service in x for x in res), 'Seems service {0} was not restarted {1}'.format(service, res)) @logwrap def pull_out_logs_via_ssh(admin_remote, name, logs_dirs=('/var/log/', '/root/', '/etc/fuel/')): def _compress_logs(_dirs, _archive_path): cmd = 'tar --absolute-names --warning=no-file-changed -czf {t} {d}'.\ format(t=_archive_path, d=' '.join(_dirs)) result = admin_remote.execute(cmd) if result['exit_code'] != 0: logger.error("Compressing of logs on master node failed: {0}". format(result)) return False return True archive_path = '/var/tmp/fail_{0}_diagnostic-logs_{1}.tgz'.format( name, time.strftime("%Y_%m_%d__%H_%M_%S", time.gmtime())) try: if _compress_logs(logs_dirs, archive_path): if not admin_remote.download(archive_path, settings.LOGS_DIR): logger.error(("Downloading of archive with logs failed, file" "wasn't saved on local host")) except Exception: logger.error(traceback.format_exc()) @logwrap def store_astute_yaml(env): func_name = get_test_method_name() for node in env.nodes().slaves: nailgun_node = env.fuel_web.get_nailgun_node_by_devops_node(node) if node.driver.node_active(node) and nailgun_node['roles']: try: remote = env.get_ssh_to_remote_by_name(node.name) filename = '{0}/{1}-{2}.yaml'.format(settings.LOGS_DIR, func_name, node.name) logger.info("Storing {0}".format(filename)) if not remote.download('/etc/astute.yaml', filename): logger.error("Downloading 'astute.yaml' from the node " "{0} failed.".format(node.name)) except Exception: logger.error(traceback.format_exc()) @logwrap def get_test_method_name(): # Find the name of the current test in the stack. It can be found # right under the class name 'NoneType' (when proboscis # run the test method with unittest.FunctionTestCase) stack = inspect.stack() method = '' for m in stack: if 'self' in m[0].f_locals: if m[0].f_locals['self'].__class__.__name__ == 'NoneType': break method = m[3] return method