Merge "heat: close file before parsing template"
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_operation_general.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.operation
13 # .operation_general
14
15 from __future__ import absolute_import
16 import mock
17 import unittest
18 from yardstick.benchmark.scenarios.availability.operation import \
19     operation_general
20
21
22 @mock.patch('yardstick.benchmark.scenarios.availability.operation.'
23             'operation_general.ssh')
24 @mock.patch('yardstick.benchmark.scenarios.availability.operation.'
25             'operation_general.open')
26 class GeneralOperaionTestCase(unittest.TestCase):
27
28     def setUp(self):
29         host = {
30             "ip": "10.20.0.5",
31             "user": "root",
32             "key_filename": "/root/.ssh/id_rsa"
33         }
34         self.context = {"node1": host}
35         self.operation_cfg = {
36             'operation_type': 'general-operation',
37             'action_parameter': {'ins_cup': 2},
38             'rollback_parameter': {'ins_id': 'id123456'},
39             'key': 'nova-create-instance',
40             'operation_key': 'nova-create-instance',
41             'host': 'node1',
42         }
43         self.operation_cfg_noparam = {
44             'operation_type': 'general-operation',
45             'key': 'nova-create-instance',
46             'operation_key': 'nova-create-instance',
47             'host': 'node1',
48         }
49
50     def test__operation_successful(self, mock_open, mock_ssh):
51         ins = operation_general.GeneralOperaion(self.operation_cfg,
52                                                 self.context)
53         mock_ssh.SSH().execute.return_value = (0, "success", '')
54         ins.setup()
55         ins.run()
56         ins.rollback()
57
58     def test__operation_successful_noparam(self, mock_open, mock_ssh):
59         ins = operation_general.GeneralOperaion(self.operation_cfg_noparam,
60                                                 self.context)
61         mock_ssh.SSH().execute.return_value = (0, "success", '')
62         ins.setup()
63         ins.run()
64         ins.rollback()
65
66     def test__operation_fail(self, mock_open, mock_ssh):
67         ins = operation_general.GeneralOperaion(self.operation_cfg,
68                                                 self.context)
69         mock_ssh.SSH().execute.return_value = (1, "failed", '')
70         ins.setup()
71         ins.run()
72         ins.rollback()