Remove clusterIP: None in ims svc.yaml
[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         # pylint: disable=bad-continuation
65         with open(pkg_resources.resource_filename(
66                 'functest_kubernetes', 'rally/all-in-one.yaml')) as tfile:
67             template = Template(tfile.read())
68         task = yaml.safe_load(template.render(
69             concurrency=kwargs.get("concurrency", self.concurrency),
70             times=kwargs.get("times", self.times),
71             namespaces_count=kwargs.get(
72                 "namespaces_count", self.namespaces_count)))
73         rapi.task.validate(deployment='my-kubernetes', config=task)
74         task_instance = rapi.task.create(deployment='my-kubernetes')
75         rapi.task.start(
76             deployment='my-kubernetes', config=task,
77             task=task_instance["uuid"])
78         self.details = rapi.task.get(task_instance["uuid"], detailed=False)
79         self.__logger.debug("details: %s", self.details)
80         if self.details['pass_sla']:
81             self.result = 100
82         result = rapi.task.export(
83             [task_instance["uuid"]], "html",
84             output_dest=os.path.join(
85                 self.res_dir, "{}.html".format(self.case_name)))
86         if "files" in result:
87             for path in result["files"]:
88                 with open(path, "w+") as output:
89                     output.write(result["files"][path])
90         result = rapi.task.export(
91             [task_instance["uuid"]], "junit-xml",
92             output_dest=os.path.join(
93                 self.res_dir, "{}.xml".format(self.case_name)))
94         if "files" in result:
95             for path in result["files"]:
96                 with open(path, "w+") as output:
97                     output.write(result["files"][path])
98         self.stop_time = time.time()