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): ...@@ -70,32 +70,61 @@ class TestCmd(Command):
for pkg in test.packages: for pkg in test.packages:
print "P: {} {}".format(pkg['name'], pkg['version']) print "P: {} {}".format(pkg['name'], pkg['version'])
# Show test results if -r/--results passed # Save test run attachments if --attachments is true
if self.args.results: # 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: for result in test.results:
ms = ("{} {}".format(result.get('measurement', ''), # Print results if --results is true
result.get('units', ''))).strip() if self.args.results:
print "- {}: {} {}".format(result['test_case_id'], ms = ("{} {}".format(result.get('measurement', ''),
result['result'], result.get('units', ''))).strip()
(ms and '/ '+ ms) or '') 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: if self.args.attachments:
# Create target directory if it doesn't exist lqa_logger.info("Attachments saved to: {}".format(\
if not os.path.exists(self.args.attachments): tr_attachments_path))
os.mkdir(self.args.attachments)
for attachment in test.attachments: def _make_attachments_dir(attachments_dir, test_dir):
# Replace any '/' in the attachment name by '_' dir_pathname = os.path.join(attachments_dir, test_dir)
name = attachment['pathname'].replace('/', '_') if not os.path.exists(dir_pathname):
# Object path is: test-name+test-uuid+attachment-name try:
filename = "{}-{}_{}".format(test.name, test.uuid, name) os.makedirs(dir_pathname)
lqa_logger.info("Saving attachment {} into directory {}" except EnvironmentError as e:
.format(filename, self.args.attachments)) lqa_logger.error(e)
pathname = os.path.join(self.args.attachments, filename) exit(APPLICATION_ERROR)
try: return dir_pathname
with open(pathname, 'w') as f:
f.write(base64.b64decode(attachment['content'])) def _save_attachment(attachment, directory):
except EnvironmentError as e: # Replace any '/' in the attachment name by '_'
lqa_logger.error(e) filename = attachment['pathname'].replace('/', '_')
exit(APPLICATION_ERROR) 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