Merge "Add validation around stream"

This commit is contained in:
Zuul 2020-12-17 15:04:13 +00:00 committed by Gerrit Code Review
commit b778bef6c5
2 changed files with 62 additions and 27 deletions

View File

@ -93,9 +93,12 @@ class NoRepoTitle(Exception):
def _get_distro():
"""Get distro info from os-release
returns: distro_id, distro_major_version_id, distro_name
"""
output = subprocess.Popen(
'source /etc/os-release && echo -e -n "$ID\n$VERSION_ID"',
'source /etc/os-release && echo -e -n "$ID\n$VERSION_ID\n$NAME"',
shell=True,
stdout=subprocess.PIPE,
stderr=open(os.devnull, 'w'),
@ -103,7 +106,7 @@ def _get_distro():
universal_newlines=True).communicate()
# distro_id and distro_version_id will always be at least an empty string
distro_id, distro_version_id = output[0].split('\n')
distro_id, distro_version_id, distro_name = output[0].split('\n')
# if distro_version_id is empty string the major version will be empty
# string too
@ -117,12 +120,10 @@ def _get_distro():
distro_id = 'centos'
distro_major_version_id = '7'
return distro_id, distro_major_version_id
return distro_id, distro_major_version_id, distro_name
def _parse_args():
distro_id, distro_major_version_id = _get_distro()
def _parse_args(distro_id, distro_major_version_id):
distro = "{}{}".format(distro_id, distro_major_version_id)
@ -263,10 +264,26 @@ def _validate_tripleo_ci_testing(repos):
return True
def _validate_args(args):
def _validate_distro_stream(args, distro_name):
"""Validate stream related args vs host
Fails if stream is to be used but the host isn't a stream OS or vice versa
"""
if args.stream and 'stream' not in distro_name.lower():
raise InvalidArguments('--stream provided, but OS is not the Stream '
'version. Please ensure the host is Stream.')
elif args.no_stream and 'stream' in distro_name.lower():
raise InvalidArguments('--no-stream provided, but OS is the Stream '
'version. Please ensure the host is not the '
'Stream version.')
return True
def _validate_args(args, distro_name):
_validate_current_tripleo(args.repos)
_validate_distro_repos(args)
_validate_tripleo_ci_testing(args.repos)
_validate_distro_stream(args, distro_name)
def _remove_existing(args):
@ -436,8 +453,9 @@ def _run_pkg_clean(distro):
def main():
args = _parse_args()
_validate_args(args)
distro_id, distro_major_version_id, distro_name = _get_distro()
args = _parse_args(distro_id, distro_major_version_id)
_validate_args(args, distro_name)
base_path = _get_base_path(args)
if args.distro in ['centos7']:
_install_priorities()

View File

