aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSurya Prakash Singh <surya.singh@nectechnologies.in>2017-01-19 13:07:05 +0530
committerSurya Prakash Singh <surya.singh@nectechnologies.in>2017-01-25 11:14:35 +0530
commita563bcce760a2e8947fef18c9696d10041ddafb0 (patch)
tree1c091207a42cebe327caff7c7ce41b5c552dee40
parenta29a5ad919d79ecbb6b180723ce5afe675c5780e (diff)
downloadkolla-a563bcce760a2e8947fef18c9696d10041ddafb0.tar.gz
kolla-a563bcce760a2e8947fef18c9696d10041ddafb0.zip
Code coverage improvement for kolla
This patch added the unit tests which improved the coverage upto 82% for kolla deliverable with respect to previous coverage report. Change-Id: I0ecb067b5b42ebda8d0a4d77811c842fc7647d1b Implemented: blueprint coverage-increment-for-kolla
-rw-r--r--kolla/tests/test_build.py92
-rw-r--r--kolla/tests/test_methods.py23
2 files changed, 112 insertions, 3 deletions
diff --git a/kolla/tests/test_build.py b/kolla/tests/test_build.py
index 95b5fd2f1..bf2e2bd37 100644
--- a/kolla/tests/test_build.py
+++ b/kolla/tests/test_build.py
@@ -30,6 +30,18 @@ FAKE_IMAGE_CHILD = build.Image(
'image-child', 'image-child:latest',
'/fake/path2', parent_name='image-base',
parent=FAKE_IMAGE, status=build.STATUS_MATCHED)
+FAKE_IMAGE_CHILD_UNMATCHED = build.Image(
+ 'image-child1', 'image-child:latest',
+ '/fake/path3', parent_name='image-base',
+ parent=FAKE_IMAGE, status=build.STATUS_UNMATCHED)
+FAKE_IMAGE_CHILD_ERROR = build.Image(
+ 'image-child2', 'image-child:latest',
+ '/fake/path4', parent_name='image-base',
+ parent=FAKE_IMAGE, status=build.STATUS_ERROR)
+FAKE_IMAGE_CHILD_BUILT = build.Image(
+ 'image-child3', 'image-child:latest',
+ '/fake/path5', parent_name='image-base',
+ parent=FAKE_IMAGE, status=build.STATUS_BUILT)
class TasksTest(base.TestCase):
@@ -39,6 +51,8 @@ class TasksTest(base.TestCase):
self.image = FAKE_IMAGE.copy()
# NOTE(jeffrey4l): use a real, temporary dir
self.image.path = self.useFixture(fixtures.TempDir()).path
+ self.imageChild = FAKE_IMAGE_CHILD.copy()
+ self.imageChild.path = self.useFixture(fixtures.TempDir()).path
@mock.patch.dict(os.environ, clear=True)
@mock.patch('docker.Client')
@@ -139,6 +153,51 @@ class TasksTest(base.TestCase):
self.assertFalse(builder.success)
+ @mock.patch('docker.Client')
+ @mock.patch('requests.get')
+ @mock.patch('shutil.rmtree')
+ @mock.patch('shutil.copyfile')
+ @mock.patch('os.utime')
+ def test_process_source(self, mock_get, mock_client,
+ mock_rmtree, mock_copyfile, mock_utime):
+ for source in [{'source': 'http://fake/source1', 'type': 'url',
+ 'name': 'fake-image-base1',
+ 'reference': 'http://fake/reference1'},
+ {'source': 'http://fake/source2', 'type': 'git',
+ 'name': 'fake-image-base2',
+ 'reference': 'http://fake/reference2'},
+ {'source': 'http://fake/source3', 'type': 'local',
+ 'name': 'fake-image-base3',
+ 'reference': 'http://fake/reference3'},
+ {'source': 'http://fake/source4', 'type': None,
+ 'name': 'fake-image-base4',
+ 'reference': 'http://fake/reference4'}]:
+ self.image.source = source
+ push_queue = mock.Mock()
+ builder = build.BuildTask(self.conf, self.image, push_queue)
+ get_result = builder.process_source(self.image, self.image.source)
+ self.assertEqual(self.image.status, build.STATUS_ERROR)
+ self.assertFalse(builder.success)
+ if source['type'] != 'local':
+ self.assertIsNone(get_result)
+ else:
+ self.assertIsNotNone(get_result)
+
+ @mock.patch('docker.Client')
+ def test_followups_docker_image(self, mock_client):
+ self.imageChild.source = {
+ 'source': 'http://fake/source',
+ 'type': 'url',
+ 'name': 'fake-image-base'
+ }
+ self.imageChild.children.append(FAKE_IMAGE_CHILD_UNMATCHED)
+ push_queue = mock.Mock()
+ builder = build.BuildTask(self.conf, self.imageChild, push_queue)
+ builder.success = True
+ self.conf.push = FAKE_IMAGE_CHILD_UNMATCHED
+ get_result = builder.followups
+ self.assertEqual(1, len(get_result))
+
class KollaWorkerTest(base.TestCase):
@@ -150,7 +209,11 @@ class KollaWorkerTest(base.TestCase):
image.status = None
image_child = FAKE_IMAGE_CHILD.copy()
image_child.status = None
- self.images = [image, image_child]
+ image_unmatched = FAKE_IMAGE_CHILD_UNMATCHED.copy()
+ image_error = FAKE_IMAGE_CHILD_ERROR.copy()
+ image_built = FAKE_IMAGE_CHILD_BUILT.copy()
+ self.images = [image, image_child, image_unmatched,
+ image_error, image_built]
def test_supported_base_type(self):
rh_base = ['centos', 'oraclelinux', 'rhel']
@@ -219,6 +282,12 @@ class KollaWorkerTest(base.TestCase):
else:
self.fail('Expected to find the base image in this test')
+ def test_set_time(self):
+ self.conf.set_override('install_type', 'source')
+ kolla = build.KollaWorker(self.conf)
+ kolla.setup_working_dir()
+ kolla.set_time()
+
def _get_matched_images(self, images):
return [image for image in images
if image.status == build.STATUS_MATCHED]
@@ -228,7 +297,7 @@ class KollaWorkerTest(base.TestCase):
kolla.images = self.images
kolla.filter_images()
- self.assertEqual(2, len(self._get_matched_images(kolla.images)))
+ self.assertEqual(5, len(self._get_matched_images(kolla.images)))
def test_build_rpm_setup(self):
"""checking the length of list of docker commands"""
@@ -270,29 +339,46 @@ class KollaWorkerTest(base.TestCase):
kolla.list_dependencies()
pprint_mock.assert_called_once_with(mock.ANY)
+ def test_summary(self):
+ kolla = build.KollaWorker(self.conf)
+ kolla.images = self.images
+ kolla.image_statuses_good['good'] = None
+ kolla.image_statuses_bad['bad'] = None
+ kolla.image_statuses_unmatched['unmatched'] = None
+ results = kolla.summary()
+ self.assertEqual(None, results['failed'][0]['status'])
+
-@mock.patch.object(build, 'run_build')
class MainTest(base.TestCase):
+ @mock.patch.object(build, 'run_build')
def test_images_built(self, mock_run_build):
image_statuses = ({}, {'img': 'built'}, {})
mock_run_build.return_value = image_statuses
result = build_cmd.main()
self.assertEqual(0, result)
+ @mock.patch.object(build, 'run_build')
def test_images_unmatched(self, mock_run_build):
image_statuses = ({}, {}, {'img': 'unmatched'})
mock_run_build.return_value = image_statuses
result = build_cmd.main()
self.assertEqual(0, result)
+ @mock.patch.object(build, 'run_build')
def test_no_images_built(self, mock_run_build):
mock_run_build.return_value = None
result = build_cmd.main()
self.assertEqual(0, result)
+ @mock.patch.object(build, 'run_build')
def test_bad_images(self, mock_run_build):
image_statuses = ({'img': 'error'}, {}, {})
mock_run_build.return_value = image_statuses
result = build_cmd.main()
self.assertEqual(1, result)
+
+ @mock.patch('sys.argv')
+ def test_run_build(self, mock_sys):
+ result = build.run_build()
+ self.assertTrue(result)
diff --git a/kolla/tests/test_methods.py b/kolla/tests/test_methods.py
new file mode 100644
index 000000000..dce013d67
--- /dev/null
+++ b/kolla/tests/test_methods.py
@@ -0,0 +1,23 @@
+# 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 kolla.template import methods
+from kolla.tests import base
+
+
+class MethodsTest(base.TestCase):
+
+ def test_debian_package_install(self):
+ packages = ['https://packages.debian.org/package1.deb', 'package2.deb']
+ result = methods.debian_package_install(packages)
+ expectCmd = 'apt-get -y install --no-install-recommends package2.deb '
+ self.assertEqual(expectCmd, result.split("&&")[0])