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.
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')
28 gi
.require_version('RwDtsYang', '1.0')
29 gi
.require_version('RwPkgMgmtYang', '1.0')
30 from gi
.repository
import (
34 import rift
.tasklets
.rwpkgmgr
.subscriber
as pkg_subscriber
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
):
45 self
._registrations
= []
48 def publish(self
, w_path
, path
, desc
):
49 ready_event
= asyncio
.Event(loop
=self
.loop
)
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
)
60 handler
= rift
.tasklets
.DTS
.RegistrationHandler(
64 self
.log
.debug("Registering path: %s, obj:%s", w_path
, desc
)
65 reg
= yield from self
.dts
.register(
68 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
70 self
._registrations
.append(reg
)
71 self
.log
.debug("Registered path : %s", w_path
)
72 yield from ready_event
.wait()
76 def unpublish_all(self
):
77 self
.log
.debug("Deregistering all published descriptors")
78 for reg
in self
._registrations
:
81 class SubscriberStoreDtsTestCase(rift
.test
.dts
.AbstractDTSTest
):
83 def configure_schema(cls
):
84 return RwPkgMgmtYang
.get_schema()
87 def configure_timeout(cls
):
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
)
99 @rift.test
.dts
.async_test
100 def test_download_status_handler(self
):
102 mock_msg
= RwPkgMgmtYang
.DownloadJob
.from_dict({
103 "url": "http://foo/bar",
105 "download_id": str(uuid
.uuid4())})
107 w_xpath
= "D,/rw-pkg-mgmt:download-jobs/rw-pkg-mgmt:job"
108 xpath
= "{}[download-id='{}']".format(w_xpath
, mock_msg
.download_id
)
111 def mock_cb(msg
, status
):
113 assert msg
== mock_msg
116 sub
= pkg_subscriber
.DownloadStatusSubscriber(
122 yield from sub
.register()
123 yield from asyncio
.sleep(1, loop
=self
.loop
)
125 yield from self
.publisher
.publish(w_xpath
, xpath
, mock_msg
)
126 yield from asyncio
.sleep(1, loop
=self
.loop
)
128 assert mock_called
is True
131 def main(argv
=sys
.argv
[1:]):
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).
137 argv
=[__file__
] + argv
,
138 testRunner
=None#xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
141 if __name__
== '__main__':