From 225053e1ae664fc4e232d9c5ab4616d372b041b9 Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Wed, 20 Feb 2019 16:58:20 +0100
Subject: [PATCH] First testinfra example tests

---
 .gitlab-ci.yml             |  3 ++-
 test/pytest/README.md      | 16 ++++++++++++++++
 test/pytest/test_docker.py | 10 ++++++++++
 test/pytest/test_system.py |  3 +++
 4 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 test/pytest/README.md
 create mode 100644 test/pytest/test_docker.py
 create mode 100644 test/pytest/test_system.py

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 36f15ba00..07963bed9 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,7 +22,7 @@ bootstrap:
   script:
     - apk update
     - apk add ansible musl-dev linux-headers gcc py3-psutil openssh-client
-    - pip3 install requests tabulate
+    - pip3 install requests tabulate testinfra
     # Ensure test/ is not world-writable otherwise ansible-playbook refuses to run, see
     # https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
     - chmod 755 test/
@@ -31,6 +31,7 @@ bootstrap:
     - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
     # - mkdir -p --mode 700 ~/.ssh
     - ANSIBLE_HOST_KEY_CHECKING=False python3 -u ./ci-bootstrap.py --create_droplet
+    - py.test -v --ansible-inventory=./inventory.yml --hosts='ansible://*'
     - python3 -c "import cosmos; cosmos.terminate_droplets_by_name(\"^ci-${CI_JOB_ID}\$\")"
   artifacts:
     paths:
diff --git a/test/pytest/README.md b/test/pytest/README.md
new file mode 100644
index 000000000..a7dbbc11b
--- /dev/null
+++ b/test/pytest/README.md
@@ -0,0 +1,16 @@
+# Run testinfra tests
+
+Test host configured in `test/inventory.yml`
+
+    py.test -v --ansible-inventory=../inventory.yml --hosts='ansible://*'
+
+Specify host manually:
+
+    py.test -v --hosts='ssh://root@varac-oas.openappstack.net'
+
+# Issues
+
+- Default ssh backend is `paramiko`, which doesn't work oout of the
+  box. It fails to connect to the host because the `ed25519` hostkey
+  was not verified. Therefore we need to force plain ssh:// with either
+  `connection=ssh` or `--hosts=ssh://…`
diff --git a/test/pytest/test_docker.py b/test/pytest/test_docker.py
new file mode 100644
index 000000000..ebac75c5b
--- /dev/null
+++ b/test/pytest/test_docker.py
@@ -0,0 +1,10 @@
+def test_docker_is_installed(host):
+    docker = host.package("docker-ce")
+    assert docker.is_installed
+    assert docker.version.startswith("5:18.09.2")
+
+
+def test_docker_running_and_enabled(host):
+    docker = host.service("docker")
+    assert docker.is_running
+    assert docker.is_enabled
diff --git a/test/pytest/test_system.py b/test/pytest/test_system.py
new file mode 100644
index 000000000..933a18e30
--- /dev/null
+++ b/test/pytest/test_system.py
@@ -0,0 +1,3 @@
+def test_release_is_bionic(host):
+    system_info = host.system_info
+    assert system_info.release == '18.04'
-- 
GitLab