blob: 0aa5334166dd74d7a7a5d91f1ee98fe611c03663 [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
15import unittest
16from mock import patch
17import mock
18import pdb
19import networkx as nx
20
21import os
22import sys
23
24test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
25sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer")
26xos_dir = os.path.join(test_path, "..", "..", "..", "xos")
27
28class TestScheduling(unittest.TestCase):
29
Scott Bakerbba67b62019-01-28 17:38:21 -080030 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 Bakerf0d7e5c2019-02-05 08:35:31 -080048 os.chdir(os.path.join(test_path, "..")) # config references xos-synchronizer-tests/model-deps
Scott Bakerbba67b62019-01-28 17:38:21 -080049
50 import xossynchronizer.event_loop
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080051 event_loop = xossynchronizer.event_loop
Scott Bakerbba67b62019-01-28 17:38:21 -080052
53 reload(xossynchronizer.event_loop)
54 import xossynchronizer.backend
55
56 reload(xossynchronizer.backend)
Scott Bakerbba67b62019-01-28 17:38:21 -080057 from xossynchronizer.modelaccessor import model_accessor
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080058 from mock_modelaccessor import mock_enumerator
Scott Bakerbba67b62019-01-28 17:38:21 -080059
60 # import all class names to globals
61 for (k, v) in model_accessor.all_model_classes.items():
62 globals()[k] = v
63
Scott Bakerf0d7e5c2019-02-05 08:35:31 -080064 b = xossynchronizer.backend.Backend(model_accessor=model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080065 steps_dir = Config.get("steps_dir")
66 self.steps = b.load_sync_step_modules(steps_dir)
Scott Bakerc2fddaa2019-01-30 15:45:03 -080067 self.synchronizer = xossynchronizer.event_loop.XOSObserver(self.steps, model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080068
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 Bakerbba67b62019-01-28 17:38:21 -080086 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 Baker4839dec2019-02-27 16:50:37 -080093 slice = Slice()
94 site1 = Site()
95 site2 = Site()
96 slice.site = site2
97 verdict, _ = self.synchronizer.concrete_path_exists(slice, site1)
Scott Bakerbba67b62019-01-28 17:38:21 -080098 self.assertFalse(verdict)
99
100 def test_concrete_object_path_adjacent(self):
Scott Baker4839dec2019-02-27 16:50:37 -0800101 slice = Slice()
102 site = Site()
103 slice.site = site
104 verdict, edge_type = self.synchronizer.concrete_path_exists(slice, site)
Scott Bakerbba67b62019-01-28 17:38:21 -0800105
106 self.assertTrue(verdict)
107 self.assertEqual(edge_type, event_loop.DIRECT_EDGE)
108
Scott Bakerbba67b62019-01-28 17:38:21 -0800109 def test_concrete_object_path_distant(self):
Scott Baker4839dec2019-02-27 16:50:37 -0800110 p = ComputeServiceInstance()
Scott Bakerbba67b62019-01-28 17:38:21 -0800111 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 Bakerbba67b62019-01-28 17:38:21 -0800118 def test_cohorting_independent(self):
119 i = Image()
120
121 p = Slice()
Scott Baker4839dec2019-02-27 16:50:37 -0800122 c = Site()
Scott Bakerbba67b62019-01-28 17:38:21 -0800123
124 cohorts = self.synchronizer.compute_dependent_cohorts([i, p, c], False)
125 self.assertEqual(len(cohorts), 3)
126
Scott Bakerbba67b62019-01-28 17:38:21 -0800127if __name__ == "__main__":
128 unittest.main()