-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutils.py
60 lines (55 loc) · 2.3 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the Licens
from minio import Minio
from junit_xml import TestSuite, TestCase
import subprocess
# Parse the workflow json to obtain the artifacts for a particular step.
# Note: the step_name could be the key words.
def get_artifact_in_minio(workflow_json, step_name, output_path):
s3_data = {}
minio_access_key = 'minio'
minio_secret_key = 'minio123'
try:
for key in workflow_json['status']['nodes'].keys():
if step_name in workflow_json['status']['nodes'][key]['name']:
s3_data = workflow_json['status']['nodes'][key]['outputs']['artifacts'][0]['s3']
minio_client = Minio(s3_data['endpoint'], access_key=minio_access_key, secret_key=minio_secret_key, secure=False)
data = minio_client.get_object(s3_data['bucket'], s3_data['key'])
with open(output_path, 'wb') as file:
for d in data.stream(32*1024):
file.write(d)
except Exception as e:
print('error in get_artifact_in_minio: %s', e)
print(workflow_json)
# Junit xml utilities
def add_junit_test(test_cases, testname, succ, message='default message', elapsed_sec=0):
test_case = TestCase(testname, elapsed_sec = elapsed_sec)
if not succ:
test_case.add_failure_info(message)
test_cases.append(test_case)
def write_junit_xml(testname, filename, test_cases):
with open(filename, 'w') as f:
ts = TestSuite(testname, test_cases)
TestSuite.to_file(f, [ts], prettyprint=False)
# Bash utilities
def run_bash_command(cmd):
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output_bytes, error_bytes = process.communicate()
output_string = ''
error_string = ''
if output_bytes != None:
output_string = output_bytes.decode('utf-8')
if error_bytes != None:
error_string = error_bytes.decode('utf-8')
return output_string, error_string