Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [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 | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 16 | import exceptions |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 17 | import os |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 18 | import random |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 19 | import string |
| 20 | import sys |
| 21 | import unittest |
| 22 | |
| 23 | # Command-line argument of -R will cause this test to use a real grpc server |
| 24 | # rather than the fake stub. |
| 25 | |
| 26 | # TODO: Investigate writing wrapper unit tests using mocks rather than using the ORM test framework |
| 27 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 28 | # by default, use fake stub rather than real core |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 29 | USE_FAKE_STUB = True |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 30 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | PARENT_DIR = os.path.join(os.path.dirname(__file__), "..") |
| 32 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 33 | |
| 34 | class TestWrappers(unittest.TestCase): |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 35 | def setUp(self): |
Scott Baker | e5f0f68 | 2018-06-18 10:21:35 -0700 | [diff] [blame] | 36 | from xosconfig import Config |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 37 | |
Scott Baker | e5f0f68 | 2018-06-18 10:21:35 -0700 | [diff] [blame] | 38 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 39 | config = os.path.join(test_path, "test_config.yaml") |
| 40 | Config.clear() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 41 | Config.init(config, "synchronizer-config-schema.yaml") |
Scott Baker | e5f0f68 | 2018-06-18 10:21:35 -0700 | [diff] [blame] | 42 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 44 | sys.path.append(PARENT_DIR) |
| 45 | |
| 46 | def tearDown(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 48 | sys.path.remove(PARENT_DIR) |
| 49 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 50 | def make_coreapi(self): |
| 51 | if USE_FAKE_STUB: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 52 | import xosapi.orm |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 53 | from fake_stub import FakeStub, FakeObj, FakeProtos |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 54 | |
Scott Baker | 5e4fc08 | 2018-05-24 09:48:56 -0700 | [diff] [blame] | 55 | xosapi.orm.import_convenience_methods() |
| 56 | |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 57 | stub = FakeStub() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 58 | api = xosapi.orm.ORMStub( |
| 59 | stub=stub, |
| 60 | package_name="xos", |
| 61 | protos=FakeProtos(), |
| 62 | empty=FakeObj, |
| 63 | enable_backoff=False, |
| 64 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 65 | return api |
| 66 | else: |
| 67 | return xos_grpc_client.coreapi |
| 68 | |
| 69 | def test_service_get_composable_networks(self): |
| 70 | orm = self.make_coreapi() |
| 71 | deployment = orm.Deployment(name="test_deployment") |
| 72 | deployment.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 73 | controller = orm.Controller(name="test_controller", deployment_id=deployment.id) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 74 | controller.save() |
| 75 | site = orm.Site(name="testsite") |
| 76 | site.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | user = orm.User( |
| 78 | email="fake_" |
| 79 | + "".join( |
| 80 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 81 | ), |
| 82 | site_id=site.id, |
| 83 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 84 | user.save() |
| 85 | vsg_access_template = orm.NetworkTemplate(name="vsg_access", vtn_kind="VSG") |
| 86 | vsg_access_template.save() |
| 87 | service_one = orm.Service(name="service_one") |
| 88 | service_one.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 89 | slice_one = orm.Slice( |
| 90 | name="testsite_sliceone", |
| 91 | service_id=service_one.id, |
| 92 | site_id=site.id, |
| 93 | creator_id=user.id, |
| 94 | network="noauto", |
| 95 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 96 | slice_one.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | network_one = orm.Network( |
| 98 | name="testsite_sliceone_access", |
| 99 | owner_id=slice_one.id, |
| 100 | template_id=vsg_access_template.id, |
| 101 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 102 | network_one.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 103 | ns = orm.NetworkSlice(slice_id=slice_one.id, network_id=network_one.id) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 104 | ns.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 105 | cn_one = orm.ControllerNetwork( |
| 106 | network_id=network_one.id, controller_id=controller.id |
| 107 | ) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 108 | cn_one.save() |
| 109 | |
| 110 | if USE_FAKE_STUB: |
| 111 | # fake_Stub doesn't handle reverse foreign keys |
| 112 | service_one.slices_ids = [slice_one.id] |
| 113 | slice_one.networks_ids = [network_one.id] |
| 114 | network_one.controllernetworks_ids = [cn_one.id] |
| 115 | |
| 116 | # make sure we're using a fresh copy of the object, with all its foreign keys filled in |
| 117 | service_one = orm.Service.objects.get(id=service_one.id) |
| 118 | |
| 119 | cns = service_one.get_composable_networks() |
| 120 | self.assertEqual(len(cns), 1) |
| 121 | self.assertEqual(cns[0].id, network_one.id) |
| 122 | |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 123 | def test_service_get_service_instance_class_name(self): |
| 124 | orm = self.make_coreapi() |
| 125 | deployment = orm.Deployment(name="test_deployment") |
| 126 | deployment.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 127 | controller = orm.Controller(name="test_controller", deployment_id=deployment.id) |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 128 | controller.save() |
| 129 | site = orm.Site(name="testsite") |
| 130 | site.save() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 131 | user = orm.User( |
| 132 | email="fake_" |
| 133 | + "".join( |
| 134 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 135 | ), |
| 136 | site_id=site.id, |
| 137 | ) |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 138 | user.save() |
| 139 | vsg_access_template = orm.NetworkTemplate(name="vsg_access", vtn_kind="VSG") |
| 140 | vsg_access_template.save() |
| 141 | service_one = orm.Service(name="service_one") |
| 142 | service_one.save() |
| 143 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 144 | self.assertEqual( |
| 145 | service_one.get_service_instance_class_name(), "ServiceInstance" |
| 146 | ) |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 147 | |
| 148 | def test_service_get_service_instance_class(self): |
| 149 | orm = self.make_coreapi() |
| 150 | deployment = orm.Deployment(name="test_deployment") |
| 151 | deployment.save() |
| 152 | controller = orm.Controller(name="test_controller", deployment_id=deployment.id) |
| 153 | controller.save() |
| 154 | site = orm.Site(name="testsite") |
| 155 | site.save() |
| 156 | user = orm.User( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 157 | email="fake_" |
| 158 | + "".join( |
| 159 | random.choice(string.ascii_uppercase + string.digits) for _ in range(10) |
| 160 | ), |
| 161 | site_id=site.id, |
| 162 | ) |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 163 | user.save() |
| 164 | vsg_access_template = orm.NetworkTemplate(name="vsg_access", vtn_kind="VSG") |
| 165 | vsg_access_template.save() |
| 166 | service_one = orm.Service(name="service_one") |
| 167 | service_one.save() |
| 168 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 169 | self.assertEqual( |
| 170 | service_one.get_service_instance_class().model_name, "ServiceInstance" |
| 171 | ) |
Scott Baker | 8f36c3f | 2017-09-26 16:44:36 -0700 | [diff] [blame] | 172 | |
Scott Baker | 2f314d5 | 2018-08-24 08:31:19 -0700 | [diff] [blame] | 173 | def test_wrapper_from__class__dot_name(self): |
| 174 | """ The Service model has a wrapper, so it should be returned when make_ORMWrapper looks for a wrapper based |
| 175 | on the class name. |
| 176 | """ |
| 177 | orm = self.make_coreapi() |
| 178 | obj = orm.Service() |
| 179 | self.assertEqual(obj.__class__.__name__, "ORMWrapperService") |
| 180 | |
| 181 | def test_wrapper_from_class_names(self): |
| 182 | """ ONOSService._wrapped_class.class_names is "ONOSService, Service" so we should fall back to getting the |
| 183 | Service wrapper. |
| 184 | """ |
| 185 | orm = self.make_coreapi() |
| 186 | obj = orm.ONOSService() |
| 187 | self.assertEqual(obj.__class__.__name__, "ORMWrapperService") |
| 188 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 189 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 190 | def main(): |
| 191 | global USE_FAKE_STUB |
| 192 | global xos_grpc_client |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 193 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 194 | # Command-line argument of -R will cause this test to use a real grpc server |
| 195 | # rather than the fake stub. |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 196 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 197 | if "-R" in sys.argv: |
| 198 | USE_FAKE_STUB = False |
| 199 | sys.argv.remove("-R") |
| 200 | # Note: will leave lots of litter (users, sites, etc) behind in the database |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 201 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 202 | if USE_FAKE_STUB: |
| 203 | unittest.main() |
| 204 | else: |
| 205 | # This assumes xos-client python library is installed, and a gRPC server |
| 206 | # is available. |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 207 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 208 | from xosapi import xos_grpc_client |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 209 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 210 | print("Using xos-client library and core server") |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 211 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 212 | def test_callback(): |
| 213 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 214 | sys.argv = sys.argv[ |
| 215 | :1 |
| 216 | ] # unittest does not like xos_grpc_client's command line arguments (TODO: find a cooperative approach) |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 217 | unittest.main() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 218 | except exceptions.SystemExit as e: |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 219 | global exitStatus |
| 220 | exitStatus = e.code |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 221 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 222 | xos_grpc_client.start_api_parseargs(test_callback) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 223 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 224 | sys.exit(exitStatus) |
Scott Baker | 5f38c7b | 2017-08-18 10:12:49 -0700 | [diff] [blame] | 225 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 226 | |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 227 | if __name__ == "__main__": |
| 228 | main() |