aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/availability/scenario_general.py
blob: 9ac55471d489b3d3681d8558a463962515d0fbdd (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
##############################################################################
# Copyright (c) 2016 Juan Qiu and others
# juan_ qiu@tongji.edu.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import
import logging

from yardstick.benchmark.scenarios import base
from yardstick.benchmark.scenarios.availability.director import Director

LOG = logging.getLogger(__name__)


class ScenarioGeneral(base.Scenario):
    """Support orchestrating general HA test scenarios."""

    __scenario_type__ = "GeneralHA"

    def __init__(self, scenario_cfg, context_cfg):
        LOG.debug(
            "scenario_cfg:%s context_cfg:%s", scenario_cfg, context_cfg)
        self.scenario_cfg = scenario_cfg
        self.context_cfg = context_cfg
        self.intermediate_variables = {}
        self.pass_flag = True

    def setup(self):
        self.director = Director(self.scenario_cfg, self.context_cfg)

    def run(self, result):
        steps = self.scenario_cfg["options"]["steps"]
        orderedSteps = sorted(steps, key=lambda x: x['index'])
        for step in orderedSteps:
            LOG.debug(
                "\033[94m running step: %s .... \033[0m",
                orderedSteps.index(step) + 1)
            try:
                actionPlayer = self.director.createActionPlayer(
                    step['actionType'], step['actionKey'],
                    self.intermediate_variables)
                actionPlayer.action()
                actionRollbacker = self.director.createActionRollbacker(
                    step['actionType'], step['actionKey'])
                if actionRollbacker:
                    self.director.executionSteps.append(actionRollbacker)
            except Exception:
                LOG.exception("Exception")
                LOG.debug(
                    "\033[91m exception when running step: %s .... \033[0m",
                    orderedSteps.index(step))
                break
            finally:
                pass

        self.director.stopMonitors()

        verify_result = self.director.verify()

        self.director.store_result(result)

        for k, v in self.director.data.items():
            if v == 0:
                result['sla_pass'] = 0
                verify_result = False
                self.pass_flag = False
                LOG.info(
                    "\033[92m The service process not found in the host \
envrioment, the HA test case NOT pass")

        if verify_result:
            result['sla_pass'] = 1
            LOG.info(
                "\033[92m Congratulations, "
                "the HA test case PASS! \033[0m")
        else:
            result['sla_pass'] = 0
            self.pass_flag = False
            LOG.info(
                "\033[91m Aoh, the HA test case FAIL,"
                "please check the detail debug information! \033[0m")

    def teardown(self):
        self.director.knockoff()

        assert self.pass_flag, "The HA test case NOT passed"