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