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"""
28 # pylint: disable=too-many-instance-attributes
30 __logger = logging.getLogger(__name__)
32 config = '/root/.kube/config'
34 def __init__(self, **kwargs):
35 super(K8sTesting, self).__init__(**kwargs)
37 self.res_dir = "/home/opnfv/functest/results/{}".format(
42 self.output_log_name = 'functest-kubernetes.log'
43 self.output_debug_log_name = 'functest-kubernetes.debug.log'
45 def run_kubetest(self): # pylint: disable=too-many-branches
46 """Run the test suites"""
48 self.__logger.info("Starting k8s test: '%s'.", cmd_line)
50 process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
51 stderr=subprocess.STDOUT)
52 boutput = process.stdout.read()
53 with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput:
54 foutput.write(boutput)
56 r'^(FAIL|SUCCESS)!.* ([0-9]+) Passed \| ([0-9]+) Failed \|'
57 r' ([0-9]+) Pending \| ([0-9]+) Skipped', boutput.decode("utf-8"),
58 re.MULTILINE | re.DOTALL)
60 self.details['passed'] = int(grp.group(2))
61 self.details['failed'] = int(grp.group(3))
62 self.details['pending'] = int(grp.group(4))
63 self.details['skipped'] = int(grp.group(5))
64 self.__logger.debug("details: %s", self.details)
65 self.result = self.details['passed'] * 100 / (
66 self.details['passed'] + self.details['failed'] +
67 self.details['pending'])
68 self.__logger.debug("result: %s", self.result)
69 if grp.group(1) == 'FAIL':
71 r'^(Summarizing [0-9]+ Failure.*)Ran', boutput.decode("utf-8"),
72 re.MULTILINE | re.DOTALL)
74 self.__logger.error(grp2.group(1))
76 def run(self, **kwargs):
77 if not os.path.isfile(self.config):
79 "Cannot run k8s testcases. Config file not found")
80 return self.EX_RUN_ERROR
81 self.start_time = time.time()
85 except Exception: # pylint: disable=broad-except
86 self.__logger.exception("Error with running kubetest:")
87 res = self.EX_RUN_ERROR
88 self.stop_time = time.time()
92 class K8sSmokeTest(K8sTesting):
93 """Kubernetes smoke test suite"""
94 def __init__(self, **kwargs):
95 if "case_name" not in kwargs:
96 kwargs.get("case_name", 'k8s_smoke')
97 super(K8sSmokeTest, self).__init__(**kwargs)
98 self.cmd = ['e2e.test', '-ginkgo.focus', 'Guestbook.application',
99 '-ginkgo.noColor', '-kubeconfig', self.config,
100 '-provider', 'local', '-report-dir', self.res_dir,
101 '-disable-log-dump', 'true']
104 class K8sConformanceTest(K8sTesting):
105 """Kubernetes conformance test suite"""
106 def __init__(self, **kwargs):
107 if "case_name" not in kwargs:
108 kwargs.get("case_name", 'k8s_conformance')
109 super(K8sConformanceTest, self).__init__(**kwargs)
111 'e2e.test', '-ginkgo.focus', r'\[Conformance\]', '-ginkgo.noColor',
112 '-ginkgo.skip', r'Alpha|\[(Disruptive|Feature:[^\]]+|Flaky)\]',
113 '-kubeconfig', self.config, '-provider', 'local',
114 '-report-dir', self.res_dir, '-disable-log-dump', 'true']