def __init__(self, verbose=False): listener_filename = scripts_path.join('listener.py').strpath self.listener_script = "{} 0.0.0.0 {}".format(listener_filename, self.listener_port) if not verbose: self.listener_script += ' --quiet' self.expectations = [] self.listener = None
def __init__(self, verbose=False): listener_filename = scripts_path.join('listener.py').strpath self.listener_script = "%s 0.0.0.0 %d" % (listener_filename, self.listener_port) if not verbose: self.listener_script += ' --quiet' self.expectations = [] self.processed_expectations = defaultdict(list) self.listener = None
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 enable_internal_db(self): """Enables internal database """ script = scripts_path.join('enable_internal_db.py') args = [str(script), self.address] with open(os.devnull, 'w') as f_devnull: status = subprocess.call(args, stdout=f_devnull) if status != 0: raise ApplianceException('Appliance {} failed to enable internal DB' .format(self.address))
def patch_ajax_wait(self, undo=False): """Patches ajax wait code Args: undo: Will undo the ajax wait code patch if set to ``True`` """ script = scripts_path.join('patch_ajax_wait.py') args = [str(script), self.address] if undo: args.append('-R') with open(os.devnull, 'w') as f_devnull: subprocess.call(args, stdout=f_devnull)
def enable_internal_db(self): """Enables internal database """ self.db_address = self.address del (self.db) script = scripts_path.join('enable_internal_db.py') args = [str(script), self.address] with open(os.devnull, 'w') as f_devnull: status = subprocess.call(args, stdout=f_devnull) if status != 0: raise ApplianceException( 'Appliance {} failed to enable internal DB'.format( self.address))
def enable_external_db(self, db_address, region=0): """Enables external database Args: db_address: Address of the external database region: Number of region to join """ self.db_address = db_address script = scripts_path.join('enable_external_db.py') args = [str(script), self.address, db_address, '--region', str(region)] with open(os.devnull, 'w') as f_devnull: status = subprocess.call(args, stdout=f_devnull) if status != 0: raise ApplianceException('Appliance {} failed to enable external DB running on {}' .format(self.address, db_address))
def enable_external_db(self, db_address, region=0): """Enables external database Args: db_address: Address of the external database region: Number of region to join """ # reset the db address and clear the cached db object if we have one self.db_address = db_address del (self.db) script = scripts_path.join('enable_external_db.py') args = [str(script), self.address, db_address, '--region', str(region)] with open(os.devnull, 'w') as f_devnull: status = subprocess.call(args, stdout=f_devnull) if status != 0: raise ApplianceException( 'Appliance {} failed to enable external DB running on {}'. format(self.address, db_address))
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 main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('hostname', nargs='?', default=None, help='hostname or ip address of target appliance') parser.add_argument('username', nargs='?', default=credentials['ssh']['username'], help='SSH username for target appliance') parser.add_argument('password', nargs='?', default=credentials['ssh']['password'], help='SSH password for target appliance') args = parser.parse_args() ssh_kwargs = {'username': args.username, 'password': args.password} if args.hostname is not None: ssh_kwargs['hostname'] = args.hostname with SSHClient(stream_output=True, **ssh_kwargs) as ssh_client: # `systemctl stop evmserverd` is a little slow, and we're destroying the # db, so rudely killing ruby speeds things up significantly print('Stopping ruby processes...') ssh_client.run_command('killall ruby') ssh_client.run_rake_command('evm:db:reset') ssh_client.run_command('systemctl start evmserverd') # SSHClient has the smarts to get our hostname if none was provided # Soon, utils.appliance.Appliance will be able to do all of this # and this will be made good hostname = ssh_client._connect_kwargs['hostname'] print('Waiting for appliance UI...') args = [ scripts_path.join('wait_for_appliance_ui.py').strpath, 'http://{}'.format(hostname) ] return subprocess.call(args)
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 main(): parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('hostname', nargs='?', default=None, help='hostname or ip address of target appliance') parser.add_argument('username', nargs='?', default=credentials['ssh']['username'], help='SSH username for target appliance') parser.add_argument('password', nargs='?', default=credentials['ssh']['password'], help='SSH password for target appliance') args = parser.parse_args() ssh_kwargs = { 'username': args.username, 'password': args.password } if args.hostname is not None: ssh_kwargs['hostname'] = args.hostname client = SSHClient(stream_output=True, **ssh_kwargs) # `service evmserverd stop` is a little slow, and we're destroying the # db, so rudely killing ruby speeds things up significantly print 'Stopping ruby processes...' client.run_command('killall ruby') client.run_rake_command('evm:db:reset') client.run_command('service evmserverd start') print 'Waiting for appliance UI...' args = [ scripts_path.join('wait_for_appliance_ui.py').strpath, # SSHClient has the smarts to get our hostname if none was provided # Soon, utils.appliance.Appliance will be able to do all of this # and this will be made good 'http://%s' % client._connect_kwargs['hostname'] ] return subprocess.call(args)
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