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
8 # http://www.apache.org/licenses/LICENSE-2.0
12 Define the parent for Kubernetes testing.
15 from __future__ import division
23 from xtesting.core import testcase
26 class K8sTesting(testcase.TestCase):
27 """Kubernetes test runner"""
29 __logger = logging.getLogger(__name__)
31 config = '/root/.kube/config'
33 def __init__(self, **kwargs):
34 super(K8sTesting, self).__init__(**kwargs)
36 self.res_dir = "/home/opnfv/functest/results/{}".format(
42 def run_kubetest(self): # pylint: disable=too-many-branches
43 """Run the test suites"""
45 self.__logger.info("Starting k8s test: '%s'.", cmd_line)
47 process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
48 stderr=subprocess.STDOUT)
49 boutput = process.stdout.read()
50 with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput:
51 foutput.write(boutput)
52 output = boutput.decode("utf-8")
53 if ('Error loading client' in output or
54 'Unexpected error' in output):
55 raise Exception(output)
58 lines = output.split('\n')
63 if '[Fail]' in lines[i] or 'Failures:' in lines[i]:
64 self.__logger.error(lines[i])
65 if re.search(r'\[(.)*[0-9]+ seconds\]', lines[i]):
66 self.__logger.debug(lines[i])
68 while i < len(lines) and lines[i] != '-' * len(lines[i]):
69 if lines[i].startswith('STEP:') or ('INFO:' in lines[i]):
71 self.__logger.debug(lines[i])
75 success = 'SUCCESS!' in lines[i]
76 failure = 'FAIL!' in lines[i]
77 if success or failure:
78 if i != 0 and 'seconds' in lines[i - 1]:
79 remarks.append(lines[i - 1])
80 remarks = remarks + lines[i].replace('--', '|').split('|')
84 self.__logger.debug('-' * 10)
85 self.__logger.info("Remarks:")
86 for remark in remarks:
87 if 'seconds' in remark:
88 self.__logger.debug(remark)
89 elif 'Passed' in remark:
90 self.__logger.info("Passed: %s", remark.split()[0])
91 elif 'Skipped' in remark:
92 self.__logger.info("Skipped: %s", remark.split()[0])
93 elif 'Failed' in remark:
94 self.__logger.info("Failed: %s", remark.split()[0])
101 def run(self, **kwargs):
103 if not os.path.isfile(self.config):
105 "Cannot run k8s testcases. Config file not found")
106 return self.EX_RUN_ERROR
108 self.start_time = time.time()
112 except Exception: # pylint: disable=broad-except
113 self.__logger.exception("Error with running kubetest:")
114 res = self.EX_RUN_ERROR
116 self.stop_time = time.time()
120 class K8sSmokeTest(K8sTesting):
121 """Kubernetes smoke test suite"""
122 def __init__(self, **kwargs):
123 if "case_name" not in kwargs:
124 kwargs.get("case_name", 'k8s_smoke')
125 super(K8sSmokeTest, self).__init__(**kwargs)
126 self.cmd = ['e2e.test', '-ginkgo.focus', 'Guestbook.application',
127 '-ginkgo.noColor', '-kubeconfig', self.config,
128 '-provider', 'local', '-report-dir', self.res_dir,
129 '-disable-log-dump', 'true']
132 class K8sConformanceTest(K8sTesting):
133 """Kubernetes conformance test suite"""
134 def __init__(self, **kwargs):
135 if "case_name" not in kwargs:
136 kwargs.get("case_name", 'k8s_conformance')
137 super(K8sConformanceTest, self).__init__(**kwargs)
139 'e2e.test', '-ginkgo.focus', r'\[Conformance\]', '-ginkgo.noColor',
140 '-ginkgo.skip', r'Alpha|\[(Disruptive|Feature:[^\]]+|Flaky)\]',
141 '-kubeconfig', self.config, '-provider', 'local',
142 '-report-dir', self.res_dir, '-disable-log-dump', 'true']