Debian Codegraph @

neutron_lib/tests/unit/callbacks/test_events.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 from oslotest import base
16
17 from neutron_lib.callbacks import events
18
19
20 class EventPayloadTestCase(base.BaseTestCase):
21
22 def test_context(self):
23 e = events.EventPayload(mock.ANY)
24 self.assertEqual(mock.ANY, e.context)
25
26 def test_metadata(self):
27 meta = {'k1': 'v1', 'k2': mock.ANY}
28 e = events.EventPayload(mock.ANY, metadata=meta)
29 self.assertEqual(meta, e.metadata)
30 event_meta = e.metadata
31 event_meta['k3'] = 'v3'
32 self.assertIn('k3', e.metadata)
33
34 def test_request_body(self):
35 e = events.EventPayload(mock.ANY, request_body={'k', 'v'})
36 self.assertEqual({'k', 'v'}, e.request_body)
37
38 def test_states(self):
39 e = events.EventPayload(mock.ANY, states=['s1', 's2'])
40 self.assertEqual(['s1', 's2'], e.states)
41 e.states.append('state')
42 self.assertIn('state', e.states)
43
44 def test_resource_id(self):
45 e = events.EventPayload(mock.ANY, resource_id='id1')
46 self.assertEqual('id1', e.resource_id)
47
48 def test_has_no_states(self):
49 e = events.EventPayload(mock.ANY)
50 self.assertFalse(e.has_states)
51
52 def test_has_states(self):
53 e = events.EventPayload(mock.ANY, states=['s1'])
54 self.assertTrue(e.has_states)
55
56 def test_latest_state_with_states(self):
57 body = object()
58 states = [object(), object()]
59 e = events.EventPayload(mock.ANY, request_body=body, states=states)
60 self.assertEqual(states[-1], e.latest_state)
61
62 def test_latest_state_without_states(self):
63 body = object()
64 e = events.EventPayload(mock.ANY, request_body=body)
65 self.assertIsNone(e.latest_state)
66
67
68 class DataStoreEventPayloadTestCase(base.BaseTestCase):
69
70 def test_states(self):
71 e = events.DBEventPayload(mock.ANY, states=['s1'])
72 self.assertEqual(['s1'], e.states)
73
74 def test_desired_state(self):
75 desired_state = {'k': object()}
76 e = events.DBEventPayload(mock.ANY, desired_state=desired_state)
77 self.assertEqual(desired_state, e.desired_state)
78 desired_state['a'] = 'A'
79 self.assertEqual(desired_state, e.desired_state)
80
81 def test_is_not_persisted(self):
82 e = events.DBEventPayload(mock.ANY, states=['s1'])
83 self.assertFalse(e.is_persisted)
84 e = events.DBEventPayload(mock.ANY, resource_id='1a')
85 self.assertFalse(e.is_persisted)
86
87 def test_is_persisted(self):
88 e = events.DBEventPayload(mock.ANY, states=['s1'],
89 resource_id='1a')
90 self.assertTrue(e.is_persisted)
91
92 def test_is_not_to_be_committed(self):
93 e = events.DBEventPayload(mock.ANY, states=['s1'],
94 resource_id='1a')
95 self.assertFalse(e.is_to_be_committed)
96
97 def test_is_to_be_committed(self):
98 e = events.DBEventPayload(mock.ANY, states=[mock.ANY],
99 resource_id='1a', desired_state=object())
100 self.assertTrue(e.is_to_be_committed)
101
102 def test_latest_state_with_desired_state(self):
103 desired_state = object()
104 e = events.DBEventPayload(mock.ANY, states=[object()],
105 desired_state=desired_state)
106 self.assertEqual(desired_state, e.latest_state)
107
108
109 class APIEventPayloadTestCase(base.BaseTestCase):
110
111 def test_action(self):
112 e = events.APIEventPayload(mock.ANY, 'post.end', 'POST')
113 self.assertEqual('POST', e.action)
114
115 def test_method_name(self):
116 e = events.APIEventPayload(mock.ANY, 'post.end', 'POST')
117 self.assertEqual('post.end', e.method_name)