From e0c099011fe902c52933992bc5e70c9a3e363c52 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 14 Aug 2020 10:17:23 +0300 Subject: fix: use files in srcdir for passing in manager address, env vars Subplot now clears the environment before running scenarios so we can't use $CONTRACTOR_ADDRESS to pass in the manager's address. We also need the SSH agent's address so we can authenticate into the manager. set up test.env get env from test.env --- check | 4 +++- funcs.py | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/check b/check index cde86a6..bc57b7a 100755 --- a/check +++ b/check @@ -4,4 +4,6 @@ set -eu rm -f test.py black --check contractor *.py -CONTRACTOR_ADDRESS="$1" sp-codegen contractor.md -o test.py --run +echo "$1" > test.address +echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK" > test.env +sp-codegen contractor.md -o test.py --run diff --git a/funcs.py b/funcs.py index f028730..fb772d0 100644 --- a/funcs.py +++ b/funcs.py @@ -2,6 +2,7 @@ # Some helpers to make step functions simpler. import json +import logging import os import re import subprocess @@ -11,10 +12,11 @@ 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) +def _run(ctx, argv, env): + p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) stdout, stderr = p.communicate("") ctx["argv"] = argv + ctx["env"] = env ctx["stdout"] = stdout.decode("utf-8") ctx["stderr"] = stderr.decode("utf-8") ctx["exit"] = p.returncode @@ -30,7 +32,31 @@ def _contractor(): # Return manager address. def _manager_address(): - return os.environ["CONTRACTOR_ADDRESS"] + filename = os.path.join(srcdir, "test.address") + addr = open(filename).read().strip() + logging.debug("Manager address from %s is %r", filename, addr) + return addr + + +# Create a config file for Contractor. Return its filename. +def _create_config(ctx): + filename = "config.yaml" + if ctx.get("config") is None: + ctx["config"] = {"manager_address": _manager_address(), "verbose": True} + with open(filename, "w") as f: + yaml.safe_dump(ctx["config"], stream=f) + logging.debug("config file: %r", open(filename).read()) + return filename + + +# Create dict with environment variables for running Contractor, merging +# os.environ and srcdir/test.env. +def _create_env(): + env = dict(os.environ) + for line in open(os.path.join(srcdir, "test.env")): + name, value = line.split("=", 1) + env[name] = value.strip() + return env ############################################################################# @@ -45,8 +71,9 @@ def nop(ctx, **kwargs): # Check that we can access the contractor VM. # FIXME: this hardcodes some things. def contractor_is_working(ctx): - argv = _contractor() + ["status", "-m", _manager_address()] - _run(ctx, argv) + config = _create_config(ctx) + argv = _contractor() + ["-c", config, "status"] + _run(ctx, argv, _create_env()) assert_eq(ctx["exit"], 0) @@ -75,13 +102,14 @@ def exit_code_is(ctx, exit_code=None): # Run contractor dump def run_contractor_dump(ctx, filename=None): argv = _contractor() + ["dump", filename] - _run(ctx, argv) + _run(ctx, argv, _create_env()) # Run the contractor to do a build. def run_contractor_build(ctx, filename=None): - argv = _contractor() + ["build", filename, "-m", _manager_address()] - _run(ctx, argv) + config = _create_config(ctx) + argv = _contractor() + ["-c", config, "build", filename] + _run(ctx, argv, _create_env()) # Parse stdout from latest subprocess as JSON into a dict. Read the -- cgit v1.2.1