Merge "heat: close file before parsing template"
[yardstick.git] / 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 @mock.patch('yardstick.benchmark.scenarios.availability.director.basemonitor')
22 @mock.patch('yardstick.benchmark.scenarios.availability.director.baseattacker')
23 @mock.patch(
24     'yardstick.benchmark.scenarios.availability.director.baseoperation')
25 @mock.patch(
26     'yardstick.benchmark.scenarios.availability.director.baseresultchecker')
27 class DirectorTestCase(unittest.TestCase):
28
29     def setUp(self):
30         self.scenario_cfg = {
31             'type': "general_scenario",
32             'options': {
33                 'attackers': [{
34                     'fault_type': "general-attacker",
35                     'key': "kill-process"}],
36                 'monitors': [{
37                     'monitor_type': "general-monitor",
38                     'key': "service-status"}],
39                 'operations': [{
40                     'operation_type': 'general-operation',
41                     'key': 'service-status'}],
42                 'resultCheckers': [{
43                     'checker_type': 'general-result-checker',
44                     'key': 'process-checker', }],
45                 'steps': [
46                     {
47                         'actionKey': "service-status",
48                         'actionType': "operation",
49                         'index': 1},
50                     {
51                         'actionKey': "kill-process",
52                         'actionType': "attacker",
53                         'index': 2},
54                     {
55                         'actionKey': "process-checker",
56                         'actionType': "resultchecker",
57                         'index': 3},
58                     {
59                         'actionKey': "service-status",
60                         'actionType': "monitor",
61                         'index': 4},
62                 ]
63             }
64         }
65         host = {
66             "ip": "10.20.0.5",
67             "user": "root",
68             "key_filename": "/root/.ssh/id_rsa"
69         }
70         self.ctx = {"nodes": {"node1": host}}
71
72     def test_director_all_successful(self, mock_checer, mock_opertion,
73                                      mock_attacker, mock_monitor):
74         ins = Director(self.scenario_cfg, self.ctx)
75         opertion_action = ins.createActionPlayer("operation", "service-status")
76         attacker_action = ins.createActionPlayer("attacker", "kill-process")
77         checker_action = ins.createActionPlayer("resultchecker",
78                                                 "process-checker")
79         monitor_action = ins.createActionPlayer("monitor", "service-status")
80
81         opertion_rollback = ins.createActionRollbacker("operation",
82                                                        "service-status")
83         attacker_rollback = ins.createActionRollbacker("attacker",
84                                                        "kill-process")
85         ins.executionSteps.append(opertion_rollback)
86         ins.executionSteps.append(attacker_rollback)
87
88         opertion_action.action()
89         attacker_action.action()
90         checker_action.action()
91         monitor_action.action()
92
93         attacker_rollback.rollback()
94         opertion_rollback.rollback()
95
96         ins.stopMonitors()
97         ins.verify()
98         ins.knockoff()
99
100     def test_director_get_wrong_item(self, mock_checer, mock_opertion,
101                                      mock_attacker, mock_monitor):
102         ins = Director(self.scenario_cfg, self.ctx)
103         ins.createActionPlayer("wrong_type", "wrong_key")
104         ins.createActionRollbacker("wrong_type", "wrong_key")