@ -24,6 +24,7 @@ from tripleo_repos import main
@ddt.ddt
class TestTripleORepos(testtools.TestCase):
@mock.patch('tripleo_repos.main._get_distro')
@mock.patch('sys.argv', ['tripleo-repos', 'current', '-d', 'centos7'])
@mock.patch('tripleo_repos.main._run_pkg_clean')
@mock.patch('tripleo_repos.main._validate_args')
@ -32,18 +33,20 @@ class TestTripleORepos(testtools.TestCase):
@mock.patch('tripleo_repos.main._remove_existing')
@mock.patch('tripleo_repos.main._install_repos')
def test_main(self, mock_install, mock_remove, mock_ip, mock_gbp,
mock_validate, mock_clean):
args = main._parse_args()
mock_validate, mock_clean, mock_distro):
mock_distro.return_value = ('centos', '8', 'CentOS 8')
args = main._parse_args('centos', '8')
mock_path = mock.Mock()
mock_gbp.return_value = mock_path
main.main()
mock_validate.assert_called_once_with(args)
mock_validate.assert_called_once_with(args, 'CentOS 8')
mock_gbp.assert_called_once_with(args)
mock_ip.assert_called_once_with()
mock_remove.assert_called_once_with(args)
mock_install.assert_called_once_with(args, mock_path)
mock_clean.assert_called_once_with('centos7')
@mock.patch('tripleo_repos.main._get_distro')
@mock.patch('sys.argv', ['tripleo-repos', 'current', '-d', 'fedora'])
@mock.patch('tripleo_repos.main._run_pkg_clean')
@mock.patch('tripleo_repos.main._validate_args')
@ -52,12 +55,13 @@ class TestTripleORepos(testtools.TestCase):
@mock.patch('tripleo_repos.main._remove_existing')
@mock.patch('tripleo_repos.main._install_repos')
def test_main_fedora(self, mock_install, mock_remove, mock_ip, mock_gbp,
mock_validate, mock_clean):
args = main._parse_args()
mock_validate, mock_clean, mock_distro):
mock_distro.return_value = ('centos', '8', 'CentOS 8')
args = main._parse_args('centos', '8')
mock_path = mock.Mock()
mock_gbp.return_value = mock_path
main.main()
mock_validate.assert_called_once_with(args)
mock_validate.assert_called_once_with(args, 'CentOS 8')
mock_gbp.assert_called_once_with(args)
assert not mock_ip.called, '_install_priorities should no tbe called'
mock_remove.assert_called_once_with(args)
@ -487,7 +491,7 @@ enabled=1
with mock.patch.object(sys, 'argv', ['', 'current', 'deps', '-d',
'centos7', '-b', 'liberty',
'-o', 'test']):
args = main._parse_args()
args = main._parse_args('centos', '8')
self.assertEqual(['current', 'deps'], args.repos)
self.assertEqual('centos7', args.distro)
self.assertEqual('liberty', args.branch)
@ -498,7 +502,7 @@ enabled=1
'centos7', '--branch',
'mitaka', '--output-path',
'test']):
args = main._parse_args()
args = main._parse_args('centos', '8')
self.assertEqual(['current'], args.repos)
self.assertEqual('centos7', args.distro)
self.assertEqual('mitaka', args.branch)
@ -630,51 +634,64 @@ class TestValidate(testtools.TestCase):
self.args.repos = ['current']
self.args.branch = 'master'
self.args.distro = 'centos7'
self.args.stream = False
self.args.no_stream = False
def test_good(self):
main._validate_args(self.args)
main._validate_args(self.args, '')
def test_current_and_tripleo_dev(self):
self.args.repos = ['current', 'current-tripleo-dev']
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args)
self.args, '')
def test_tripleo_ci_testing_and_current_tripleo(self):
self.args.repos = ['current-tripleo', 'tripleo-ci-testing']
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args)
self.args, '')
def test_tripleo_ci_testing_and_ceph_opstools_allowed(self):
self.args.repos = ['ceph', 'opstools', 'tripleo-ci-testing']
main._validate_args(self.args)
main._validate_args(self.args, '')
def test_tripleo_ci_testing_and_deps_allowed(self):
self.args.repos = ['deps', 'tripleo-ci-testing']
main._validate_args(self.args)
main._validate_args(self.args, '')
def test_ceph_and_tripleo_dev(self):
self.args.repos = ['current-tripleo-dev', 'ceph']
self.args.output_path = main.DEFAULT_OUTPUT_PATH
main._validate_args(self.args)
main._validate_args(self.args, '')
def test_deps_and_tripleo_dev(self):
self.args.repos = ['deps', 'current-tripleo-dev']
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args)
self.args, '')
def test_current_and_tripleo(self):
self.args.repos = ['current', 'current-tripleo']
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args)
self.args, '')
def test_deps_and_tripleo_allowed(self):
self.args.repos = ['deps', 'current-tripleo']
main._validate_args(self.args)
main._validate_args(self.args, '')
def test_invalid_distro(self):
self.args.distro = 'Jigawatts 1.21'
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args)
self.args, '')
def test_invalid_stream(self):
self.args.stream = True
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args, 'CentOS 8')
def test_invalid_no_stream(self):
self.args.stream = False
self.args.no_stream = True
self.assertRaises(main.InvalidArguments, main._validate_args,
self.args, 'CentOS 8 Stream')
def test_validate_distro_repos(self):
self.assertTrue(main._validate_distro_repos(self.args))