blob: 54c73e10162cb25f5bcf0b9d0e666350d54dcd5f [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14import json
15from unittest import TestCase
16
17from jsonschema import ValidationError
18from magma.eventd.event_validator import EventValidator
19
20
21class EventValidationTests(TestCase):
22
23 def setUp(self):
24 # A test event registry that specifies the test events
25 test_events_location = {
26 'module': 'orc8r',
27 'filename': 'test_event_definitions.yml',
28 }
29 config = {
30 'fluent_bit_port': '',
31 'tcp_timeout': '',
32 'event_registry': {
33 'simple_event': test_events_location,
34 'array_and_object_event': test_events_location,
35 'null_event': test_events_location,
36 },
37 }
38 self.validator = EventValidator(config)
39
40 def test_event_registration(self):
41 data = json.dumps({
42 'foo': 'magma', # required
43 'bar': 123,
44 })
45 # Errors when event is not registered
46 with self.assertRaises(Exception):
47 self.validator.validate_event(data, 'non_existent_event')
48
49 # Does not error when event is registered
50 self.validator.validate_event(data, 'simple_event')
51
52 def test_field_consistency(self):
53 # Errors when there are missing fields (required fields)
54 with self.assertRaises(ValidationError):
55 # foo is missing
56 data = json.dumps({
57 'bar': 123,
58 })
59 self.validator.validate_event(data, 'simple_event')
60
61 # Errors on excess fields (additionalProperties set to false)
62 with self.assertRaises(ValidationError):
63 data = json.dumps({
64 'extra_field': 12,
65 'foo': 'asdf',
66 'bar': 123,
67 })
68 self.validator.validate_event(data, 'simple_event')
69
70 # Errors when there are missing AND excess fields
71 with self.assertRaises(ValidationError):
72 data = json.dumps({
73 'extra_field': 12,
74 'bar': 123,
75 })
76 # foo is missing
77 self.validator.validate_event(data, 'simple_event')
78
79 # Does not error when the fields are equivalent
80 data = json.dumps({
81 'foo': 'magma', # required
82 'bar': 123,
83 })
84 self.validator.validate_event(data, 'simple_event')
85
86 # Does not error when event has no fields
87 self.validator.validate_event(json.dumps({}), 'null_event')
88
89 def test_type_checking(self):
90 data = json.dumps({
91 'an_array': ["a", "b"],
92 'an_object': {
93 "a_key": 1,
94 "b_key": 1,
95 },
96 })
97 # Does not error when the types match
98 self.validator.validate_event(data, 'array_and_object_event')
99
100 # Errors when the type is wrong for primitive fields
101 with self.assertRaises(ValidationError):
102 data = json.dumps({
103 'foo': 123,
104 'bar': 'asdf',
105 })
106 self.validator.validate_event(data, 'simple_event')
107
108 # Errors when the type is wrong for array
109 with self.assertRaises(ValidationError):
110 data = json.dumps({
111 'an_array': [1, 2, 3],
112 'an_object': {},
113 })
114 self.validator.validate_event(data, 'array_and_object_event')
115
116 # Errors when the value type is wrong for object
117 with self.assertRaises(ValidationError):
118 data = json.dumps({
119 'an_array': ["a", "b"],
120 'an_object': {
121 "a_key": "wrong_value",
122 },
123 })
124 self.validator.validate_event(data, 'array_and_object_event')