Merge "Add host specific VM list to inspector design guide line"
[doctor.git] / doctor_tests / scenario / network_failure.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation 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 from doctor_tests.identity_auth import get_session
10 from doctor_tests.os_clients import nova_client
11 from doctor_tests.common.utils import SSHClient
12
13 LINK_DOWN_SCRIPT = """
14 #!/bin/bash -x
15 dev=$(sudo ip a | awk '/ {compute_ip}\//{{print $NF}}')
16 sleep 1
17 sudo ip link set $dev down
18 echo "doctor set link down at" $(date "+%s.%N")
19 sleep 10
20 sudo ip link set $dev up
21 sleep 1
22 """
23
24
25 class NetworkFault(object):
26
27     def __init__(self, conf, installer, log):
28         self.conf = conf
29         self.log = log
30         self.installer = installer
31         self.nova = nova_client(self.conf.nova_version, get_session())
32         self.host = None
33         self.GetLog = False
34
35     def start(self, host):
36         self.log.info('fault inject start......')
37         self._set_link_down(host.ip)
38         self.host = host
39         self.log.info('fault inject end......')
40
41     def cleanup(self):
42         self.log.info('fault inject cleanup......')
43         self.get_disable_network_log()
44
45     def get_disable_network_log(self):
46         if self.GetLog:
47             self.log.info('Already get the disable_netork.log from down_host......')
48             return
49         if self.host is not None:
50             client = SSHClient(self.host.ip,
51                                self.installer.node_user_name,
52                                key_filename=self.installer.get_ssh_key_from_installer(),
53                                look_for_keys=True,
54                                log=self.log)
55             client.scp('disable_network.log', './disable_network.log', method='get')
56             self.log.info('Get the disable_netork.log from down_host(host_name:%s, host_ip:%s)'
57                           % (self.host.name, self.host.ip))
58         self.GetLog = True
59
60     def _set_link_down(self, compute_ip):
61         file_name = './disable_network.sh'
62         with open(file_name, 'w') as file:
63             file.write(LINK_DOWN_SCRIPT.format(compute_ip=compute_ip))
64         client = SSHClient(compute_ip,
65                            self.installer.node_user_name,
66                            key_filename=self.installer.get_ssh_key_from_installer(),
67                            look_for_keys=True,
68                            log=self.log)
69         client.scp('./disable_network.sh', 'disable_network.sh')
70         command = 'bash disable_network.sh > disable_network.log 2>&1 &'
71         client.ssh(command)