Use pip==20.2.4 in tox venv
[functest-kubernetes.git] / functest_kubernetes / k8stest.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2018 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 """
12 Define the parent for Kubernetes testing.
13 """
14
15 from __future__ import division
16
17 import logging
18 import os
19 import re
20 import subprocess
21 import time
22
23 from xtesting.core import testcase
24
25
26 class E2ETesting(testcase.TestCase):
27     """Kubernetes test runner"""
28     # pylint: disable=too-many-instance-attributes
29
30     __logger = logging.getLogger(__name__)
31
32     config = '/root/.kube/config'
33
34     def __init__(self, **kwargs):
35         super(E2ETesting, self).__init__(**kwargs)
36         self.cmd = []
37         self.dir_results = "/home/opnfv/functest/results"
38         self.res_dir = os.path.join(self.dir_results, self.case_name)
39         self.result = 0
40         self.start_time = 0
41         self.stop_time = 0
42         self.output_log_name = 'functest-kubernetes.log'
43         self.output_debug_log_name = 'functest-kubernetes.debug.log'
44
45     def run_kubetest(self, **kwargs):  # pylint: disable=too-many-branches
46         """Run the test suites"""
47         cmd_line = ['e2e.test', '-ginkgo.noColor', '-kubeconfig', self.config,
48                     '-provider', 'local', '-report-dir', self.res_dir]
49         for arg in kwargs:
50             cmd_line.extend(['-ginkgo.{}'.format(arg), kwargs.get(arg)])
51         if "NON_BLOCKING_TAINTS" in os.environ:
52             cmd_line.extend(
53                 ['-non-blocking-taints', os.environ["NON_BLOCKING_TAINTS"]])
54         cmd_line.extend(['-disable-log-dump', 'true'])
55         self.__logger.info("Starting k8s test: '%s'.", cmd_line)
56         process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
57                                    stderr=subprocess.STDOUT)
58         boutput = process.stdout.read()
59         with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput:
60             foutput.write(boutput)
61         grp = re.search(
62             r'^(FAIL|SUCCESS)!.* ([0-9]+) Passed \| ([0-9]+) Failed \|'
63             r' ([0-9]+) Pending \| ([0-9]+) Skipped',
64             boutput.decode("utf-8", errors="ignore"),
65             re.MULTILINE | re.DOTALL)
66         assert grp
67         self.details['passed'] = int(grp.group(2))
68         self.details['failed'] = int(grp.group(3))
69         self.details['pending'] = int(grp.group(4))
70         self.details['skipped'] = int(grp.group(5))
71         self.__logger.debug("details: %s", self.details)
72         self.result = self.details['passed'] * 100 / (
73             self.details['passed'] + self.details['failed'] +
74             self.details['pending'])
75         self.__logger.debug("result: %s", self.result)
76         if grp.group(1) == 'FAIL':
77             grp2 = re.search(
78                 r'^(Summarizing [0-9]+ Failure.*)Ran',
79                 boutput.decode("utf-8", errors="ignore"),
80                 re.MULTILINE | re.DOTALL)
81             if grp2:
82                 self.__logger.error(grp2.group(1))
83
84     def run(self, **kwargs):
85         if not os.path.isfile(self.config):
86             self.__logger.error(
87                 "Cannot run k8s testcases. Config file not found")
88             return self.EX_RUN_ERROR
89         self.start_time = time.time()
90         try:
91             self.run_kubetest(**kwargs)
92             res = self.EX_OK
93         except Exception:  # pylint: disable=broad-except
94             self.__logger.exception("Error with running kubetest:")
95             res = self.EX_RUN_ERROR
96         self.stop_time = time.time()
97         return res