Commit 9f1384a1 authored by Luis Araujo's avatar Luis Araujo

Add support to fetch test case attachments

Now the --attachments option of the test command will fetch all
test run and test case attachments files.

These files will be saved in a directory hierarchy of the form
<attachments-dir>/<test-run-dir>/<test-case-dir>, and each of the files
will be saved in the specific category for each of the test runs and
test cases.

Also the output message is much simpler now, and it will only print
a logger info message after all the files have been saved to the
location.
parent 390b3c43
......@@ -70,32 +70,61 @@ class TestCmd(Command):
for pkg in test.packages:
print "P: {} {}".format(pkg['name'], pkg['version'])
# Show test results if -r/--results passed
if self.args.results:
# Save test run attachments if --attachments is true
# and there are attachments available.
if self.args.attachments and test.attachments:
tr_attachments_path = _make_attachments_dir(
self.args.attachments,
"{}-{}".format(test.name, test.uuid))
for attachment in test.attachments:
_save_attachment(attachment, tr_attachments_path)
# Show test results if -r/--results passed and/or save test case
# attachments if --attachments option passed.
if self.args.results or self.args.attachments:
for result in test.results:
ms = ("{} {}".format(result.get('measurement', ''),
result.get('units', ''))).strip()
print "- {}: {} {}".format(result['test_case_id'],
result['result'],
(ms and '/ '+ ms) or '')
# Print results if --results is true
if self.args.results:
ms = ("{} {}".format(result.get('measurement', ''),
result.get('units', ''))).strip()
print "- {}: {} {}".format(result['test_case_id'],
result['result'],
(ms and '/ '+ ms) or '')
# Save test case attachments if --attachments is true
# and there are attachments available.
tc_attachments = \
self.args.attachments and result.get('attachments', [])
if tc_attachments:
# Test case attachments path is the
# test_run_atttachments path + test_case_id.
tc_attachments_path = _make_attachments_dir(
tr_attachments_path,
result['test_case_id'])
for attachment in tc_attachments:
_save_attachment(attachment, tc_attachments_path)
# Fetch and save test attachments inside directory
if self.args.attachments:
# Create target directory if it doesn't exist
if not os.path.exists(self.args.attachments):
os.mkdir(self.args.attachments)
lqa_logger.info("Attachments saved to: {}".format(\
tr_attachments_path))
for attachment in test.attachments:
# Replace any '/' in the attachment name by '_'
name = attachment['pathname'].replace('/', '_')
# Object path is: test-name+test-uuid+attachment-name
filename = "{}-{}_{}".format(test.name, test.uuid, name)
lqa_logger.info("Saving attachment {} into directory {}"
.format(filename, self.args.attachments))
pathname = os.path.join(self.args.attachments, filename)
try:
with open(pathname, 'w') as f:
f.write(base64.b64decode(attachment['content']))
except EnvironmentError as e:
lqa_logger.error(e)
exit(APPLICATION_ERROR)
def _make_attachments_dir(attachments_dir, test_dir):
dir_pathname = os.path.join(attachments_dir, test_dir)
if not os.path.exists(dir_pathname):
try:
os.makedirs(dir_pathname)
except EnvironmentError as e:
lqa_logger.error(e)
exit(APPLICATION_ERROR)
return dir_pathname
def _save_attachment(attachment, directory):
# Replace any '/' in the attachment name by '_'
filename = attachment['pathname'].replace('/', '_')
attachment_pathname = os.path.join(directory, filename)
try:
with open(attachment_pathname, 'w') as f:
f.write(base64.b64decode(attachment['content']))
except EnvironmentError as e:
lqa_logger.error(e)
exit(APPLICATION_ERROR)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment