fix the custom profile raise exception issue

Closes-Bug: #1536272
Change-Id: I6c91e522923eade16ba23711e6014e6b4b8cd3e0
This commit is contained in:
Jeffrey Zhang 2016-01-21 16:23:10 +08:00 committed by Jeffrey Zhang
parent 477fc18bd0
commit da0f0cf1e6
3 changed files with 65 additions and 10 deletions

View File

@ -493,7 +493,15 @@ class KollaWorker(object):
if self.conf.profile: if self.conf.profile:
for profile in self.conf.profile: for profile in self.conf.profile:
filter_ += self.conf.profiles[profile] if profile not in self.conf.profiles:
self.conf.register_opt(cfg.ListOpt(profile,
default=[]),
'profiles')
if len(self.conf.profiles[profile]) == 0:
msg = 'Profile: {} does not exist'.format(profile)
raise ValueError(msg)
else:
filter_ += self.conf.profiles[profile]
if filter_: if filter_:
patterns = re.compile(r"|".join(filter_).join('()')) patterns = re.compile(r"|".join(filter_).join('()'))

View File

@ -5,3 +5,6 @@ debug=True
reference = master reference = master
location = https://github.com/openstack/networking-arista location = https://github.com/openstack/networking-arista
type = git type = git
[profiles]
default = image-base

View File

@ -23,6 +23,7 @@ FAKE_IMAGE = {
'name': 'image-base', 'name': 'image-base',
'status': 'matched', 'status': 'matched',
'parent': None, 'parent': None,
'parent_name': None,
'path': '/fake/path', 'path': '/fake/path',
'plugins': [], 'plugins': [],
'fullname': 'image-base:latest', 'fullname': 'image-base:latest',
@ -33,8 +34,9 @@ class WorkerThreadTest(base.TestCase):
def setUp(self): def setUp(self):
super(WorkerThreadTest, self).setUp() super(WorkerThreadTest, self).setUp()
self.image = FAKE_IMAGE.copy()
# NOTE(jeffrey4l): use a real, temporary dir # NOTE(jeffrey4l): use a real, temporary dir
FAKE_IMAGE['path'] = self.useFixture(fixtures.TempDir()).path self.image['path'] = self.useFixture(fixtures.TempDir()).path
@mock.patch.dict(os.environ, clear=True) @mock.patch.dict(os.environ, clear=True)
@mock.patch('docker.Client') @mock.patch('docker.Client')
@ -44,10 +46,10 @@ class WorkerThreadTest(base.TestCase):
worker = build.WorkerThread(queue, worker = build.WorkerThread(queue,
push_queue, push_queue,
self.conf) self.conf)
worker.builder(FAKE_IMAGE) worker.builder(self.image)
mock_client().build.assert_called_once_with( mock_client().build.assert_called_once_with(
path=FAKE_IMAGE['path'], tag=FAKE_IMAGE['fullname'], path=self.image['path'], tag=self.image['fullname'],
nocache=False, rm=True, pull=True, forcerm=True, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=None) buildargs=None)
@ -62,10 +64,10 @@ class WorkerThreadTest(base.TestCase):
worker = build.WorkerThread(mock.Mock(), worker = build.WorkerThread(mock.Mock(),
mock.Mock(), mock.Mock(),
self.conf) self.conf)
worker.builder(FAKE_IMAGE) worker.builder(self.image)
mock_client().build.assert_called_once_with( mock_client().build.assert_called_once_with(
path=FAKE_IMAGE['path'], tag=FAKE_IMAGE['fullname'], path=self.image['path'], tag=self.image['fullname'],
nocache=False, rm=True, pull=True, forcerm=True, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args) buildargs=build_args)
@ -79,10 +81,10 @@ class WorkerThreadTest(base.TestCase):
worker = build.WorkerThread(mock.Mock(), worker = build.WorkerThread(mock.Mock(),
mock.Mock(), mock.Mock(),
self.conf) self.conf)
worker.builder(FAKE_IMAGE) worker.builder(self.image)
mock_client().build.assert_called_once_with( mock_client().build.assert_called_once_with(
path=FAKE_IMAGE['path'], tag=FAKE_IMAGE['fullname'], path=self.image['path'], tag=self.image['fullname'],
nocache=False, rm=True, pull=True, forcerm=True, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args) buildargs=build_args)
@ -97,10 +99,10 @@ class WorkerThreadTest(base.TestCase):
worker = build.WorkerThread(mock.Mock(), worker = build.WorkerThread(mock.Mock(),
mock.Mock(), mock.Mock(),
self.conf) self.conf)
worker.builder(FAKE_IMAGE) worker.builder(self.image)
mock_client().build.assert_called_once_with( mock_client().build.assert_called_once_with(
path=FAKE_IMAGE['path'], tag=FAKE_IMAGE['fullname'], path=self.image['path'], tag=self.image['fullname'],
nocache=False, rm=True, pull=True, forcerm=True, nocache=False, rm=True, pull=True, forcerm=True,
buildargs=build_args) buildargs=build_args)
@ -109,6 +111,12 @@ class KollaWorkerTest(base.TestCase):
config_file = 'default.conf' config_file = 'default.conf'
def setUp(self):
super(KollaWorkerTest, self).setUp()
image = FAKE_IMAGE.copy()
image['status'] = None
self.images = [image]
def test_supported_base_type(self): def test_supported_base_type(self):
rh_base = ['fedora', 'centos', 'oraclelinux', 'rhel'] rh_base = ['fedora', 'centos', 'oraclelinux', 'rhel']
rh_type = ['source', 'binary', 'rdo', 'rhos'] rh_type = ['source', 'binary', 'rdo', 'rhos']
@ -152,3 +160,39 @@ class KollaWorkerTest(base.TestCase):
break break
else: else:
self.fail('Can not find the expected neutron arista plugin') self.fail('Can not find the expected neutron arista plugin')
def _get_matched_images(self, images):
return [image for image in images if image['status'] == 'matched']
def test_without_profile(self):
kolla = build.KollaWorker(self.conf)
kolla.images = self.images
kolla.filter_images()
self.assertEqual(1, len(self._get_matched_images(kolla.images)))
def test_pre_defined_exist_profile(self):
# default profile include the fake image: image-base
self.conf.set_override('profile', ['default'])
kolla = build.KollaWorker(self.conf)
kolla.images = self.images
kolla.filter_images()
self.assertEqual(1, len(self._get_matched_images(kolla.images)))
def test_pre_defined_exist_profile_not_include(self):
# infra profile do not include the fake image: image-base
self.conf.set_override('profile', ['infra'])
kolla = build.KollaWorker(self.conf)
kolla.images = self.images
kolla.filter_images()
self.assertEqual(0, len(self._get_matched_images(kolla.images)))
def test_pre_defined_not_exist_profile(self):
# NOTE(jeffrey4l): not exist profile will raise ValueError
self.conf.set_override('profile', ['not_exist'])
kolla = build.KollaWorker(self.conf)
kolla.images = self.images
self.assertRaises(ValueError,
kolla.filter_images)