aboutsummaryrefslogtreecommitdiffstats
path: root/test/security/check_versions/tests/test_main.py
blob: 0dff0b230aa826ef40174db2081ec862ff8b869f (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3

import k8s_bin_versions_inspector as kbvi
import json
import tempfile
import yaml


def exec_main(pod_name_trimmer, acceptable_data):

    with tempfile.NamedTemporaryFile() as output_temp, tempfile.NamedTemporaryFile() as acceptable_temp:

        with open(acceptable_temp.name, "w") as stream:
            yaml.safe_dump(acceptable_data, stream)

        result = kbvi.main(
            [
                "--quiet",
                "--output-file",
                output_temp.name,
                "--output-format",
                "json",
                "--acceptable",
                acceptable_temp.name,
            ]
        )

        with open(output_temp.name, "r") as stream:
            output_data = json.load(stream)
            output_extracted = (
                (
                    item["namespace"],
                    pod_name_trimmer(item["pod"]),
                    item["container"],
                    item["versions"]["python"],
                    item["versions"]["java"],
                )
                for item in output_data
            )
            output_sorted = sorted(output_extracted)

    assert output_sorted == [
        ("default", "kbvi-test-java-keycloak", "keycloak", [], ["11.0.8"]),
        ("default", "kbvi-test-java-keycloak-old", "keycloak-old", [], ["11.0.5"]),
        (
            "default",
            "kbvi-test-java-keycloak-very-old",
            "keycloak-very-old",
            ["2.7.5"],
            [],
        ),
        ("default", "kbvi-test-python-jupyter", "jupyter", ["3.8.4"], []),
        ("default", "kbvi-test-python-jupyter-old", "jupyter-old", ["3.6.6"], []),
        ("default", "kbvi-test-python-stderr-filebeat", "filebeat", ["2.7.5"], []),
        ("default", "kbvi-test-terminated", "python", [], []),
        ("ingress-nginx", "kbvi-test-ingress-nginx", "echo-server", [], []),
        ("kube-system", "kbvi-test-kube-system", "echo-server", [], []),
    ]

    return result


def test_main(pod_name_trimmer):

    acceptable_data = {
        "python": ["2.7.5", "3.6.6", "3.8.4"],
        "java": ["11.0.5", "11.0.8"],
    }

    result = exec_main(pod_name_trimmer, acceptable_data)

    assert result == 0


def test_main_neg(pod_name_trimmer):

    acceptable_data = {
        "python": ["3.6.6", "3.8.4"],
        "java": ["11.0.5", "11.0.8"],
    }

    result = exec_main(pod_name_trimmer, acceptable_data)

    assert result == 1