Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import json |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | service_dir=os.path.join(test_path, "../../../..") |
| 24 | xos_dir=os.path.join(test_path, "../../..") |
| 25 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 26 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 27 | services_dir=os.path.join(xos_dir, "../../xos_services") |
| 28 | # END Hack to load synchronizer framework |
| 29 | |
| 30 | # generate model from xproto |
| 31 | def get_models_fn(service_name, xproto_name): |
| 32 | name = os.path.join(service_name, "xos", xproto_name) |
| 33 | if os.path.exists(os.path.join(services_dir, name)): |
| 34 | return name |
| 35 | else: |
| 36 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 37 | if os.path.exists(os.path.join(services_dir, name)): |
| 38 | return name |
| 39 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 40 | # END generate model from xproto |
| 41 | |
| 42 | class TestSubscriberAuthEvent(unittest.TestCase): |
| 43 | |
| 44 | def setUp(self): |
| 45 | |
| 46 | self.sys_path_save = sys.path |
| 47 | sys.path.append(xos_dir) |
| 48 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 49 | |
| 50 | # Setting up the config module |
| 51 | from xosconfig import Config |
| 52 | config = os.path.join(test_path, "../test_config.yaml") |
| 53 | Config.clear() |
| 54 | Config.init(config, "synchronizer-config-schema.yaml") |
| 55 | from multistructlog import create_logger |
| 56 | log = create_logger(Config().get('logging')) |
| 57 | # END Setting up the config module |
| 58 | |
| 59 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 60 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 61 | |
| 62 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 63 | get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"), |
| 64 | get_models_fn("olt-service", "volt.xproto"), |
| 65 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 66 | ]) |
| 67 | import synchronizers.new_base.modelaccessor |
| 68 | from dhcp_event import SubscriberDhcpEventStep, model_accessor |
| 69 | |
| 70 | # import all class names to globals |
| 71 | for (k, v) in model_accessor.all_model_classes.items(): |
| 72 | globals()[k] = v |
| 73 | |
| 74 | self.log = log |
| 75 | |
| 76 | self.event_step = SubscriberDhcpEventStep(self.log) |
| 77 | |
| 78 | self.event = Mock() |
| 79 | |
| 80 | self.volt = Mock() |
| 81 | self.volt.name = "vOLT" |
| 82 | self.volt.leaf_model = Mock() |
| 83 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 84 | # self.subscriber = RCORDSubscriber() |
| 85 | # self.subscriber.onu_device = "BRCM1234" |
| 86 | # self.subscriber.save = Mock() |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 87 | |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 88 | self.mac_address = "00:AA:00:00:00:01" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 89 | self.ip_address = "192.168.3.5" |
| 90 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 91 | self.si = AttWorkflowDriverServiceInstance() |
| 92 | self.si.serial_number = "BRCM1234" |
| 93 | self.si.save = Mock() |
| 94 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 95 | |
| 96 | def tearDown(self): |
| 97 | sys.path = self.sys_path_save |
| 98 | |
| 99 | def test_dhcp_subscriber(self): |
| 100 | |
| 101 | self.event.value = json.dumps({ |
| 102 | "deviceId" : "of:0000000000000001", |
| 103 | "portNumber" : "1", |
| 104 | "macAddress" : self.mac_address, |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 105 | "ipAddress" : self.ip_address, |
| 106 | "messageType": "DHCPREQUEST" |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 107 | }) |
| 108 | |
| 109 | with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \ |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 110 | patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as si_mock, \ |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 111 | patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn: |
| 112 | |
| 113 | volt_service_mock.return_value = [self.volt] |
| 114 | get_onu_sn.return_value = "BRCM1234" |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 115 | si_mock.return_value = [self.si] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 116 | |
| 117 | self.event_step.process_event(self.event) |
| 118 | |
Matteo Scandolo | de8cfa8 | 2018-10-16 13:49:05 -0700 | [diff] [blame] | 119 | self.si.save.assert_called() |
| 120 | self.assertEqual(self.si.dhcp_state, "DHCPREQUEST") |
| 121 | self.assertEqual(self.si.mac_address, self.mac_address) |
| 122 | self.assertEqual(self.si.ip_address, self.ip_address) |