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