def get_node_cfg(msg): """ Process valid cfg msg, ie, msg must be a valid node ID. :param str node ID: zerotier node identity :return: str JSON object with node ID and network ID(s) """ import json if valid_announce_msg(msg): node_data = lookup_node_id(msg, tmp_q) if node_data: logger.info( 'Got valid cfg request msg from host {} (node {})'.format( node_data[msg], msg)) res = wait_for_cfg_msg(cfg_q, hold_q, reg_q, msg) logger.debug('hold_q size: {}'.format(len(list(hold_q)))) if res: logger.info('Got cfg result: {}'.format(res)) return res else: logger.debug('Null result for ID: {}'.format(msg)) # raise ServiceError else: node_data = lookup_node_id(msg, tmp_q) if node_data: logger.warning('Bad cfg msg from host {} (node {})'.format( node_data[msg], msg)) else: logger.warning('Bad cfg msg: {}'.format(msg))
def test_wait_for_cfg(self): import json self.assertIn(self.cfg1, self.cfg_q) res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node1) self.assertNotIn(self.cfg1, self.cfg_q) self.assertIsInstance(res, str) self.assertIn(self.node1, res) self.assertEqual(len(json.loads(res)['networks']), 2)
def test_wait_for_cfg_none(self): import json res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node3) self.assertIsNone(res) self.assertIn(self.cfg2, self.cfg_q) res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node2) self.assertNotIn(self.cfg2, self.cfg_q) self.assertIn(self.node2, res) self.assertEqual(len(json.loads(res)['networks']), 1) res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node3) self.assertIsNone(res) self.assertEqual(len(self.hold_q), 3) res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node3) self.assertIsNone(res) self.assertEqual(len(self.hold_q), 0) self.assertEqual(len(self.reg_q), 1) self.assertIn(self.node3, list(self.reg_q)) self.cfg_q.clear() res = wait_for_cfg_msg(self.cfg_q, self.hold_q, self.reg_q, self.node3) self.assertIsNone(res) self.assertEqual(len(self.hold_q), 1) self.assertIn(self.node3, list(self.hold_q))