blob: d51ee7a962ad1547dfaec1e46e19ead6550392da [file] [log] [blame]
Andy Bavier561f3e52019-03-15 10:46:05 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
Andy Bavier289d7aa2019-04-25 10:00:28 -070016from mock import patch, Mock
Andy Bavier561f3e52019-03-15 10:46:05 -070017import json
18
Andy Bavier289d7aa2019-04-25 10:00:28 -070019import os
20import sys
Andy Bavier561f3e52019-03-15 10:46:05 -070021
Andy Bavier289d7aa2019-04-25 10:00:28 -070022test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23
Andy Bavier561f3e52019-03-15 10:46:05 -070024
25class TestSubscriberAuthEvent(unittest.TestCase):
26
27 def setUp(self):
28
29 self.sys_path_save = sys.path
30
31 # Setting up the config module
32 from xosconfig import Config
33 config = os.path.join(test_path, "../test_config.yaml")
34 Config.clear()
35 Config.init(config, "synchronizer-config-schema.yaml")
36 from multistructlog import create_logger
37 log = create_logger(Config().get('logging'))
38 # END Setting up the config module
39
40 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
41 mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"),
42 ("olt-service", "volt.xproto"),
43 ("rcord", "rcord.xproto")])
44
45 import xossynchronizer.modelaccessor
46 import mock_modelaccessor
Andy Bavier289d7aa2019-04-25 10:00:28 -070047 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Andy Bavier561f3e52019-03-15 10:46:05 -070048 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
49
50 from xossynchronizer.modelaccessor import model_accessor
51 from dhcp_event import SubscriberDhcpEventStep
52
53 # import all class names to globals
54 for (k, v) in model_accessor.all_model_classes.items():
55 globals()[k] = v
56
57 self.model_accessor = model_accessor
58 self.log = log
59
60 self.event_step = SubscriberDhcpEventStep(model_accessor=self.model_accessor, log=self.log)
61
62 self.event = Mock()
63
64 self.volt = Mock()
65 self.volt.name = "vOLT"
66 self.volt.leaf_model = Mock()
67
68 # self.subscriber = RCORDSubscriber()
69 # self.subscriber.onu_device = "BRCM1234"
70 # self.subscriber.save = Mock()
71
72 self.mac_address = "00:AA:00:00:00:01"
73 self.ip_address = "192.168.3.5"
74
75 self.si = TtWorkflowDriverServiceInstance()
76 self.si.serial_number = "BRCM1234"
77 self.si.save = Mock()
78
Andy Bavier561f3e52019-03-15 10:46:05 -070079 def tearDown(self):
80 sys.path = self.sys_path_save
81
82 def test_dhcp_subscriber(self):
83
84 self.event.value = json.dumps({
Andy Bavier289d7aa2019-04-25 10:00:28 -070085 "deviceId": "of:0000000000000001",
86 "portNumber": "1",
87 "macAddress": self.mac_address,
88 "ipAddress": self.ip_address,
89 "messageType": "DHCPREQUEST",
90 'serialNumber': "BRCM1234",
Andy Bavier561f3e52019-03-15 10:46:05 -070091 })
92
Andy Bavier289d7aa2019-04-25 10:00:28 -070093 with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as si_mock:
Andy Bavier561f3e52019-03-15 10:46:05 -070094
Andy Bavier561f3e52019-03-15 10:46:05 -070095 si_mock.return_value = [self.si]
96
97 self.event_step.process_event(self.event)
98
99 self.si.save.assert_called()
100 self.assertEqual(self.si.dhcp_state, "DHCPREQUEST")
101 self.assertEqual(self.si.mac_address, self.mac_address)
102 self.assertEqual(self.si.ip_address, self.ip_address)
103
Andy Bavier289d7aa2019-04-25 10:00:28 -0700104
Andy Bavier561f3e52019-03-15 10:46:05 -0700105if __name__ == '__main__':
Andy Bavier289d7aa2019-04-25 10:00:28 -0700106 sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py
107 unittest.main()