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 | |
| 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") |
| 27 | |
| 28 | class TestScheduling(unittest.TestCase): |
| 29 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 30 | def setUp(self): |
| 31 | global mock_enumerator, event_loop |
| 32 | |
| 33 | self.sys_path_save = sys.path |
| 34 | self.cwd_save = os.getcwd() |
| 35 | |
| 36 | config = os.path.join(test_path, "test_config.yaml") |
| 37 | from xosconfig import Config |
| 38 | |
| 39 | Config.clear() |
| 40 | Config.init(config, "synchronizer-config-schema.yaml") |
| 41 | |
| 42 | from xossynchronizer.mock_modelaccessor_build import ( |
| 43 | build_mock_modelaccessor, |
| 44 | ) |
| 45 | |
| 46 | build_mock_modelaccessor(sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[]) |
| 47 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 48 | 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] | 49 | |
| 50 | import xossynchronizer.event_loop |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 51 | event_loop = xossynchronizer.event_loop |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 52 | |
| 53 | reload(xossynchronizer.event_loop) |
| 54 | import xossynchronizer.backend |
| 55 | |
| 56 | reload(xossynchronizer.backend) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 57 | from xossynchronizer.modelaccessor import model_accessor |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 58 | from mock_modelaccessor import mock_enumerator |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 59 | |
| 60 | # import all class names to globals |
| 61 | for (k, v) in model_accessor.all_model_classes.items(): |
| 62 | globals()[k] = v |
| 63 | |
Scott Baker | f0d7e5c | 2019-02-05 08:35:31 -0800 | [diff] [blame] | 64 | b = xossynchronizer.backend.Backend(model_accessor=model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 65 | steps_dir = Config.get("steps_dir") |
| 66 | self.steps = b.load_sync_step_modules(steps_dir) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 67 | self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 68 | |
| 69 | def tearDown(self): |
| 70 | sys.path = self.sys_path_save |
| 71 | os.chdir(self.cwd_save) |
| 72 | |
| 73 | def test_same_object_trivial(self): |
| 74 | s = Slice(pk=4) |
| 75 | t = Slice(pk=4) |
| 76 | same, t = self.synchronizer.same_object(s, t) |
| 77 | self.assertTrue(same) |
| 78 | self.assertEqual(t, event_loop.DIRECT_EDGE) |
| 79 | |
| 80 | def test_same_object_trivial2(self): |
| 81 | s = Slice(pk=4) |
| 82 | t = Slice(pk=5) |
| 83 | same, t = self.synchronizer.same_object(s, t) |
| 84 | self.assertFalse(same) |
| 85 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 86 | def test_concrete_path_no_model_path(self): |
| 87 | p = Port() |
| 88 | n = NetworkParameter() |
| 89 | verdict, _ = self.synchronizer.concrete_path_exists(p, n) |
| 90 | self.assertFalse(verdict) |
| 91 | |
| 92 | def test_concrete_no_object_path_adjacent(self): |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame^] | 93 | slice = Slice() |
| 94 | site1 = Site() |
| 95 | site2 = Site() |
| 96 | slice.site = site2 |
| 97 | verdict, _ = self.synchronizer.concrete_path_exists(slice, site1) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 98 | self.assertFalse(verdict) |
| 99 | |
| 100 | def test_concrete_object_path_adjacent(self): |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame^] | 101 | slice = Slice() |
| 102 | site = Site() |
| 103 | slice.site = site |
| 104 | verdict, edge_type = self.synchronizer.concrete_path_exists(slice, site) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 105 | |
| 106 | self.assertTrue(verdict) |
| 107 | self.assertEqual(edge_type, event_loop.DIRECT_EDGE) |
| 108 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 109 | def test_concrete_object_path_distant(self): |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame^] | 110 | p = ComputeServiceInstance() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 111 | s = Slice() |
| 112 | t = Site() |
| 113 | p.slice = s |
| 114 | s.site = t |
| 115 | verdict = self.synchronizer.concrete_path_exists(p, t) |
| 116 | self.assertTrue(verdict) |
| 117 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 118 | def test_cohorting_independent(self): |
| 119 | i = Image() |
| 120 | |
| 121 | p = Slice() |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame^] | 122 | c = Site() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 123 | |
| 124 | cohorts = self.synchronizer.compute_dependent_cohorts([i, p, c], False) |
| 125 | self.assertEqual(len(cohorts), 3) |
| 126 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 127 | if __name__ == "__main__": |
| 128 | unittest.main() |