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

Adds pytest testing #35

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 18 additions & 0 deletions blackbox_test/common/test_help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from conftest import wait_for_container, get_container_logs

help_commands = ['h', '-h', 'help', '--help']


@pytest.mark.parametrize('command', help_commands)
def test_help_with_help_commands(no_args_image, command):
status, container = wait_for_container(no_args_image, command)

assert status['StatusCode'] == 0, "Container exited with non-zero status"

logs = get_container_logs(container)

assert "Usage:" in logs
assert "Commands:" in logs
assert "https://github.com/artemkaxboy/docker-opener" in logs
18 changes: 18 additions & 0 deletions blackbox_test/common/test_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from conftest import wait_for_container, get_container_logs

# update_commands = ['u', 'update'] # FIXME - does not work for second update
update_commands = ['u']


@pytest.mark.parametrize('command', update_commands)
def test_update_with_no_image(no_args_image, command):
status, container = wait_for_container(no_args_image, command)

assert status['StatusCode'] == 0, "Container exited with non-zero status"

logs = get_container_logs(container)

assert "Pulling" in logs
assert "OK" in logs
18 changes: 18 additions & 0 deletions blackbox_test/common/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from conftest import wait_for_container, get_container_logs

version_commands = ['v', '-v', 'version', '--version']


@pytest.mark.parametrize('command', version_commands)
def test_version(no_args_image, command):
status, container = wait_for_container(no_args_image, command)

assert status['StatusCode'] == 0, "Container exited with non-zero status"

logs = get_container_logs(container)

assert "Version: SNAPSHOT" in logs
assert "Revision: LOCAL" in logs
assert "Created: " in logs
58 changes: 58 additions & 0 deletions blackbox_test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import docker
import pytest
from docker.models.containers import Container

docker_tag_to_test = 'artemkaxboy/opener:latest'
path_to_docker_build_context = '../dockerbuild'


@pytest.fixture(scope='session')
def no_args_image():
""" Create a docker image. """
client = docker.from_env()
image = client.images.build(path=path_to_docker_build_context, tag=docker_tag_to_test)
return image


def create_container(image, command):
""" Create a container from the image and command. """
volumes = {
'/var/run/docker.sock': {
'bind': '/var/run/docker.sock',
'mode': 'ro',
}
}
client = docker.from_env()
return client.containers.create(image[0].id, command=command, volumes=volumes)


def start_container(image, command):
""" Run a container from the image and command. """
container: Container = create_container(image, command)
container.start()
return container


def wait_for_container(image, command):
""" Wait for a container to exit. """
container: Container = start_container(image, command)
return container.wait(), container


def run_container_for_logs(image, command):
""" Run a container from the image and command, wait for it to exit and return logs. """
status, container = wait_for_container(image, command)
logs = get_container_logs(container)
container.remove()
return logs


def get_container_logs(container):
logs = container.logs()
return logs.decode('utf-8')


def test_capsys(capsys):
print("hello")
out, err = capsys.readouterr()
assert out == "hello\n"
10 changes: 6 additions & 4 deletions dockerbuild/python/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@

# todo - kill confirmation should be positive by default if only one container found

# noinspection PyShadowingBuiltins
def help():
# noinspection PyShadowingBuiltins,PyUnusedLocal
def help(*args):
"""
Prints help message.
:return: None
"""
print(help_msg)


def update():
# noinspection PyUnusedLocal
def update(*args):
"""
Updates current image tags.
:return: None
Expand All @@ -78,7 +79,8 @@ def update():
print("error: %s" % e)


def version():
# noinspection PyUnusedLocal
def version(*args):
"""
Prints current image version.
:return: None
Expand Down
19 changes: 10 additions & 9 deletions dockerbuild/python/commands/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def prepare_command(args, command, search_all=False):
"""
Prepares compose command to run.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:param command: command to prepare
:param search_all: include stopped containers
:return: None
Expand All @@ -35,7 +35,8 @@ def prepare_command(args, command, search_all=False):
system_tools.prepare_command(command % (target_name, arg_string))


def clist():
# noinspection PyUnusedLocal
def clist(*args):
"""
Prints all available compose projects and stacks with running/all containers count and compose directories.
:return: None
Expand Down Expand Up @@ -89,7 +90,7 @@ def clist_stack():
def logs(args):
"""
Prepare compose logs command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -102,7 +103,7 @@ def logs(args):
def kill(args):
"""
Prepare compose kill command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -115,7 +116,7 @@ def kill(args):
def down(args):
"""
Prepare compose down command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -128,7 +129,7 @@ def down(args):
def start(args):
"""
Prepare compose start command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -141,7 +142,7 @@ def start(args):
def stop(args):
"""
Prepare compose stop command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -154,7 +155,7 @@ def stop(args):
def top(args):
"""
Prepare compose top command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand All @@ -167,7 +168,7 @@ def top(args):
def ps(args):
"""
Prepare compose ps command.
:param args: array of command args, must have attribute to find compose at first place
:param args: array of command args, must have an attribute to find compose at first place
:return: None
:raises ValueError if no target found
"""
Expand Down
130 changes: 39 additions & 91 deletions dockerbuild/python/make_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,42 @@
from errors import OpenerBaseException
from tools import system_tools, docker_common_tools

