f0921c0f610374ae89af01a10871c7e9b14f1a73
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / availability / test_attacker_baremetal.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
13 # yardstick.benchmark.scenarios.availability.attacker.attacker_baremetal
14
15 from __future__ import absolute_import
16 import mock
17 import unittest
18
19 from yardstick.benchmark.scenarios.availability.attacker import \
20     attacker_baremetal
21
22
23 # pylint: disable=unused-argument
24 # disable this for now because I keep forgetting mock patch arg ordering
25
26
27 @mock.patch('yardstick.benchmark.scenarios.availability.attacker.attacker_baremetal.subprocess')
28 class ExecuteShellTestCase(unittest.TestCase):
29
30     def test__fun_execute_shell_command_successful(self, mock_subprocess):
31         cmd = "env"
32         mock_subprocess.check_output.return_value = (0, 'unittest')
33         exitcode, _ = attacker_baremetal._execute_shell_command(cmd)
34         self.assertEqual(exitcode, 0)
35
36     @mock.patch('yardstick.benchmark.scenarios.availability.attacker.attacker_baremetal.LOG')
37     def test__fun_execute_shell_command_fail_cmd_exception(self, mock_log, mock_subprocess):
38         cmd = "env"
39         mock_subprocess.check_output.side_effect = RuntimeError
40         exitcode, _ = attacker_baremetal._execute_shell_command(cmd)
41         self.assertEqual(exitcode, -1)
42         mock_log.error.assert_called_once()
43
44
45 @mock.patch('yardstick.benchmark.scenarios.availability.attacker.attacker_baremetal.subprocess')
46 @mock.patch('yardstick.benchmark.scenarios.availability.attacker.attacker_baremetal.ssh')
47 class AttackerBaremetalTestCase(unittest.TestCase):
48
49     def setUp(self):
50         host = {
51             "ipmi_ip": "10.20.0.5",
52             "ipmi_user": "root",
53             "ipmi_pwd": "123456",
54             "ip": "10.20.0.5",
55             "user": "root",
56             "key_filename": "/root/.ssh/id_rsa"
57         }
58         self.context = {"node1": host}
59         self.attacker_cfg = {
60             'fault_type': 'bear-metal-down',
61             'host': 'node1',
62         }
63
64     def test__attacker_baremetal_all_successful(self, mock_ssh, mock_subprocess):
65         mock_ssh.SSH.from_node().execute.return_value = (0, "running", '')
66         ins = attacker_baremetal.BaremetalAttacker(self.attacker_cfg,
67                                                    self.context)
68
69         ins.setup()
70         ins.inject_fault()
71         ins.recover()
72
73     def test__attacker_baremetal_check_failuer(self, mock_ssh, mock_subprocess):
74         mock_ssh.SSH.from_node().execute.return_value = (0, "error check", '')
75         ins = attacker_baremetal.BaremetalAttacker(self.attacker_cfg,
76                                                    self.context)
77         ins.setup()
78
79     def test__attacker_baremetal_recover_successful(self, mock_ssh, mock_subprocess):
80
81         self.attacker_cfg["jump_host"] = 'node1'
82         self.context["node1"]["pwd"] = "123456"
83         mock_ssh.SSH.from_node().execute.return_value = (0, "running", '')
84         ins = attacker_baremetal.BaremetalAttacker(self.attacker_cfg,
85                                                    self.context)
86
87         ins.setup()
88         ins.recover()