3 """Run workloads via Rally against Kubernetes platform
5 xrally/kubernetes_ provides xRally plugins for Kubernetes platform.
7 .. _xrally/kubernetes: https://github.com/xrally/xrally-kubernetes/
14 from jinja2 import Template
17 from rally import exceptions
18 from rally.common import yamlutils as yaml
19 import rally.common.logging
20 from rally.env import env_mgr
22 from xtesting.core import testcase
25 class RallyKubernetes(testcase.TestCase):
26 # pylint: disable=too-many-instance-attributes
27 """Run tasks for checking basic functionality of Kubernetes cluster"""
29 __logger = logging.getLogger(__name__)
34 dockerhub_repo = os.getenv("MIRROR_REPO", "docker.io")
35 gcr_repo = os.getenv("MIRROR_REPO", "gcr.io")
36 k8s_gcr_repo = os.getenv("MIRROR_REPO", "k8s.gcr.io")
38 def __init__(self, **kwargs):
39 super().__init__(**kwargs)
40 self.dir_results = "/home/opnfv/functest/results"
41 self.res_dir = os.path.join(self.dir_results, self.case_name)
42 self.output_log_name = 'functest-kubernetes.log'
43 self.output_debug_log_name = 'functest-kubernetes.debug.log'
45 def run(self, **kwargs):
46 self.start_time = time.time()
47 if not os.path.exists(self.res_dir):
48 os.makedirs(self.res_dir)
50 api.CONF.set_default("use_stderr", False)
51 api.CONF.set_default('log_dir', self.res_dir)
52 api.CONF.set_default('log_file', 'rally.log')
53 rally.common.logging.setup("rally")
54 spec = env_mgr.EnvManager.create_spec_from_sys_environ()["spec"]
56 env_mgr.EnvManager.get('my-kubernetes').delete(force=True)
57 except exceptions.DBRecordNotFound:
59 env = env_mgr.EnvManager.create('my-kubernetes', spec)
60 result = env.check_health()
61 self.__logger.debug("check health %s: %s", 'my-kubernetes', result)
62 if not result['existing@kubernetes']['available']:
64 "Cannot check env heath: %s",
65 result['existing@kubernetes']['message'])
67 with open(pkg_resources.resource_filename(
68 'functest_kubernetes', 'rally/all-in-one.yaml'),
69 encoding='utf-8') as file:
70 template = Template(file.read())
71 task = yaml.safe_load(template.render(
72 concurrency=kwargs.get("concurrency", self.concurrency),
73 times=kwargs.get("times", self.times),
74 namespaces_count=kwargs.get(
75 "namespaces_count", self.namespaces_count),
76 dockerhub_repo=os.getenv("DOCKERHUB_REPO", self.dockerhub_repo),
77 gcr_repo=os.getenv("GCR_REPO", self.gcr_repo),
78 k8s_gcr_repo=os.getenv("K8S_GCR_REPO", self.k8s_gcr_repo)))
79 rapi.task.validate(deployment='my-kubernetes', config=task)
80 task_instance = rapi.task.create(deployment='my-kubernetes')
82 deployment='my-kubernetes', config=task,
83 task=task_instance["uuid"])
84 self.details = rapi.task.get(task_instance["uuid"], detailed=False)
85 self.__logger.debug("details: %s", self.details)
86 if self.details['pass_sla']:
88 result = rapi.task.export(
89 [task_instance["uuid"]], "html",
90 output_dest=os.path.join(
91 self.res_dir, f"{self.case_name}.html"))
93 for path in result["files"]:
94 with open(path, "w+", encoding='utf-8') as output:
95 output.write(result["files"][path])
96 result = rapi.task.export(
97 [task_instance["uuid"]], "junit-xml",
98 output_dest=os.path.join(
99 self.res_dir, f"{self.case_name}.xml"))
100 if "files" in result:
101 for path in result["files"]:
102 with open(path, "w+", encoding='utf-8') as output:
103 output.write(result["files"][path])
104 self.stop_time = time.time()