# ------------------------------------ common commands
help_commands = ["h", "-h", "help", "--help"]
update_commands = ["u", "update"]
version_commands = ["v", "-v", "version", "--version"]

# ------------------------------------ compose commands
compose_down_commands = ["cd", "cdown", "compose-down"]
compose_kill_commands = ["ck", "ckill", "compose-kill"]
compose_logs_commands = ["cl", "clog", "clogs", "compose-logs"]
compose_list_commands = ["clist", "compose-list"]
compose_ps_commands = ["cps", "compose-ps"]
compose_start_commands = ["cstart", "compose-start"]
compose_stop_commands = ["cstop", "compose-stop"]
compose_top_commands = ["ct", "ctop", "compose-top"]

# ------------------------------------ container commands
shell_commands = ["--"]
attach_commands = ["a", "attach"]
exec_commands = ["e", "exec"]
inspect_commands = ["i", "inspect"]
kill_commands = ["k", "kill"]
logs_commands = ["l", "logs"]
rm_commands = ["rm"]

pause_commands = ["p", "pause"]
unpause_commands = ["un", "unpause"]

stop_commands = ["sto", "stop"]
start_commands = ["sta", "start"]
restart_commands = ["res", "restart"]

port_commands = ["port"]
recreate_commands = ["recreate"]
upgrade_commands = ["upgrade"]
commands = [
# ------------------------------------ common commands
{'commands': ["h", "-h", "help", "--help"], 'function': common.help},
{'commands': ["u", "update"], 'function': common.update},
{'commands': ["v", "-v", "version", "--version"], 'function': common.version},

# ------------------------------------ compose commands
{'commands': ["cd", "cdown", "compose-down"], 'function': compose.down},
{'commands': ["ck", "ckill", "compose-kill"], 'function': compose.kill},
{'commands': ["cl", "clog", "clogs", "compose-logs"], 'function': compose.logs},
{'commands': ["clist", "compose-list"], 'function': compose.clist},
{'commands': ["cps", "compose-ps"], 'function': compose.ps},
{'commands': ["cstart", "compose-start"], 'function': compose.start},
{'commands': ["cstop", "compose-stop"], 'function': compose.stop},
{'commands': ["ct", "ctop", "compose-top"], 'function': compose.top},

# ------------------------------------ container commands
{'commands': ["--"], 'function': container.shell},
{'commands': ["a", "attach"], 'function': container.attach},
{'commands': ["e", "exec"], 'function': container.docker_exec},
{'commands': ["i", "inspect"], 'function': container.inspect},
{'commands': ["k", "kill"], 'function': container.kill},
{'commands': ["l", "logs", "log"], 'function': container.logs},
{'commands': ["rm"], 'function': container.rm},

{'commands': ["p", "pause"], 'function': container.pause},
{'commands': ["un", "unpause"], 'function': container.unpause},

{'commands': ["sto", "stop"], 'function': container.stop},
{'commands': ["sta", "start"], 'function': container.start},
{'commands': ["res", "restart"], 'function': container.restart},

{'commands': ["port"], 'function': container.open_port},
{'commands': ["recreate"], 'function': container.recreate},
{'commands': ["upgrade"], 'function': container.upgrade},
]

try:
if not docker_common_tools.is_docker_available():
Expand All @@ -51,63 +53,9 @@

command = args[0]

if command in help_commands:
common.help()
elif command in update_commands:
common.update()
elif command in version_commands:
common.version()

elif command in compose_down_commands:
compose.down(args[1:])
elif command in compose_kill_commands:
compose.kill(args[1:])
elif command in compose_list_commands:
compose.clist()
elif command in compose_logs_commands:
compose.logs(args[1:])
elif command in compose_ps_commands:
compose.ps(args[1:])
elif command in compose_start_commands:
compose.start(args[1:])
elif command in compose_stop_commands:
compose.stop(args[1:])
elif command in compose_top_commands:
compose.top(args[1:])

elif command in shell_commands:
container.shell(args[1:])
elif command in attach_commands:
container.attach(args[1:])
elif command in exec_commands:
container.docker_exec(args[1:])
elif command in inspect_commands:
container.inspect(args[1:])
elif command in kill_commands:
container.kill(args[1:])
elif command in logs_commands:
container.logs(args[1:])
elif command in rm_commands:
container.rm(args[1:])

elif command in pause_commands:
container.pause(args[1:])
elif command in unpause_commands:
container.unpause(args[1:])

elif command in stop_commands:
container.stop(args[1:])
elif command in start_commands:
container.start(args[1:])
elif command in restart_commands:
container.restart(args[1:])

elif command in port_commands:
container.open_port(args[1:])
elif command in recreate_commands:
container.recreate(args[1:])
elif command in upgrade_commands:
container.upgrade(args[1:])
function = next((e['function'] for e in commands if command in e['commands']), None)
if function is not None:
function(args[1:])
else:
container.shell(args)

Expand Down