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('RwDts', '1.0')
25 gi
.require_version('RwPkgMgmtYang', '1.0')
26 from gi
.repository
import (
30 import rift
.tasklets
.rwpkgmgr
.subscriber
as pkg_subscriber
32 from rift
.mano
.utils
.project
import ManoProject
, DEFAULT_PROJECT
35 class DescriptorPublisher(object):
36 # TODO: Need to be moved to a central page, too many copy pastes
37 def __init__(self
, log
, dts
, loop
):
42 self
._registrations
= []
45 def publish(self
, w_path
, path
, desc
):
46 ready_event
= asyncio
.Event(loop
=self
.loop
)
49 def on_ready(regh
, status
):
50 self
.log
.debug("Create element: %s, obj-type:%s obj:%s",
51 path
, type(desc
), desc
)
52 with self
.dts
.transaction() as xact
:
53 regh
.create_element(path
, desc
, xact
.xact
)
54 self
.log
.debug("Created element: %s, obj:%s", path
, desc
)
57 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
61 self
.log
.debug("Registering path: %s, obj:%s", w_path
, desc
)
62 reg
= yield from self
.dts
.register(
65 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
67 self
._registrations
.append(reg
)
68 self
.log
.debug("Registered path : %s", w_path
)
69 yield from ready_event
.wait()
73 def unpublish_all(self
):
74 self
.log
.debug("Deregistering all published descriptors")
75 for reg
in self
._registrations
:
78 class SubscriberStoreDtsTestCase(rift
.test
.dts
.AbstractDTSTest
):
80 def configure_schema(cls
):
81 return RwPkgMgmtYang
.get_schema()
84 def configure_timeout(cls
):
87 def configure_test(self
, loop
, test_id
):
88 self
.log
.debug("STARTING - %s", test_id
)
89 self
.tinfo
= self
.new_tinfo(str(test_id
))
90 self
.dts
= rift
.tasklets
.DTS(self
.tinfo
, self
.schema
, self
.loop
)
91 self
.publisher
= DescriptorPublisher(self
.log
, self
.dts
, self
.loop
)
92 self
.project
= ManoProject(self
.log
, name
=DEFAULT_PROJECT
)
97 @rift.test
.dts
.async_test
98 def test_download_status_handler(self
):
100 mock_msg
= RwPkgMgmtYang
.DownloadJob
.from_dict({
101 "url": "http://foo/bar",
103 "download_id": str(uuid
.uuid4())})
105 w_xpath
= self
.project
.add_project("D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job")
106 xpath
= "{}[download-id='{}']".format(w_xpath
, mock_msg
.download_id
)
109 def mock_cb(msg
, status
):
111 assert msg
== mock_msg
114 sub
= pkg_subscriber
.DownloadStatusSubscriber(
121 yield from sub
.register()
122 yield from asyncio
.sleep(1, loop
=self
.loop
)
124 yield from self
.publisher
.publish(w_xpath
, xpath
, mock_msg
)
125 yield from asyncio
.sleep(1, loop
=self
.loop
)
127 assert mock_called
is True
130 def main(argv
=sys
.argv
[1:]):
132 # The unittest framework requires a program name, so use the name of this
133 # file instead (we do not want to have to pass a fake program name to main
134 # when this is called from the interpreter).
136 argv
=[__file__
] + argv
,
137 testRunner
=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
140 if __name__
== '__main__':