Handle test_runs without metadata in write_subunit

This commit fixes an issue when write_subunit encounters a test_run
that has no test_run_metadata associated with it. There is no
guarantee that a test_run has metadata so write_subunit shouldn't
assume it's always there.

Change-Id: I442a2d3722d840d4f53b322419d818da66e39bda
This commit is contained in:
Matthew Treinish 2015-12-09 15:00:21 -05:00
parent 0b8d9fe0b6
commit d76dcbbc96
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
2 changed files with 76 additions and 6 deletions

View File

@ -0,0 +1,66 @@
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# 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 License.
import datetime
import mock
from subunit2sql.tests import base
from subunit2sql import write_subunit
timestamp_a = datetime.datetime.utcnow()
timestamp_b = datetime.datetime.utcnow()
class TestWriteSubunit(base.TestCase):
@mock.patch('subunit2sql.db.api.get_tests_run_dicts_from_run_id',
return_value={'fake_test_id': {
'start_time': timestamp_a,
'stop_time': timestamp_b,
'status': 'success'}})
@mock.patch('subunit2sql.write_subunit.write_test')
@mock.patch('subunit2sql.db.api.get_session')
def test_test_runs_without_metdata(self, api_mock, write_mock,
sesion_mock):
out = mock.MagicMock()
write_subunit.sql2subunit('fake_id', out)
self.assertEqual(write_mock.call_count, 1)
args = write_mock.call_args_list[0][0]
self.assertEqual(timestamp_a, args[1])
self.assertEqual(timestamp_b, args[2])
self.assertEqual('success', args[3])
self.assertEqual('fake_test_id', args[4])
self.assertEqual(None, args[5])
@mock.patch('subunit2sql.db.api.get_tests_run_dicts_from_run_id',
return_value={'fake_test_id': {
'start_time': timestamp_a,
'stop_time': timestamp_b,
'status': 'success',
'metadata': {
'key': 'value'}}})
@mock.patch('subunit2sql.write_subunit.write_test')
@mock.patch('subunit2sql.db.api.get_session')
def test_test_runs_with_metdata(self, api_mock, write_mock, session_mock):
out = mock.MagicMock()
write_subunit.sql2subunit('fake_id', out)
self.assertEqual(write_mock.call_count, 1)
args = write_mock.call_args_list[0][0]
self.assertEqual(timestamp_a, args[1])
self.assertEqual(timestamp_b, args[2])
self.assertEqual('success', args[3])
self.assertEqual('fake_test_id', args[4])
self.assertEqual({'key': 'value'}, args[5])

View File

@ -59,11 +59,12 @@ def convert_datetime(timestamp):
def write_test(output, start_time, stop_time, status, test_id, metadatas):
write_status = output.status
kwargs = {}
if 'tags' in metadatas:
tags = metadatas['tags']
kwargs['test_tags'] = tags.split(',')
if 'attrs' in metadatas:
test_id = test_id + '[' + metadatas['attrs'] + ']'
if metadatas:
if 'tags' in metadatas:
tags = metadatas['tags']
kwargs['test_tags'] = tags.split(',')
if 'attrs' in metadatas:
test_id = test_id + '[' + metadatas['attrs'] + ']'
start_time = convert_datetime(start_time)
kwargs['timestamp'] = start_time
kwargs['test_id'] = test_id
@ -82,8 +83,11 @@ def sql2subunit(run_id, output=sys.stdout):
output.startTestRun()
for test_id in test_runs:
test = test_runs[test_id]
# NOTE(mtreinish): test_run_metadata is not guaranteed to be present
# for the test_run.
metadata = test.get('metadata', None)
write_test(output, test['start_time'], test['stop_time'],
test['status'], test_id, test['metadata'])
test['status'], test_id, metadata)
output.stopTestRun()