From 7d9de999e6e5ce0421dbb031732d885945af177d Mon Sep 17 00:00:00 2001 From: Phuc LeHong Date: Wed, 24 Feb 2016 17:44:20 +0100 Subject: [PATCH] Fix util.exit_code always returning 0 --- features/steps/step_definitions.py | 26 +++++++++++--------------- tests/util.py | 10 +++++----- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/features/steps/step_definitions.py b/features/steps/step_definitions.py index 2ac575f..44ce01e 100644 --- a/features/steps/step_definitions.py +++ b/features/steps/step_definitions.py @@ -47,20 +47,15 @@ def alloc_pty(ctx, f, *args, **kwargs): client = get_client() f(client, *args, **kwargs) sys.exit(0) - else: - ctx.pty = fd - util.set_pty_size( - ctx.pty, - (ctx.rows, ctx.cols) - ) - ctx.pid = pid - util.wait(ctx.pty, timeout=5) - time.sleep(1) # give the terminal some time to print prompt - - # util.exit_code can be called only once - ctx.exit_code = util.exit_code(ctx.pid, timeout=5) - if ctx.exit_code != 0: - raise Exception("child process did not finish correctly") + + ctx.pty = fd + util.set_pty_size( + ctx.pty, + (ctx.rows, ctx.cols) + ) + ctx.pid = pid + ctx.exit_code = -1 + util.wait(ctx.pty, timeout=5) @given('I am using a TTY') @@ -199,7 +194,8 @@ def step_impl(ctx): @then('The PTY will be closed cleanly') def step_impl(ctx): - if not hasattr(ctx, "exit_code"): + # if not hasattr(ctx, "exit_code"): + if ctx.exit_code < 0: ctx.exit_code = util.exit_code(ctx.pid, timeout=5) expect(ctx.exit_code).to(equal(0)) diff --git a/tests/util.py b/tests/util.py index 9b4f5ab..d86453c 100644 --- a/tests/util.py +++ b/tests/util.py @@ -136,12 +136,12 @@ def exit_code(pid, timeout=5): start = time.time() while True: - _, status = os.waitpid(pid, os.WNOHANG) - if os.WIFEXITED(status): + rpid, status = os.waitpid(pid, os.WNOHANG) + if rpid != 0 and os.WIFEXITED(status): return os.WEXITSTATUS(status) - else: - if (time.time() - start) > timeout: - return -1 + if (time.time() - start) > timeout: + return -1 + time.sleep(0.1) def container_running(client, container, duration=2):