d5cb26d3c7a6df35d2514f8daceb9f5e3b99ca42
[functest-kubernetes.git] / functest_kubernetes / rally / rally_kubernetes.py
1 #!/usr/bin/env python
2
3 """Run workloads via Rally against Kubernetes platform
4
5 xrally/kubernetes_ provides xRally plugins for Kubernetes platform.
6
7 .. _xrally/kubernetes: https://github.com/xrally/xrally-kubernetes/
8 """
9
10 import logging
11 import os
12 import time
13
14 from jinja2 import Template
15 import pkg_resources
16 from rally import api
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
21
22 from xtesting.core import testcase
23
24
25 class RallyKubernetes(testcase.TestCase):
26     # pylint: disable=too-many-instance-attributes
27     """Run tasks for checking basic functionality of Kubernetes cluster"""
28
29     __logger = logging.getLogger(__name__)
30
31     concurrency = 1
32     times = 1
33     namespaces_count = 1
34
35     def __init__(self, **kwargs):
36         super(RallyKubernetes, self).__init__(**kwargs)
37         self.dir_results = "/home/opnfv/functest/results"
38         self.res_dir = os.path.join(self.dir_results, self.case_name)
39         self.output_log_name = 'functest-kubernetes.log'
40         self.output_debug_log_name = 'functest-kubernetes.debug.log'
41
42     def run(self, **kwargs):
43         self.start_time = time.time()
44         if not os.path.exists(self.res_dir):
45             os.makedirs(self.res_dir)
46         rapi = api.API()
47         api.CONF.set_default("use_stderr", False)
48         api.CONF.set_default('log_dir', self.res_dir)
49         api.CONF.set_default('log_file', 'rally.log')
50         rally.common.logging.setup("rally")
51         spec = env_mgr.EnvManager.create_spec_from_sys_environ()["spec"]
52         try:
53             env_mgr.EnvManager.get('my-kubernetes').delete(force=True)
54         except exceptions.DBRecordNotFound:
55             pass
56         env = env_mgr.EnvManager.create('my-kubernetes', spec)
57         result = env.check_health()
58         self.__logger.debug("check health %s: %s", 'my-kubernetes', result)
59         if not result['existing@kubernetes']['available']:
60             self.__logger.error(
61                 "Cannot check env heath: %s",
62                 result['existing@kubernetes']['message'])
63             return
64         with open(pkg_resources.resource_filename(
65                 'functest_kubernetes', 'rally/all-in-one.yaml')) as file:
66             template = Template(file.read())
67         task = yaml.safe_load(template.render(
68             concurrency=kwargs.get("concurrency", self.concurrency),
69             times=kwargs.get("times", self.times),
70             namespaces_count=kwargs.get(
71                 "namespaces_count", self.namespaces_count)))
72         rapi.task.validate(deployment='my-kubernetes', config=task)
73         task_instance = rapi.task.create(deployment='my-kubernetes')
74         rapi.task.start(
75             deployment='my-kubernetes', config=task,
76             task=task_instance["uuid"])
77         self.details = rapi.task.get(task_instance["uuid"], detailed=True)
78         self.__logger.debug("details: %s", self.details)
79         if self.details['pass_sla']:
80             self.result = 100
81         result = rapi.task.export(
82             [task_instance["uuid"]], "html",
83             output_dest=os.path.join(
84                 self.res_dir, "{}.html".format(self.case_name)))
85         if "files" in result:
86             for path in result["files"]:
87                 with open(path, "w+") as output:
88                     output.write(result["files"][path])
89         result = rapi.task.export(
90             [task_instance["uuid"]], "junit-xml",
91             output_dest=os.path.join(
92                 self.res_dir, "{}.xml".format(self.case_name)))
93         if "files" in result:
94             for path in result["files"]:
95                 with open(path, "w+") as output:
96                     output.write(result["files"][path])
97         self.stop_time = time.time()