inject_user_key routine fixes
[osm/RO.git] / osm_ro / wim / tests / test_engine.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Copyright 2018 University of Bristol - High Performance Networks Research
4 # Group
5 # All Rights Reserved.
6 #
7 # Contributors: Anderson Bravalheri, Dimitrios Gkounis, Abubakar Siddique
8 # Muqaddas, Navdeep Uniyal, Reza Nejabati and Dimitra Simeonidou
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License"); you may
11 # not use this file except in compliance with the License. You may obtain
12 # a copy of the License at
13 #
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
19 # License for the specific language governing permissions and limitations
20 # under the License.
21 #
22 # For those usages not covered by the Apache License, Version 2.0 please
23 # contact with: <highperformance-networks@bristol.ac.uk>
24 #
25 # Neither the name of the University of Bristol nor the names of its
26 # contributors may be used to endorse or promote products derived from
27 # this software without specific prior written permission.
28 #
29 # This work has been performed in the context of DCMS UK 5G Testbeds
30 # & Trials Programme and in the framework of the Metro-Haul project -
31 # funded by the European Commission under Grant number 761727 through the
32 # Horizon 2020 and 5G-PPP programmes.
33 ##
34
35 from __future__ import unicode_literals
36
37 import unittest
38
39 from mock import MagicMock
40
41 from . import fixtures as eg
42 from ...tests.db_helpers import TestCaseWithDatabasePerTest, uuid
43 from ..errors import NoWimConnectedToDatacenters
44 from ..engine import WimEngine
45 from ..persistence import WimPersistence
46
47
48 class TestWimEngineDbMethods(TestCaseWithDatabasePerTest):
49 def setUp(self):
50 super(TestWimEngineDbMethods, self).setUp()
51 self.persist = WimPersistence(self.db)
52 self.engine = WimEngine(persistence=self.persist)
53 self.addCleanup(self.engine.stop_threads)
54
55 def populate(self, seeds=None):
56 super(TestWimEngineDbMethods, self).populate(
57 seeds or eg.consistent_set())
58
59 def test_find_common_wims(self):
60 # Given we have 2 WIM, 3 datacenters, but just 1 of the WIMs have
61 # access to them
62 self.populate([{'nfvo_tenants': [eg.tenant(0)]}] +
63 eg.wim_set(0, 0) +
64 eg.wim_set(1, 0) +
65 eg.datacenter_set(0, 0) +
66 eg.datacenter_set(1, 0) +
67 eg.datacenter_set(2, 0) +
68 [{'wim_port_mappings': [
69 eg.wim_port_mapping(0, 0),
70 eg.wim_port_mapping(0, 1),
71 eg.wim_port_mapping(0, 2)]}])
72
73 # When we retrieve the wims interconnecting some datacenters
74 wim_ids = self.engine.find_common_wims(
75 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant='tenant0')
76
77 # Then we should have just the first wim
78 self.assertEqual(len(wim_ids), 1)
79 self.assertEqual(wim_ids[0], uuid('wim0'))
80
81 def test_find_common_wims_multiple(self):
82 # Given we have 2 WIM, 3 datacenters, and all the WIMs have access to
83 # all datacenters
84 self.populate([{'nfvo_tenants': [eg.tenant(0)]}] +
85 eg.wim_set(0, 0) +
86 eg.wim_set(1, 0) +
87 eg.datacenter_set(0, 0) +
88 eg.datacenter_set(1, 0) +
89 eg.datacenter_set(2, 0) +
90 [{'wim_port_mappings': [
91 eg.wim_port_mapping(0, 0),
92 eg.wim_port_mapping(0, 1),
93 eg.wim_port_mapping(0, 2),
94 eg.wim_port_mapping(1, 0),
95 eg.wim_port_mapping(1, 1),
96 eg.wim_port_mapping(1, 2)]}])
97
98 # When we retrieve the wims interconnecting tree datacenters
99 wim_ids = self.engine.find_common_wims(
100 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant='tenant0')
101
102 # Then we should have all the wims
103 self.assertEqual(len(wim_ids), 2)
104 self.assertItemsEqual(wim_ids, [uuid('wim0'), uuid('wim1')])
105
106 def test_find_common_wim(self):
107 # Given we have 1 WIM, 3 datacenters but the WIM have access to just 2
108 # of them
109 self.populate([{'nfvo_tenants': [eg.tenant(0)]}] +
110 eg.wim_set(0, 0) +
111 eg.datacenter_set(0, 0) +
112 eg.datacenter_set(1, 0) +
113 eg.datacenter_set(2, 0) +
114 [{'wim_port_mappings': [
115 eg.wim_port_mapping(0, 0),
116 eg.wim_port_mapping(0, 1)]}])
117
118 # When we retrieve the common wim for the 2 datacenter that are
119 # interconnected
120 wim_id = self.engine.find_common_wim(
121 [uuid('dc0'), uuid('dc1')], tenant='tenant0')
122
123 # Then we should find the wim
124 self.assertEqual(wim_id, uuid('wim0'))
125
126 # When we try to retrieve the common wim for the all the datacenters
127 # Then a NoWimConnectedToDatacenters exception should be raised
128 with self.assertRaises(NoWimConnectedToDatacenters):
129 self.engine.find_common_wim(
130 [uuid('dc0'), uuid('dc1'), uuid('dc2')], tenant='tenant0')
131
132 def test_find_common_wim__different_tenants(self):
133 # Given we have 1 WIM and 2 datacenters connected but the WIMs don't
134 # belong to the tenant we have access to...
135 self.populate([{'nfvo_tenants': [eg.tenant(0), eg.tenant(1)]}] +
136 eg.wim_set(0, 0) +
137 eg.datacenter_set(0, 0) +
138 eg.datacenter_set(1, 0) +
139 [{'wim_port_mappings': [
140 eg.wim_port_mapping(0, 0),
141 eg.wim_port_mapping(0, 1)]}])
142
143 # When we retrieve the common wim for the 2 datacenter that are
144 # interconnected, but using another tenant,
145 # Then we should get an exception
146 with self.assertRaises(NoWimConnectedToDatacenters):
147 self.engine.find_common_wim(
148 [uuid('dc0'), uuid('dc1')], tenant='tenant1')
149
150
151 class TestWimEngine(unittest.TestCase):
152 def test_derive_wan_link(self):
153 # Given we have 2 datacenters connected by the same WIM, with port
154 # mappings registered
155 mappings = [eg.processed_port_mapping(0, 0),
156 eg.processed_port_mapping(0, 1)]
157 persist = MagicMock(
158 get_wim_port_mappings=MagicMock(return_value=mappings))
159
160 engine = WimEngine(persistence=persist)
161 self.addCleanup(engine.stop_threads)
162
163 # When we receive a list of 4 instance nets, representing
164 # 2 VLDs connecting 2 datacenters each
165 instance_nets = eg.instance_nets(2, 2)
166 wan_links = engine.derive_wan_links({}, instance_nets, uuid('tenant0'))
167
168 # Then we should derive 2 wan_links with the same instance_scenario_id
169 # and different scenario_network_id
170 self.assertEqual(len(wan_links), 2)
171 for link in wan_links:
172 self.assertEqual(link['instance_scenario_id'], uuid('nsr0'))
173 # Each VLD needs a network to be created in each datacenter
174 self.assertItemsEqual([l['sce_net_id'] for l in wan_links],
175 [uuid('vld0'), uuid('vld1')])
176
177
178 if __name__ == '__main__':
179 unittest.main()