blob: 1dc8fdff3829dca6c31c8a34d9ed2283ec6e49a2 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# 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 Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
Scott Bakerbba67b62019-01-28 17:38:21 -080016import unittest
17from mock import patch
18import mock
19import pdb
20import networkx as nx
21
22import os
23import sys
24
Zack Williams5c2ea232019-01-30 15:23:01 -070025try:
26 # Python 2: "reload" is built-in
27 reload # pylint: disable=reload-builtin
28except NameError:
29 from importlib import reload
30
Scott Bakerbba67b62019-01-28 17:38:21 -080031test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
32sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
33xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
34
Scott Bakerbba67b62019-01-28 17:38:21 -080035
Zack Williams5c2ea232019-01-30 15:23:01 -070036class TestScheduling(unittest.TestCase):
Scott Bakerbba67b62019-01-28 17:38:21 -080037 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 Williams5c2ea232019-01-30 15:23:01 -070049 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 Bakerbba67b62019-01-28 17:38:21 -080053 )
54
Zack Williams5c2ea232019-01-30 15:23:01 -070055 os.chdir(
56 os.path.join(test_path, "..")
57 ) # config references xos-synchronizer-tests/model-deps
Scott Bakerbba67b62019-01-28 17:38:21 -080058
59 import xossynchronizer.event_loop
Zack Williams5c2ea232019-01-30 15:23:01 -070060
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080061 event_loop = xossynchronizer.event_loop
Scott Bakerbba67b62019-01-28 17:38:21 -080062
63 reload(xossynchronizer.event_loop)
64 import xossynchronizer.backend
65
66 reload(xossynchronizer.backend)
Scott Bakerbba67b62019-01-28 17:38:21 -080067 from xossynchronizer.modelaccessor import model_accessor
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080068 from mock_modelaccessor import mock_enumerator
Scott Bakerbba67b62019-01-28 17:38:21 -080069
70 # import all class names to globals
71 for (k, v) in model_accessor.all_model_classes.items():
72 globals()[k] = v
73
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080074 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080075 steps_dir = Config.get("steps_dir")
76 self.steps = b.load_sync_step_modules(steps_dir)
Zack Williams5c2ea232019-01-30 15:23:01 -070077 self.synchronizer = xossynchronizer.event_loop.XOSObserver(
78 self.steps, model_accessor
79 )
Scott Bakerbba67b62019-01-28 17:38:21 -080080
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 Bakerbba67b62019-01-28 17:38:21 -080098 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 Baker4839dec2019-02-27 16:50:37 -0800105 slice = Slice()
106 site1 = Site()
107 site2 = Site()
108 slice.site = site2
109 verdict, _ = self.synchronizer.concrete_path_exists(slice, site1)
Scott Bakerbba67b62019-01-28 17:38:21 -0800110 self.assertFalse(verdict)
111
112 def test_concrete_object_path_adjacent(self):
Scott Baker4839dec2019-02-27 16:50:37 -0800113 slice = Slice()
114 site = Site()
115 slice.site = site
116 verdict, edge_type = self.synchronizer.concrete_path_exists(slice, site)
Scott Bakerbba67b62019-01-28 17:38:21 -0800117
118 self.assertTrue(verdict)
119 self.assertEqual(edge_type, event_loop.DIRECT_EDGE)
120
Zack Williams5c2ea232019-01-30 15:23:01 -0700121
Scott Bakerbba67b62019-01-28 17:38:21 -0800122 def test_concrete_object_path_distant(self):
Scott Baker4839dec2019-02-27 16:50:37 -0800123 p = ComputeServiceInstance()
Scott Bakerbba67b62019-01-28 17:38:21 -0800124 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 Bakerbba67b62019-01-28 17:38:21 -0800131 def test_cohorting_independent(self):
132 i = Image()
133
134 p = Slice()
Scott Baker4839dec2019-02-27 16:50:37 -0800135 c = Site()
Scott Bakerbba67b62019-01-28 17:38:21 -0800136
137 cohorts = self.synchronizer.compute_dependent_cohorts([i, p, c], False)
138 self.assertEqual(len(cohorts), 3)
139
Scott Bakerbba67b62019-01-28 17:38:21 -0800140if __name__ == "__main__":
141 unittest.main()