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 json |
| 17 | import unittest |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 18 | import mock |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 23 | try: |
| 24 | # Python 2: "reload" is built-in |
| 25 | reload # pylint: disable=reload-builtin |
| 26 | except NameError: |
| 27 | from importlib import reload |
| 28 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 29 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 30 | sync_lib_dir = os.path.join(test_path, "..", "xossynchronizer") |
| 31 | xos_dir = os.path.join(test_path, "..", "..", "..", "xos") |
| 32 | |
| 33 | ANSIBLE_FILE = "/tmp/payload_test" |
| 34 | |
| 35 | |
| 36 | def run_fake_ansible_template(*args, **kwargs): |
| 37 | opts = args[1] |
| 38 | open(ANSIBLE_FILE, "w").write(json.dumps(opts)) |
| 39 | |
| 40 | |
| 41 | def get_ansible_output(): |
| 42 | ansible_str = open(ANSIBLE_FILE).read() |
| 43 | return json.loads(ansible_str) |
| 44 | |
| 45 | |
| 46 | class TestRun(unittest.TestCase): |
| 47 | def setUp(self): |
| 48 | self.sys_path_save = sys.path |
| 49 | self.cwd_save = os.getcwd() |
| 50 | |
| 51 | config = os.path.join(test_path, "test_config.yaml") |
| 52 | from xosconfig import Config |
| 53 | |
| 54 | Config.clear() |
| 55 | Config.init(config, "synchronizer-config-schema.yaml") |
| 56 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 57 | from xossynchronizer.mock_modelaccessor_build import build_mock_modelaccessor |
| 58 | |
| 59 | build_mock_modelaccessor( |
| 60 | sync_lib_dir, xos_dir, services_dir=None, service_xprotos=[] |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 61 | ) |
| 62 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 63 | os.chdir( |
| 64 | os.path.join(test_path, "..") |
| 65 | ) # config references xos-synchronizer-tests/model-deps |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 66 | |
| 67 | import xossynchronizer.event_loop |
| 68 | |
| 69 | reload(xossynchronizer.event_loop) |
| 70 | import xossynchronizer.backend |
| 71 | |
| 72 | reload(xossynchronizer.backend) |
| 73 | from xossynchronizer.modelaccessor import model_accessor |
| 74 | |
| 75 | # import all class names to globals |
| 76 | for (k, v) in model_accessor.all_model_classes.items(): |
| 77 | globals()[k] = v |
| 78 | |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 79 | from xossynchronizer.modelaccessor import model_accessor |
| 80 | |
| 81 | b = xossynchronizer.backend.Backend(model_accessor=model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 82 | steps_dir = Config.get("steps_dir") |
| 83 | self.steps = b.load_sync_step_modules(steps_dir) |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 84 | self.synchronizer = xossynchronizer.event_loop.XOSObserver( |
| 85 | self.steps, model_accessor |
| 86 | ) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 87 | try: |
| 88 | os.remove("/tmp/sync_ports") |
| 89 | except OSError: |
| 90 | pass |
| 91 | try: |
| 92 | os.remove("/tmp/delete_ports") |
| 93 | except OSError: |
| 94 | pass |
| 95 | |
| 96 | def tearDown(self): |
| 97 | sys.path = self.sys_path_save |
| 98 | os.chdir(self.cwd_save) |
| 99 | |
Scott Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 100 | def test_run_once(self): |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 101 | pending_objects, pending_steps = self.synchronizer.fetch_pending() |
| 102 | pending_objects2 = list(pending_objects) |
| 103 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 104 | slice = Slice() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 105 | |
| 106 | self.synchronizer.run_once() |
| 107 | |
| 108 | sync_ports = open("/tmp/sync_ports").read() |
| 109 | delete_ports = open("/tmp/delete_ports").read() |
| 110 | |
| 111 | self.assertIn("successful", sync_ports) |
| 112 | self.assertIn("successful", delete_ports) |
| 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | unittest.main() |