dockerpty
dockerpty copied to clipboard
How to feature test tools built on dockerpty?
I have been using dockerpty and find it very useful. Thank you for creating and releasing it - managing ttys seems very complicated and I absolutely sure I would have never been able to achieve the funtionality this library provides myself.
I am using this library to write a tool that makes it simpler for users who are not familiar with Docker (in this case biologists) to be able to use Docker containers in their work. I've managed to get create a command to launch a tty using dockerpty, this was very straight forward.
I would however like to create feature tests to make sure that this tty
functionality is not broken in future releases of the tool. I am able to
manually drive the tool and test it works but obviously automated tests on a CI
server are more desireable. Having a behave drive these tests however seems
quite difficult and I just seem to only be able to get the tests to hang when
the tty is launched. I've looked through the step_definitions
in this project
however they are bit beyond me. Documentation online related to subprocesses
and ptys is quite dense and I'm not sure if I'm working in the right direction.
I was hoping that by opening this issue you might be able to provide some direction, or perhaps give a simple example of where I might get started with integrating this kind of test into behave. My non-working features are here if that is of any use.
Thank you.
I think you might want to copy some of the steps from the dockerpty suite.
An important one is https://github.com/d11wtq/dockerpty/blob/master/features/steps/step_definitions.py#L74 @when('I start dockerpty')
, which sets up the environment.
It's been a while since I've worked with these tests, but I think if you replace the dockerpty.start()
with your subprocess.Popen
, and you set stdin/out/err to the ttys like the dockerpty step does, you should be able to get it to work.
Thanks for your quick response. Could you tell me what pid == pty.CHILD
is
checking for in this step? In my behave code this evaluates to False, and so
the second clause would be evaluated. The second clause does not contain a call
to dockerpty.start
and so I'm not sure how I should put in a
subprocess.Popen
call.
In subprocess.Popen
, the stdin, stderr and stdout have to be defined. In many
examples I see subprocess.PIPE
used, for a tty would I replace this with the
result of os.ttyname
?
Thank you again.
It's literally forking, so your code would hit both branches, just in different processes (https://docs.python.org/2/library/pty.html#pty.fork)
Check out the docs for subprocess (https://docs.python.org/2/library/subprocess.html#subprocess.Popen)
stdin, stdout and stderr ... Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None.
In the dockerpty code it's being assigned a file object, so you can use that with subprocess as well.
In my (quite complicated to be honest) functional tests for Harpoon I swap out stdout and stderr for temporary files and then compare the output with what I expect https://github.com/realestate-com-au/harpoon/blob/master/tests/docker/test_docker_run.py
Thanks for the feedback. I ended up using the library pexpect. This allowed me to wait for the prompt based on a regex before sending input. This is a bit of hack but I wasn't fully able to grok this topic.