def setup_module(self, module=None): """Set up a test hub on a random free incoming/outgoing port. This will attempt to import evasion.messenger modules. This will populate self.config with the current configuration. For example:: self.config = dict( endpoint=dict( incoming='tcp://localhost:%d'%port1, outgoing='tcp://localhost:%d'%port2, ), hub=dict( outgoing='tcp://*:%d'%port1, incoming='tcp://*:%d'%port2, # Set to True to log messages received to DEBUG logging: show_messages=True, # Set to True to log HUB_PRESNET dispatches: show_hub_presence=False, # Disable HUB_PRESENT in testing send_hub_present=False, ) ) """ self.log.info("setup_module: for <%s>" % module) port1 = net.get_free_port() port2 = net.get_free_port(exclude_ports=[port1]) self.config = dict( endpoint=dict( incoming='tcp://localhost:%d'%port1, outgoing='tcp://localhost:%d'%port2, ), hub=dict( outgoing='tcp://*:%d'%port1, incoming='tcp://*:%d'%port2, # Set to True to log messages received to DEBUG logging: show_messages=True, # Set to True to log HUB_PRESNET dispatches: show_hub_presence=False, # Disable HUB_PRESENT in testing send_hub_present=False, ) ) self.log.debug("setup_module: generated configuration %s" % self.config) self.broker = hub.MessagingHub(self.config['hub']) self.log.info("setup_module: starting the hub.") self.broker.start() self.log.info("setup_module: the hub has been started.")
def setup_module(self, module=None): """Set up a test hub on a random free incoming/outgoing port. This will attempt to import evasion.messenger modules. This will populate self.config with the current configuration. For example:: self.config = dict( endpoint=dict( incoming='tcp://localhost:%d'%port1, outgoing='tcp://localhost:%d'%port2, ), hub=dict( outgoing='tcp://*:%d'%port1, incoming='tcp://*:%d'%port2, # Set to True to log messages received to DEBUG logging: show_messages=True, # Set to True to log HUB_PRESNET dispatches: show_hub_presence=False, # Disable HUB_PRESENT in testing send_hub_present=False, ) ) """ self.log.info("setup_module: for <%s>" % module) port1 = net.get_free_port() port2 = net.get_free_port(exclude_ports=[port1]) self.config = dict( endpoint=dict( incoming='tcp://localhost:%d' % port1, outgoing='tcp://localhost:%d' % port2, ), hub=dict( outgoing='tcp://*:%d' % port1, incoming='tcp://*:%d' % port2, # Set to True to log messages received to DEBUG logging: show_messages=True, # Set to True to log HUB_PRESNET dispatches: show_hub_presence=False, # Disable HUB_PRESENT in testing send_hub_present=False, )) self.log.debug("setup_module: generated configuration %s" % self.config) self.broker = hub.MessagingHub(self.config['hub']) self.log.info("setup_module: starting the hub.") self.broker.start() self.log.info("setup_module: the hub has been started.")
def testNetTools(self): """Test as much of the net helper tools as I can. """ # Recover a free port to construct a uri for a ficticious web servce # we can test the fail path of the wait_for_ready with: port = net.get_free_port() class XYZ(object): def __init__(self, port): self.port = port def freeport_range(self): return self.port fpr = XYZ(port) gport = net.get_free_port(fp=fpr.freeport_range) self.assertEquals(gport, port) # use the port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('localhost', port)) # Now exlucde this port and try again. self.assertRaises(net.NoFreePort, net.get_free_port, exclude_ports=[port], fp=fpr.freeport_range) s.close() # Recover a free port to construct a uri for a ficticious web servce # we can test the fail path of the wait_for_ready with: port = net.get_free_port() ficticious_uri = "http://localhost:%d" % port # Use this made up uri and see it did indeed NOT manage to connect: result = net.wait_for_ready(ficticious_uri, retries=1) self.assertEquals(result, False) # Use this made up uri and see it did indeed NOT manage to connect: result = net.wait_for_service('localhost', port, retries=1) self.assertEquals(result, False)
def testWaitForReadyLive(self): """Test wait for ready against a basic local web service. """ # Get a free port to use for this test: port1 = net.get_free_port() web = webhelpers.BasicWeb(port=port1) self.stop_needed.append(web) # Run the web app and wait for ready should connect: web.start() result = net.wait_for_ready(web.uri) self.assertEquals(result, True)
def __init__(self, config={}): """ """ self.log = get_log("ServerRunner") self.serverPid = None self.serverProcess = None self.port = int(config.get('port', net.get_free_port())) self.interface = config.get('interface', '127.0.0.1') self.mongo_host = config.get('mongo_host', '127.0.0.1') self.mongo_port = config.get('mongo_port', 27017) self.mongo_dbname = config.get('mongo_dbname', 'testing') self.URI = "http://%s:%s" % (self.interface, self.port) # Make directory to put file and other data into: self.test_dir = tempfile.mkdtemp() self.log.info("Test Server temp directory <%s>" % self.test_dir) # Get template in the tests dir: self.temp_config = os.path.join(self.test_dir, 'test_cfg.ini') # The service to run with the rendered configuration: self.cmd = "pserve {}".format(self.temp_config) self.test_cfg = resource_string(__name__, 'test_cfg.ini.template') cfg_tmpl = Template(self.test_cfg) data = dict( interface=self.interface, port=self.port, mongo_host=self.mongo_host, mongo_port=self.mongo_port, mongo_dbname=self.mongo_dbname, ) self.log.info('ServerRunner template config: {}'.format(data)) data = cfg_tmpl.substitute(data) # print "data:" # print data # print with open(self.temp_config, "wb") as fd: fd.write(data) self.config = ConfigParser.ConfigParser() self.config.readfp(StringIO.StringIO(data)) config = ConfigParser.ConfigParser(dict(here=self.test_dir)) self.log.info("Setting up common db from <%s>" % self.temp_config) config.read(self.temp_config)
def __init__(self, port=None, configfile=None, interface='localhost'): """ """ self.log = get_log("ServerRunner") self.serverPid = None self.serverProcess = None if interface: self.interface = interface else: self.interface = '127.0.0.1' if port: self.port = port else: self.port = net.get_free_port() self.URI = "http://%s:%s" % (self.interface, self.port) # Make directory to put file and other data into: self.test_dir = tempfile.mkdtemp() self.log.info("Test Server temp directory <%s>" % self.test_dir) # Get template in the tests dir: self.temp_config = os.path.join(self.test_dir, 'test_cfg.ini') # The service to run with the rendered configuration: self.cmd = "pserve {0}".format(self.temp_config) self.test_cfg = resource_string(__name__, 'test_cfg.ini.template') cfg_tmpl = Template(self.test_cfg) data = dict( interface=self.interface, port=int(self.port), ) data = cfg_tmpl.substitute(data) # print "data:" # print data # print with open(self.temp_config, "wb") as fd: fd.write(data) self.config = ConfigParser.ConfigParser() self.config.readfp(StringIO.StringIO(data)) config = ConfigParser.ConfigParser(dict(here=self.test_dir)) self.log.info("Setting up common db from <%s>" % self.temp_config) config.read(self.temp_config)
def __init__(self, port=None, configfile=None, interface='localhost'): """ """ self.log = get_log("ServerRunner") self.serverPid = None self.serverProcess = None if interface: self.interface = interface else: self.interface = '127.0.0.1' if port: self.port = port else: self.port = net.get_free_port() self.URI = "http://%s:%s" % (self.interface, self.port) # Make directory to put file and other data into: self.test_dir = tempfile.mkdtemp() self.log.info("Test Server temp directory <%s>" % self.test_dir) # Get template in the tests dir: self.temp_config = os.path.join(self.test_dir, 'test_cfg.ini') # The service to run with the rendered configuration: self.cmd = "restservice --config=%s" % self.temp_config self.test_cfg = resource_string(__name__, 'test_cfg.ini.tmpl') cfg_tmpl = Template(self.test_cfg) data = cfg_tmpl.substitute( interface=self.interface, port=self.port, ) with open(self.temp_config, "wb") as fd: fd.write(data) self.config = ConfigParser.ConfigParser() self.config.readfp(StringIO.StringIO(data)) config = ConfigParser.ConfigParser(dict(here=self.test_dir)) self.log.info("Setting up common db from <%s>" % self.temp_config) config.read(self.temp_config)