diff --git a/.gitignore b/.gitignore index 894a44c..a65914e 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,9 @@ venv.bak/ # mypy .mypy_cache/ + +# IDEs and Editors +.vscode/ + +# Robot Framework test results +results/ \ No newline at end of file diff --git a/README.md b/README.md index ae3a17a..2ba01aa 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,51 @@ # robotframework-dockerlibrary -A Robot Framework library to control Docker containers + +## What? +A [Robot Framework](https://robotframework.org/) library to control [Docker](https://www.docker.com/) containers It will mostly be a wrapper around [docker.py](https://github.com/docker/docker-py) - the Python library for the Docker Engine API [1][2] + +## Why? +because + + +## How to get started? +``` +cd tests/ +pip install -r requirements.txt +robot -d results -L TRACE robot/ + +# check logs.html in results folder (auto-created after test run) +# look at the code of the lib and the tests +# You got the idea! +``` +> NOTE: The initial run will take a while because MongoDB Docker image (aprox. 400 MB) will be pulled in the background. ... may be I should use a smaller image for demo :-) + + +FEATURES +--- + + - Docker Containers + - run, stop, remove, inspect + + - Docker Services + - ... + +Status +--- +PRE-ALPHA :-) + + +Contributiion +--- +Clone/fork, create a pull-request, create an issue ... what ever + +Any contribution is welcome! + + + + [1] https://github.com/docker/docker-py [2] https://docker-py.readthedocs.io/en/stable/ diff --git a/dockerlibrary/dockerlibrary.py b/dockerlibrary/dockerlibrary.py new file mode 100644 index 0000000..187348c --- /dev/null +++ b/dockerlibrary/dockerlibrary.py @@ -0,0 +1,35 @@ +import docker +client = docker.from_env() + + +def run_mongodb_container(): + """run a mondodb container in background""" + container = client.containers.run("mongo:latest", name="MongoDB", + ports={'27017/tcp': 27017}, + detach=True) + container = client.containers.get("MongoDB") + logs = container.logs() + return logs + +def stop_mongodb_container(): + container = client.containers.get("MongoDB") + container.stop() + status = container.wait(condition="not-running") + logs = container.logs() + return logs, status + +def remove_mongodb_container(): + container = client.containers.get("MongoDB") + container.remove() + + try: + status = container.wait(condition='removed') + # GITHUB: https://github.com/docker/docker-py/issues/2270 + except docker.errors.NotFound: + pass + return status + +def get_logs_from_mongobd(): + container = client.containers.get("MongoDB") + logs = container.logs() + return logs diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..68bdd2d --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,2 @@ +docker +robotframework \ No newline at end of file diff --git a/tests/robot/container_tests.robot b/tests/robot/container_tests.robot new file mode 100644 index 0000000..6481f12 --- /dev/null +++ b/tests/robot/container_tests.robot @@ -0,0 +1,74 @@ +*** Settings *** +Documentation Container Test of the Robot Framework DockerLibrary + ... + +Library OperatingSystem +Library String + +Library ${EXECDIR}${/}../dockerlibrary/dockerlibrary.py + +# Suite Setup +# Suite Teardown +Test Setup start mongodb +Test Teardown stop and remove mongodb + +Force Tags container mongodb + + +*** Variables *** + + + + +*** Test Cases *** +MONGO TEST I + THIS IS JUST A PLACEHOLDER! + +MONGO TEST II + THIS IS JUST A PLACEHOLDER! + +MONGO TEST III + THIS IS JUST A PLACEHOLDER! + + + +*** Keywords *** +start mongodb + run mongodb container + wait until mongodb is ready + +stop mongodb + ${logs} ${status} stop mongodb container + Log ${status} + wait until mongodb is stopped + Should Be Equal As Integers ${status}[StatusCode] 0 + +stop and remove mongodb + stop mongodb + remove mongodb container + +reset mongodb + stop mongodb + remove mongodb container + start mongodb + +mongodb is stopped + ${logs}= get logs from mongobd + ${mongo_logs}= Convert To String ${logs} + Should Contain ${mongo_logs} [signalProcessingThread] shutting down with code:0 + +mongodb is ready + ${logs}= get logs from mongobd + ${mongo_logs}= Convert To String ${logs} + Should Contain ${mongo_logs} [LogicalSessionCacheRefresh] build index done + Should Contain ${mongo_logs} scanned 0 total records + +wait until mongodb is stopped + Wait Until Keyword Succeeds 10 sec 3 sec mongodb is stopped + +wait until mongodb is ready + Wait Until Keyword Succeeds 10 sec 3 sec mongodb is ready + +THIS IS JUST A PLACEHOLDER! + Pass Execution Robot Rules Test Automation! + [Teardown] Set Tags ROBOT DOCKER