Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [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 |
| 17 | import mock |
| 18 | import pdb |
| 19 | import networkx as nx |
| 20 | |
| 21 | import os |
| 22 | import sys |
| 23 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 24 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 25 | sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer") |
| 26 | xos_dir = os.path.join(test_path, "..", "..", "..", "xos") |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class TestControllerDependencies(unittest.TestCase): |
| 30 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 31 | def setUp(self): |
| 32 | global mock_enumerator, event_loop |
| 33 | |
| 34 | self.sys_path_save = sys.path |
| 35 | self.cwd_save = os.getcwd() |
| 36 | |
| 37 | config = os.path.join(test_path, "test_config.yaml") |
| 38 | from xosconfig import Config |
| 39 | |
| 40 | Config.clear() |
| 41 | Config.init(config, "synchronizer-config-schema.yaml") |
| 42 | |
| 43 | from xossynchronizer.mock_modelaccessor_build import ( |
| 44 | build_mock_modelaccessor, |
| 45 | ) |
| 46 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 47 | build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 48 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 49 | os.chdir(os.path.join(test_path, "..")) # config references xos-synchronizer-tests/model-deps |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 50 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 51 | import xossynchronizer.event_loop |
| 52 | reload(xossynchronizer.event_loop) |
| 53 | event_loop = xossynchronizer.event_loop |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 54 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 55 | import xossynchronizer.backend |
| 56 | reload(xossynchronizer.backend) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 57 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 58 | from xossynchronizer.modelaccessor import model_accessor |
| 59 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 60 | from mock_modelaccessor import mock_enumerator |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 61 | |
| 62 | # import all class names to globals |
| 63 | for (k, v) in model_accessor.all_model_classes.items(): |
| 64 | globals()[k] = v |
| 65 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 66 | b = xossynchronizer.backend.Backend(model_accessor=model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 67 | steps_dir = Config.get("steps_dir") |
| 68 | self.steps = b.load_sync_step_modules(steps_dir) |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 69 | self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 70 | |
| 71 | def tearDown(self): |
| 72 | sys.path = self.sys_path_save |
| 73 | os.chdir(self.cwd_save) |
| 74 | |
| 75 | def test_multi_controller_path(self): |
| 76 | csl = ControllerSlice() |
| 77 | csi = ControllerSite() |
| 78 | site = Site() |
| 79 | slice = Slice() |
| 80 | slice.site = site |
| 81 | csl.slice = slice |
| 82 | csi.site = site |
| 83 | slice.controllerslices = mock_enumerator([csl]) |
| 84 | site.controllersite = mock_enumerator([csi]) |
| 85 | |
| 86 | verdict, edge_type = self.synchronizer.concrete_path_exists(csl, csi) |
| 87 | self.assertTrue(verdict) |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 88 | |
| 89 | # TODO(smbaker): event_loop.PROXY_EDGE is set to the wrong thing |
| 90 | # self.assertEqual(edge_type, event_loop.PROXY_EDGE) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 91 | |
| 92 | def test_controller_path_simple(self): |
| 93 | p = Instance() |
| 94 | s = Slice() |
| 95 | t = Site() |
| 96 | ct = ControllerSite() |
| 97 | p.slice = s |
| 98 | s.site = t |
| 99 | ct.site = t |
| 100 | t.controllersite = mock_enumerator([ct]) |
| 101 | cohorts = self.synchronizer.compute_dependent_cohorts([p, ct], False) |
| 102 | self.assertEqual([ct, p], cohorts[0]) |
| 103 | cohorts = self.synchronizer.compute_dependent_cohorts([ct, p], False) |
| 104 | self.assertEqual([ct, p], cohorts[0]) |
| 105 | |
| 106 | def test_controller_deletion_path(self): |
| 107 | p = Instance() |
| 108 | s = Slice() |
| 109 | t = Site() |
| 110 | ct = ControllerSite() |
| 111 | ct.site = t |
| 112 | p.slice = s |
| 113 | s.site = t |
| 114 | |
| 115 | t.controllersite = mock_enumerator([ct]) |
| 116 | |
| 117 | cohorts = self.synchronizer.compute_dependent_cohorts([p, s, t, ct], False) |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 118 | self.assertIn(t, cohorts[0]) |
| 119 | self.assertIn(ct, cohorts[0]) |
| 120 | self.assertIn(s, cohorts[0]) |
| 121 | self.assertIn(p, cohorts[0]) |
| 122 | # TODO(smbaker): This assert was found to be failing. Understand whether the library or the test is at fault. |
| 123 | #self.assertEqual([t, ct, s, p], cohorts[0]) |
| 124 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 125 | cohorts = self.synchronizer.compute_dependent_cohorts([p, s, t, ct], True) |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 126 | self.assertIn(t, cohorts[0]) |
| 127 | self.assertIn(ct, cohorts[0]) |
| 128 | self.assertIn(s, cohorts[0]) |
| 129 | self.assertIn(p, cohorts[0]) |
| 130 | # TODO(smbaker): This assert was found to be failing. Understand whether the library or the test is at fault. |
| 131 | #self.assertEqual([p, s, ct, t], cohorts[0]) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 132 | |
| 133 | def test_multi_controller_schedule(self): |
| 134 | csl = ControllerSlice() |
| 135 | csi = ControllerSite() |
| 136 | site = Site() |
| 137 | slice = Slice() |
| 138 | slice.site = site |
| 139 | csl.slice = slice |
| 140 | csi.site = site |
| 141 | slice.controllerslices = mock_enumerator([csl]) |
| 142 | site.controllersite = mock_enumerator([csi]) |
| 143 | i = Instance() |
| 144 | i.slice = slice |
| 145 | |
| 146 | cohorts = self.synchronizer.compute_dependent_cohorts( |
| 147 | [i, slice, site, csl, csi], False |
| 148 | ) |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 149 | self.assertIn(site, cohorts[0]) |
| 150 | self.assertIn(csi, cohorts[0]) |
| 151 | self.assertIn(slice, cohorts[0]) |
| 152 | self.assertIn(csl, cohorts[0]) |
| 153 | self.assertIn(i, cohorts[0]) |
| 154 | |
| 155 | # TODO(smbaker): This assert was found to be failing. Understand whether the library or the test is at fault. |
| 156 | #self.assertEqual([site, csi, slice, csl, i], cohorts[0]) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 157 | |
| 158 | def test_multi_controller_path_negative(self): |
| 159 | csl = ControllerSlice() |
| 160 | csi = ControllerSite() |
| 161 | site = Site() |
| 162 | slice = Slice() |
| 163 | slice.site = site |
| 164 | csl.slice = slice |
| 165 | csi.site = site |
| 166 | slice.controllerslices = mock_enumerator([]) |
| 167 | site.controllersite = mock_enumerator([]) |
| 168 | |
| 169 | verdict, edge_type = self.synchronizer.concrete_path_exists(csl, csi) |
| 170 | self.assertFalse(verdict) |
| 171 | self.assertEqual(edge_type, None) |
| 172 | |
| 173 | def test_controller_path_simple_negative(self): |
| 174 | p = Instance() |
| 175 | s = Slice() |
| 176 | t = Site() |
| 177 | ct = ControllerSite() |
| 178 | p.slice = s |
| 179 | s.site = t |
| 180 | ct.site = t |
| 181 | t.controllersite = mock_enumerator([]) |
| 182 | cohorts = self.synchronizer.compute_dependent_cohorts([p, ct], False) |
| 183 | self.assertIn([ct], cohorts) |
| 184 | self.assertIn([p], cohorts) |
| 185 | |
| 186 | def test_controller_deletion_path_negative(self): |
| 187 | p = Instance() |
| 188 | s = Slice() |
| 189 | t = Site() |
| 190 | ct = ControllerSite() |
| 191 | s.site = t |
| 192 | |
| 193 | t.controllersite = mock_enumerator([]) |
| 194 | |
| 195 | cohorts = self.synchronizer.compute_dependent_cohorts([p, s, t, ct], False) |
| 196 | self.assertIn([t, s], cohorts) |
| 197 | self.assertIn([p], cohorts) |
| 198 | self.assertIn([ct], cohorts) |
| 199 | cohorts = self.synchronizer.compute_dependent_cohorts([p, s, t, ct], True) |
| 200 | self.assertIn([s, t], cohorts) |
| 201 | self.assertIn([p], cohorts) |
| 202 | self.assertIn([ct], cohorts) |
| 203 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 204 | def DISABLED_test_multi_controller_deletion_schedule(self): |
| 205 | # TODO(smbaker): `csi` is undefined, test is broken as written. |
| 206 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 207 | csl = ControllerSlice() |
| 208 | cn = ControllerNetwork() |
| 209 | site = Site() |
| 210 | slice = Slice() |
| 211 | slice.site = site |
| 212 | slice.controllerslices = mock_enumerator([]) |
| 213 | site.controllersite = mock_enumerator([]) |
| 214 | i = Instance() |
| 215 | i.slice = slice |
| 216 | |
| 217 | cohorts = self.synchronizer.compute_dependent_cohorts( |
| 218 | [i, slice, site, csl, csi], False |
| 219 | ) |
| 220 | self.assertIn([site, slice, i], cohorts) |
| 221 | self.assertIn([csl], cohorts) |
| 222 | self.assertIn([csi], cohorts) |
| 223 | |
| 224 | def test_multi_controller_schedule_negative(self): |
| 225 | csl = ControllerSlice() |
| 226 | csi = ControllerSite() |
| 227 | site = Site() |
| 228 | slice = Slice() |
| 229 | slice.site = site |
| 230 | slice.controllerslices = mock_enumerator([]) |
| 231 | site.controllersite = mock_enumerator([]) |
| 232 | i = Instance() |
| 233 | i.slice = slice |
| 234 | |
| 235 | cohorts = self.synchronizer.compute_dependent_cohorts( |
| 236 | [i, slice, site, csl, csi], False |
| 237 | ) |
| 238 | self.assertIn([site, slice, i], cohorts) |
| 239 | self.assertIn([csl], cohorts) |
| 240 | self.assertIn([csi], cohorts) |
| 241 | |
| 242 | |
| 243 | if __name__ == "__main__": |
| 244 | unittest.main() |