summaryrefslogtreecommitdiffstats
path: root/functest_kubernetes/k8stest.py
blob: c36863b835407621a71eb4cece60a19a6a581662 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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 os
import re
import subprocess
import time

from xtesting.core import testcase


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

    __logger = logging.getLogger(__name__)

    config = '/root/.kube/config'

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

    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()
        # Remove color code escape sequences
        output = re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', str(output))

        if ('Error loading client' in output or
                'Unexpected error' in output):
            raise Exception(output)

        remarks = []
        lines = output.split('\n')
        success = False
        failure = False
        i = 0
        while i < len(lines):
            if '[Fail]' in lines[i] or 'Failures:' in lines[i]:
                self.__logger.error(lines[i])
            if re.search(r'\[(.)*[0-9]+ seconds\]', lines[i]):
                self.__logger.debug(lines[i])
                i = i + 1
                while i < len(lines) and lines[i] != '-' * len(lines[i]):
                    if lines[i].startswith('STEP:') or ('INFO:' in lines[i]):
                        break
                    self.__logger.debug(lines[i])
                    i = i + 1
            if i >= len(lines):
                break
            success = 'SUCCESS!' in lines[i]
            failure = 'FAIL!' in lines[i]
            if success or failure:
                if i != 0 and 'seconds' in lines[i - 1]:
                    remarks.append(lines[i - 1])
                remarks = remarks + lines[i].replace('--', '|').split('|')
                break
            i = i + 1

        self.__logger.debug('-' * 10)
        self.__logger.info("Remarks:")
        for remark in remarks:
            if 'seconds' in remark:
                self.__logger.debug(remark)
            elif 'Passed' in remark:
                self.__logger.info("Passed: %s", remark.split()[0])
            elif 'Skipped' in remark:
                self.__logger.info("Skipped: %s", remark.split()[0])
            elif 'Failed' in remark:
                self.__logger.info("Failed: %s", remark.split()[0])

        if success:
            self.result = 100
        elif failure:
            self.result = 0

    def run(self, **kwargs):

        if not os.path.isfile(self.config):
            self.__logger.error(
                "Cannot run k8s testcases. Config file not found")
            return self.EX_RUN_ERROR

        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 K8sSmokeTest(K8sTesting):
    """Kubernetes smoke test suite"""
    def __init__(self, **kwargs):
        if "case_name" not in kwargs:
            kwargs.get("case_name", 'k8s_smoke')
        super(K8sSmokeTest, self).__init__(**kwargs)
        self.cmd = ["/src/k8s.io/kubernetes/_output/bin/e2e.test",
                    "-ginkgo.focus", "Guestbook.application",
                    "-kubeconfig", self.config, "--provider", "local"]


class K8sConformanceTest(K8sTesting):
    """Kubernetes conformance test suite"""
    def __init__(self, **kwargs):
        if "case_name" not in kwargs:
            kwargs.get("case_name", 'k8s_conformance')
        super(K8sConformanceTest, self).__init__(**kwargs)
        self.cmd = ['/src/k8s.io/kubernetes/_output/bin/e2e.test',
                    '-ginkgo.focus', 'Conformance',
                    '-kubeconfig', self.config, "--provider", "local"]