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