aboutsummaryrefslogtreecommitdiffstats
path: root/infra-healthcheck/infra_healthcheck/k8stest.py
blob: 1d69fac059d7785923c8d4f74a33ceedd3907aa3 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
#
# Copyright (c) 2018 All rights reserved
# This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

"""
Define the parent for Kubernetes testing.
"""

from __future__ import division

import logging
import subprocess
import os
import time

from xtesting.core import testcase

class K8sTesting(testcase.TestCase):
    """Kubernetes test runner"""

    __logger = logging.getLogger(__name__)

    def __init__(self, **kwargs):
        super(K8sTesting, self).__init__(**kwargs)
        self.cmd = []
        self.result = 0
        self.details = {}
        self.start_time = 0
        self.stop_time = 0
        self.criteria_string = ""

    def run_kubetest(self):  # pylint: disable=too-many-branches
        """Run the test suites"""
        cmd_line = self.cmd
        self.__logger.info("Starting k8s test: '%s'.", cmd_line)

        process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT)
        output = process.stdout.read().decode("utf-8")
        if ('Error loading client' in output or
                'Unexpected error' in output):
            raise Exception(output)

        # create a log file
        result_folder = "{}/{}".format(self.dir_results, self.case_name)
        file_name = "{}/{}.log".format(result_folder, self.case_name)
        os.makedirs(result_folder, exist_ok=True)

        log_file = open(file_name, "w")
        log_file.write(output)
        log_file.close()

        remarks = []
        details = {}
        lines = output.split('\n')
        success = False
        str_remarks = ""

        for log in lines:
            if log.startswith(">>>"):
                remarks.append(log.replace('>', ''))
            else:
                remarks.append(log)
        if 'PASS' in remarks:
            success = True

        self.details = details
        self.__logger.info("details: %s", details)

        if success:
            self.result = 100
        else:
            self.result = 0

    def run(self, **kwargs):
        self.start_time = time.time()
        try:
            self.run_kubetest()
            res = self.EX_OK
        except Exception:  # pylint: disable=broad-except
            self.__logger.exception("Error with running kubetest:")
            res = self.EX_RUN_ERROR

        self.stop_time = time.time()
        return res


class OnapSecurityNodePortsIngress(K8sTesting):
    """Check that there is no NodePort without corresponding Ingress port."""
    def __init__(self, **kwargs):
        super(OnapSecurityNodePortsIngress, self).__init__(**kwargs)
        self.cmd = ['python3', '/check_for_ingress_and_nodeports.py',
                    '--conf', '/root/.kube/config']
        self.error_string = "NodePort without corresponding Ingress found"

class OnapSecurityNodePortsCerts(K8sTesting):
    """Check the cerfificates for the nodeports."""
    def __init__(self, **kwargs):
        super(OnapSecurityNodePortsCerts, self).__init__(**kwargs)
        os.chdir('/usr/lib/python3.8/site-packages/check_certificates')
        self.cmd = ['python3', 'check_certificates_validity.py',
                    '--mode','nodeport','--namespace','onap','--dir',
                    '/var/lib/xtesting/results/nodeport_check_certs']
        self.criteria_string = ">>> Test Check certificates PASS"

class OnapSecurityInternalPortsCerts(K8sTesting):
    """Check the cerfificates for the internal ports."""
    def __init__(self, **kwargs):
        super(OnapSecurityInternalPortsCerts, self).__init__(**kwargs)
        os.chdir('/usr/lib/python3.8/site-packages/check_certificates')
        self.cmd = ['python3', 'check_certificates_validity.py',
                    '--mode','internal','--namespace','onap','--dir',
                    '/var/lib/xtesting/results/internal_check_certs']
        self.criteria_string = ">>> Test Check certificates PASS"