Removed heatbride call
[testsuite/python-testing-utils.git] / robotframework-onap / eteutils / EteGatherDataListener.py
1 import os.path
2 import paramiko
3 import logging
4 from sys import stderr
5
6 """
7 EteGatherDataListener implements the ROBOT listener API version 2 and is
8 instantiated via the robot cammmand line option
9
10    --listener EteGatherDataListener:<jobbumber>:<key filename>
11
12 The purpose is to gather and preserve debugging data from each of the application
13 VMs when an ETE test fails.
14
15 This listener counts the number of test
16 cases that have failed and, if > 0 at then end of the robot exection (close()),
17 will connect to each application vm and
18
19 2. upload the gather_data.sh
20 2. execute gather_data.sh
21 3. Transfer the resulting zip file to the Robot reports folder
22
23 This will enable the Jenkins job to retrieve the debug data along with the
24 Robot logs and reports  and archive it with the failed job for later retreival.
25
26 Note that the gather_data.sh depends upon the application providing
27 a /opt/gather_application_data.sh on their respective VMs for the zip file
28 to be created.
29 """
30
31
32 class EteGatherDataListener(object):
33     ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
34     ROBOT_LISTENER_API_VERSION = 2
35
36     APPLICATIONS = {
37         "aai" : "10.0.1.1",
38         "appc" : "10.0.2.1",
39         "sdc" : "10.0.3.1",
40         "dcae" : "10.0.4.1",
41         "mso" : "10.0.5.1",
42         "policy" : "10.0.6.1",
43         "sdnc" : "10.0.7.1",
44         "vid" : "10.0.8.1",
45         "portal" : "10.0.9.1",
46         "message_router" : "10.0.11.1",
47         "dcae_pstg00" : "10.0.4.101",
48         "dcae_coll00" : "10.0.4.102",
49         "dcae_cdap00" : "10.0.4.103",
50         "dcae_cdap01" : "10.0.4.104",
51         "dcae_cdap02" : "10.0.4.105"
52         }
53
54     keyfile = ""
55     local_gather_data_sh = ""
56
57     def __init__(self, job='10', keyfile='/share/config/key.pvt', shell="gather_data.sh"):
58         self.tests_passed = 0
59         self.tests_failed = 0
60         self.output_folder = ''
61         self.job = job
62         self.folder= ''
63         self.keyfile = keyfile
64         self.local_gather_data_sh = shell
65         print "EteGatherDataListener instantiated"
66
67     def end_test(self, name, attrs):
68         if attrs['status'] == 'PASS':
69             self.tests_passed+=1
70         else:
71             self.tests_failed+=1
72
73     def output_file(self, path):
74         if (self.folder != ''):
75             return
76         self.folder = os.path.dirname(path)
77         print(self.folder)
78
79     def close(self):
80         print "EteGatherDataListener tests failed=" + str(self.tests_failed)
81         if (self.tests_failed > 0):
82             self.gather_debug_data()
83
84     def gather_debug_data(self):
85
86         for application in self.APPLICATIONS.keys():
87             self.gather_application_data(application, self.APPLICATIONS.get(application))
88
89     def gather_application_data(self, application, ip):
90         extra = {"_threadid" : 1}
91         paramiko.util.log_to_file(self.folder + "/paramiko.log", level=0)
92         log = logging.getLogger("paramiko")
93         ssh = paramiko.SSHClient()
94         try:
95             ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
96             ssh.connect(ip,username="root",  key_filename=self.keyfile)
97         except paramiko.SSHException:
98             log.error("Connection Failed to " + ip, extra=extra)
99             return
100         try:
101             gather_data_sh = "/tmp/gather_data.sh"
102             ftp = ssh.open_sftp()
103             ftp.put(self.local_gather_data_sh, gather_data_sh)
104             ftp.close()
105             stdin, stdout, stderr = ssh.exec_command("/bin/bash "+ gather_data_sh + " " + application + " " + self.job)
106             error = stderr.read()
107             if (error != ''):
108                 log.info("stderr:" + error, extra=extra)
109                 ssh.close()
110                 return;
111             # No error? ASsume we have a file to download.
112             out = stdout.read()
113             log.info("stdout:" + out, extra=extra)
114             filename = application + "_" + self.job + ".tar.gz"
115             localzip = self.folder + "/" + filename
116             remotezip = "/tmp/gather_data/" + filename
117             ftp = ssh.open_sftp()
118             ftp.get(remotezip, localzip)
119             ftp.close()
120             stdin, stdout, stderr = ssh.exec_command("rm -rf " + remotezip);
121             ssh.close()
122         except paramiko.SSHException:
123             ssh.close()
124             return
125
126