From fc19dbca1e10776dea0343f360a2ddf3c8ae968d Mon Sep 17 00:00:00 2001 From: Tset Noitamotua Date: Sat, 9 Mar 2019 02:20:09 +0100 Subject: [PATCH 1/4] demo of idea --- .gitignore | 6 +++ README.md | 33 ++++++++++++++ dockerlibrary/dockerlibrary.py | 31 +++++++++++++ tests/requirements.txt | 2 + tests/robot/container_tests.robot | 74 +++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 dockerlibrary/dockerlibrary.py create mode 100644 tests/requirements.txt create mode 100644 tests/robot/container_tests.robot 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..3828222 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,41 @@ # robotframework-dockerlibrary + +## What? A Robot Framework library to control Docker 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 +# 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 :-) + + [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..abde290 --- /dev/null +++ b/dockerlibrary/dockerlibrary.py @@ -0,0 +1,31 @@ +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() + # OPEN ISSUE: https://github.com/docker/docker-py/issues/2270 + # status = container.wait(condition="removed") + return + +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 From 0ebaa8767adcda3013ee2b2103131ebcddbd7d29 Mon Sep 17 00:00:00 2001 From: Tset Noitamotua Date: Sat, 9 Mar 2019 02:23:25 +0100 Subject: [PATCH 2/4] demo of idea --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3828222..afdf4cb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ cd tests/ pip install -r requirements.txt robot -d results -L TRACE robot/ -# check logs.html in results folder +# 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! ``` From 26bc674f55b88102270fd3c7d7667df74defc1fa Mon Sep 17 00:00:00 2001 From: Tset Noitamotua Date: Sat, 9 Mar 2019 03:55:50 +0100 Subject: [PATCH 3/4] demo of idea --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index afdf4cb..2ba01aa 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # robotframework-dockerlibrary ## What? -A Robot Framework library to control Docker containers +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] @@ -22,6 +22,7 @@ robot -d results -L TRACE robot/ ``` > 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 --- @@ -31,11 +32,20 @@ FEATURES - Docker Services - ... -STATUS +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/ From 96b0a8d95ecd5718626235bcd00f4d8d60a34a4c Mon Sep 17 00:00:00 2001 From: Tset Noitamotua Date: Thu, 20 Jun 2019 21:05:08 +0200 Subject: [PATCH 4/4] added wait.condition=removed --- dockerlibrary/dockerlibrary.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/dockerlibrary/dockerlibrary.py b/dockerlibrary/dockerlibrary.py index abde290..187348c 100644 --- a/dockerlibrary/dockerlibrary.py +++ b/dockerlibrary/dockerlibrary.py @@ -21,9 +21,13 @@ def stop_mongodb_container(): def remove_mongodb_container(): container = client.containers.get("MongoDB") container.remove() - # OPEN ISSUE: https://github.com/docker/docker-py/issues/2270 - # status = container.wait(condition="removed") - return + + 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")