diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-04-09 08:39:35 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-04-09 08:39:35 +0300 |
commit | f270c36f40edd5ead9310670c2fae60a92c06340 (patch) | |
tree | 2c5a294dea6c43413978488b3d2485158b88bc22 /funcs.py | |
parent | 23d6b03efc8c650357ddb38d9e80745cbbf77dae (diff) | |
download | ick-contractor-f270c36f40edd5ead9310670c2fae60a92c06340.tar.gz |
Fix up subplot
Diffstat (limited to 'funcs.py')
-rw-r--r-- | funcs.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/funcs.py b/funcs.py new file mode 100644 index 0000000..931ce4a --- /dev/null +++ b/funcs.py @@ -0,0 +1,83 @@ +############################################################################# +# Some helpers to make step functions simpler. + +import json +import os +import re +import subprocess +import time + +import yaml + + +# Run a subprocess, capture its output and exit code in context. +def _run(ctx, argv): + p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate("") + ctx['argv'] = argv + ctx['stdout'] = stdout.decode('utf-8') + ctx['stderr'] = stderr.decode('utf-8') + ctx['exit'] = p.returncode + + +# Return argv prefix to run contractor from source directory. This +# ignores any config from the user running the test program, but gets +# the address of the manager VM from CONTRACTOR_ADDRESS in the +# environment. +def _contractor(): + addr = os.environ['CONTRACTOR_ADDRESS'] + return [ + os.path.join(srcdir, 'contractor'), + '--no-default-config', + '--manager-address', addr, + ] + + +############################################################################# +# The actual step functions. + +# Do nothing. Useful to have bindings that are recognized, but don't +# actually do anything. +def nop(ctx, **kwargs): + pass + +# Check that we can access the contractor VM. +# FIXME: this hardcodes some things. +def contractor_is_working(ctx): + argv = _contractor() + ['manager-status'] + _run(ctx, argv) + assert_eq(ctx['exit'], 0) + + +# Create a file from the files embedded in the input document. +def create_file(ctx, filename=None): + with open(filename, 'wb') as f: + f.write(get_file(filename)) + + +# Check that the subprocess we last ran ended with the expected exit +# code. +def exit_code_is(ctx, exit_code=None): + assert_eq(ctx['exit'], int(exit_code)) + + +# Run contractor dump +def run_contractor_dump(ctx, filename=None): + argv = _contractor() + ['dump', filename] + _run(ctx, argv) + + +# Run the contractor to do a build. +def run_contractor_build(ctx, filename=None): + pass + argv = _contractor() + ['build', filename] + _run(ctx, argv) + + +# Parse stdout from latest subprocess as JSON into a dict. Read the +# named YAML file, parse as a dict. Do the two dicts match? +def stdout_json_matches_yaml_file(ctx, filename=None): + dict1 = json.loads(ctx['stdout']) + with open(filename) as f: + dict2 = yaml.safe_load(f) + assert_eq(dict1, dict2) |