Init commit for standalone enodebd
Change-Id: I88eeef5135dd7ba8551ddd9fb6a0695f5325337b
diff --git a/eventd/tests/__init__.py b/eventd/tests/__init__.py
new file mode 100644
index 0000000..5c6cb64
--- /dev/null
+++ b/eventd/tests/__init__.py
@@ -0,0 +1,12 @@
+"""
+Copyright 2020 The Magma Authors.
+
+This source code is licensed under the BSD-style license found in the
+LICENSE file in the root directory of this source tree.
+
+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.
+"""
diff --git a/eventd/tests/event_validation_tests.py b/eventd/tests/event_validation_tests.py
new file mode 100644
index 0000000..54c73e1
--- /dev/null
+++ b/eventd/tests/event_validation_tests.py
@@ -0,0 +1,124 @@
+"""
+Copyright 2020 The Magma Authors.
+
+This source code is licensed under the BSD-style license found in the
+LICENSE file in the root directory of this source tree.
+
+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.
+"""
+
+import json
+from unittest import TestCase
+
+from jsonschema import ValidationError
+from magma.eventd.event_validator import EventValidator
+
+
+class EventValidationTests(TestCase):
+
+ def setUp(self):
+ # A test event registry that specifies the test events
+ test_events_location = {
+ 'module': 'orc8r',
+ 'filename': 'test_event_definitions.yml',
+ }
+ config = {
+ 'fluent_bit_port': '',
+ 'tcp_timeout': '',
+ 'event_registry': {
+ 'simple_event': test_events_location,
+ 'array_and_object_event': test_events_location,
+ 'null_event': test_events_location,
+ },
+ }
+ self.validator = EventValidator(config)
+
+ def test_event_registration(self):
+ data = json.dumps({
+ 'foo': 'magma', # required
+ 'bar': 123,
+ })
+ # Errors when event is not registered
+ with self.assertRaises(Exception):
+ self.validator.validate_event(data, 'non_existent_event')
+
+ # Does not error when event is registered
+ self.validator.validate_event(data, 'simple_event')
+
+ def test_field_consistency(self):
+ # Errors when there are missing fields (required fields)
+ with self.assertRaises(ValidationError):
+ # foo is missing
+ data = json.dumps({
+ 'bar': 123,
+ })
+ self.validator.validate_event(data, 'simple_event')
+
+ # Errors on excess fields (additionalProperties set to false)
+ with self.assertRaises(ValidationError):
+ data = json.dumps({
+ 'extra_field': 12,
+ 'foo': 'asdf',
+ 'bar': 123,
+ })
+ self.validator.validate_event(data, 'simple_event')
+
+ # Errors when there are missing AND excess fields
+ with self.assertRaises(ValidationError):
+ data = json.dumps({
+ 'extra_field': 12,
+ 'bar': 123,
+ })
+ # foo is missing
+ self.validator.validate_event(data, 'simple_event')
+
+ # Does not error when the fields are equivalent
+ data = json.dumps({
+ 'foo': 'magma', # required
+ 'bar': 123,
+ })
+ self.validator.validate_event(data, 'simple_event')
+
+ # Does not error when event has no fields
+ self.validator.validate_event(json.dumps({}), 'null_event')
+
+ def test_type_checking(self):
+ data = json.dumps({
+ 'an_array': ["a", "b"],
+ 'an_object': {
+ "a_key": 1,
+ "b_key": 1,
+ },
+ })
+ # Does not error when the types match
+ self.validator.validate_event(data, 'array_and_object_event')
+
+ # Errors when the type is wrong for primitive fields
+ with self.assertRaises(ValidationError):
+ data = json.dumps({
+ 'foo': 123,
+ 'bar': 'asdf',
+ })
+ self.validator.validate_event(data, 'simple_event')
+
+ # Errors when the type is wrong for array
+ with self.assertRaises(ValidationError):
+ data = json.dumps({
+ 'an_array': [1, 2, 3],
+ 'an_object': {},
+ })
+ self.validator.validate_event(data, 'array_and_object_event')
+
+ # Errors when the value type is wrong for object
+ with self.assertRaises(ValidationError):
+ data = json.dumps({
+ 'an_array': ["a", "b"],
+ 'an_object': {
+ "a_key": "wrong_value",
+ },
+ })
+ self.validator.validate_event(data, 'array_and_object_event')