blob: a0d20b515e35770aebecf61be54fbf50f419ee91 [file] [log] [blame]
Matteo Scandolob8dec872018-07-17 16:56:23 -04001# 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
16from mock import patch, call, Mock, PropertyMock
17import json
18
19import os, sys
20
21# Hack to load synchronizer framework
22test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23service_dir=os.path.join(test_path, "../../../..")
24xos_dir=os.path.join(test_path, "../../..")
25if 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
31def 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
42class 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("hippie-oss", "hippie-oss.xproto"),
64 get_models_fn("olt-service", "volt.xproto"),
Matteo Scandolobbbdb1a2019-02-14 14:05:04 -080065 get_models_fn("rcord", "rcord.xproto")
Matteo Scandolob8dec872018-07-17 16:56:23 -040066 ])
67 import synchronizers.new_base.modelaccessor
68 from auth_event import SubscriberAuthEventStep, 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 = SubscriberAuthEventStep(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.hippie_si = HippieOSSServiceInstance()
85 self.hippie_si.serial_number = "BRCM1234"
86 self.hippie_si.save = Mock()
87
88
89 def tearDown(self):
90 sys.path = self.sys_path_save
91
92 def test_authenticate_subscriber(self):
93
94 self.event.value = json.dumps({
Matteo Scandolo710ad622018-07-30 10:52:41 -040095 'authenticationState': "APPROVED",
96 'deviceId': "of:0000000ce2314000",
97 'portNumber': "101",
Matteo Scandolob8dec872018-07-17 16:56:23 -040098 })
99
100 with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
101 patch.object(HippieOSSServiceInstance.objects, "get_items") as hippie_si_mock, \
102 patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
103
104 volt_service_mock.return_value = [self.volt]
105 get_onu_sn.return_value = "BRCM1234"
106 hippie_si_mock.return_value = [self.hippie_si]
107
108 self.event_step.process_event(self.event)
109
110 self.hippie_si.save.assert_called_with(always_update_timestamp=True, update_fields=['authentication_state', 'no_sync', 'updated'])
111 self.assertEqual(self.hippie_si.authentication_state, 'APPROVED')