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
22 from functest.core import testcase
25 LOGGER = logging.getLogger(__name__)
28 class K8sTesting(testcase.TestCase):
29 """Kubernetes test runner"""
31 def __init__(self, **kwargs):
32 super(K8sTesting, self).__init__(**kwargs)
38 def run_kubetest(self):
39 """Run the test suites"""
41 LOGGER.info("Starting k8s test: '%s'.", cmd_line)
43 process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
44 stderr=subprocess.STDOUT)
46 lines = process.stdout.readlines()
47 for i in range(len(lines) - 1, -1, -1):
48 new_line = str(lines[i])
50 if 'SUCCESS!' in new_line or 'FAIL!' in new_line:
51 remark = new_line.replace('--', '|').split('|')
54 if remark and 'SUCCESS!' in remark[0]:
59 if not os.path.isfile(os.getenv('KUBECONFIG')):
60 LOGGER.error("Cannot run k8s testcases. Config file not found ")
61 return self.EX_RUN_ERROR
63 self.start_time = time.time()
67 except Exception as ex: # pylint: disable=broad-except
68 LOGGER.error("Error with running %s", str(ex))
69 res = self.EX_RUN_ERROR
71 self.stop_time = time.time()
74 def check_envs(self): # pylint: disable=no-self-use
75 """Check if required environment variables are set"""
77 assert 'DEPLOY_SCENARIO' in os.environ
78 assert 'KUBECONFIG' in os.environ
79 assert 'KUBE_MASTER' in os.environ
80 assert 'KUBE_MASTER_IP' in os.environ
81 assert 'KUBERNETES_PROVIDER' in os.environ
82 assert 'KUBE_MASTER_URL' in os.environ
83 except Exception as ex:
84 raise Exception("Cannot run k8s testcases. "
85 "Please check env var: %s" % str(ex))
88 class K8sSmokeTest(K8sTesting):
89 """Kubernetes smoke test suite"""
90 def __init__(self, **kwargs):
91 if "case_name" not in kwargs:
92 kwargs.get("case_name", 'k8s_smoke')
93 super(K8sSmokeTest, self).__init__(**kwargs)
95 self.cmd = ['/src/k8s.io/kubernetes/cluster/test-smoke.sh', '--host',
96 os.getenv('KUBE_MASTER_URL')]