Start implementing tobiko-fixture command.

Change-Id: I05e26bb118e7201ecb806bedd7c8c6cdcbbac2aa
This commit is contained in:
Federico Ressi 2019-03-04 15:15:44 +01:00
parent 19a8039f11
commit 7407fe8f02
5 changed files with 199 additions and 5 deletions

View File

@ -29,6 +29,7 @@ packages =
console_scripts =
tobiko-create = tobiko.cmd.create:main
tobiko-delete = tobiko.cmd.delete:main
tobiko-fixture = tobiko.cmd.fixture:main
tobiko-list = tobiko.cmd.list:main
tobiko = tobiko.cmd.run:main

100
tobiko/cmd/fixture.py Normal file
View File

@ -0,0 +1,100 @@
# Copyright 2018 Red Hat
#
# 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.
from __future__ import absolute_import
import argparse
import sys
from oslo_log import log
import tobiko
from tobiko.cmd import base
LOG = log.getLogger(__name__)
class FixtureUtil(base.TobikoCMD):
def __init__(self):
super(FixtureUtil, self).__init__()
self.parser = self.get_parser()
self.args = self.parser.parse_args()
def get_parser(self):
parser = argparse.ArgumentParser(add_help=True)
subparsers_params = {}
subparsers = parser.add_subparsers(**subparsers_params)
for subcommand_name in ['cleanup', 'list', 'setup']:
subcommand_parser = subparsers.add_parser(
subcommand_name, help=(subcommand_name + ' fixtures'))
subcommand_parser.set_defaults(subcommand=subcommand_name)
subcommand_parser.add_argument(
'--config', '-c',
default='.stestr.conf',
help=("Set a stestr config file to use with this command. "
"If one isn't specified then .stestr.conf in the "
"directory that a command is running from is used"))
subcommand_parser.add_argument(
'--repo-type', '-r',
choices=['file'],
default='file',
help=("Select the repo backend to use"))
subcommand_parser.add_argument(
'--repo-url', '-u',
default=None,
help=("Set the repo url to use. An acceptable value for "
"this depends on the repository type used."))
return parser
def execute(self):
action = self.args.subcommand or 'list'
if action == 'list':
return self.list_fixtures()
elif action == 'setup':
return self.setup_fixtures()
elif action == 'cleanup':
return self.cleanup_fixtures()
def discovertest_cases(self):
return tobiko.discover_testcases(config=self.args.config)
def list_fixtures(self, stream=sys.stdout):
stream = stream or sys.stdout
test_cases = tobiko.discover_testcases()
fixtures_names = tobiko.list_required_fixtures(test_cases)
stream.write('\n'.join(fixtures_names) + '\n')
def setup_fixtures(self):
test_cases = tobiko.discover_testcases()
for fixture in tobiko.setup_required_fixtures(test_cases):
fixture_name = tobiko.get_fixture_name(fixture)
LOG.debug("Fixture setUp called, %s", fixture_name)
def cleanup_fixtures(self):
test_cases = tobiko.discover_testcases()
for fixture in tobiko.cleanup_required_fixtures(test_cases):
fixture_name = tobiko.get_fixture_name(fixture)
LOG.debug("Fixture cleanUp called, %s", fixture_name)
def main():
"""Create CLI main entry."""
fixture_util = FixtureUtil()
fixture_util.execute()
if __name__ == '__main__':
sys.exit(main())

View File

@ -101,13 +101,13 @@ def list_required_fixtures(objects):
def setup_required_fixtures(objects, manager=None):
for name in iter_required_fixtures(objects=objects):
for name in list_required_fixtures(objects=objects):
yield setup_fixture(name, manager=manager)
def cleanup_required_fixtures(objects, manager=None):
manager = manager or FIXTURES
for name in iter_required_fixtures(objects=objects):
for name in list_required_fixtures(objects=objects):
yield cleanup_fixture(name, manager=manager)

View File

@ -31,7 +31,7 @@ def discover_testcases(manager=None, **kwargs):
class TestCaseManager(object):
def __init__(self, config='.stestr.conf', repo_type='file', repo_url=None,
def __init__(self, config=None, repo_type=None, repo_url=None,
test_path=None, top_dir=None, group_regex=None,
blacklist_file=None, whitelist_file=None, black_regex=None,
filters=None):
@ -62,8 +62,8 @@ class TestCaseManager(object):
(assuming any other filtering specified also uses it)
"""
self.config = config
self.repo_type = repo_type
self.config = config or '.stestr.conf'
self.repo_type = repo_type or 'file'
self.repo_url = repo_url
self.test_path = test_path
self.top_dir = top_dir

View File

@ -0,0 +1,93 @@
# Copyright (c) 2018 Red Hat
# All Rights Reserved.
#
# 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.
from __future__ import absolute_import
import fixtures
from tobiko.cmd import fixture
from tobiko.tests import unit
class ExitCalled(Exception):
pass
class MyFixture1(fixtures.Fixture):
pass
class MyFixture2(fixtures.Fixture):
pass
class FixtureUtilTest(unit.TobikoUnitTest):
command_name = 'tobiko-fixture'
command_class = fixture.FixtureUtil
def setUp(self):
super(FixtureUtilTest, self).setUp()
self.mock_error = self.patch('argparse.ArgumentParser.error',
side_effect=self.fail)
def patch_argv(self, argv=None, subcommand=None):
subcommand = subcommand or 'list'
argv = [self.command_name, subcommand] + list(argv or [])
return self.patch('sys.argv', argv)
def test_init(self, argv=None, subcommand=None,
config_file='.stestr.conf', repo_type='file',
repo_url=None):
self.patch_argv(argv=argv, subcommand=subcommand)
cmd = self.command_class()
self.mock_error.assert_not_called()
self.assertIsNotNone(cmd.args)
self.assertEqual(config_file, cmd.args.config)
self.assertEqual(subcommand or 'list', cmd.args.subcommand)
self.assertEqual(repo_type, cmd.args.repo_type)
self.assertEqual(repo_url, cmd.args.repo_url)
def test_init_with_c(self):
self.test_init(argv=['-c', 'some-config-file'],
config_file='some-config-file')
def test_init_with_config(self):
self.test_init(argv=['--config', 'some-config-file'],
config_file='some-config-file')
def test_init_with_r(self):
self.test_init(argv=['-r', 'file'], repo_type='file')
def test_init_with_repo_type_file(self):
self.test_init(argv=['--repo-type', 'file'], repo_type='file')
def test_init_with_repo_type_sql(self):
self.assertRaises(self.failureException, self.test_init,
argv=['--repo-type', 'sql'])
def test_init_with_u(self):
self.test_init(argv=['-u', 'some-url'], repo_url='some-url')
def test_init_with_repo_url(self):
self.test_init(argv=['--repo-url', 'some-url'], repo_url='some-url')
def test_init_with_list(self):
self.test_init(subcommand='list')
def test_init_with_seutp(self):
self.test_init(subcommand='setup')
def test_init_with_cleanup(self):
self.test_init(subcommand='cleanup')