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 | |
| 84 | self.subscriber = RCORDSubscriber() |
| 85 | self.subscriber.onu_device = "BRCM1234" |
| 86 | self.subscriber.save = Mock() |
| 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 | |
| 91 | |
| 92 | def tearDown(self): |
| 93 | sys.path = self.sys_path_save |
| 94 | |
| 95 | def test_dhcp_subscriber(self): |
| 96 | |
| 97 | self.event.value = json.dumps({ |
| 98 | "deviceId" : "of:0000000000000001", |
| 99 | "portNumber" : "1", |
| 100 | "macAddress" : self.mac_address, |
| 101 | "ipAddress" : self.ip_address |
| 102 | }) |
| 103 | |
| 104 | with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \ |
| 105 | patch.object(RCORDSubscriber.objects, "get_items") as subscriber_mock, \ |
| 106 | patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn: |
| 107 | |
| 108 | volt_service_mock.return_value = [self.volt] |
| 109 | get_onu_sn.return_value = "BRCM1234" |
| 110 | subscriber_mock.return_value = [self.subscriber] |
| 111 | |
| 112 | self.event_step.process_event(self.event) |
| 113 | |
| 114 | self.subscriber.save.assert_called() |
| 115 | self.assertEqual(self.subscriber.mac_address, self.mac_address) |
| 116 | self.assertEqual(self.subscriber.ip_address, self.ip_address) |