blob: fe4f9c1c5fcab24fc7c326d9962ec8e1a60258f4 [file] [log] [blame]
Scott Baker5f38c7b2017-08-18 10:12:49 -07001# 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 Williams045b63d2019-01-22 16:30:57 -070015from __future__ import print_function
Scott Baker5f38c7b2017-08-18 10:12:49 -070016import exceptions
Scott Baker7dddd512017-10-24 10:13:34 -070017import os
Scott Baker5f38c7b2017-08-18 10:12:49 -070018import random
Scott Baker5f38c7b2017-08-18 10:12:49 -070019import string
20import sys
21import 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 Baker7dddd512017-10-24 10:13:34 -070028# by default, use fake stub rather than real core
Zack Williams045b63d2019-01-22 16:30:57 -070029USE_FAKE_STUB = True
Scott Baker7dddd512017-10-24 10:13:34 -070030
Zack Williams045b63d2019-01-22 16:30:57 -070031PARENT_DIR = os.path.join(os.path.dirname(__file__), "..")
32
Scott Baker5f38c7b2017-08-18 10:12:49 -070033
34class TestWrappers(unittest.TestCase):
Scott Baker7dddd512017-10-24 10:13:34 -070035 def setUp(self):
Scott Bakere5f0f682018-06-18 10:21:35 -070036 from xosconfig import Config
Zack Williams045b63d2019-01-22 16:30:57 -070037
Scott Bakere5f0f682018-06-18 10:21:35 -070038 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 Williams045b63d2019-01-22 16:30:57 -070041 Config.init(config, "synchronizer-config-schema.yaml")
Scott Bakere5f0f682018-06-18 10:21:35 -070042
Zack Williams045b63d2019-01-22 16:30:57 -070043 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070044 sys.path.append(PARENT_DIR)
45
46 def tearDown(self):
Zack Williams045b63d2019-01-22 16:30:57 -070047 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070048 sys.path.remove(PARENT_DIR)
49
Scott Baker5f38c7b2017-08-18 10:12:49 -070050 def make_coreapi(self):
51 if USE_FAKE_STUB:
Scott Baker7dddd512017-10-24 10:13:34 -070052 import xosapi.orm
Scott Bakerb96ba432018-02-26 09:53:48 -080053 from fake_stub import FakeStub, FakeObj, FakeProtos
Scott Baker7dddd512017-10-24 10:13:34 -070054
Scott Baker5e4fc082018-05-24 09:48:56 -070055 xosapi.orm.import_convenience_methods()
56
Scott Baker5f38c7b2017-08-18 10:12:49 -070057 stub = FakeStub()
Zack Williams045b63d2019-01-22 16:30:57 -070058 api = xosapi.orm.ORMStub(
59 stub=stub,
60 package_name="xos",
61 protos=FakeProtos(),
62 empty=FakeObj,
63 enable_backoff=False,
64 )
Scott Baker5f38c7b2017-08-18 10:12:49 -070065 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 Williams045b63d2019-01-22 16:30:57 -070073 controller = orm.Controller(name="test_controller", deployment_id=deployment.id)
Scott Baker5f38c7b2017-08-18 10:12:49 -070074 controller.save()
75 site = orm.Site(name="testsite")
76 site.save()
Zack Williams045b63d2019-01-22 16:30:57 -070077 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 Baker5f38c7b2017-08-18 10:12:49 -070084 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 Williams045b63d2019-01-22 16:30:57 -070089 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 Baker5f38c7b2017-08-18 10:12:49 -070096 slice_one.save()
Zack Williams045b63d2019-01-22 16:30:57 -070097 network_one = orm.Network(
98 name="testsite_sliceone_access",
99 owner_id=slice_one.id,
100 template_id=vsg_access_template.id,
101 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700102 network_one.save()
Zack Williams045b63d2019-01-22 16:30:57 -0700103 ns = orm.NetworkSlice(slice_id=slice_one.id, network_id=network_one.id)
Scott Baker5f38c7b2017-08-18 10:12:49 -0700104 ns.save()
Zack Williams045b63d2019-01-22 16:30:57 -0700105 cn_one = orm.ControllerNetwork(
106 network_id=network_one.id, controller_id=controller.id
107 )
Scott Baker5f38c7b2017-08-18 10:12:49 -0700108 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 Baker8f36c3f2017-09-26 16:44:36 -0700123 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 Williams045b63d2019-01-22 16:30:57 -0700127 controller = orm.Controller(name="test_controller", deployment_id=deployment.id)
Scott Baker8f36c3f2017-09-26 16:44:36 -0700128 controller.save()
129 site = orm.Site(name="testsite")
130 site.save()
Zack Williams045b63d2019-01-22 16:30:57 -0700131 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 Baker8f36c3f2017-09-26 16:44:36 -0700138 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 Williams045b63d2019-01-22 16:30:57 -0700144 self.assertEqual(
145 service_one.get_service_instance_class_name(), "ServiceInstance"
146 )
Scott Baker8f36c3f2017-09-26 16:44:36 -0700147
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 Williams045b63d2019-01-22 16:30:57 -0700157 email="fake_"
158 + "".join(
159 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
160 ),
161 site_id=site.id,
162 )
Scott Baker8f36c3f2017-09-26 16:44:36 -0700163 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 Williams045b63d2019-01-22 16:30:57 -0700169 self.assertEqual(
170 service_one.get_service_instance_class().model_name, "ServiceInstance"
171 )
Scott Baker8f36c3f2017-09-26 16:44:36 -0700172
Scott Baker2f314d52018-08-24 08:31:19 -0700173 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 Williams045b63d2019-01-22 16:30:57 -0700189
Scott Baker7dddd512017-10-24 10:13:34 -0700190def main():
191 global USE_FAKE_STUB
192 global xos_grpc_client
Scott Baker5f38c7b2017-08-18 10:12:49 -0700193
Scott Baker7dddd512017-10-24 10:13:34 -0700194 # Command-line argument of -R will cause this test to use a real grpc server
195 # rather than the fake stub.
Scott Baker5f38c7b2017-08-18 10:12:49 -0700196
Scott Baker7dddd512017-10-24 10:13:34 -0700197 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 Baker5f38c7b2017-08-18 10:12:49 -0700201
Scott Baker7dddd512017-10-24 10:13:34 -0700202 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 Baker5f38c7b2017-08-18 10:12:49 -0700207
Scott Baker7dddd512017-10-24 10:13:34 -0700208 from xosapi import xos_grpc_client
Scott Baker5f38c7b2017-08-18 10:12:49 -0700209
Zack Williams045b63d2019-01-22 16:30:57 -0700210 print("Using xos-client library and core server")
Scott Baker5f38c7b2017-08-18 10:12:49 -0700211
Scott Baker7dddd512017-10-24 10:13:34 -0700212 def test_callback():
213 try:
Zack Williams045b63d2019-01-22 16:30:57 -0700214 sys.argv = sys.argv[
215 :1
216 ] # unittest does not like xos_grpc_client's command line arguments (TODO: find a cooperative approach)
Scott Baker7dddd512017-10-24 10:13:34 -0700217 unittest.main()
Zack Williams045b63d2019-01-22 16:30:57 -0700218 except exceptions.SystemExit as e:
Scott Baker7dddd512017-10-24 10:13:34 -0700219 global exitStatus
220 exitStatus = e.code
Scott Baker5f38c7b2017-08-18 10:12:49 -0700221
Scott Baker7dddd512017-10-24 10:13:34 -0700222 xos_grpc_client.start_api_parseargs(test_callback)
Scott Baker5f38c7b2017-08-18 10:12:49 -0700223
Scott Baker7dddd512017-10-24 10:13:34 -0700224 sys.exit(exitStatus)
Scott Baker5f38c7b2017-08-18 10:12:49 -0700225
Zack Williams045b63d2019-01-22 16:30:57 -0700226
Scott Baker7dddd512017-10-24 10:13:34 -0700227if __name__ == "__main__":
228 main()