Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return exit code from exec_command() #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dockerpty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ def start(client, container, interactive=True, stdout=None, stderr=None, stdin=N

def exec_command(
client, container, command, interactive=True, stdout=None, stderr=None, stdin=None):
"""
Run provided command via exec API in provided container.
"""Run provided command via exec API in provided container.

This is just a wrapper for PseudoTerminal(client, container).exec_command()

Returns a dictionary that allows you to fetch the exit code with exec_command(..)['ExitCode']
"""
exec_id = exec_create(client, container, command, interactive=interactive)

operation = ExecOperation(client, exec_id,
interactive=interactive, stdout=stdout, stderr=stderr, stdin=stdin)
PseudoTerminal(client, operation).start()
return client.exec_inspect(exec_id)


def start_exec(client, exec_id, interactive=True, stdout=None, stderr=None, stdin=None):
Expand Down
8 changes: 8 additions & 0 deletions features/exec_non_interactive.feature
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ Feature: Executing command in a running docker container non-interactively
nobody:x:99:99:nobody:/home:/bin/false
"""
And The container will still be running


Scenario: Getting the exit code
Given I am using a TTY
And I run "cat" in a docker container with stdin open
And I start the container
When I exec "sh -c 'exit 4'" in a running docker container
Then the exit code is 4
14 changes: 12 additions & 2 deletions features/steps/step_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import time

from utils import get_client

from multiprocessing.queues import SimpleQueue

def alloc_pty(ctx, f, *args, **kwargs):
queue = SimpleQueue()
pid, fd = pty.fork()

if pid == pty.CHILD:
Expand All @@ -45,7 +46,8 @@ def alloc_pty(ctx, f, *args, **kwargs):

# Create a new client for the child process to avoid concurrency issues
client = get_client()
f(client, *args, **kwargs)
ret = f(client, *args, **kwargs)
queue.put(ret)
sys.exit(0)
else:
ctx.pty = fd
Expand All @@ -62,6 +64,9 @@ def alloc_pty(ctx, f, *args, **kwargs):
if ctx.exit_code != 0:
raise Exception("child process did not finish correctly")

if not queue.empty():
ctx.return_value = queue.get()


@given('I am using a TTY')
def step_impl(ctx):
Expand Down Expand Up @@ -197,6 +202,11 @@ def step_impl(ctx):
expect(actual[-len(wanted):]).to(equal(wanted))


@then('The exit code is {value}')
def step_impl(ctx, value):
expect(ctx.return_value['ExitCode']).to(equal(int(value)))


@then('The PTY will be closed cleanly')
def step_impl(ctx):
if not hasattr(ctx, "exit_code"):
Expand Down