import time
import logging
import uuid
-import errno
import collections
from six.moves import filter
+from jinja2 import Environment
from yardstick.benchmark.contexts.base import Context
from yardstick.benchmark.runners import base as base_runner
+from yardstick.common.yaml_loader import yaml_load
from yardstick.dispatcher.base import Base as DispatcherBase
from yardstick.common.task_template import TaskTemplate
-from yardstick.common.utils import source_env
from yardstick.common import utils
from yardstick.common import constants
+from yardstick.common.html_template import report_template
output_file_default = "/tmp/yardstick.out"
config_file = '/etc/yardstick/yardstick.conf'
task_id = getattr(args, 'task_id')
self.task_id = task_id if task_id else str(uuid.uuid4())
- check_environment()
+ self._set_log()
try:
output_config = utils.parse_ini_file(config_file)
task_args = [args.task_args]
task_args_fnames = [args.task_args_file]
- LOG.info("\ntask_files:%s, \ntask_args:%s, \ntask_args_fnames:%s",
- task_files, task_args, task_args_fnames)
+ LOG.debug("task_files:%s, task_args:%s, task_args_fnames:%s",
+ task_files, task_args, task_args_fnames)
if args.parse_only:
sys.exit(0)
except KeyboardInterrupt:
raise
except Exception:
- LOG.exception("Running test case %s failed!", case_name)
+ LOG.error('Testcase: "%s" FAILED!!!', case_name, exe_info=True)
testcases[case_name] = {'criteria': 'FAIL', 'tc_data': []}
else:
+ LOG.info('Testcase: "%s" SUCCESS!!!', case_name)
testcases[case_name] = {'criteria': 'PASS', 'tc_data': data}
if args.keep_deploy:
context.undeploy()
self.contexts = []
one_task_end_time = time.time()
- LOG.info("task %s finished in %d secs", task_files[i],
+ LOG.info("Task %s finished in %d secs", task_files[i],
one_task_end_time - one_task_start_time)
result = self._get_format_result(testcases)
self._do_output(output_config, result)
+ self._generate_reporting(result)
total_end_time = time.time()
- LOG.info("total finished in %d secs",
+ LOG.info("Total finished in %d secs",
total_end_time - total_start_time)
scenario = scenarios[0]
- print("To generate report execute => yardstick report generate ",
- scenario['task_id'], scenario['tc'])
-
- print("Done, exiting")
+ LOG.info("To generate report, execute command "
+ "'yardstick report generate %(task_id)s %(tc)s'", scenario)
+ LOG.info("Task ALL DONE, exiting")
return result
+ def _generate_reporting(self, result):
+ env = Environment()
+ with open(constants.REPORTING_FILE, 'w') as f:
+ f.write(env.from_string(report_template).render(result))
+
+ LOG.info("Report can be found in '%s'", constants.REPORTING_FILE)
+
+ def _set_log(self):
+ log_format = '%(asctime)s %(name)s %(filename)s:%(lineno)d %(levelname)s %(message)s'
+ log_formatter = logging.Formatter(log_format)
+
+ utils.makedirs(constants.TASK_LOG_DIR)
+ log_path = os.path.join(constants.TASK_LOG_DIR, '{}.log'.format(self.task_id))
+ log_handler = logging.FileHandler(log_path)
+ log_handler.setFormatter(log_formatter)
+ log_handler.setLevel(logging.DEBUG)
+
+ logging.root.addHandler(log_handler)
+
def _init_output_config(self, output_config):
output_config.setdefault('DEFAULT', {})
output_config.setdefault('dispatcher_http', {})
raise RuntimeError
self.outputs.update(runner.get_output())
result.extend(runner.get_result())
- print("Runner ended, output in", output_file)
+ LOG.info("Runner ended, output in %s", output_file)
else:
# run serially
for scenario in scenarios:
runner = self.run_one_scenario(scenario, output_file)
status = runner_join(runner)
if status != 0:
- LOG.error('Scenario: %s ERROR', scenario.get('type'))
+ LOG.error('Scenario NO.%s: "%s" ERROR!',
+ scenarios.index(scenario) + 1,
+ scenario.get('type'))
raise RuntimeError
self.outputs.update(runner.get_output())
result.extend(runner.get_result())
- print("Runner ended, output in", output_file)
+ LOG.info("Runner ended, output in %s", output_file)
# Abort background runners
for runner in background_runners:
base_runner.Runner.terminate_all()
if self.contexts:
- print("Undeploying all contexts")
+ LOG.info("Undeploying all contexts")
for context in self.contexts[::-1]:
context.undeploy()
# TODO support get multi hosts/vms info
context_cfg = {}
- if "host" in scenario_cfg:
- context_cfg['host'] = Context.get_server(scenario_cfg["host"])
+ server_name = scenario_cfg.get('options', {}).get('server_name', {})
- if "target" in scenario_cfg:
- if is_ip_addr(scenario_cfg["target"]):
- context_cfg['target'] = {}
- context_cfg['target']["ipaddr"] = scenario_cfg["target"]
+ def config_context_target(cfg):
+ target = cfg['target']
+ if is_ip_addr(target):
+ context_cfg['target'] = {"ipaddr": target}
else:
- context_cfg['target'] = Context.get_server(
- scenario_cfg["target"])
- if self._is_same_heat_context(scenario_cfg["host"],
- scenario_cfg["target"]):
- context_cfg["target"]["ipaddr"] = \
- context_cfg["target"]["private_ip"]
+ context_cfg['target'] = Context.get_server(target)
+ if self._is_same_context(cfg["host"], target):
+ context_cfg['target']["ipaddr"] = context_cfg['target']["private_ip"]
else:
- context_cfg["target"]["ipaddr"] = \
- context_cfg["target"]["ip"]
+ context_cfg['target']["ipaddr"] = context_cfg['target']["ip"]
+
+ host_name = server_name.get('host', scenario_cfg.get('host'))
+ if host_name:
+ context_cfg['host'] = Context.get_server(host_name)
+
+ for item in [server_name, scenario_cfg]:
+ try:
+ config_context_target(item)
+ except KeyError:
+ pass
+ else:
+ break
if "targets" in scenario_cfg:
ip_list = []
context_cfg['target'] = {}
else:
context_cfg['target'] = Context.get_server(target)
- if self._is_same_heat_context(scenario_cfg["host"],
- target):
+ if self._is_same_context(scenario_cfg["host"],
+ target):
ip_list.append(context_cfg["target"]["private_ip"])
else:
ip_list.append(context_cfg["target"]["ip"])
context_cfg["nodes"] = parse_nodes_with_context(scenario_cfg)
context_cfg["networks"] = get_networks_from_nodes(
context_cfg["nodes"])
+
runner = base_runner.Runner.get(runner_cfg)
- print("Starting runner of type '%s'" % runner_cfg["type"])
+ LOG.info("Starting runner of type '%s'", runner_cfg["type"])
runner.run(scenario_cfg, context_cfg)
return runner
- def _is_same_heat_context(self, host_attr, target_attr):
+ def _is_same_context(self, host_attr, target_attr):
"""check if two servers are in the same heat context
host_attr: either a name for a server created by yardstick or a dict
with attribute name mapping when using external heat templates
target_attr: either a name for a server created by yardstick or a dict
with attribute name mapping when using external heat templates
"""
- host = None
- target = None
for context in self.contexts:
- if context.__context_type__ != "Heat":
+ if context.__context_type__ not in {"Heat", "Kubernetes"}:
continue
host = context._get_server(host_attr)
try:
with open(self.path) as stream:
- cfg = yaml.safe_load(stream)
+ cfg = yaml_load(stream)
except IOError as ioerror:
sys.exit(ioerror)
def parse_task(self, task_id, task_args=None, task_args_file=None):
"""parses the task file and return an context and scenario instances"""
- print("Parsing task config:", self.path)
+ LOG.info("Parsing task config: %s", self.path)
try:
kw = {}
input_task = f.read()
rendered_task = TaskTemplate.render(input_task, **kw)
except Exception as e:
- print("Failed to render template:\n%(task)s\n%(err)s\n"
- % {"task": input_task, "err": e})
+ LOG.exception('Failed to render template:\n%s\n', input_task)
raise e
- print("Input task is:\n%s\n" % rendered_task)
+ LOG.debug("Input task is:\n%s\n", rendered_task)
- cfg = yaml.safe_load(rendered_task)
+ cfg = yaml_load(rendered_task)
except IOError as ioerror:
sys.exit(ioerror)
continue
interfaces = node.get('interfaces', {})
for interface in interfaces.values():
- vld_id = interface.get('vld_id')
- # mgmt network doesn't have vld_id
- if not vld_id:
+ # vld_id is network_name
+ network_name = interface.get('network_name')
+ if not network_name:
continue
- network = Context.get_network({"vld_id": vld_id})
+ network = Context.get_network(network_name)
if network:
networks[network['name']] = network
return networks
return args
try:
- kw = args and yaml.safe_load(args)
+ kw = args and yaml_load(args)
kw = {} if kw is None else kw
except yaml.parser.ParserError as e:
print_invalid_header(src_name, args)
return kw
-def check_environment():
- auth_url = os.environ.get('OS_AUTH_URL', None)
- if not auth_url:
- try:
- source_env(constants.OPENRC)
- except IOError as e:
- if e.errno != errno.EEXIST:
- raise
- LOG.debug('OPENRC file not found')
-
-
def change_server_name(scenario, suffix):
- try:
- host = scenario['host']
- except KeyError:
- pass
- else:
- try:
- host['name'] += suffix
- except TypeError:
- scenario['host'] += suffix
- try:
- target = scenario['target']
- except KeyError:
- pass
- else:
+ def add_suffix(cfg, key):
try:
- target['name'] += suffix
- except TypeError:
- scenario['target'] += suffix
+ value = cfg[key]
+ except KeyError:
+ pass
+ else:
+ try:
+ value['name'] += suffix
+ except TypeError:
+ cfg[key] += suffix
+
+ server_name = scenario.get('options', {}).get('server_name', {})
+
+ add_suffix(scenario, 'host')
+ add_suffix(scenario, 'target')
+ add_suffix(server_name, 'host')
+ add_suffix(server_name, 'target')
try:
key = 'targets'