From 1ec19b0598382aa45258ed630ee26cba4fb3a7e5 Mon Sep 17 00:00:00 2001 From: Michal Zegan Date: Tue, 30 Apr 2019 14:52:13 +0200 Subject: Add molecule tests for rke role This change adds molecule tests for the rke role, and modifies the rke role itself to be more idempotent/to pass linter. Note that this molecule test case uses a separate role to install docker in containers, that runs docker daemon inside of them instead of using host docker. Issue-ID: OOM-1778 Change-Id: I875f3ff2ab961e5428acee5a02287a8d2d6e9969 Signed-off-by: Michal Zegan --- .../molecule/default/tests/test_controlplane.py | 14 ++++++ .../molecule/default/tests/test_infrastructure.py | 56 ++++++++++++++++++++++ .../rke/molecule/default/tests/test_kubernetes.py | 13 +++++ 3 files changed, 83 insertions(+) create mode 100644 ansible/roles/rke/molecule/default/tests/test_controlplane.py create mode 100644 ansible/roles/rke/molecule/default/tests/test_infrastructure.py create mode 100644 ansible/roles/rke/molecule/default/tests/test_kubernetes.py (limited to 'ansible/roles/rke/molecule/default/tests') diff --git a/ansible/roles/rke/molecule/default/tests/test_controlplane.py b/ansible/roles/rke/molecule/default/tests/test_controlplane.py new file mode 100644 index 00000000..0bfbca2d --- /dev/null +++ b/ansible/roles/rke/molecule/default/tests/test_controlplane.py @@ -0,0 +1,14 @@ +import os +import pytest + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts( + 'kubernetes-control-plane') + + +@pytest.mark.parametrize('container_name', [ + 'kube-apiserver', 'kube-controller-manager', 'kube-scheduler', 'kubelet']) +def test_container_running(host, container_name): + assert host.docker(container_name).is_running diff --git a/ansible/roles/rke/molecule/default/tests/test_infrastructure.py b/ansible/roles/rke/molecule/default/tests/test_infrastructure.py new file mode 100644 index 00000000..9ba11d6e --- /dev/null +++ b/ansible/roles/rke/molecule/default/tests/test_infrastructure.py @@ -0,0 +1,56 @@ +import os +import pytest +import json + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('infrastructure') + + +@pytest.mark.parametrize('filename', [ + '/root/.kube/config', + '/opt/onap/cluster/cluster.yml', + '/opt/onap/cluster/cluster.rkestate']) +def test_file_existence(host, filename): + assert host.file(filename).exists + + +def test_rke_in_path(host): + assert host.find_command('rke') == '/usr/local/bin/rke' + + +def test_rke_version_works(host): + # Note that we need to cd to the cluster data dir first, really. + assert host.run('cd /opt/onap/cluster && rke version').rc == 0 + + +def test_nodes_ready(host): + # Retrieve all node names. + nodecmdres = host.run('kubectl get nodes -o name') + assert nodecmdres.rc == 0 + nodes = nodecmdres.stdout.split('\n') + for node in nodes: + assert host.run( + 'kubectl wait --timeout=0 --for=condition=ready ' + node).rc == 0 + + +def test_pods_ready(host): + # Retrieve all pods from all namespaces. + # Because we need pod and namespace name, we get full json representation. + podcmdres = host.run('kubectl get pods --all-namespaces -o json') + assert podcmdres.rc == 0 + pods = json.loads(podcmdres.stdout)['items'] + for pod in pods: + # Each pod may be either created by a job or not. + # In job case they should already be completed + # when we are here so we ignore them. + namespace = pod['metadata']['namespace'] + podname = pod['metadata']['name'] + condition = 'Ready' + if len(pod['metadata']['ownerReferences']) == 1 and pod[ + 'metadata']['ownerReferences'][0]['kind'] == 'Job': + continue + assert host.run( + 'kubectl wait --timeout=120s --for=condition=' + condition + ' -n ' + + namespace + ' pods/' + podname).rc == 0 diff --git a/ansible/roles/rke/molecule/default/tests/test_kubernetes.py b/ansible/roles/rke/molecule/default/tests/test_kubernetes.py new file mode 100644 index 00000000..887494fa --- /dev/null +++ b/ansible/roles/rke/molecule/default/tests/test_kubernetes.py @@ -0,0 +1,13 @@ +import os +import pytest + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('kubernetes') + + +@pytest.mark.parametrize('container_name', [ + 'etcd', 'kubelet', 'kube-proxy']) +def test_container_running(host, container_name): + assert host.docker(container_name).is_running -- cgit 1.2.3-korg