3b377c7b4cd5659f717c754ca1a2b46315baeaab
[osm/SO.git] / rwlaunchpad / plugins / rwpkgmgr / test / utest_subscriber_dts.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 unittest
21 import uuid
22
23 import gi
24 gi.require_version('RwDtsYang', '1.0')
25 gi.require_version('RwPkgMgmtYang', '1.0')
26 from gi.repository import (
27 RwPkgMgmtYang,
28 RwDts as rwdts,
29 )
30 import rift.tasklets.rwpkgmgr.subscriber as pkg_subscriber
31 import rift.test.dts
32
33
34 class DescriptorPublisher(object):
35 # TODO: Need to be moved to a central page, too many copy pastes
36 def __init__(self, log, dts, loop):
37 self.log = log
38 self.loop = loop
39 self.dts = dts
40
41 self._registrations = []
42
43 @asyncio.coroutine
44 def publish(self, w_path, path, desc):
45 ready_event = asyncio.Event(loop=self.loop)
46
47 @asyncio.coroutine
48 def on_ready(regh, status):
49 self.log.debug("Create element: %s, obj-type:%s obj:%s",
50 path, type(desc), desc)
51 with self.dts.transaction() as xact:
52 regh.create_element(path, desc, xact.xact)
53 self.log.debug("Created element: %s, obj:%s", path, desc)
54 ready_event.set()
55
56 handler = rift.tasklets.DTS.RegistrationHandler(
57 on_ready=on_ready
58 )
59
60 self.log.debug("Registering path: %s, obj:%s", w_path, desc)
61 reg = yield from self.dts.register(
62 w_path,
63 handler,
64 flags=rwdts.Flag.PUBLISHER | rwdts.Flag.NO_PREP_READ
65 )
66 self._registrations.append(reg)
67 self.log.debug("Registered path : %s", w_path)
68 yield from ready_event.wait()
69
70 return reg
71
72 def unpublish_all(self):
73 self.log.debug("Deregistering all published descriptors")
74 for reg in self._registrations:
75 reg.deregister()
76
77 class SubscriberStoreDtsTestCase(rift.test.dts.AbstractDTSTest):
78 @classmethod
79 def configure_schema(cls):
80 return RwPkgMgmtYang.get_schema()
81
82 @classmethod
83 def configure_timeout(cls):
84 return 240
85
86 def configure_test(self, loop, test_id):
87 self.log.debug("STARTING - %s", test_id)
88 self.tinfo = self.new_tinfo(str(test_id))
89 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
90 self.publisher = DescriptorPublisher(self.log, self.dts, self.loop)
91
92 def tearDown(self):
93 super().tearDown()
94
95 @rift.test.dts.async_test
96 def test_download_status_handler(self):
97
98 mock_msg = RwPkgMgmtYang.DownloadJob.from_dict({
99 "url": "http://foo/bar",
100 "package_id": "123",
101 "download_id": str(uuid.uuid4())})
102
103 w_xpath = "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job"
104 xpath = "{}[download-id='{}']".format(w_xpath, mock_msg.download_id)
105
106 mock_called = False
107 def mock_cb(msg, status):
108 nonlocal mock_called
109 assert msg == mock_msg
110 mock_called = True
111
112 sub = pkg_subscriber.DownloadStatusSubscriber(
113 self.log,
114 self.dts,
115 self.loop,
116 callback=mock_cb)
117
118 yield from sub.register()
119 yield from asyncio.sleep(1, loop=self.loop)
120
121 yield from self.publisher.publish(w_xpath, xpath, mock_msg)
122 yield from asyncio.sleep(1, loop=self.loop)
123
124 assert mock_called is True
125
126
127 def main(argv=sys.argv[1:]):
128
129 # The unittest framework requires a program name, so use the name of this
130 # file instead (we do not want to have to pass a fake program name to main
131 # when this is called from the interpreter).
132 unittest.main(
133 argv=[__file__] + argv,
134 testRunner=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
135 )
136
137 if __name__ == '__main__':
138 main()