def smtp_test(request): """Fixture, which prepares the appliance for e-mail capturing tests Returns: :py:class:`util.smtp_collector_client.SMTPCollectorClient` instance. """ logger.info("Preparing start for e-mail collector") ports = env.get("mail_collector", {}).get("ports", {}) mail_server_port = ports.get("smtp", None) or random_port() mail_query_port = ports.get("json", None) or random_port() my_ip = my_ip_address() logger.info("Mind that it needs ports %s and %s open", mail_query_port, mail_server_port) smtp_conf = configuration.SMTPSettings(host=my_ip, port=mail_server_port, auth="none") smtp_conf.update() server_filename = scripts_path.join("smtp_collector.py").strpath server_command = server_filename + " --smtp-port {} --query-port {}".format(mail_server_port, mail_query_port) logger.info("Starting mail collector %s", server_command) collector = None def _finalize(): if collector is None: return logger.info("Sending KeyboardInterrupt to collector") try: collector.send_signal(signal.SIGINT) except OSError as e: # TODO: Better logging. logger.exception(e) logger.error("Something happened to the e-mail collector!") return time.sleep(2) if collector.poll() is None: logger.info("Sending SIGTERM to collector") collector.send_signal(signal.SIGTERM) time.sleep(5) if collector.poll() is None: logger.info("Sending SIGKILL to collector") collector.send_signal(signal.SIGKILL) collector.wait() logger.info("Collector finished") collector = subprocess.Popen(server_command, shell=True) request.addfinalizer(_finalize) logger.info("Collector pid %s", collector.pid) logger.info("Waiting for collector to become alive.") time.sleep(3) assert collector.poll() is None, "Collector has died. Something must be blocking selected ports" logger.info("Collector alive") query_port_open = net_check_remote(mail_query_port, my_ip, force=True) server_port_open = net_check_remote(mail_server_port, my_ip, force=True) assert query_port_open and server_port_open, ( "Ports {} and {} on the machine executing the tests are closed.\n" "The ports are randomly chosen -> turn firewall off.".format(mail_query_port, mail_server_port) ) client = SMTPCollectorClient(my_ip, mail_query_port) client.set_test_name(request.node.name) client.clear_database() return client
def _smtp_test_session(request): """Fixture, which prepares the appliance for e-mail capturing tests Returns: :py:class:`util.smtp_collector_client.SMTPCollectorClient` instance. """ logger.info("Preparing start for e-mail collector") ports = env.get("mail_collector", {}).get("ports", {}) mail_server_port = ports.get("smtp", None) or random_port() mail_query_port = ports.get("json", None) or random_port() my_ip = my_ip_address() logger.info("Mind that it needs ports %d and %d open" % (mail_query_port, mail_server_port)) smtp_conf = configuration.SMTPSettings( host=my_ip, port=mail_server_port, auth="none", ) smtp_conf.update() server_filename = scripts_path.join('smtp_collector.py').strpath server_command = server_filename + " --smtp-port %d --query-port %d" % ( mail_server_port, mail_query_port ) logger.info("Starting mail collector %s" % server_command) collector = subprocess.Popen(server_command, shell=True) logger.info("Collector pid %d" % collector.pid) logger.info("Waiting for collector to become alive.") time.sleep(3) assert collector.poll() is None, "Collector has died. Something must be blocking selected ports" logger.info("Collector alive") query_port_open = net_check_remote(mail_query_port, my_ip, force=True) server_port_open = net_check_remote(mail_server_port, my_ip, force=True) assert query_port_open and server_port_open,\ 'Ports %d and %d on the machine executing the tests are closed.\n'\ 'The ports are randomly chosen -> turn firewall off.'\ % (mail_query_port, mail_server_port) client = SMTPCollectorClient( my_ip, mail_query_port ) yield client logger.info("Sending KeyboardInterrupt to collector") collector.send_signal(signal.SIGINT) collector.wait() logger.info("Collector finished")
def _smtp_test_session(request): """Fixture, which prepares the appliance for e-mail capturing tests Returns: :py:class:`util.smtp_collector_client.SMTPCollectorClient` instance. """ logger.info("Preparing start for e-mail collector") mail_server_port = random_port() mail_query_port = random_port() my_ip = my_ip_address() logger.info("Mind that it needs ports %d and %d open" % (mail_query_port, mail_server_port)) smtp_conf = configuration.SMTPSettings( host=my_ip, port=mail_server_port, auth="none", ) smtp_conf.update() server_filename = scripts_path.join('smtp_collector.py').strpath server_command = server_filename + " --smtp-port %d --query-port %d" % ( mail_server_port, mail_query_port) logger.info("Starting mail collector %s" % server_command) collector = subprocess.Popen(server_command, shell=True) logger.info("Collector pid %d" % collector.pid) logger.info("Waiting for collector to become alive.") time.sleep(3) assert collector.poll( ) is None, "Collector has died. Something must be blocking selected ports" logger.info("Collector alive") query_port_open = net_check_remote(mail_query_port, my_ip, force=True) server_port_open = net_check_remote(mail_server_port, my_ip, force=True) assert query_port_open and server_port_open,\ 'Ports %d and %d on the machine executing the tests are closed.\n'\ 'The ports are randomly chosen -> turn firewall off.'\ % (mail_query_port, mail_server_port) client = SMTPCollectorClient(my_ip, mail_query_port) yield client logger.info("Sending KeyboardInterrupt to collector") collector.send_signal(signal.SIGINT) collector.wait() logger.info("Collector finished")
def smtp_test(request): """Fixture, which prepares the appliance for e-mail capturing tests Returns: :py:class:`util.smtp_collector_client.SMTPCollectorClient` instance. """ logger.info("Preparing start for e-mail collector") ports = env.get("mail_collector", {}).get("ports", {}) mail_server_port = ports.get("smtp", None) or random_port() mail_query_port = ports.get("json", None) or random_port() my_ip = my_ip_address() logger.info("Mind that it needs ports {} and {} open".format( mail_query_port, mail_server_port)) smtp_conf = configuration.SMTPSettings( host=my_ip, port=mail_server_port, auth="none", ) smtp_conf.update() server_filename = scripts_path.join('smtp_collector.py').strpath server_command = server_filename + " --smtp-port {} --query-port {}".format( mail_server_port, mail_query_port) logger.info("Starting mail collector {}".format(server_command)) collector = None def _finalize(): if collector is None: return logger.info("Sending KeyboardInterrupt to collector") try: collector.send_signal(signal.SIGINT) except OSError as e: # TODO: Better logging. logger.exception(e) logger.error("Something happened to the e-mail collector!") return time.sleep(2) if collector.poll() is None: logger.info("Sending SIGTERM to collector") collector.send_signal(signal.SIGTERM) time.sleep(5) if collector.poll() is None: logger.info("Sending SIGKILL to collector") collector.send_signal(signal.SIGKILL) collector.wait() logger.info("Collector finished") collector = subprocess.Popen(server_command, shell=True) request.addfinalizer(_finalize) logger.info("Collector pid {}".format(collector.pid)) logger.info("Waiting for collector to become alive.") time.sleep(3) assert collector.poll( ) is None, "Collector has died. Something must be blocking selected ports" logger.info("Collector alive") query_port_open = net_check_remote(mail_query_port, my_ip, force=True) server_port_open = net_check_remote(mail_server_port, my_ip, force=True) assert query_port_open and server_port_open,\ 'Ports {} and {} on the machine executing the tests are closed.\n'\ 'The ports are randomly chosen -> turn firewall off.'\ .format(mail_query_port, mail_server_port) client = SMTPCollectorClient(my_ip, mail_query_port) client.set_test_name(request.node.name) client.clear_database() return client