Merge "Prohibit the importation of a list of libraries"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / availability / test_result_checker_general.py
1 ##############################################################################
2 # Copyright (c) 2016 Huan Li and others
3 # lihuansse@tongji.edu.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # Unittest for yardstick.benchmark.scenarios.availability.result_checker
11 # .result_checker_general
12
13 from __future__ import absolute_import
14 import mock
15 import unittest
16 import copy
17
18 from yardstick.benchmark.scenarios.availability.result_checker import \
19     result_checker_general
20
21
22 # pylint: disable=unused-argument
23 # disable this for now because I keep forgetting mock patch arg ordering
24
25
26 @mock.patch('yardstick.benchmark.scenarios.availability.result_checker.'
27             'result_checker_general.ssh')
28 @mock.patch('yardstick.benchmark.scenarios.availability.result_checker.'
29             'result_checker_general.open')
30 class GeneralResultCheckerTestCase(unittest.TestCase):
31
32     def setUp(self):
33         host = {
34             "ip": "10.20.0.5",
35             "user": "root",
36             "key_filename": "/root/.ssh/id_rsa"
37         }
38         self.context = {"node1": host}
39         self.checker_cfg = {
40             'parameter': {'processname': 'process'},
41             'checker_type': 'general-result-checker',
42             'condition': 'eq',
43             'expectedValue': 1,
44             'key': 'process-checker',
45             'checker_key': 'process-checker',
46             'host': 'node1'
47         }
48
49     def test__result_checker_eq(self, mock_open, mock_ssh):
50         ins = result_checker_general.GeneralResultChecker(self.checker_cfg,
51                                                           self.context)
52         mock_ssh.SSH.from_node().execute.return_value = (0, "1", '')
53         ins.setup()
54         self.assertTrue(ins.verify())
55
56     def test__result_checker_gt(self, mock_open, mock_ssh):
57         config = copy.deepcopy(self.checker_cfg)
58         config['condition'] = 'gt'
59         ins = result_checker_general.GeneralResultChecker(config,
60                                                           self.context)
61         mock_ssh.SSH.from_node().execute.return_value = (0, "2", '')
62         ins.setup()
63         self.assertTrue(ins.verify())
64
65     def test__result_checker_gt_eq(self, mock_open, mock_ssh):
66         config = copy.deepcopy(self.checker_cfg)
67         config['condition'] = 'gt_eq'
68         ins = result_checker_general.GeneralResultChecker(config,
69                                                           self.context)
70         mock_ssh.SSH.from_node().execute.return_value = (0, "1", '')
71         ins.setup()
72         self.assertTrue(ins.verify())
73
74     def test__result_checker_lt(self, mock_open, mock_ssh):
75         config = copy.deepcopy(self.checker_cfg)
76         config['condition'] = 'lt'
77         ins = result_checker_general.GeneralResultChecker(config,
78                                                           self.context)
79         mock_ssh.SSH.from_node().execute.return_value = (0, "0", '')
80         ins.setup()
81         self.assertTrue(ins.verify())
82
83     def test__result_checker_lt_eq(self, mock_open, mock_ssh):
84         config = copy.deepcopy(self.checker_cfg)
85         config['condition'] = 'lt_eq'
86         ins = result_checker_general.GeneralResultChecker(config,
87                                                           self.context)
88         mock_ssh.SSH.from_node().execute.return_value = (0, "1", '')
89         ins.setup()
90         self.assertTrue(ins.verify())
91
92     def test__result_checker_in(self, mock_open, mock_ssh):
93         config = copy.deepcopy(self.checker_cfg)
94         config['condition'] = 'in'
95         config['expectedValue'] = "value"
96         ins = result_checker_general.GeneralResultChecker(config,
97                                                           self.context)
98         mock_ssh.SSH.from_node().execute.return_value = (0, "value return", '')
99         ins.setup()
100         self.assertTrue(ins.verify())
101
102     def test__result_checker_wrong(self, mock_open, mock_ssh):
103         config = copy.deepcopy(self.checker_cfg)
104         config['condition'] = 'wrong'
105         ins = result_checker_general.GeneralResultChecker(config,
106                                                           self.context)
107         mock_ssh.SSH.from_node().execute.return_value = (0, "1", '')
108         ins.setup()
109         self.assertFalse(ins.verify())
110
111     def test__result_checker_fail(self, mock_open, mock_ssh):
112         config = copy.deepcopy(self.checker_cfg)
113         config.pop('parameter')
114         ins = result_checker_general.GeneralResultChecker(config,
115                                                           self.context)
116         mock_ssh.SSH.from_node().execute.return_value = (1, "fail", '')
117         ins.setup()
118         ins.verify()