72ce7b0d5e1040a9e9f6225e1166ffe705ddcb4c
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / availability / test_director.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huan Li and others
5 # lihuansse@tongji.edu.cn
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.availability.director
13
14 from __future__ import absolute_import
15 import mock
16 import unittest
17
18 from yardstick.benchmark.scenarios.availability.director import Director
19
20
21 # pylint: disable=unused-argument
22 # disable this for now because I keep forgetting mock patch arg ordering
23
24
25 @mock.patch('yardstick.benchmark.scenarios.availability.director.basemonitor')
26 @mock.patch('yardstick.benchmark.scenarios.availability.director.baseattacker')
27 @mock.patch(
28     'yardstick.benchmark.scenarios.availability.director.baseoperation')
29 @mock.patch(
30     'yardstick.benchmark.scenarios.availability.director.baseresultchecker')
31 class DirectorTestCase(unittest.TestCase):
32
33     def setUp(self):
34         self.scenario_cfg = {
35             'type': "general_scenario",
36             'options': {
37                 'attackers': [{
38                     'fault_type': "general-attacker",
39                     'key': "kill-process"}],
40                 'monitors': [{
41                     'monitor_type': "general-monitor",
42                     'key': "service-status"}],
43                 'operations': [{
44                     'operation_type': 'general-operation',
45                     'key': 'service-status'}],
46                 'resultCheckers': [{
47                     'checker_type': 'general-result-checker',
48                     'key': 'process-checker', }],
49                 'steps': [
50                     {
51                         'actionKey': "service-status",
52                         'actionType': "operation",
53                         'index': 1},
54                     {
55                         'actionKey': "kill-process",
56                         'actionType': "attacker",
57                         'index': 2},
58                     {
59                         'actionKey': "process-checker",
60                         'actionType': "resultchecker",
61                         'index': 3},
62                     {
63                         'actionKey': "service-status",
64                         'actionType': "monitor",
65                         'index': 4},
66                 ]
67             }
68         }
69         host = {
70             "ip": "10.20.0.5",
71             "user": "root",
72             "key_filename": "/root/.ssh/id_rsa"
73         }
74         self.ctx = {"nodes": {"node1": host}}
75
76     def test_director_all_successful(self, mock_checer, mock_opertion,
77                                      mock_attacker, mock_monitor):
78         ins = Director(self.scenario_cfg, self.ctx)
79         opertion_action = ins.createActionPlayer("operation", "service-status")
80         attacker_action = ins.createActionPlayer("attacker", "kill-process")
81         checker_action = ins.createActionPlayer("resultchecker",
82                                                 "process-checker")
83         monitor_action = ins.createActionPlayer("monitor", "service-status")
84
85         opertion_rollback = ins.createActionRollbacker("operation",
86                                                        "service-status")
87         attacker_rollback = ins.createActionRollbacker("attacker",
88                                                        "kill-process")
89         ins.executionSteps.append(opertion_rollback)
90         ins.executionSteps.append(attacker_rollback)
91
92         opertion_action.action()
93         attacker_action.action()
94         checker_action.action()
95         monitor_action.action()
96
97         attacker_rollback.rollback()
98         opertion_rollback.rollback()
99
100         ins.stopMonitors()
101         ins.verify()
102         ins.knockoff()
103
104     def test_director_get_wrong_item(self, mock_checer, mock_opertion,
105                                      mock_attacker, mock_monitor):
106         ins = Director(self.scenario_cfg, self.ctx)
107         ins.createActionPlayer("wrong_type", "wrong_key")
108         ins.createActionRollbacker("wrong_type", "wrong_key")