Test port data plane status on Sample Inspector
[doctor.git] / tests / utils.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 import json
10 import os
11 import paramiko
12
13
14 def load_json_file(full_path):
15     """Loads JSON from file
16     :param target_filename:
17     :return:
18     """
19     if not os.path.isfile(full_path):
20         raise Exception('File(%s) does not exist' % full_path)
21
22     with open(full_path, 'r') as file:
23         return json.load(file)
24
25
26 def write_json_file(full_path, data):
27     """write JSON from file
28     :param target_filename:
29     :return:
30     """
31
32     with open(full_path, 'w+') as file:
33         file.write(json.dumps(data))
34
35
36 class SSHClient(object):
37     def __init__(self, ip, username, password=None, pkey=None,
38                  key_filename=None, log=None, look_for_keys=False,
39                  allow_agent=False):
40         self.client = paramiko.SSHClient()
41         self.client.load_system_host_keys()
42         self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
43         self.client.connect(ip, username=username, password=password,
44                             pkey=pkey, key_filename=key_filename,
45                             look_for_keys=look_for_keys,
46                             allow_agent=allow_agent)
47         self.log = log
48
49     def __del__(self):
50         self.client.close()
51
52     def ssh(self, command):
53         if self.log:
54             self.log.debug("Executing: %s" % command)
55         stdin, stdout, stderr = self.client.exec_command(command)
56         ret = stdout.channel.recv_exit_status()
57         output = list()
58         for line in stdout.read().splitlines():
59             output.append(line.decode('utf-8'))
60         if ret:
61             if self.log:
62                 self.log.debug("*** FAILED to run command %s (%s)" % (command, ret))
63             raise Exception(
64                 "Unable to run \ncommand: %s\nret: %s"
65                 % (command, ret))
66         if self.log:
67             self.log.debug("*** SUCCESSFULLY run command %s" % command)
68         return ret, output
69
70     def scp(self, source, dest, method='put'):
71         if self.log:
72             self.log.info("Copy %s -> %s" % (source, dest))
73         ftp = self.client.open_sftp()
74         if method == 'put':
75             ftp.put(source, dest)
76         elif method == 'get':
77             ftp.get(source, dest)
78         ftp.close()
79
80 def run_async(func):
81     from threading import Thread
82     from functools import wraps
83
84     @wraps(func)
85     def async_func(*args, **kwargs):
86         thread = Thread(target=func, args=args, kwargs=kwargs)
87         thread.start()
88         return thread
89
90     return async_func