Skip to content

Commit

Permalink
chore: improved testinfra
Browse files Browse the repository at this point in the history
  • Loading branch information
fatz committed Mar 7, 2023
1 parent 3d5071f commit 90c3293
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ansible/molecule/default/tests/test_cloudinit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")

def test_cloudinit_feature_flags(host):
"""
ubuntu 18.04: does not need the feature flag
all other except flatcar: expect feature overrides
"""
distro = host.system_info.distribution
release = host.system_info.release

# for flatcar we can skip
if distro == "flatcar":
pytest.skip("no changes on flatcar")

# if cloud-init is lower than 20.0 we can skip
cloud_init_version = host.run("cloud-init --version")
assert cloud_init_version.succeeded

cloud_init_version_str = cloud_init_version.stdout.strip('\n')
if not cloud_init_version_str:
cloud_init_version_str = cloud_init_version.stderr.strip('\n')

assert cloud_init_version_str

cloud_init_version_str_version_part = cloud_init_version_str.split(" ")[-1]
major_version = cloud_init_version_str_version_part.split(".")[0]

if int(major_version) < 20:
pytest.skip("cloud-init major version ({}) below 20".format(major_version))

if distro != "ubuntu":
cmd = host.run("python3 -c \"import sysconfig; print(sysconfig.get_path('purelib'))\"")
assert cmd.succeeded

featurefile = host.file("{}/cloudinit/feature_overrides.py".format(cmd.stdout.strip('\n')))
assert featurefile.exists
assert b'ERROR_ON_USER_DATA_FAILURE = False' in featurefile.content
# ubuntu 18.04 still supported and no need for this feature flag
elif distro == "ubuntu" and not release == "18.04":
featurefile = host.file("/usr/lib/python3/dist-packages/cloudinit/feature_overrides.py")
assert featurefile.exists
assert b'ERROR_ON_USER_DATA_FAILURE = False' in featurefile.content
else:
assert True
56 changes: 56 additions & 0 deletions ansible/molecule/default/tests/test_kubernetes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
import pytest
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")

def test_kubelet_kubectl_installed(host):
"""
we expect kubectl and kubelet package to be installed
flatcar: skip no packages
"""
distro = host.system_info.distribution
if distro == "flatcar":
pytest.skip("no packages on flatcar")

assert host.package("kubectl").is_installed
assert host.package("kubelet").is_installed

def test_kubeadm_installed(host):
"""
we expect kubeadm package to be installed
flatcar: skip no packages
"""
distro = host.system_info.distribution
if distro == "flatcar":
pytest.skip("no packages on flatcar")

assert host.package("kubeadm").is_installed

def test_kube_cmd_path(host):
"""
kubelet, kubeadm and kubectl must be in path
"""
distro = host.system_info.distribution
if distro == "flatcar":
pytest.skip("flatcar uses different PATH for non-interactive")

assert host.exists("kubelet")
assert host.exists("kubeadm")
assert host.exists("kubectl")

def test_kube_cmd_path_flatcar(host):
distro = host.system_info.distribution
if distro != "flatcar":
pytest.skip("flatcar uses different PATH for non-interactive")
# the path is only set on interactive shell. SO lets append it here
cmd = host.run("bash -c 'PATH=$PATH:/opt/bin type kubectl'")
assert cmd.succeeded is True
cmd = host.run("bash -c 'PATH=$PATH:/opt/bin type kubelet'")
assert cmd.succeeded is True
cmd = host.run("bash -c 'PATH=$PATH:/opt/bin type kubeadm'")
assert cmd.succeeded is True

0 comments on commit 90c3293

Please sign in to comment.