From 1e5c9da51ba80e59e4f616ab46b2c05b201ec89c Mon Sep 17 00:00:00 2001 From: Ilya Shakhat Date: Tue, 10 Mar 2015 17:31:41 +0300 Subject: [PATCH] Allow to store and read execution results in JSON format Change-Id: I92a197dbe18bde44d2304e94aa034fb9a1c333ea --- etc/shaker.conf | 7 ++ scenarios/misc/static_agent.yaml | 15 ++++ ...ic_agents.yaml => static_agents_pair.yaml} | 0 shaker/engine/config.py | 10 ++- shaker/engine/server.py | 75 ++++++++++++------- shaker/engine/utils.py | 14 ++++ 6 files changed, 93 insertions(+), 28 deletions(-) create mode 100644 scenarios/misc/static_agent.yaml rename scenarios/misc/{static_agents.yaml => static_agents_pair.yaml} (100%) diff --git a/etc/shaker.conf b/etc/shaker.conf index 9d36ed2..96df3e5 100644 --- a/etc/shaker.conf +++ b/etc/shaker.conf @@ -137,6 +137,13 @@ # report is printed to stdout. (string value) #report = +# File for output in JSON format, defaults to env[SHAKER_OUTPUT]. (string +# value) +#output = + +# File to read test results from, defaults to env[SHAKER_INPUT]. (string value) +#input = + # # From shaker.engine.config # diff --git a/scenarios/misc/static_agent.yaml b/scenarios/misc/static_agent.yaml new file mode 100644 index 0000000..c4e7021 --- /dev/null +++ b/scenarios/misc/static_agent.yaml @@ -0,0 +1,15 @@ +description: + This scenario runs tests on pre-deployed static agents + +deployment: + agents: + - + id: the-agent + mode: alone + +execution: + tests: + - + title: List all files + class: shell + method: ls -al diff --git a/scenarios/misc/static_agents.yaml b/scenarios/misc/static_agents_pair.yaml similarity index 100% rename from scenarios/misc/static_agents.yaml rename to scenarios/misc/static_agents_pair.yaml diff --git a/shaker/engine/config.py b/shaker/engine/config.py index dabe934..f51be25 100644 --- a/shaker/engine/config.py +++ b/shaker/engine/config.py @@ -74,7 +74,6 @@ OPENSTACK_OPTS = [ SERVER_OPTS = [ cfg.StrOpt('scenario', default=utils.env('SHAKER_SCENARIO'), - required=True, help='Scenario file name, defaults to env[SHAKER_SCENARIO].'), cfg.StrOpt('report-template', @@ -85,6 +84,15 @@ SERVER_OPTS = [ default=utils.env('SHAKER_REPORT'), help='Report file name, defaults to env[SHAKER_REPORT]. ' 'If no value provided the report is printed to stdout.'), + + cfg.StrOpt('output', + default=utils.env('SHAKER_OUTPUT'), + help='File for output in JSON format, ' + 'defaults to env[SHAKER_OUTPUT].'), + cfg.StrOpt('input', + default=utils.env('SHAKER_INPUT'), + help='File to read test results from, ' + 'defaults to env[SHAKER_INPUT].'), ] diff --git a/shaker/engine/server.py b/shaker/engine/server.py index 4ae3155..ba254cc 100644 --- a/shaker/engine/server.py +++ b/shaker/engine/server.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import logging as std_logging import os import time @@ -207,6 +208,9 @@ def main(): try: conf(project='shaker') utils.validate_required_opts(conf, opts) + if not cfg.CONF.scenario and not cfg.CONF.input: + raise cfg.RequiredOptError('One of "scenario" or "input" options ' + 'must be set') except cfg.RequiredOptError as e: print('Error: %s' % e) conf.print_usage() @@ -216,38 +220,55 @@ def main(): LOG.info('Logging enabled') conf.log_opt_values(LOG, std_logging.DEBUG) - deployment = None - try: + report_data = None + + if cfg.CONF.scenario: + # run scenario scenario = read_scenario() - deployment = deploy.Deployment(cfg.CONF.os_username, - cfg.CONF.os_password, - cfg.CONF.os_tenant_name, - cfg.CONF.os_auth_url, - cfg.CONF.os_region_name, - cfg.CONF.server_endpoint, - cfg.CONF.external_net, - cfg.CONF.flavor_name, - cfg.CONF.image_name) - agents = deployment.deploy(scenario['deployment'], - base_dir=os.path.dirname(cfg.CONF.scenario)) - if not agents: - LOG.info('No agents deployed. Terminating.') - return + deployment = None + agents = {} + result = [] - LOG.debug('Agents: %s', agents) + try: + deployment = deploy.Deployment(cfg.CONF.os_username, + cfg.CONF.os_password, + cfg.CONF.os_tenant_name, + cfg.CONF.os_auth_url, + cfg.CONF.os_region_name, + cfg.CONF.server_endpoint, + cfg.CONF.external_net, + cfg.CONF.flavor_name, + cfg.CONF.image_name) - result = execute(scenario['execution'], agents) - LOG.debug('Result: %s', result) + agents = deployment.deploy( + scenario['deployment'], + base_dir=os.path.dirname(cfg.CONF.scenario)) + LOG.debug('Deployed agents: %s', agents) - report.generate_report(cfg.CONF.report_template, - cfg.CONF.report, - dict(scenario=yaml.dump(scenario), - agents=agents.values(), - result=result)) - finally: - if deployment: - deployment.cleanup() + if not agents: + LOG.warning('No agents deployed.') + else: + result = execute(scenario['execution'], agents) + LOG.debug('Result: %s', result) + except Exception as e: + LOG.error('Error while executing scenario: %s', cfg.CONF.scenario) + finally: + if deployment: + deployment.cleanup() + + report_data = dict(scenario=yaml.dump(scenario), + agents=agents.values(), + result=result) + if cfg.CONF.output: + utils.write_file(json.dumps(report_data), cfg.CONF.output) + + elif cfg.CONF.input: + # read json results + report_data = json.loads(utils.read_file(cfg.CONF.input)) + + report.generate_report(cfg.CONF.report_template, cfg.CONF.report, + report_data) if __name__ == "__main__": diff --git a/shaker/engine/utils.py b/shaker/engine/utils.py index 9b7b9fd..5dac243 100644 --- a/shaker/engine/utils.py +++ b/shaker/engine/utils.py @@ -58,6 +58,20 @@ def read_file(file_name, base_dir=''): fd.close() +def write_file(data, file_name, base_dir=''): + full_path = os.path.normpath(os.path.join(base_dir, file_name)) + fd = None + try: + fd = open(full_path, 'w') + return fd.write(data) + except IOError as e: + LOG.error('Error writing file: %s', e) + raise + finally: + if fd: + fd.close() + + def split_address(address): try: host, port = address.split(':')