7e629ec9da476bea679904194a6864e05e323504
[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 import yaml
23
24 from xtesting.core import testcase
25
26
27 class E2ETesting(testcase.TestCase):
28     """Kubernetes test runner"""
29     # pylint: disable=too-many-instance-attributes
30
31     __logger = logging.getLogger(__name__)
32
33     config = '/root/.kube/config'
34     gcr_repo = os.getenv("MIRROR_REPO", "gcr.io")
35     k8s_gcr_repo = os.getenv("MIRROR_REPO", "k8s.gcr.io")
36
37     def __init__(self, **kwargs):
38         super(E2ETesting, self).__init__(**kwargs)
39         self.cmd = []
40         self.dir_results = "/home/opnfv/functest/results"
41         self.res_dir = os.path.join(self.dir_results, self.case_name)
42         self.result = 0
43         self.start_time = 0
44         self.stop_time = 0
45         self.output_log_name = 'functest-kubernetes.log'
46         self.output_debug_log_name = 'functest-kubernetes.debug.log'
47
48     def run_kubetest(self, **kwargs):  # pylint: disable=too-many-branches
49         """Run the test suites"""
50         cmd_line = ['e2e.test', '-ginkgo.noColor', '-kubeconfig', self.config,
51                     '-provider', 'skeleton', '-report-dir', self.res_dir]
52         for arg in kwargs:
53             cmd_line.extend(['-ginkgo.{}'.format(arg), kwargs.get(arg)])
54         if "NON_BLOCKING_TAINTS" in os.environ:
55             cmd_line.extend(
56                 ['-non-blocking-taints', os.environ["NON_BLOCKING_TAINTS"]])
57         cmd_line.extend(['-disable-log-dump', 'true'])
58         self._generate_repo_list_file()
59         self.__logger.info("Starting k8s test: '%s'.", cmd_line)
60         env = os.environ.copy()
61         env["GINKGO_PARALLEL"] = 'y'
62         env["KUBE_TEST_REPO_LIST"] = "{}/repositories.yml".format(self.res_dir)
63         process = subprocess.Popen(cmd_line, stdout=subprocess.PIPE,
64                                    stderr=subprocess.STDOUT, env=env)
65         boutput = process.stdout.read()
66         with open(os.path.join(self.res_dir, 'e2e.log'), 'wb') as foutput:
67             foutput.write(boutput)
68         grp = re.search(
69             r'^(FAIL|SUCCESS)!.* ([0-9]+) Passed \| ([0-9]+) Failed \|'
70             r' ([0-9]+) Pending \| ([0-9]+) Skipped',
71             boutput.decode("utf-8", errors="ignore"),
72             re.MULTILINE | re.DOTALL)
73         assert grp
74         self.details['passed'] = int(grp.group(2))
75         self.details['failed'] = int(grp.group(3))
76         self.details['pending'] = int(grp.group(4))
77         self.details['skipped'] = int(grp.group(5))
78         self.__logger.debug("details: %s", self.details)
79         self.result = self.details['passed'] * 100 / (
80             self.details['passed'] + self.details['failed'] +
81             self.details['pending'])
82         self.__logger.debug("result: %s", self.result)
83         if grp.group(1) == 'FAIL':
84             grp2 = re.search(
85                 r'^(Summarizing [0-9]+ Failure.*)Ran',
86                 boutput.decode("utf-8", errors="ignore"),
87                 re.MULTILINE | re.DOTALL)
88             if grp2:
89                 self.__logger.error(grp2.group(1))
90
91     def run(self, **kwargs):
92         if not os.path.exists(self.res_dir):
93             os.makedirs(self.res_dir)
94         if not os.path.isfile(self.config):
95             self.__logger.error(
96                 "Cannot run k8s testcases. Config file not found")
97             return self.EX_RUN_ERROR
98         self.start_time = time.time()
99         try:
100             self.run_kubetest(**kwargs)
101             res = self.EX_OK
102         except Exception:  # pylint: disable=broad-except
103             self.__logger.exception("Error with running kubetest:")
104             res = self.EX_RUN_ERROR
105         self.stop_time = time.time()
106         return res
107
108     def _generate_repo_list_file(self):
109         """Generate the repositories list for the test."""
110         # The list is taken from
111         # https://github.com/kubernetes/kubernetes/blob/master/test/utils/image/manifest.go
112         # It may needs update regularly
113         gcr_repo = os.getenv("GCR_REPO", self.gcr_repo)
114         k8s_gcr_repo = os.getenv("K8S_GCR_REPO", self.k8s_gcr_repo)
115         repo_list = {
116             "GcAuthenticatedRegistry": "{}/authenticated-image-pulling".format(
117                 gcr_repo),
118             "E2eRegistry":             "{}/kubernetes-e2e-test-images".format(
119                 gcr_repo),
120             "PromoterE2eRegistry":     "{}/e2e-test-images".format(
121                 k8s_gcr_repo),
122             "BuildImageRegistry":      "{}/build-image".format(k8s_gcr_repo),
123             "InvalidRegistry":         "invalid.com/invalid",
124             "GcEtcdRegistry":          "{}".format(k8s_gcr_repo),
125             "GcRegistry":              "{}".format(k8s_gcr_repo),
126             "SigStorageRegistry":      "{}/sig-storage".format(k8s_gcr_repo),
127             "PrivateRegistry":         "{}/k8s-authenticated-test".format(
128                 gcr_repo),
129             "SampleRegistry":          "{}/google-samples".format(gcr_repo),
130             "GcrReleaseRegistry":      "{}/gke-release".format(gcr_repo),
131             "MicrosoftRegistry":       "mcr.microsoft.com",
132         }
133         with open("{}/repositories.yml".format(self.res_dir), 'w') as file:
134             yaml.dump(repo_list, file)