From d3e041b785f9e4a2bd2bd2714d938c7afb8ab3c5 Mon Sep 17 00:00:00 2001 From: Ronald Bradford Date: Mon, 25 May 2015 17:19:28 -0400 Subject: [PATCH] Improving Unit Test coverage of k8s_manifest In addition these tests are used to provide a documented example to demonstrate writing unit tests for new contributors for increasing project code coverage. Change-Id: I8448bf9ea050513dfb271cdce7f61d37f89fd186 Closes-Bug: #1458879 Partially-Implements: blueprint integrate-ci-metrics --- magnum/common/k8s_manifest.py | 3 +- magnum/tests/unit/common/test_k8s_manifest.py | 35 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/magnum/common/k8s_manifest.py b/magnum/common/k8s_manifest.py index 5db39389d0..62ee372dcc 100644 --- a/magnum/common/k8s_manifest.py +++ b/magnum/common/k8s_manifest.py @@ -50,7 +50,8 @@ def parse(manifest_str): raise ValueError(msg) else: if manifest is None: - manifest = {} + msg = _("'manifest' can't be empty") + raise ValueError(msg) if not isinstance(manifest, dict): raise ValueError(_('The manifest is not a JSON object ' diff --git a/magnum/tests/unit/common/test_k8s_manifest.py b/magnum/tests/unit/common/test_k8s_manifest.py index 77b009e3d3..dba5239c18 100644 --- a/magnum/tests/unit/common/test_k8s_manifest.py +++ b/magnum/tests/unit/common/test_k8s_manifest.py @@ -19,13 +19,15 @@ from magnum.tests import base class K8sManifestTestCase(base.TestCase): def test_parse_with_json(self): + port = 6379 + containerPort = 6380 json_str = ''' { "id": "redis-master", "kind": "Service", "apiVersion": "v1beta1", - "port": 6379, - "containerPort": 6379, + "port": %d, + "containerPort": %d, "selector": { "name": "redis-master" }, @@ -33,27 +35,48 @@ class K8sManifestTestCase(base.TestCase): "name": "redis-master" } } - ''' + ''' % (port, containerPort) manifest = k8s_manifest.parse(json_str) self.assertIsInstance(manifest, dict) + self.assertEqual(port, manifest['port']) + self.assertEqual(containerPort, manifest['containerPort']) def test_parse_with_yaml(self): + port = 6389 + containerPort = 6380 yaml_str = ''' id: redis-master kind: Service - port: 6379 - containerPort: 6379 + port: %d + containerPort: %d selector: name: redis-master labels: name: redis-master - ''' + ''' % (port, containerPort) manifest = k8s_manifest.parse(yaml_str) self.assertIsInstance(manifest, dict) + self.assertEqual(port, manifest['port']) + self.assertEqual(containerPort, manifest['containerPort']) def test_parse_invalid_value(self): invalid_str = 'aoa89**' self.assertRaises(ValueError, k8s_manifest.parse, invalid_str) + + def test_parse_empty_value(self): + empty_str = '' + + self.assertRaises(ValueError, k8s_manifest.parse, empty_str) + + def test_parse_empty_yaml_response(self): + blank_str = ' ' + + self.assertRaises(ValueError, k8s_manifest.parse, blank_str) + + def test_parse_yaml_error(self): + invalid_str = "}invalid: y'm'l3!" + + self.assertRaises(ValueError, k8s_manifest.parse, invalid_str)