de7d26cbf9d2ea73991223eec9f8feeb874ccb65
[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             'monitor_key': 'service-status',
36             'host': 'node1',
37             'monitor_time': 3,
38             'parameter': {'serviceName': 'haproxy'},
39             'sla': {'max_outage_time': 1}
40         }
41         self.monitor_cfg_noparam = {
42             'monitor_type': 'general-monitor',
43             'key': 'service-status',
44             'monitor_key': 'service-status',
45             'host': 'node1',
46             'monitor_time': 3,
47             'sla': {'max_outage_time': 1}
48         }
49
50     def test__monitor_general_all_successful(self, mock_open, mock_ssh):
51         ins = monitor_general.GeneralMonitor(self.monitor_cfg, self.context)
52
53         ins.setup()
54         mock_ssh.SSH().execute.return_value = (0, "running", '')
55         ins.monitor_func()
56         ins._result = {'outage_time' : 0}
57         ins.verify_SLA()
58
59     def test__monitor_general_all_successful_noparam(self, mock_open, mock_ssh):
60         ins = monitor_general.GeneralMonitor(self.monitor_cfg_noparam, self.context)
61
62         ins.setup()
63         mock_ssh.SSH().execute.return_value = (0, "running", '')
64         ins.monitor_func()
65         ins._result = {'outage_time' : 0}
66         ins.verify_SLA()
67
68     def test__monitor_general_failure(self, mock_open, mock_ssh):
69         ins = monitor_general.GeneralMonitor(self.monitor_cfg_noparam, self.context)
70
71         ins.setup()
72         mock_ssh.SSH().execute.return_value = (1, "error", 'error')
73         ins.monitor_func()
74         ins._result = {'outage_time' : 2}
75         ins.verify_SLA()