From da0f0cf1e672c42edf69f0a7080338c25b61706c Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <zhang.lei.fly@gmail.com>
Date: Thu, 21 Jan 2016 16:23:10 +0800
Subject: [PATCH] fix the custom profile raise exception issue

Closes-Bug: #1536272
Change-Id: I6c91e522923eade16ba23711e6014e6b4b8cd3e0
---
 kolla/cmd/build.py           | 10 +++++-
 kolla/tests/etc/default.conf |  3 ++
 kolla/tests/test_build.py    | 62 ++++++++++++++++++++++++++++++------
 3 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/kolla/cmd/build.py b/kolla/cmd/build.py
index 1202203310..dcbef77186 100755
--- a/kolla/cmd/build.py
+++ b/kolla/cmd/build.py
@@ -493,7 +493,15 @@ class KollaWorker(object):
 
         if 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_:
             patterns = re.compile(r"|".join(filter_).join('()'))
diff --git a/kolla/tests/etc/default.conf b/kolla/tests/etc/default.conf
index 234b5448eb..844ae20d18 100644
--- a/kolla/tests/etc/default.conf
+++ b/kolla/tests/etc/default.conf
@@ -5,3 +5,6 @@ debug=True
 reference = master
 location = https://github.com/openstack/networking-arista
 type = git
+
+[profiles]
+default = image-base
diff --git a/kolla/tests/test_build.py b/kolla/tests/test_build.py
index b014bc3b1f..76020ef2a8 100644
--- a/kolla/tests/test_build.py
+++ b/kolla/tests/test_build.py
@@ -23,6 +23,7 @@ FAKE_IMAGE = {
     'name': 'image-base',
     'status': 'matched',
     'parent': None,
+    'parent_name': None,
     'path': '/fake/path',
     'plugins': [],
     'fullname': 'image-base:latest',
@@ -33,8 +34,9 @@ class WorkerThreadTest(base.TestCase):
 
     def setUp(self):
         super(WorkerThreadTest, self).setUp()
+        self.image = FAKE_IMAGE.copy()
         # 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('docker.Client')
@@ -44,10 +46,10 @@ class WorkerThreadTest(base.TestCase):
         worker = build.WorkerThread(queue,
                                     push_queue,
                                     self.conf)
-        worker.builder(FAKE_IMAGE)
+        worker.builder(self.image)
 
         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,
             buildargs=None)
 
@@ -62,10 +64,10 @@ class WorkerThreadTest(base.TestCase):
         worker = build.WorkerThread(mock.Mock(),
                                     mock.Mock(),
                                     self.conf)
-        worker.builder(FAKE_IMAGE)
+        worker.builder(self.image)
 
         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,
             buildargs=build_args)
 
@@ -79,10 +81,10 @@ class WorkerThreadTest(base.TestCase):
         worker = build.WorkerThread(mock.Mock(),
                                     mock.Mock(),
                                     self.conf)
-        worker.builder(FAKE_IMAGE)
+        worker.builder(self.image)
 
         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,
             buildargs=build_args)
 
@@ -97,10 +99,10 @@ class WorkerThreadTest(base.TestCase):
         worker = build.WorkerThread(mock.Mock(),
                                     mock.Mock(),
                                     self.conf)
-        worker.builder(FAKE_IMAGE)
+        worker.builder(self.image)
 
         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,
             buildargs=build_args)
 
@@ -109,6 +111,12 @@ class KollaWorkerTest(base.TestCase):
 
     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):
         rh_base = ['fedora', 'centos', 'oraclelinux', 'rhel']
         rh_type = ['source', 'binary', 'rdo', 'rhos']
@@ -152,3 +160,39 @@ class KollaWorkerTest(base.TestCase):
                 break
         else:
             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)