blob: 40e164f8eb2951f1cca84b9bf7ceab2c700eda73 [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# SPDX-FileCopyrightText: 2020 The Magma Authors.
2# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
3#
4# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08005
6import json
7from unittest import TestCase
8
9from jsonschema import ValidationError
10from magma.eventd.event_validator import EventValidator
11
12
13class EventValidationTests(TestCase):
14
15 def setUp(self):
16 # A test event registry that specifies the test events
17 test_events_location = {
18 'module': 'orc8r',
19 'filename': 'test_event_definitions.yml',
20 }
21 config = {
22 'fluent_bit_port': '',
23 'tcp_timeout': '',
24 'event_registry': {
25 'simple_event': test_events_location,
26 'array_and_object_event': test_events_location,
27 'null_event': test_events_location,
28 },
29 }
30 self.validator = EventValidator(config)
31
32 def test_event_registration(self):
33 data = json.dumps({
34 'foo': 'magma', # required
35 'bar': 123,
36 })
37 # Errors when event is not registered
38 with self.assertRaises(Exception):
39 self.validator.validate_event(data, 'non_existent_event')
40
41 # Does not error when event is registered
42 self.validator.validate_event(data, 'simple_event')
43
44 def test_field_consistency(self):
45 # Errors when there are missing fields (required fields)
46 with self.assertRaises(ValidationError):
47 # foo is missing
48 data = json.dumps({
49 'bar': 123,
50 })
51 self.validator.validate_event(data, 'simple_event')
52
53 # Errors on excess fields (additionalProperties set to false)
54 with self.assertRaises(ValidationError):
55 data = json.dumps({
56 'extra_field': 12,
57 'foo': 'asdf',
58 'bar': 123,
59 })
60 self.validator.validate_event(data, 'simple_event')
61
62 # Errors when there are missing AND excess fields
63 with self.assertRaises(ValidationError):
64 data = json.dumps({
65 'extra_field': 12,
66 'bar': 123,
67 })
68 # foo is missing
69 self.validator.validate_event(data, 'simple_event')
70
71 # Does not error when the fields are equivalent
72 data = json.dumps({
73 'foo': 'magma', # required
74 'bar': 123,
75 })
76 self.validator.validate_event(data, 'simple_event')
77
78 # Does not error when event has no fields
79 self.validator.validate_event(json.dumps({}), 'null_event')
80
81 def test_type_checking(self):
82 data = json.dumps({
83 'an_array': ["a", "b"],
84 'an_object': {
85 "a_key": 1,
86 "b_key": 1,
87 },
88 })
89 # Does not error when the types match
90 self.validator.validate_event(data, 'array_and_object_event')
91
92 # Errors when the type is wrong for primitive fields
93 with self.assertRaises(ValidationError):
94 data = json.dumps({
95 'foo': 123,
96 'bar': 'asdf',
97 })
98 self.validator.validate_event(data, 'simple_event')
99
100 # Errors when the type is wrong for array
101 with self.assertRaises(ValidationError):
102 data = json.dumps({
103 'an_array': [1, 2, 3],
104 'an_object': {},
105 })
106 self.validator.validate_event(data, 'array_and_object_event')
107
108 # Errors when the value type is wrong for object
109 with self.assertRaises(ValidationError):
110 data = json.dumps({
111 'an_array': ["a", "b"],
112 'an_object': {
113 "a_key": "wrong_value",
114 },
115 })
116 self.validator.validate_event(data, 'array_and_object_event')