1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
13 from yardstick.benchmark.scenarios.availability.attacker import \
17 class ExecuteShellTestCase(unittest.TestCase):
20 self._mock_subprocess = mock.patch.object(attacker_baremetal,
22 self.mock_subprocess = self._mock_subprocess.start()
24 self.addCleanup(self._stop_mocks)
26 def _stop_mocks(self):
27 self._mock_subprocess.stop()
29 def test__execute_shell_command_successful(self):
30 self.mock_subprocess.check_output.return_value = (0, 'unittest')
31 exitcode, _ = attacker_baremetal._execute_shell_command("env")
32 self.assertEqual(exitcode, 0)
34 @mock.patch.object(attacker_baremetal, 'LOG')
35 def test__execute_shell_command_fail_cmd_exception(self, mock_log):
36 self.mock_subprocess.check_output.side_effect = RuntimeError
37 exitcode, _ = attacker_baremetal._execute_shell_command("env")
38 self.assertEqual(exitcode, -1)
39 mock_log.error.assert_called_once()
42 class AttackerBaremetalTestCase(unittest.TestCase):
45 self._mock_ssh = mock.patch.object(attacker_baremetal, 'ssh')
46 self.mock_ssh = self._mock_ssh.start()
47 self._mock_subprocess = mock.patch.object(attacker_baremetal,
49 self.mock_subprocess = self._mock_subprocess.start()
50 self.addCleanup(self._stop_mocks)
52 self.mock_ssh.SSH.from_node().execute.return_value = (
56 "ipmi_ip": "10.20.0.5",
58 "ipmi_password": "123456",
61 "key_filename": "/root/.ssh/id_rsa"
63 self.context = {"node1": host}
65 'fault_type': 'bear-metal-down',
69 self.ins = attacker_baremetal.BaremetalAttacker(self.attacker_cfg,
72 def _stop_mocks(self):
74 self._mock_subprocess.stop()
76 def test__attacker_baremetal_all_successful(self):
78 self.ins.inject_fault()
81 def test__attacker_baremetal_check_failure(self):
82 self.mock_ssh.SSH.from_node().execute.return_value = (
86 def test__attacker_baremetal_recover_successful(self):
87 self.attacker_cfg["jump_host"] = 'node1'
88 self.context["node1"]["password"] = "123456"
89 ins = attacker_baremetal.BaremetalAttacker(self.attacker_cfg,