3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
24 gi
.require_version('RwDtsYang', '1.0')
25 gi
.require_version('RwPkgMgmtYang', '1.0')
26 from gi
.repository
import (
30 import rift
.tasklets
.rwpkgmgr
.subscriber
as pkg_subscriber
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
):
41 self
._registrations
= []
44 def publish(self
, w_path
, path
, desc
):
45 ready_event
= asyncio
.Event(loop
=self
.loop
)
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
)
56 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
60 self
.log
.debug("Registering path: %s, obj:%s", w_path
, desc
)
61 reg
= yield from self
.dts
.register(
64 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
66 self
._registrations
.append(reg
)
67 self
.log
.debug("Registered path : %s", w_path
)
68 yield from ready_event
.wait()
72 def unpublish_all(self
):
73 self
.log
.debug("Deregistering all published descriptors")
74 for reg
in self
._registrations
:
77 class SubscriberStoreDtsTestCase(rift
.test
.dts
.AbstractDTSTest
):
79 def configure_schema(cls
):
80 return RwPkgMgmtYang
.get_schema()
83 def configure_timeout(cls
):
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
)
95 @rift.test
.dts
.async_test
96 def test_download_status_handler(self
):
98 mock_msg
= RwPkgMgmtYang
.DownloadJob
.from_dict({
99 "url": "http://foo/bar",
101 "download_id": str(uuid
.uuid4())})
103 w_xpath
= "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job"
104 xpath
= "{}[download-id='{}']".format(w_xpath
, mock_msg
.download_id
)
107 def mock_cb(msg
, status
):
109 assert msg
== mock_msg
112 sub
= pkg_subscriber
.DownloadStatusSubscriber(
118 yield from sub
.register()
119 yield from asyncio
.sleep(1, loop
=self
.loop
)
121 yield from self
.publisher
.publish(w_xpath
, xpath
, mock_msg
)
122 yield from asyncio
.sleep(1, loop
=self
.loop
)
124 assert mock_called
is True
127 def main(argv
=sys
.argv
[1:]):
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).
133 argv
=[__file__
] + argv
,
134 testRunner
=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
137 if __name__
== '__main__':