585a0d922e71c0dfa3acd22e0d21b1ee986c9ff6
[osm/SO.git] / rwlaunchpad / plugins / rwstagingmgr / test / utest_publisher_dts.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import argparse
20 import asyncio
21 import logging
22 import os
23 import sys
24 import unittest
25 import uuid
26 import xmlrunner
27
28 import gi
29 gi.require_version('RwDts', '1.0')
30 gi.require_version('RwStagingMgmtYang', '1.0')
31 from gi.repository import (
32 RwDts as rwdts,
33 RwStagingMgmtYang
34 )
35 import rift.tasklets.rwstagingmgr.publisher as publisher
36 import rift.test.dts
37
38
39 class TestCase(rift.test.dts.AbstractDTSTest):
40 @classmethod
41 def configure_schema(cls):
42 return RwStagingMgmtYang.get_schema()
43
44 @classmethod
45 def configure_timeout(cls):
46 return 240
47
48 def configure_test(self, loop, test_id):
49 self.log.debug("STARTING - %s", test_id)
50 self.tinfo = self.new_tinfo(str(test_id))
51 self.dts = rift.tasklets.DTS(self.tinfo, self.schema, self.loop)
52
53 self.job_handler = publisher.StagingStorePublisher(self.log, self.dts, self.loop)
54
55 def tearDown(self):
56 super().tearDown()
57
58 @asyncio.coroutine
59 def get_published_xpaths(self):
60 published_xpaths = set()
61
62 res_iter = yield from self.dts.query_read("D,/rwdts:dts")
63 for i in res_iter:
64 res = (yield from i).result
65 for member in res.member:
66 published_xpaths |= {reg.keyspec for reg in member.state.registration if reg.flags == "publisher"}
67
68 return published_xpaths
69
70 @asyncio.coroutine
71 def read_xpath(self, xpath):
72 itr = yield from self.dts.query_read(xpath)
73
74 result = None
75 for fut in itr:
76 result = yield from fut
77 return result.result
78
79 @rift.test.dts.async_test
80 def test_download_publisher(self):
81 yield from self.job_handler.register()
82 yield from asyncio.sleep(2, loop=self.loop)
83 published_xpaths = yield from self.get_published_xpaths()
84 assert self.job_handler.xpath() in published_xpaths
85
86 @rift.test.dts.async_test
87 def test_publish(self):
88 """
89 """
90 yield from self.job_handler.register()
91
92 mock_msg = RwStagingMgmtYang.StagingArea.from_dict({
93 "area_id": "123"})
94
95 self.job_handler.on_staging_area_create(mock_msg)
96 yield from asyncio.sleep(5, loop=self.loop)
97
98 itr = yield from self.dts.query_read("/staging-areas/staging-area[area-id='{}']".format(
99 mock_msg.area_id))
100
101
102 result = None
103 for fut in itr:
104 result = yield from fut
105 result = result.result
106
107 print (result)
108 assert result == mock_msg
109
110 def main():
111 runner = xmlrunner.XMLTestRunner(output=os.environ["RIFT_MODULE_TEST"])
112
113 parser = argparse.ArgumentParser()
114 parser.add_argument('-v', '--verbose', action='store_true')
115 parser.add_argument('-n', '--no-runner', action='store_true')
116 args, unittest_args = parser.parse_known_args()
117 if args.no_runner:
118 runner = None
119
120 TestCase.log_level = logging.DEBUG if args.verbose else logging.WARN
121
122 unittest.main(testRunner=runner, argv=[sys.argv[0]] + unittest_args)
123
124 if __name__ == '__main__':
125 main()