Inside a Container
Every container TestShed gives you, from shed, docker_sidecar or shed_attach, has the same helpers: execute runs commands, fs reads files and proc inspects processes.
At a Glance
- A container from any TestShed fixture.
- The image needs the tools the helpers call: a shell,
test,ls,stat,tr,catandtail.
Run Commands With execute
execute runs a command through the container's shell (/bin/sh -c) and returns its output, without the trailing newline:
def test_commands(shed):
assert shed.execute("echo hello") == "hello"
assert shed.execute(["echo", "hello"]) == "hello"
# A specific shell.
shed.execute.bash("set -o pipefail; cat /etc/os-release | head -1")
shed.execute.sh(["echo", "portable"])
shed.execute.zsh("print -r -- $ZSH_VERSION")
# Another user.
assert shed.execute("whoami", user="nobody") == "nobody"A list is joined with spaces, unquoted, before it reaches the shell. login_shell=True runs it in a login shell, and other keyword arguments (envs, workdir, …) go to python-on-whales' execute.
When a Command Fails
A command that exits non-zero fails the test with the command's error output, without a Python traceback. Pass raises=True to get a DockerException instead, to assert on it or recover:
import pytest
from kloudkit.testshed.docker import DockerException
def test_missing_file(shed):
with pytest.raises(DockerException):
shed.execute("cat /nope", raises=True)With --shed-container-logs, the container's logs are printed when a command fails the test.
Check the Exit Code
Pass check=False to get a Result instead of failing the test. It works the same with execute.bash, execute.sh and execute.zsh:
def test_exit_code(shed):
result = shed.execute("grep -q nope /etc/hostname", check=False)
assert result.exit_code == 1
assert not result.ok
result = shed.execute.bash("echo out; echo warn >&2", check=False)
assert result.stdout == "out"
assert result.stderr == "warn"Result holds exit_code, ok, stdout and stderr, each stream without its trailing newline, and keeps stderr when the command succeeds too. With check=False, only user, workdir and envs are passed on, and a missing container still fails the test.
Read Files With fs
def test_files(shed):
assert shed.fs.exists("/etc/hostname")
assert "hostname" in shed.fs.ls("/etc") # hidden=True includes dotfiles
assert "root" in shed.fs("/etc/passwd") # same as shed.fs.text(...)
raw = shed.fs.bytes("/etc/hostname")
config = shed.fs.json("/app/config.json")
manifest = shed.fs.yaml("/app/manifest.yaml")
project = shed.fs.toml("/app/pyproject.toml")
assert shed.fs.mode("/etc/hostname") == "644"
assert shed.fs.owner("/etc/hostname") == "root"
assert shed.fs.group("/etc/hostname") == "root"
assert shed.fs.writable("/tmp")
assert not shed.fs.writable("/etc/passwd", user="nobody")Files are copied out of the container, so reading works even when the path isn't readable by the container's default user. writable asks as the command's user, or the user= you pass.
Inspect Processes With proc
def test_processes(shed):
assert shed.proc.running("nginx")
assert shed.proc.pids("nginx") # tuple of matching PIDs
assert "nginx" in shed.proc.cmdline("nginx") # command line of the first match
assert shed.proc.pids("sleep infinity", full=True) # match the command line
assert shed.proc.environ()["PATH"] # environment of PID 1Names match the process name exactly. The kernel cuts process names at 15 characters, so look up a long name by its first 15. full=True on pids, running and cmdline matches the command line instead, its arguments joined with spaces, as pgrep -f does. PIDs come lowest first. cmdline raises LookupError when nothing matches, and environ takes another PID.
The Container Itself
ip(): the container's address on the session's network.whoami(): the user commands run as.env()/env(name): the environment commands see, as a dict, or one value (Nonewhen unset). Unlikeproc.environ(), which reads PID 1, it includes what the shell's profile or activation exports.wrapped: the underlying python-on-whales container, with its exact type.name,id,logs(), …: anything else is passed to the underlying python-on-whales container.
Custom Container Classes
Subclass Container to change how commands run, and set it as container_class (per call, or in shed_container_defaults):
from kloudkit.testshed.docker import Container
class AppContainer(Container):
DEFAULT_USER = "app" # user= for every command
DEFAULT_SHELL = "/bin/bash" # shell for execute(...)
LOGIN_SHELL = True # run commands in a login shell
ZSH_PATH = "/bin/zsh" # where execute.zsh finds zshBASH_PATH (/bin/bash), SH_PATH (/bin/sh) and ZSH_PATH (/usr/bin/zsh) are the shells execute.bash, execute.sh and execute.zsh use.
Next Steps
- Readiness Probes: make sure the container is ready before you use it.
- Containers: set a container class for the whole suite.