Merge "heat: close file before parsing template"
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_baseoperation.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
13 # yardstick.benchmark.scenarios.availability.operation.baseoperation
14
15 from __future__ import absolute_import
16 import mock
17 import unittest
18
19 from yardstick.benchmark.scenarios.availability.operation import baseoperation
20
21
22 @mock.patch(
23     'yardstick.benchmark.scenarios.availability.operation.baseoperation'
24     '.BaseOperation')
25 class OperationMgrTestCase(unittest.TestCase):
26
27     def setUp(self):
28         config = {
29             'operation_type': 'general-operation',
30             'key': 'service-status'
31         }
32
33         self.operation_configs = []
34         self.operation_configs.append(config)
35
36     def test_all_successful(self, mock_operation):
37         mgr_ins = baseoperation.OperationMgr()
38         mgr_ins.init_operations(self.operation_configs, None)
39         operation_ins = mgr_ins["service-status"]
40         mgr_ins.rollback()
41
42     def test_getitem_fail(self, mock_operation):
43         mgr_ins = baseoperation.OperationMgr()
44         mgr_ins.init_operations(self.operation_configs, None)
45         with self.assertRaises(KeyError):
46             operation_ins = mgr_ins["operation-not-exist"]
47
48
49 class TestOperation(baseoperation.BaseOperation):
50     __operation__type__ = "test-operation"
51
52     def setup(self):
53         pass
54
55     def run(self):
56         pass
57
58     def rollback(self):
59         pass
60
61
62 class BaseOperationTestCase(unittest.TestCase):
63
64     def setUp(self):
65         self.config = {
66             'operation_type': 'general-operation',
67             'key': 'service-status'
68         }
69
70     def test_all_successful(self):
71         base_ins = baseoperation.BaseOperation(self.config, None)
72         base_ins.setup()
73         base_ins.run()
74         base_ins.rollback()
75
76     def test_get_script_fullpath(self):
77         base_ins = baseoperation.BaseOperation(self.config, None)
78         base_ins.get_script_fullpath("ha_tools/test.bash")
79
80     def test_get_operation_cls_successful(self):
81         base_ins = baseoperation.BaseOperation(self.config, None)
82         operation_ins = base_ins.get_operation_cls("test-operation")
83
84     def test_get_operation_cls_fail(self):
85         base_ins = baseoperation.BaseOperation(self.config, None)
86         with self.assertRaises(RuntimeError):
87             operation_ins = base_ins.get_operation_cls("operation-not-exist")