Merge "vsperf: Initial integration with VSPERF"
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_monitor_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.monitor
13 # .monitor_general
14
15 import mock
16 import unittest
17 from yardstick.benchmark.scenarios.availability.monitor import monitor_general
18
19
20 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.'
21             'monitor_general.ssh')
22 @mock.patch('yardstick.benchmark.scenarios.availability.monitor.'
23             'monitor_general.open')
24 class GeneralMonitorServiceTestCase(unittest.TestCase):
25     def setUp(self):
26         host = {
27             "ip": "10.20.0.5",
28             "user": "root",
29             "key_filename": "/root/.ssh/id_rsa"
30         }
31         self.context = {"node1": host}
32         self.monitor_cfg = {
33             'monitor_type': 'general-monitor',
34             'key': 'service_status',
35             'host': 'node1',
36             'monitor_time': 3,
37             'parameter': {'serviceName': 'haproxy'},
38             'sla': {'max_recover_time': 1}
39         }
40         self.monitor_cfg_noparam = {
41             'monitor_type': 'general-monitor',
42             'key': 'service_status',
43             'host': 'node1',
44             'monitor_time': 3,
45             'sla': {'max_recover_time': 1}
46         }
47
48     def test__monitor_general_all_successful(self, mock_open, mock_ssh):
49         ins = monitor_general.GeneralMonitor(self.monitor_cfg, self.context)
50
51         ins.setup()
52         mock_ssh.SSH().execute.return_value = (0, "running", '')
53         ins.monitor_func()
54         ins._result = {'outage_time' : 0}
55         ins.verify_SLA()
56
57     def test__monitor_general_all_successful_noparam(self, mock_open, mock_ssh):
58         ins = monitor_general.GeneralMonitor(self.monitor_cfg_noparam, self.context)
59
60         ins.setup()
61         mock_ssh.SSH().execute.return_value = (0, "running", '')
62         ins.monitor_func()
63         ins._result = {'outage_time' : 0}
64         ins.verify_SLA()
65
66     def test__monitor_general_failure(self, mock_open, mock_ssh):
67         ins = monitor_general.GeneralMonitor(self.monitor_cfg_noparam, self.context)
68
69         ins.setup()
70         mock_ssh.SSH().execute.return_value = (1, "error", 'error')
71         ins.monitor_func()
72         ins._result = {'outage_time' : 2}
73         ins.verify_SLA()