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