Merge ~chad.smith/cloud-init:skip-jsonschema-unittest-when-missing-deps into cloud-init:master

Proposed by Chad Smith on 2017-06-02
Status: Merged
Approved by: Scott Moser on 2017-06-02
Approved revision: 9b893f87bb936423b2e0d762fd857a8a7d15cfa5
Merged at revision: a62a94b4edd7c61a268350c84e43b0aa8f68b0c2
Proposed branch: ~chad.smith/cloud-init:skip-jsonschema-unittest-when-missing-deps
Merge into: cloud-init:master
Diff against target: 134 lines (+25/-4)
2 files modified
tests/unittests/test_handler/test_handler_ntp.py (+11/-1)
tests/unittests/test_handler/test_schema.py (+14/-3)
Reviewer Review Type Date Requested Status
Joshua Powers (community) Approve on 2017-06-02
cloud-init commiters 2017-06-02 Pending
Review via email: mp+325024@code.launchpad.net

Description of the Change

Tests: Skip jsonschema related unit tests when dependency is absent.

On some build environments we don't have python-jsonschema installed. Since this dependency is an optional runtime dependency, we can also make it an optional unit test dependency. Add a skip of related unittests when jsonschema is not present.

Also, KeyError messages on CentOs don't have single quotes around the missing 'key-name'. Make our KeyError assertion a bit more flexible with the assertIn call.

LP: #1695318

To post a comment you must log in.
Joshua Powers (powersj) wrote :

LGTM passes tox locally and in my centos6 container.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tests/unittests/test_handler/test_handler_ntp.py b/tests/unittests/test_handler/test_handler_ntp.py
2index 6cafa63..25d7365 100644
3--- a/tests/unittests/test_handler/test_handler_ntp.py
4+++ b/tests/unittests/test_handler/test_handler_ntp.py
5@@ -3,7 +3,7 @@
6 from cloudinit.config import cc_ntp
7 from cloudinit.sources import DataSourceNone
8 from cloudinit import (distros, helpers, cloud, util)
9-from ..helpers import FilesystemMockingTestCase, mock
10+from ..helpers import FilesystemMockingTestCase, mock, skipIf
11
12
13 import os
14@@ -16,6 +16,12 @@ servers {{servers}}
15 pools {{pools}}
16 """
17
18+try:
19+ import jsonschema # NOQA
20+ _missing_jsonschema_dep = False
21+except ImportError:
22+ _missing_jsonschema_dep = True
23+
24
25 class TestNtp(FilesystemMockingTestCase):
26
27@@ -232,6 +238,7 @@ class TestNtp(FilesystemMockingTestCase):
28 "servers []\npools {0}\n".format(default_pools),
29 content)
30
31+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
32 def test_ntp_handler_schema_validation_warns_non_string_item_type(self):
33 """Ntp schema validation warns of non-strings in pools or servers.
34
35@@ -252,6 +259,7 @@ class TestNtp(FilesystemMockingTestCase):
36 content = stream.read()
37 self.assertEqual("servers ['valid', None]\npools [123]\n", content)
38
39+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
40 def test_ntp_handler_schema_validation_warns_of_non_array_type(self):
41 """Ntp schema validation warns of non-array pools or servers types.
42
43@@ -272,6 +280,7 @@ class TestNtp(FilesystemMockingTestCase):
44 content = stream.read()
45 self.assertEqual("servers non-array\npools 123\n", content)
46
47+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
48 def test_ntp_handler_schema_validation_warns_invalid_key_present(self):
49 """Ntp schema validation warns of invalid keys present in ntp config.
50
51@@ -295,6 +304,7 @@ class TestNtp(FilesystemMockingTestCase):
52 "servers []\npools ['0.mycompany.pool.ntp.org']\n",
53 content)
54
55+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
56 def test_ntp_handler_schema_validation_warns_of_duplicates(self):
57 """Ntp schema validation warns of duplicates in servers or pools.
58
59diff --git a/tests/unittests/test_handler/test_schema.py b/tests/unittests/test_handler/test_schema.py
60index 3239e32..fd0e4f5 100644
61--- a/tests/unittests/test_handler/test_schema.py
62+++ b/tests/unittests/test_handler/test_schema.py
63@@ -6,12 +6,18 @@ from cloudinit.config.schema import (
64 main)
65 from cloudinit.util import write_file
66
67-from ..helpers import CiTestCase, mock
68+from ..helpers import CiTestCase, mock, skipIf
69
70 from copy import copy
71 from six import StringIO
72 from textwrap import dedent
73
74+try:
75+ import jsonschema # NOQA
76+ _missing_jsonschema_dep = False
77+except ImportError:
78+ _missing_jsonschema_dep = True
79+
80
81 class SchemaValidationErrorTest(CiTestCase):
82 """Test validate_cloudconfig_schema"""
83@@ -35,6 +41,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
84
85 with_logs = True
86
87+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
88 def test_validateconfig_schema_non_strict_emits_warnings(self):
89 """When strict is False validate_cloudconfig_schema emits warnings."""
90 schema = {'properties': {'p1': {'type': 'string'}}}
91@@ -43,6 +50,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
92 "Invalid config:\np1: -1 is not of type 'string'\n",
93 self.logs.getvalue())
94
95+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
96 def test_validateconfig_schema_emits_warning_on_missing_jsonschema(self):
97 """Warning from validate_cloudconfig_schema when missing jsonschema."""
98 schema = {'properties': {'p1': {'type': 'string'}}}
99@@ -52,6 +60,7 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
100 'Ignoring schema validation. python-jsonschema is not present',
101 self.logs.getvalue())
102
103+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
104 def test_validateconfig_schema_strict_raises_errors(self):
105 """When strict is True validate_cloudconfig_schema raises errors."""
106 schema = {'properties': {'p1': {'type': 'string'}}}
107@@ -61,8 +70,9 @@ class ValidateCloudConfigSchemaTest(CiTestCase):
108 "Cloud config schema errors: p1: -1 is not of type 'string'",
109 str(context_mgr.exception))
110
111+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
112 def test_validateconfig_schema_honors_formats(self):
113- """When strict is True validate_cloudconfig_schema raises errors."""
114+ """With strict True, validate_cloudconfig_schema errors on format."""
115 schema = {
116 'properties': {'p1': {'type': 'string', 'format': 'hostname'}}}
117 with self.assertRaises(SchemaValidationError) as context_mgr:
118@@ -111,6 +121,7 @@ class ValidateCloudConfigFileTest(CiTestCase):
119 self.config_file),
120 str(context_mgr.exception))
121
122+ @skipIf(_missing_jsonschema_dep, "No python-jsonschema dependency")
123 def test_validateconfig_file_sctricty_validates_schema(self):
124 """validate_cloudconfig_file raises errors on invalid schema."""
125 schema = {
126@@ -183,7 +194,7 @@ class GetSchemaDocTest(CiTestCase):
127 invalid_schema.pop(key)
128 with self.assertRaises(KeyError) as context_mgr:
129 get_schema_doc(invalid_schema)
130- self.assertEqual("'{0}'".format(key), str(context_mgr.exception))
131+ self.assertIn(key, str(context_mgr.exception))
132
133
134 class MainTest(CiTestCase):

Subscribers

People subscribed via source and target branches