Merge from OSM SO master
[osm/SO.git] / rwlaunchpad / test / utest_ro_account.py
1
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
18 import asyncio
19 import sys
20 import types
21 import unittest
22 import uuid
23
24 import rift.test.dts
25 import rift.tasklets.rwnsmtasklet.cloud as cloud
26 import rift.tasklets.rwnsmtasklet.openmano_nsm as openmano_nsm
27 from rift.mano.utils.project import ManoProject
28 import rw_peas
29
30 import gi
31 gi.require_version('RwDts', '1.0')
32 from gi.repository import (
33 RwLaunchpadYang as launchpadyang,
34 RwDts as rwdts,
35 RwProjectVnfdYang as RwVnfdYang,
36 RwVnfrYang,
37 RwNsrYang,
38 RwProjectNsdYang as RwNsdYang,
39 VnfrYang,
40 )
41
42
43 class 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
85 class 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)
98 self.project = ManoProject(self.log)
99
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):
110 orch = cloud.ROAccountPluginSelector(self.dts, self.log, self.loop, self.project, None)
111
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
119 # Test rift-ro plugin CREATE
120 w_xpath = self.project.add_project("C,/rw-launchpad:resource-orchestrator")
121 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
127 # Test Openmano plugin CREATE
128 mock_orch_acc = launchpadyang.ResourceOrchestrator.from_dict(
129 {'name': 'openmano',
130 'account_type': 'openmano',
131 'openmano': {'tenant_id': "abc",
132 "port": 9999,
133 "host": "10.64.11.77"}})
134 yield from self.publisher.publish(w_xpath, xpath, mock_orch_acc)
135 yield from asyncio.sleep(5, loop=self.loop)
136
137 assert type(orch.ro_plugin) is openmano_nsm.OpenmanoNsPlugin
138 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"
144 yield from self.dts.query_update(w_xpath,
145 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):
155 yield from self.dts.query_update(w_xpath,
156 rwdts.XactFlag.ADVISE, mock_orch_acc)
157
158 # Test delete
159 yield from self.dts.query_delete(w_xpath,
160 flags=rwdts.XactFlag.ADVISE)
161 assert orch.ro_plugin == None
162
163
164 def 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
174 if __name__ == '__main__':
175 main()