Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_test(self):
stream = self.get_subunit_stream()
case = subunit.ByteStreamToStreamResult(stream)
return case
def get_metadata(self):
if self._run_id:
session = self.session_factory()
metadata = db_api.get_run_metadata(self._run_id, session=session)
for meta in metadata:
if meta.key == 'stestr_run_meta':
return meta.value
return None
class _SqlInserter(repository.AbstractTestRun):
"""Insert test results into a sql repository."""
def __init__(self, repository, partial=False, run_id=None, metadata=None):
self._repository = repository
self.partial = partial
self._subunit = None
self._run_id = run_id
self._metadata = metadata
# Create a new session factory
self.engine = sqlalchemy.create_engine(self._repository.base)
self.session_factory = orm.sessionmaker(bind=self.engine,
autocommit=True)
def startTestRun(self):
self._subunit = io.BytesIO()
self.subunit_stream = subunit.v2.StreamResultToBytes(self._subunit)
def open(self, url):
repo = Repository(url)
# To test the repository's existence call get_ids_for_all_tests()
# if it raises an OperationalError that means the DB doesn't exist or
# it couldn't connect, either way the repository was not found.
try:
session = repo.session_factory()
db_api.get_ids_for_all_tests(session=session)
session.close()
except sqlalchemy.exc.OperationalError:
raise repository.RepositoryNotFound(url)
return repo
class Repository(repository.AbstractRepository):
"""subunit2sql based storage of test results.
This repository stores each stream in a subunit2sql DB. Refer to the
subunit2sql documentation for
"""
def __init__(self, url):
"""Create a subunit2sql-based repository object for the repo at 'url'.
:param base: The path to the repository.
"""
self.base = url
self.engine = sqlalchemy.create_engine(url)
self.session_factory = orm.sessionmaker(bind=self.engine)
# TODO(mtreinish): We need to add a subunit2sql api to get the count
def __init__(self):
self.repos = {}
def initialise(self, url):
self.repos[url] = Repository()
return self.repos[url]
def open(self, url):
try:
return self.repos[url]
except KeyError:
raise repository.RepositoryNotFound(url)
class Repository(repository.AbstractRepository):
"""In memory storage of test results."""
def __init__(self):
# Test runs:
self._runs = []
self._failing = OrderedDict() # id -> test
self._times = {} # id -> duration
def count(self):
return len(self._runs)
def get_failing(self):
return _Failures(self)
def get_test_run(self, run_id):
if run_id < 0:
from subunit import TestProtocolClient
import subunit.v2
import testtools
from testtools.compat import _b
from stestr.repository import abstract as repository
from stestr import utils
def atomicish_rename(source, target):
if os.name != "posix" and os.path.exists(target):
os.remove(target)
os.rename(source, target)
class RepositoryFactory(repository.AbstractRepositoryFactory):
def initialise(klass, url):
"""Create a repository at url/path."""
base = os.path.join(os.path.expanduser(url), '.stestr')
try:
os.mkdir(base)
except OSError as e:
if e.errno == errno.EEXIST and not os.listdir(base):
# It shouldn't be harmful initializing an empty dir
pass
else:
raise
with open(os.path.join(base, 'format'), 'wt') as stream:
stream.write('1\n')
result = Repository(base)
result._write_next_stream(0)
stream.write('%d\n' % value)
atomicish_rename(prefix + '.new', prefix)
def find_metadata(self, metadata):
run_ids = []
db = my_dbm.open(self._path('meta.dbm'), 'c')
try:
for run_id in db:
if db.get(run_id) == metadata:
run_ids.append(run_id)
finally:
db.close()
return run_ids
class _DiskRun(repository.AbstractTestRun):
"""A test run that was inserted into the repository."""
def __init__(self, run_id, subunit_content, metadata=None):
"""Create a _DiskRun with the content subunit_content."""
self._run_id = run_id
self._content = subunit_content
assert type(subunit_content) is bytes
self._metadata = metadata
def get_id(self):
return self._run_id
def get_subunit_stream(self):
# Transcode - we want V2.
v1_stream = BytesIO(self._content)
v1_case = subunit.ProtocolTestCase(v1_stream)