blob: 2ac784ad9c87e34b68a167ee951666c5ea701274 [file] [log] [blame]
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -04001
2#
3# Copyright 2016 RIFT.IO Inc
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import asyncio
19import sys
20import types
21import unittest
22import uuid
23
24import rift.test.dts
25import rift.tasklets.rwnsmtasklet.cloud as cloud
26import rift.tasklets.rwnsmtasklet.openmano_nsm as openmano_nsm
Philip Josephba63fbf2017-04-04 15:46:10 +053027from rift.mano.utils.project import ManoProject
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040028import rw_peas
29
30import gi
Philip Josephba63fbf2017-04-04 15:46:10 +053031gi.require_version('RwDts', '1.0')
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040032from gi.repository import (
33 RwLaunchpadYang as launchpadyang,
34 RwDts as rwdts,
Philip Joseph4f810f22017-03-07 23:09:10 +053035 RwProjectVnfdYang as RwVnfdYang,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040036 RwVnfrYang,
37 RwNsrYang,
Philip Joseph4f810f22017-03-07 23:09:10 +053038 RwProjectNsdYang as RwNsdYang,
Philip Josephba63fbf2017-04-04 15:46:10 +053039 VnfrYang,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040040 )
41
42
43class DescriptorPublisher(object):
44 def __init__(self, log, dts, loop):
45 self.log = log
46 self.loop = loop
47 self.dts = dts
48
49 self._registrations = []
50
51 @asyncio.coroutine
52 def publish(self, w_path, path, desc):
53 ready_event = asyncio.Event(loop=self.loop)
54
55 @asyncio.coroutine
56 def on_ready(regh, status):
57 self.log.debug("Create element: %s, obj-type:%s obj:%s",
58 path, type(desc), desc)
59 with self.dts.transaction() as xact:
60 regh.create_element(path, desc, xact.xact)
61 self.log.debug("Created element: %s, obj:%s", path, desc)
62 ready_event.set()
63
64 handler = rift.tasklets.DTS.RegistrationHandler(
65 on_ready=on_ready
66 )
67
68 self.log.debug("Registering path: %s, obj:%s", w_path, desc)
69 reg = yield from self.dts.register(
70 w_path,
71 handler,
72 flags=rwdts.Flag.PUBLISHER | rwdts.Flag.NO_PREP_READ
73 )
74 self._registrations.append(reg)
75 self.log.debug("Registered path : %s", w_path)
76 yield from ready_event.wait()
77
78 return reg
79
80 def unpublish_all(self):
81 self.log.debug("Deregistering all published descriptors")
82 for reg in self._registrations:
83 reg.deregister()
84
85class RoAccountDtsTestCase(rift.test.dts.AbstractDTSTest):
86 @classmethod
87 def configure_schema(cls):
88 return launchpadyang.get_schema()
89
90 @classmethod
91 def configure_timeout(cls):
92 return 240
93
94 def configure_test(self, loop, test_id):
95 self.log.debug("STARTING - %s", test_id)
96 self.tinfo = self.new_tinfo(str(test_id))
97 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
Philip Josephba63fbf2017-04-04 15:46:10 +053098 self.project = ManoProject(self.log)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -040099
100 self.tinfo_sub = self.new_tinfo(str(test_id) + "_sub")
101 self.dts_sub = rift.tasklets.DTS(self.tinfo_sub, self.schema, self.loop)
102
103 self.publisher = DescriptorPublisher(self.log, self.dts, self.loop)
104
105 def tearDown(self):
106 super().tearDown()
107
108 @rift.test.dts.async_test
109 def test_orch_account_create(self):
Philip Josephba63fbf2017-04-04 15:46:10 +0530110 orch = cloud.ROAccountPluginSelector(self.dts, self.log, self.loop, self.project, None)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400111
112 yield from orch.register()
113
114 # Test if we have a default plugin in case no RO is specified.
115 assert type(orch.ro_plugin) is cloud.RwNsPlugin
116 mock_orch_acc = launchpadyang.ResourceOrchestrator.from_dict(
117 {'name': 'rift-ro', 'account_type': 'rift_ro', 'rift_ro': {'rift_ro': True}})
118
Varun Prasadbe1e6292016-10-04 04:41:07 -0400119 # Test rift-ro plugin CREATE
Philip Josephba63fbf2017-04-04 15:46:10 +0530120 w_xpath = self.project.add_project("C,/rw-launchpad:resource-orchestrator")
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400121 xpath = w_xpath
122 yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc)
123 yield from asyncio.sleep(5, loop=self.loop)
124
125 assert type(orch.ro_plugin) is cloud.RwNsPlugin
126
Varun Prasadbe1e6292016-10-04 04:41:07 -0400127 # Test Openmano plugin CREATE
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400128 mock_orch_acc = launchpadyang.ResourceOrchestrator.from_dict(
129 {'name': 'openmano',
130 'account_type': 'openmano',
Varun Prasadbe1e6292016-10-04 04:41:07 -0400131 'openmano': {'tenant_id': "abc",
132 "port": 9999,
133 "host": "10.64.11.77"}})
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400134 yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc)
135 yield from asyncio.sleep(5, loop=self.loop)
136
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400137 assert type(orch.ro_plugin) is openmano_nsm.OpenmanoNsPlugin
Varun Prasadbe1e6292016-10-04 04:41:07 -0400138 assert orch.ro_plugin._cli_api._port == mock_orch_acc.openmano.port
139 assert orch.ro_plugin._cli_api._host == mock_orch_acc.openmano.host
140
141 # Test update
142 mock_orch_acc.openmano.port = 9789
143 mock_orch_acc.openmano.host = "10.64.11.78"
Philip Josephba63fbf2017-04-04 15:46:10 +0530144 yield from self.dts.query_update(w_xpath,
Varun Prasadbe1e6292016-10-04 04:41:07 -0400145 rwdts.XactFlag.ADVISE, mock_orch_acc)
146 assert orch.ro_plugin._cli_api._port == mock_orch_acc.openmano.port
147 assert orch.ro_plugin._cli_api._host == mock_orch_acc.openmano.host
148
149 # Test update when a live instance exists
150 # Exception should be thrown
151 orch.handle_nsr(None, rwdts.QueryAction.CREATE)
152 mock_orch_acc.openmano.port = 9788
153
154 with self.assertRaises(Exception):
Philip Josephba63fbf2017-04-04 15:46:10 +0530155 yield from self.dts.query_update(w_xpath,
Varun Prasadbe1e6292016-10-04 04:41:07 -0400156 rwdts.XactFlag.ADVISE, mock_orch_acc)
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400157
158 # Test delete
Philip Josephba63fbf2017-04-04 15:46:10 +0530159 yield from self.dts.query_delete(w_xpath,
Jeremy Mordkoff6f07e6f2016-09-07 18:56:51 -0400160 flags=rwdts.XactFlag.ADVISE)
161 assert orch.ro_plugin == None
162
163
164def main(argv=sys.argv[1:]):
165
166 # The unittest framework requires a program name, so use the name of this
167 # file instead (we do not want to have to pass a fake program name to main
168 # when this is called from the interpreter).
169 unittest.main(
170 argv=[__file__] + argv,
171 testRunner=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
172 )
173
174if __name__ == '__main__':
Philip Joseph4f810f22017-03-07 23:09:10 +0530175 main()