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 LOGGER = logging.getLogger(__name__)
29 class K8sTesting(testcase.TestCase):
30 """Kubernetes test runner"""
32 def __init__(self, **kwargs):
33 super(K8sTesting, self).__init__(**kwargs)
39 def run_kubetest(self): # pylint: disable=too-many-branches
40 """Run the test suites"""
42 LOGGER.info("Starting k8s test: '%s'.", cmd_line)
44 process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
45 stderr=subprocess.STDOUT)
46 output = process.stdout.read()
47 # Remove color code escape sequences
48 output = re.sub(r'\x1B\[[0-?]*[ -/]*[@-~]', '', str(output))
50 file_logger = logging.getLogger(self.case_name)
52 lines = output.split('\n')
55 if '[k8s.io]' in lines[i]:
56 if i != 0 and 'seconds' in lines[i-1]:
57 file_logger.debug(lines[i-1])
58 while lines[i] != '-'*len(lines[i]):
59 if lines[i].startswith('STEP:') or ('INFO:' in lines[i]):
61 file_logger.debug(lines[i])
64 success = 'SUCCESS!' in lines[i]
65 failure = 'FAIL!' in lines[i]
66 if success or failure:
67 if i != 0 and 'seconds' in lines[i-1]:
68 remarks.append(lines[i-1])
69 remarks = remarks + lines[i].replace('--', '|').split('|')
73 file_logger.debug('-'*10)
74 file_logger.debug("Remarks:")
75 for remark in remarks:
76 if 'seconds' in remark:
77 file_logger.debug(remark)
78 elif 'Passed' in remark:
79 file_logger.debug("Passed: %s", remark.split()[0])
80 elif 'Skipped' in remark:
81 file_logger.debug("Skipped: %s", remark.split()[0])
82 elif 'Failed' in remark:
83 file_logger.debug("Failed: %s", remark.split()[0])
90 def run(self, **kwargs):
92 if not os.path.isfile(os.getenv('KUBECONFIG')):
93 LOGGER.error("Cannot run k8s testcases. Config file not found ")
94 return self.EX_RUN_ERROR
96 self.start_time = time.time()
100 except Exception as ex: # pylint: disable=broad-except
101 LOGGER.error("Error with running %s", str(ex))
102 res = self.EX_RUN_ERROR
104 self.stop_time = time.time()
107 def check_envs(self): # pylint: disable=no-self-use
108 """Check if required environment variables are set"""
110 assert 'DEPLOY_SCENARIO' in os.environ
111 assert 'KUBECONFIG' in os.environ
112 assert 'KUBE_MASTER_IP' in os.environ
113 assert 'KUBERNETES_PROVIDER' in os.environ
114 assert 'KUBE_MASTER_URL' in os.environ
115 except Exception as ex:
116 raise Exception("Cannot run k8s testcases. "
117 "Please check env var: %s" % str(ex))
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)
127 self.cmd = ['/src/k8s.io/kubernetes/cluster/test-smoke.sh', '--host',
128 os.getenv('KUBE_MASTER_URL')]
131 class K8sConformanceTest(K8sTesting):
132 """Kubernetes conformance test suite"""
133 def __init__(self, **kwargs):
134 if "case_name" not in kwargs:
135 kwargs.get("case_name", 'k8s_conformance')
136 super(K8sConformanceTest, self).__init__(**kwargs)
138 self.cmd = ['/src/k8s.io/kubernetes/_output/bin/e2e.test',
139 '--ginkgo.focus', 'Conformance']