Molecule role tests for setup role

Issue-ID: OOM-1742

Change-Id: I3087952f3d4e5de20af83e714c0d5444999a5ee2
Signed-off-by: Samuli Silvius <s.silvius@partner.samsung.com>
diff --git a/ansible/roles/setup/molecule/default/Dockerfile.j2 b/ansible/roles/setup/molecule/default/Dockerfile.j2
new file mode 100644
index 0000000..0a60553
--- /dev/null
+++ b/ansible/roles/setup/molecule/default/Dockerfile.j2
@@ -0,0 +1,14 @@
+# Molecule managed
+
+{% if item.registry is defined %}
+FROM {{ item.registry.url }}/{{ item.image }}
+{% else %}
+FROM {{ item.image }}
+{% endif %}
+
+RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \
+    elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \
+    elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
+    elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \
+    elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \
+    elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi
diff --git a/ansible/roles/setup/molecule/default/molecule.yml b/ansible/roles/setup/molecule/default/molecule.yml
new file mode 100644
index 0000000..1e9a4c1
--- /dev/null
+++ b/ansible/roles/setup/molecule/default/molecule.yml
@@ -0,0 +1,20 @@
+---
+dependency:
+  name: galaxy
+driver:
+  name: docker
+lint:
+  name: yamllint
+platforms:
+  - name: instance
+    image: ${PLATFORM_DISTRO:-centos}:${DISTRO_VERSION:-7}
+provisioner:
+  name: ansible
+  lint:
+    name: ansible-lint
+scenario:
+  name: default
+verifier:
+  name: testinfra
+  lint:
+    name: flake8
diff --git a/ansible/roles/setup/molecule/default/playbook.yml b/ansible/roles/setup/molecule/default/playbook.yml
new file mode 100644
index 0000000..19a9050
--- /dev/null
+++ b/ansible/roles/setup/molecule/default/playbook.yml
@@ -0,0 +1,5 @@
+---
+- name: Converge
+  hosts: all
+  roles:
+    - role: setup
diff --git a/ansible/roles/setup/molecule/default/tests/test_default.py b/ansible/roles/setup/molecule/default/tests/test_default.py
new file mode 100644
index 0000000..6bf6b6b
--- /dev/null
+++ b/ansible/roles/setup/molecule/default/tests/test_default.py
@@ -0,0 +1,30 @@
+import os
+import pytest
+from os.path import expanduser
+
+import testinfra.utils.ansible_runner
+
+testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
+    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
+
+
+@pytest.fixture
+def get_vars(host):
+    defaults_files = "file=../../defaults/main.yml name=role_defaults"
+    ansible_vars = host.ansible(
+        "include_vars",
+        defaults_files)["ansible_facts"]["role_defaults"]
+    return ansible_vars
+
+
+def test_authorized_keys(host, get_vars):
+    public_key_file = get_vars['offline_ssh_key_file_name'] + '.pub'
+    with open(expanduser("~") + '/.ssh/' + public_key_file, 'r') as pkf:
+        public_key_content = pkf.read().strip()
+
+    f = host.file('/root/.ssh/authorized_keys')
+    assert f.exists
+    assert f.user == 'root'
+    assert f.group == 'root'
+    assert oct(f.mode) == '0o600'
+    assert f.content_string == public_key_content