def setUp(self): """setup gateway and topology for each test""" # gateway self.gateway = Gateway() self.chan = self.gateway.channel # topology graph = TopologyGraph() graph.add_route(NodeSet(HOSTNAME), NodeSet('n[1-2]')) graph.add_route(NodeSet('n1'), NodeSet('n[10-49]')) graph.add_route(NodeSet('n2'), NodeSet('n[50-89]')) self.topology = graph.to_tree(HOSTNAME) # xml parser with Communication.XMLReader as content handler self.xml_reader = XMLReader() self.parser = xml.sax.make_parser(["IncrementalParser"]) self.parser.setContentHandler(self.xml_reader)
def setUp(self): """setup gateway and topology for each test""" # gateway self.gateway = Gateway('n1') self.chan = self.gateway.channel # topology graph = TopologyGraph() graph.add_route(NodeSet(HOSTNAME), NodeSet('n[1-2]')) graph.add_route(NodeSet('n1'), NodeSet('n[10-49]')) graph.add_route(NodeSet('n2'), NodeSet('n[50-89]')) self.topology = graph.to_tree(HOSTNAME) # xml parser with Communication.XMLReader as content handler self.xml_reader = XMLReader() self.parser = xml.sax.make_parser(["IncrementalParser"]) self.parser.setContentHandler(self.xml_reader)
def testInvalidMsgStreams(self): """test detecting invalid messages""" patterns = [ '<message type="BLA" msgid="-1"></message>', '<message type="ACK"></message>', '<message type="ACK" msgid="0" ack="12"><foo></foo></message>', '<message type="ACK" msgid="0" ack="12">some stuff</message>', '<message type="ACK" msgid="123"></message>', '<message type="OUT" msgid="123" reason="foo"></message>', '<message type="OUT" msgid="123" output="foo" nodes="bar">shoomp</message>', '<message type="CFG" msgid="123"><foo></bar></message>', '<message type="CFG" msgid="123"><foo></message>', '<message type="CTL" msgid="123"><param></param></message>', '<message type="CTL" msgid="123"></message>', '<message type="CTL" msgid="123"><param><action target="node123" type="foobar"></action></param></message>', '<message type="CTL" msgid="123"><action type="foobar"></message>', '<message type="CTL" msgid="123"><action type="foobar" target="node1"><param cmd="yeepee"></param></action></message>', '<message type="CTL" msgid="123"><action type="foobar"><param cmd="echo fnords"></param></action></message>', '<message type="CTL" msgid="123"><action type="shell" target="node1"></action></message>', '<message type="CTL" msgid="123"><action type="" target="node1"><param cmd="echo fnords"></param></action></message>', '<param cmd=""></param></message>', '<message type="ERR" msgid="123"></message>', '<message type="ERR" msgid="123" reason="blah">unexpected payload</message>', '<message type="ERR" msgid="123" reason="blah"><foo bar="boo"></foo></message>', ] for msg_xml in patterns: parser = xml.sax.make_parser(['IncrementalParser']) parser.setContentHandler(XMLReader()) parser.feed('<?xml version="1.0" encoding="UTF-8"?>\n') parser.feed('<channel>\n') try: parser.feed(msg_xml) except MessageProcessingError as m: # actually this is Ok, we want this exception to be raised pass else: self.fail('Invalid message goes undetected: %s' % msg_xml)
class TreeGatewayBaseTest(unittest.TestCase): """base test class""" def setUp(self): """setup gateway and topology for each test""" # gateway self.gateway = Gateway() self.chan = self.gateway.channel # topology graph = TopologyGraph() graph.add_route(NodeSet(HOSTNAME), NodeSet('n[1-2]')) graph.add_route(NodeSet('n1'), NodeSet('n[10-49]')) graph.add_route(NodeSet('n2'), NodeSet('n[50-89]')) self.topology = graph.to_tree(HOSTNAME) # xml parser with Communication.XMLReader as content handler self.xml_reader = XMLReader() self.parser = xml.sax.make_parser(["IncrementalParser"]) self.parser.setContentHandler(self.xml_reader) def tearDown(self): """destroy gateway after each test""" self.gateway.destroy() self.gateway = None # # Send to GW # def channel_send_start(self): """send starting channel tag""" self.gateway.send_str('<channel version="%s">' % __version__) def channel_send_stop(self): """send channel ending tag""" self.gateway.send_str("</channel>") def channel_send_cfg(self, gateway): """send configuration part of channel""" # code snippet from PropagationChannel.start() cfg = ConfigurationMessage(gateway) cfg.data_encode(self.topology) self.gateway.send(cfg.xml()) # # Receive from GW # def assert_isinstance(self, msg, msg_class): """helper to check a message instance""" self.assertTrue(isinstance(msg, msg_class), "%s is not a %s" % (type(msg), msg_class)) def _recvxml(self): while not self.xml_reader.msg_available(): xml_msg = self.gateway.recv() if len(xml_msg) == 0: return None self.assertTrue(type(xml_msg) is bytes) self.parser.feed(xml_msg) return self.xml_reader.pop_msg() def recvxml(self, expected_msg_class=None): msg = self._recvxml() if expected_msg_class is None: self.assertEqual(msg, None) else: self.assert_isinstance(msg, expected_msg_class) return msg
class TreeGatewayBaseTest(unittest.TestCase): """base test class""" def setUp(self): """setup gateway and topology for each test""" # gateway self.gateway = Gateway('n1') self.chan = self.gateway.channel # topology graph = TopologyGraph() graph.add_route(NodeSet(HOSTNAME), NodeSet('n[1-2]')) graph.add_route(NodeSet('n1'), NodeSet('n[10-49]')) graph.add_route(NodeSet('n2'), NodeSet('n[50-89]')) self.topology = graph.to_tree(HOSTNAME) # xml parser with Communication.XMLReader as content handler self.xml_reader = XMLReader() self.parser = xml.sax.make_parser(["IncrementalParser"]) self.parser.setContentHandler(self.xml_reader) def tearDown(self): """destroy gateway after each test""" self.gateway.destroy() self.gateway = None # # Send to GW # def channel_send_start(self): """send starting channel tag""" self.gateway.send("<channel>") def channel_send_stop(self): """send channel ending tag""" self.gateway.send("</channel>") def channel_send_cfg(self): """send configuration part of channel""" # code snippet from PropagationChannel.start() cfg = ConfigurationMessage() cfg.data_encode(self.topology) self.gateway.send(cfg.xml()) # # Receive from GW # def assert_isinstance(self, msg, msg_class): """helper to check a message instance""" self.assertTrue(isinstance(msg, msg_class), "%s is not a %s" % (type(msg), msg_class)) def _recvxml(self): while not self.xml_reader.msg_available(): xml_msg = self.gateway.recv() if len(xml_msg) == 0: return None self.parser.feed(xml_msg) return self.xml_reader.pop_msg() def recvxml(self, expected_msg_class=None): msg = self._recvxml() if expected_msg_class is None: self.assertEqual(msg, None) else: self.assert_isinstance(msg, expected_msg_class) return msg