A initial HA test case
[yardstick.git] / tests / unit / benchmark / scenarios / availability / test_serviceha.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
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.serviceha
13
14 import mock
15 import unittest
16
17 from yardstick.benchmark.scenarios.availability import serviceha
18
19 @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.ssh')
20 class ServicehaTestCase(unittest.TestCase):
21
22     def setUp(self):
23         self.args = {
24             'options':{
25                 'component':'nova-api',
26                 'fault_type':'stop-service',
27                 'fault_time':0
28             },
29             'sla':{
30                 'outage_time':'2'
31             }
32         }
33         self.ctx = {
34             'host': {
35                 'ip': '10.20.0.3',
36                 'user': 'cirros',
37                 'key_filename': 'mykey.key'
38             }
39         }
40
41     def test__serviceha_setup_successful(self, mock_ssh):
42         p = serviceha.ServiceHA(self.args, self.ctx)
43         mock_ssh.SSH().execute.return_value = (0, 'running', '')
44         p.setup()
45
46         self.assertEqual(p.setup_done, True)
47
48     def test__serviceha_setup_fail_service(self, mock_ssh):
49
50         self.args['options']['component'] = 'error'
51         p = serviceha.ServiceHA(self.args, self.ctx)
52         mock_ssh.SSH().execute.return_value = (0, 'running', '')
53         p.setup()
54
55         self.assertEqual(p.setup_done, False)
56
57     def test__serviceha_setup_fail_fault_type(self, mock_ssh):
58
59         self.args['options']['fault_type'] = 'error'
60         p = serviceha.ServiceHA(self.args, self.ctx)
61         mock_ssh.SSH().execute.return_value = (0, 'running', '')
62         p.setup()
63
64         self.assertEqual(p.setup_done, False)
65
66     def test__serviceha_setup_fail_check(self, mock_ssh):
67
68         p = serviceha.ServiceHA(self.args, self.ctx)
69         mock_ssh.SSH().execute.return_value = (0, 'error', '')
70         p.setup()
71
72         self.assertEqual(p.setup_done, False)
73
74     def test__serviceha_setup_fail_script(self, mock_ssh):
75
76         p = serviceha.ServiceHA(self.args, self.ctx)
77
78         mock_ssh.SSH().execute.return_value = (-1, 'false', '')
79
80         self.assertRaises(RuntimeError, p.setup)
81         self.assertEqual(p.setup_done, False)
82
83     @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.monitor')
84     def test__serviceha_run_successful(self, mock_monitor, mock_ssh):
85         p = serviceha.ServiceHA(self.args, self.ctx)
86         mock_ssh.SSH().execute.return_value = (0, 'running', '')
87         p.setup()
88
89         monitor_result = {'total_time': 5, 'outage_time': 0, 'total_count': 16, 'outage_count': 0}
90         mock_monitor.Monitor().get_result.return_value = monitor_result
91
92         p.connection = mock_ssh.SSH()
93         mock_ssh.SSH().execute.return_value = (0, 'success', '')
94
95         result = {}
96         p.run(result)
97         self.assertEqual(result,{ 'outage_time': 0})
98
99     def test__serviceha_run_fail_nosetup(self, mock_ssh):
100         p = serviceha.ServiceHA(self.args, self.ctx)
101         p.run(None)
102
103     @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.monitor')
104     def test__serviceha_run_fail_script(self, mock_monitor, mock_ssh):
105         p = serviceha.ServiceHA(self.args, self.ctx)
106         mock_ssh.SSH().execute.return_value = (0, 'running', '')
107         p.setup()
108
109         monitor_result = {'total_time': 5, 'outage_time': 0, 'total_count': 16, 'outage_count': 0}
110         mock_monitor.Monitor().get_result.return_value = monitor_result
111
112         p.connection = mock_ssh.SSH()
113         mock_ssh.SSH().execute.return_value = (-1, 'error', '')
114
115         result = {}
116         self.assertRaises(RuntimeError, p.run, result)
117
118     @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.monitor')
119     def test__serviceha_run_fail_sla(self, mock_monitor, mock_ssh):
120         p = serviceha.ServiceHA(self.args, self.ctx)
121         mock_ssh.SSH().execute.return_value = (0, 'running', '')
122         p.setup()
123
124         monitor_result = {'total_time': 10, 'outage_time': 5, 'total_count': 16, 'outage_count': 0}
125         mock_monitor.Monitor().get_result.return_value = monitor_result
126
127         p.connection = mock_ssh.SSH()
128         mock_ssh.SSH().execute.return_value = (0, 'success', '')
129
130         result = {}
131         self.assertRaises(AssertionError, p.run, result)
132
133     def test__serviceha_teardown_successful(self, mock_ssh):
134         p = serviceha.ServiceHA(self.args, self.ctx)
135         mock_ssh.SSH().execute.return_value = (0, 'running', '')
136         p.setup()
137         p.need_teardown = True
138
139         mock_ssh.SSH().execute.return_value = (0, 'success', '')
140         p.teardown()
141
142         self.assertEqual(p.need_teardown, False)
143
144     def test__serviceha_teardown_fail_script(self, mock_ssh):
145         p = serviceha.ServiceHA(self.args, self.ctx)
146         mock_ssh.SSH().execute.return_value = (0, 'running', '')
147         p.setup()
148         p.need_teardown = True
149
150         mock_ssh.SSH().execute.return_value = (-1, 'false', '')
151
152         self.assertRaises(RuntimeError, p.teardown)
153