aboutsummaryrefslogtreecommitdiffstats
path: root/functest_kubernetes/rally/rally_kubernetes.py
blob: 28415579535a8ba57c330e4a3dd09641f855cb61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python

"""Run workloads via Rally against Kubernetes platform

xrally/kubernetes_ provides xRally plugins for Kubernetes platform.

.. _xrally/kubernetes: https://github.com/xrally/xrally-kubernetes/
"""

import logging
import os
import time

from jinja2 import Template
import pkg_resources
from rally import api
from rally import exceptions
from rally.common import yamlutils as yaml
import rally.common.logging
from rally.env import env_mgr

from xtesting.core import testcase


class RallyKubernetes(testcase.TestCase):
    """Run tasks for checking basic functionality of Kubernetes cluster"""

    __logger = logging.getLogger(__name__)

    concurrency = 1
    times = 1
    namespaces_count = 1

    def __init__(self, **kwargs):
        super(RallyKubernetes, self).__init__(**kwargs)
        self.dir_results = "/home/opnfv/functest/results"
        self.res_dir = os.path.join(self.dir_results, self.case_name)

    def run(self, **kwargs):
        self.start_time = time.time()
        if not os.path.exists(self.res_dir):
            os.makedirs(self.res_dir)
        rapi = api.API()
        api.CONF.set_default("use_stderr", False)
        api.CONF.set_default('log_dir', self.res_dir)
        api.CONF.set_default('log_file', 'rally.log')
        rally.common.logging.setup("rally")
        spec = env_mgr.EnvManager.create_spec_from_sys_environ()["spec"]
        try:
            env_mgr.EnvManager.get('my-kubernetes').delete(force=True)
        except exceptions.DBRecordNotFound:
            pass
        env = env_mgr.EnvManager.create('my-kubernetes', spec)
        result = env.check_health()
        self.__logger.debug("check health %s: %s", 'my-kubernetes', result)
        if not result['existing@kubernetes']['available']:
            self.__logger.error(
                "Cannot check env heath: %s",
                result['existing@kubernetes']['message'])
            return
        with open(pkg_resources.resource_filename(
                'functest_kubernetes', 'rally/all-in-one.yaml')) as file:
            template = Template(file.read())
        task = yaml.safe_load(template.render(
            concurrency=kwargs.get("concurrency", self.concurrency),
            times=kwargs.get("times", self.times),
            namespaces_count=kwargs.get(
                "namespaces_count", self.namespaces_count)))
        rapi.task.validate(deployment='my-kubernetes', config=task)
        task_instance = rapi.task.create(deployment='my-kubernetes')
        rapi.task.start(
            deployment='my-kubernetes', config=task,
            task=task_instance["uuid"])
        self.details = rapi.task.get(task_instance["uuid"], detailed=True)
        self.__logger.debug("details: %s", self.details)
        if self.details['pass_sla']:
            self.result = 100
        result = rapi.task.export(
            [task_instance["uuid"]], "html",
            output_dest=os.path.join(
                self.res_dir, "{}.html".format(self.case_name)))
        if "files" in result:
            for path in result["files"]:
                with open(path, "w+") as output:
                    output.write(result["files"][path])
        result = rapi.task.export(
            [task_instance["uuid"]], "junit-xml",
            output_dest=os.path.join(
                self.res_dir, "{}.xml".format(self.case_name)))
        if "files" in result:
            for path in result["files"]:
                with open(path, "w+") as output:
                    output.write(result["files"][path])
        self.stop_time = time.time()