2 # Copyright 2019 Huawei Technologies Co., Ltd.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
22 from datetime import datetime
25 from argparse import RawTextHelpFormatter
41 "validation": "enabled"
47 "portal_key_file": "",
54 def __init__(self, execution_id, tar_path, vtp_home):
55 self.execution_id = execution_id
56 self.tar_path = tar_path
57 self.vtp_home = vtp_home + "/data/executions"
59 def find_folder_by_execution_id(self):
62 # TODO: Improve by better seraching.
63 dir_obj = os.listdir(self.vtp_home)
64 for dir_name in dir_obj:
65 if dir_name.__contains__(self.execution_id):
66 ret_dir_name = dir_name
70 raise Exception('Given execution-id does not exist\n')
74 def extract_execution_id_from_name(self, dir_name=None):
76 temp_arr = dir_name.split("__")
78 temp_arr.remove(exec_id)
80 nm_arr.extend(temp_arr)
81 objective = "__".join(nm_arr)
82 return exec_id + "," + objective
84 def extract_request_id_from_execution_id(self, exec_id):
85 request_id = exec_id.split("-")[0]
88 def find_folder_by_request_id(self, request_id):
90 if os.path.exists(self.vtp_home):
91 for dir_name in os.listdir(self.vtp_home):
92 if re.match('^' + request_id + '-', dir_name):
93 if not dir_name.__contains__(self.execution_id):
94 file_list.append(dir_name)
97 def check_pass_fail(self, dir_path):
98 completed_path = dir_path + "/completed"
99 if os.path.isfile(completed_path):
104 def get_testdate(self, dir_path=None):
105 t = os.path.getmtime(dir_path)
106 return str(datetime.utcfromtimestamp(t)) + " UTC"
108 def get_duration(self, dir_path=None):
109 input_file = dir_path + "/input"
110 output_file = dir_path + "/output"
111 t1 = os.path.getmtime(input_file)
112 input_timestamp = datetime.utcfromtimestamp(t1)
113 t2 = os.path.getmtime(output_file)
114 output_timestamp = datetime.utcfromtimestamp(t2)
115 time_diff = output_timestamp - input_timestamp
116 return time_diff.seconds * 1000
118 def generate_ovp_result_json(self):
119 arr_temp = [self.sub_test]
120 self.result_json.update({"testcases_list": arr_temp})
121 with open('./result.json', 'w') as json_file:
122 json.dump(self.result_json, json_file, default=str, indent=4)
125 def generate_result_tar(self):
126 arr_sub_testcase = []
129 dir_name = self.find_folder_by_execution_id()
130 exec_id = self.extract_execution_id_from_name(dir_name)
131 objective = exec_id.split(',')[1]
132 self.sub_test['objective'] = objective
133 output_file_path = "/data/executions/" + dir_name + "/output"
134 self.sub_test.update({'portal_key_file': output_file_path})
135 self.sub_test['name'] = objective
136 self.result_json.update({'duration': str(self.get_duration(self.vtp_home + "/" + dir_name)) + " ms"})
137 self.sub_test['result'] = self.check_pass_fail(self.vtp_home + "/" + dir_name)
138 test_date = self.get_testdate(self.vtp_home + "/" + dir_name)
139 self.result_json.update({'test_date': test_date})
140 self.result_json.update({'build_tag': self.execution_id})
141 request_id = self.extract_request_id_from_execution_id(exec_id)
142 file_list = self.find_folder_by_request_id(request_id)
143 for sub_test in file_list:
145 sub_exec_id = self.extract_execution_id_from_name(sub_test)
146 exec_id_name = sub_exec_id.split(",")
147 sub_test_name = exec_id_name[1]
148 sub_test_case['name'] = sub_test_name
149 full_name = "__".join(exec_id_name)
150 pass_fail = self.check_pass_fail(self.vtp_home + "/" + full_name)
151 sub_test_case['result'] = pass_fail
152 arr_sub_testcase.append(sub_test_case)
153 self.sub_test['sub_testcase'] = arr_sub_testcase
154 self.generate_ovp_result_json()
156 self.display_result_json()
158 except Exception as e:
161 def display_result_json(self):
162 print "\nOVP result JSON: "
163 pretty_printer = pprint.PrettyPrinter(indent=4)
164 pretty_printer.pprint(self.result_json)
166 def make_tar_dir(self):
167 tar_file_name = "vtp-ovp-result-" + self.execution_id + ".tar"
168 head, tail = os.path.split(self.vtp_home)
169 with tarfile.open(tar_file_name, "w:gz") as tar_handle:
170 tar_handle.add('./result.json')
171 tar_handle.add(head, arcname='./data')
173 if not os.path.exists(self.tar_path):
174 os.mkdir(self.tar_path)
175 elif not os.path.isdir(self.tar_path):
176 raise Exception('Given path for OVP output tar file is not a directory\n')
177 shutil.move(tar_file_name, os.path.join(self.tar_path, tar_file_name))
178 if os.path.exists('result.json'):
179 os.remove('result.json')
180 print "\n\nOVP Result Tar file: " + os.path.abspath(self.tar_path + '/' + tar_file_name)
184 text = 'This command helps to produce VTP results in LFV OVP format. ' \
185 'Run this command from the server machine, where VTP is running'
186 parser = argparse.ArgumentParser(description=text, formatter_class=RawTextHelpFormatter)
187 parser.add_argument('--execution-id', action='store', dest='execution_id',
188 help='VTP test case execution id')
189 parser.add_argument('--tar-path', action='store', dest='tar_path',
190 help='Location to OVP output tar file')
191 parser.add_argument('--vtp-home', default=os.getenv('OPEN_CLI_HOME'), action='store', dest='vtp_home',
192 help='VTP installation home directory')
193 args = parser.parse_args()
195 if not args.execution_id or not args.tar_path or not args.vtp_home:
196 sys.stderr.write(str('Too few arguments\n'))
198 if args.execution_id:
199 execution_id = args.execution_id
201 tar_path = args.tar_path
203 vtp_home = args.vtp_home
205 if os.path.exists(vtp_home):
206 if not os.path.isdir(vtp_home):
207 print 'Given VTP path is not a directory\n'
209 vtp2ovp = VTP2OVPResult(execution_id, tar_path, vtp_home)
210 vtp2ovp.generate_result_tar()
212 print 'Given VTP path does not exist\n'
216 usage: vtp2ovp-result.py [-h] [--execution-id EXECUTION_ID]
217 [--tar-path TAR_PATH] [--vtp-home VTP_HOME]
219 This command helps to produce VTP results in LFV OVP format.
220 Run this command from the server machine, where VTP is running
223 -h, --help show this help message and exit
224 --execution-id EXECUTION_ID
225 VTP test case execution id
226 --tar-path TAR_PATH Location to OVP output tar file
227 --vtp-home VTP_HOME VTP installation home directory
230 if __name__ == '__main__':