forked from osism/ansible-collection-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl.py
72 lines (48 loc) · 2.45 KB
/
kubectl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pytest
from .util.util import get_ansible, get_variable, get_from_url, jinja_replacement
testinfra_runner, testinfra_hosts = get_ansible()
def test_apt_transport_https_installed(host):
"""Check if the apt-transport-https package is installed."""
kubectl_configure_repository = get_variable(host, "kubectl_configure_repository")
if not kubectl_configure_repository:
pytest.skip("kubectl_configure_repository is not set")
pkg = host.package("apt-transport-https")
assert pkg.is_installed
def test_kubectl_gpg_key_present(host):
"""Check if the GPG key for the kubectl repository is correctly added."""
kubectl_configure_repository = get_variable(host, "kubectl_configure_repository")
if not kubectl_configure_repository:
pytest.skip("kubectl_configure_repository is not set")
gpg_key_file = host.file("/usr/share/keyrings/kubectl.gpg")
# Ensure the GPG key file exists
assert gpg_key_file.exists
# Ensure the correct permissions are set
assert gpg_key_file.user == "root"
assert gpg_key_file.group == "root"
assert gpg_key_file.mode == 0o644
kubectl_repository_key = get_variable(host, "kubectl_debian_repository_key")
expected_key = get_from_url(kubectl_repository_key, True)
assert gpg_key_file.content == expected_key
def test_kubectl_repository_configured(host):
"""Check if the kubectl repository is correctly configured."""
kubectl_configure_repository = get_variable(host, "kubectl_configure_repository")
if not kubectl_configure_repository:
pytest.skip("kubectl_configure_repository is not set")
kubectl_repository = get_variable(host, "kubectl_debian_repository")
kubectl_debian_repository_arch = get_variable(
host, "kubectl_debian_repository_arch"
)
repo_file = host.file("/etc/apt/sources.list.d/kubectl.list")
# Ensure the repository file exists
assert repo_file.exists
# Ensure the content matches the expected repository configuration
formatted_kubectl_repository = jinja_replacement(
kubectl_repository,
{"kubectl_debian_repository_arch": kubectl_debian_repository_arch},
)
assert formatted_kubectl_repository in repo_file.content_string
def test_kubectl_package_installed(host):
"""Check if the kubectl package is installed."""
kubectl_package_name = get_variable(host, "kubectl_package_name")
kubectl_pkg = host.package(kubectl_package_name)
assert kubectl_pkg.is_installed