alpy.container¶
In Alpy, containers are managed using the Docker SDK for Python. This module contains a few extra functions for managing containers. The functions are helpful when testing a network appliance. The functions do primarily the following tasks:
Write container logs via Python
logging
module.Write container exit code to the log.
Check container exit code.
Configure container interface.
- alpy.container.add_default_route(container_name, gateway, *, docker_client, image, timeout)[source]¶
Add the default route in a network namespace of a running container.
This function spawns a temporary container which shares a network namespace with container container_name. The temporary container adds the default gateway using ip command.
- Parameters:
container_name (str) – container ID or name
gateway (str) – the default gateway IP address
docker_client (
docker.client.DockerClient
) – Docker clientimage (str) – ID or name of Docker image containing ip executable, for example, “busybox:latest”
timeout (int) – maximum number of seconds to wait. The function waits for the temporary container to stop.
- alpy.container.add_ip_address(container_name, address, *, docker_client, image, timeout)[source]¶
Add an ip address to a network interface of a running container.
This function spawns a temporary container which shares a network namespace with container container_name. The temporary container configures the interface using ip command.
- Parameters:
container_name (str) – container ID or name
address (str) – IP address followed by a slash and network prefix length, for example, “192.168.1.2/24”
docker_client (
docker.client.DockerClient
) – Docker clientimage (str) – ID or name of Docker image containing ip executable, for example, “busybox:latest”
timeout (int) – maximum number of seconds to wait. The function waits for the temporary container to configure the interface.
- alpy.container.check_exit_code(code)[source]¶
Check container exit code.
- Parameters:
code (int) – container exit code
- Raises:
alpy.utils.NonZeroExitCode – if code is not zero
- alpy.container.close(container, timeout)[source]¶
Wait for a container to stop. If the container does not stop, kill it.
Check the exit code. Write container logs and the exit code via Python
logging
module.- Parameters:
container (
docker.models.containers.Container
) – containertimeout (int) – maximum number of seconds to wait
- alpy.container.configure_interface(container_name, address, gateway=None, *, docker_client, image, timeout)[source]¶
Configure a network interface of a running container.
This function spawns a temporary container which shares a network namespace with container container_name. The temporary container configures the interface using ip command.
- Parameters:
container_name (str) – container ID or name
address (str) – IP address followed by a slash and network prefix length, for example, “192.168.1.2/24”
gateway (str) – the default gateway IP address. If omitted, the default route is not configured.
docker_client (
docker.client.DockerClient
) – Docker clientimage (str) – ID or name of Docker image containing ip executable, for example, “busybox:latest”
timeout (int) – maximum number of seconds to wait. The function waits for the temporary container to configure the interface.
- alpy.container.get_signal_number_from_exit_code(code)[source]¶
Convert container exit code to signal number.
- alpy.container.log_exit_code(code, name)[source]¶
Log container exit code via Python
logging
module.Log message examples:
“Container apache exited with code 0”
“Container apache was killed by signal SIGKILL”
- alpy.container.stop(container, timeout, signal='SIGTERM')[source]¶
Kill a container and wait until it stops.
Write the container logs and exit code via Python
logging
module.- Parameters:
container (
docker.models.containers.Container
) – containertimeout (int) – maximum number of seconds to wait
signal (str) – signal to send
- Returns:
exit code
- Return type:
- alpy.container.wait_running(container, timeout)[source]¶
Wait for a container to start.
- Parameters:
container (
docker.models.containers.Container
) – containertimeout (int) – maximum number of seconds to wait
- Raises:
alpy.container.Timeout – if the container does not start before timeout expires.
- alpy.container.write_logs(container)[source]¶
Write container logs via Python
logging
module.- Parameters:
container (
docker.models.containers.Container
) – container