3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.scenarios.availability.serviceha
17 from yardstick.benchmark.scenarios.availability import serviceha
19 @mock.patch('yardstick.benchmark.scenarios.availability.serviceha.ssh')
20 class ServicehaTestCase(unittest.TestCase):
25 'component':'nova-api',
26 'fault_type':'stop-service',
37 'key_filename': 'mykey.key'
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', '')
46 self.assertEqual(p.setup_done, True)
48 def test__serviceha_setup_fail_service(self, mock_ssh):
50 self.args['options']['component'] = 'error'
51 p = serviceha.ServiceHA(self.args, self.ctx)
52 mock_ssh.SSH().execute.return_value = (0, 'running', '')
55 self.assertEqual(p.setup_done, False)
57 def test__serviceha_setup_fail_fault_type(self, mock_ssh):
59 self.args['options']['fault_type'] = 'error'
60 p = serviceha.ServiceHA(self.args, self.ctx)
61 mock_ssh.SSH().execute.return_value = (0, 'running', '')
64 self.assertEqual(p.setup_done, False)
66 def test__serviceha_setup_fail_check(self, mock_ssh):
68 p = serviceha.ServiceHA(self.args, self.ctx)
69 mock_ssh.SSH().execute.return_value = (0, 'error', '')
72 self.assertEqual(p.setup_done, False)
74 def test__serviceha_setup_fail_script(self, mock_ssh):
76 p = serviceha.ServiceHA(self.args, self.ctx)
78 mock_ssh.SSH().execute.return_value = (-1, 'false', '')
80 self.assertRaises(RuntimeError, p.setup)
81 self.assertEqual(p.setup_done, False)
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', '')
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
92 p.connection = mock_ssh.SSH()
93 mock_ssh.SSH().execute.return_value = (0, 'success', '')
97 self.assertEqual(result,{ 'outage_time': 0})
99 def test__serviceha_run_fail_nosetup(self, mock_ssh):
100 p = serviceha.ServiceHA(self.args, self.ctx)
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', '')
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
112 p.connection = mock_ssh.SSH()
113 mock_ssh.SSH().execute.return_value = (-1, 'error', '')
116 self.assertRaises(RuntimeError, p.run, result)
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', '')
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
127 p.connection = mock_ssh.SSH()
128 mock_ssh.SSH().execute.return_value = (0, 'success', '')
131 self.assertRaises(AssertionError, p.run, result)
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', '')
137 p.need_teardown = True
139 mock_ssh.SSH().execute.return_value = (0, 'success', '')
142 self.assertEqual(p.need_teardown, False)
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', '')
148 p.need_teardown = True
150 mock_ssh.SSH().execute.return_value = (-1, 'false', '')
152 self.assertRaises(RuntimeError, p.teardown)