def non_existing_filter_conf_v2(): ret = True darwin_configure(CONF_THREE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "non_existing_filter: Mismatching monitor response; got \"{}\"". format(resp)) ret = False sleep(1) # Need this because of the starting delay resp = requests(REQ_UPDATE_NON_EXISTING) if RESP_ERROR_FILTER_NOT_EXISTING not in resp: logging.error( "non_existing_filter: Update response error; got \"{}\"".format( resp)) ret = False resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "non_existing_filter: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def proc_stats_custom(): ret = False darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis="", conf_file="", proc_stats="")) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() try: resp = json.loads(requests(REQ_MONITOR_CUSTOM_STATS)) try: result = resp['logs_1']['proc_stats'] if [x in result for x in ['name', 'pid', 'memory_percent'] ] and len(result) == 3: ret = True else: logging.error( "proc_stats_custom(): not expected result -> {}".format( result)) except KeyError as e: logging.error("proc_stats_custom(): {}".format(e)) except Exception as e: logging.error("proc_stats_custom(): {}".format(e)) darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def one_update_one_conf_v2(): ret = True darwin_configure(CONF_ONE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_TEST_1 not in resp: logging.error( "one_update_one: Mismatching monitor response; got \"{}\"".format( resp)) ret = False sleep(2) # Need this because of the starting delay resp = requests(REQ_UPDATE_ONE) if RESP_STATUS_OK not in resp: logging.error( "one_update_one: Update response error; got \"{}\"".format(resp)) ret = False sleep(1) # Need this because of the starting delay resp = requests(REQ_MONITOR) if RESP_TEST_1 not in resp: logging.error( "one_update_one: Mismatching monitor response; got \"{}\"".format( resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def proc_stats_wrong(): ret = False darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis="", conf_file="", proc_stats="")) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() try: resp = json.loads(requests(REQ_MONITOR_ERROR)) try: result = resp['logs_1']['proc_stats'] if len(result) == 0: ret = True else: logging.error( "proc_stats_wrong(): not expected result -> {} (supposed to have empty dict {{}})" .format(result)) except KeyError as e: logging.error("proc_stats_wrong(): {}".format(e)) except Exception as e: logging.error("proc_stats_wrong(): {}".format(e)) darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def update_no_filter(): ret = True darwin_configure(CONF_ONE) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "update_no_filter: Mismatching monitor response; got \"{}\"". format(resp)) ret = False sleep(1) # Need this because of the starting delay resp = requests(REQ_UPDATE_NO_FILTER) if RESP_STATUS_OK not in resp: logging.error( "update_no_filter: Update response error; got \"{}\"".format(resp)) ret = False resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "update_no_filter: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def no_filter_to_none(): ret = True darwin_configure(CONF_EMPTY) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_EMPTY not in resp: logging.error( "no_filter_to_none: Mismatching monitor response; got \"{}\"". format(resp)) ret = False resp = requests(REQ_UPDATE_EMPTY) if RESP_STATUS_OK not in resp: logging.error( "no_filter_to_none: Update response error; got \"{}\"".format( resp)) ret = False resp = requests(REQ_MONITOR) if RESP_EMPTY not in resp: logging.error( "no_filter_to_none: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() return ret
def many_update_many_conf_v2(): ret = True darwin_configure(CONF_THREE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "many_update_many: Mismatching monitor response; got \"{}\"". format(resp)) ret = False sleep(1) # Need this because of the starting delay resp = requests(REQ_UPDATE_TWO) if RESP_STATUS_OK not in resp: logging.error( "many_update_many: Update response error; got \"{}\"".format(resp)) ret = False resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "many_update_many: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def no_filter(): ret = False darwin_configure(CONF_EMPTY) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_EMPTY in resp: ret = True darwin_stop(process) darwin_remove_configuration() return ret
def one_filters_running_conf_v2(): ret = False darwin_configure(CONF_ONE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_TEST_1 in resp: ret = True darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def one_filters_running(): ret = False darwin_configure(CONF_ONE) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_LOGS_1 in resp: ret = True darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def multiple_filters_running_conf_v2(): ret = False darwin_configure(CONF_THREE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): ret = True darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def multiple_filters_running_one_fail_conf_v2(): ret = False darwin_configure(CONF_THREE_ONE_WRONG_V2) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if all(x in resp for x in [RESP_LOGS_1, RESP_LOGS_3]): ret = True darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def no_filter_to_many(): ret = True darwin_configure(CONF_EMPTY) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_EMPTY not in resp: logging.error( "no_filter_to_many: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_configure(CONF_THREE) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) resp = requests(REQ_UPDATE_THREE) if RESP_STATUS_OK not in resp: logging.error( "no_filter_to_many: Update response error; got \"{}\"".format( resp)) ret = False resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_LOGS_1, RESP_LOGS_2, RESP_LOGS_3]): logging.error( "no_filter_to_many: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def one_filter_to_none_conf_v2(): ret = True darwin_configure(CONF_ONE_V2) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "one_filter_to_none: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_configure(CONF_EMPTY) resp = requests(REQ_UPDATE_ONE) if RESP_STATUS_OK not in resp: logging.error( "one_filter_to_none: Update response error; got \"{}\"".format( resp)) ret = False resp = requests(REQ_MONITOR) if RESP_EMPTY not in resp: logging.error( "one_filter_to_none: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def many_filters_to_none_conf_v2(): ret = True darwin_configure(CONF_THREE_V2) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "many_filters_to_none: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_configure(CONF_EMPTY) resp = requests(REQ_UPDATE_THREE) if RESP_STATUS_OK not in resp: logging.error( "many_filters_to_none: Update response error; got \"{}\"".format( resp)) ret = False resp = requests(REQ_MONITOR) if RESP_EMPTY not in resp: logging.error( "many_filters_to_none: Mismatching second monitor response; got \"{}\"" .format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def proc_stats_other_defaults(): ret = False darwin_configure( CONF_TEMPLATE.substitute( log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis="", conf_file="", proc_stats='"proc_stats": ["name", "username", "memory_full_info"],' )) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() try: resp = json.loads(requests(REQ_MONITOR)) try: result = resp['logs_1']['proc_stats'] if [x in result for x in ['name', 'username', 'memory_full_info'] ] and len(result) == 3: ret = True else: logging.error( "proc_stats_other_defaults(): not expected result -> {}". format(result)) except KeyError as e: logging.error("proc_stats_other_defaults(): {}".format(e)) except Exception as e: logging.error("proc_stats_other_defaults(): {}".format(e)) darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def proc_stats_default(): ret = False darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FTEST, conf_redis="", conf_file="", proc_stats="")) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() try: resp = json.loads(requests(REQ_MONITOR)) try: result = resp['test_1']['proc_stats'] if [x in result for x in ['memory_percent', 'cpu_percent'] ] and len(result) == 2: ret = True else: logging.error( "proc_stats_default(): not expected result -> {}".format( result)) except KeyError as e: logging.error("proc_stats_default(): {}".format(e)) except Exception as e: logging.error( "proc_stats_default(): error loading json -> {}".format(e)) darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def many_update_all_wrong_conf(): ret = True darwin_configure(CONF_THREE) darwin_configure(CONF_FTEST, path=PATH_CONF_FTEST) process = darwin_start() resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "one_update_one_wrong_conf: Mismatching monitor response; got \"{}\"" .format(resp)) ret = False sleep(2) # Need this because of the starting delay darwin_configure(CONF_FTEST_WRONG_CONF, path=PATH_CONF_FTEST) resp = requests(REQ_UPDATE_THREE) if RESP_STATUS_KO not in resp: logging.error( "one_update_one_wrong_conf: Update response error; got \"{}\"". format(resp)) ret = False resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_TEST_1, RESP_TEST_2, RESP_TEST_3]): logging.error( "one_update_one_wrong_conf: Mismatching monitor response; got \"{}\"" .format(resp)) ret = False if not check_filter_files("test_1", ".1"): logging.error("Error: filter files check failed") ret = False if not check_filter_files("test_2", ".1"): logging.error("Error: filter files check failed") ret = False if not check_filter_files("test_3", ".1"): logging.error("Error: filter files check failed") ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FTEST) return ret
def one_update_one_wrong_conf_conf_v2(): ret = True darwin_configure(CONF_ONE_V2) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "one_update_one_wrong_conf: Mismatching monitor response; got \"{}\"" .format(resp)) ret = False sleep(2) # Need this because of the starting delay darwin_configure(CONF_FLOGS_WRONG_CONF, path=PATH_CONF_FLOGS) resp = requests(REQ_UPDATE_ONE) if RESP_STATUS_KO not in resp: logging.error( "one_update_one_wrong_conf: Update response error; got \"{}\"". format(resp)) ret = False resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "one_update_one_wrong_conf: Mismatching monitor response; got \"{}\"" .format(resp)) ret = False if not check_filter_files("logs_1", ".1"): logging.error("Error: filter files check failed") ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def many_filters_to_one(): ret = True darwin_configure(CONF_THREE) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() resp = requests(REQ_MONITOR) if not all(x in resp for x in [RESP_LOGS_1, RESP_LOGS_2, RESP_LOGS_3]): logging.error( "many_filters_to_one: Mismatching monitor response; got \"{}\"". format(resp)) ret = False sleep(1) # Need this beacause of the starting delay darwin_configure(CONF_ONE) resp = requests(REQ_UPDATE_TWO) if RESP_STATUS_OK not in resp: logging.error( "many_filters_to_one: Update response error; got \"{}\"".format( resp)) ret = False sleep(1) resp = requests(REQ_MONITOR) if RESP_LOGS_1 not in resp: logging.error( "many_filters_to_one: Mismatching monitor response; got \"{}\"". format(resp)) ret = False darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def file_and_redis_simple_report(): ret = False file_conf = """"file": {{"filepath": "{}"}},""".format(DEFAULT_STATS_FILE) redis_conf = """"redis": {{"ip": "127.0.0.1", "port": 7890, "channel": "{}", "list": "{}"}},""".format( DEFAULT_REDIS_CHANNEL, DEFAULT_REDIS_LIST) darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis=redis_conf, conf_file=file_conf, proc_stats="")) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) redis_server = RedisServer(address="127.0.0.1", port=7890) redis_probe = redis_server.connect() redis_pubsub = redis_probe.pubsub(ignore_subscribe_messages=True) redis_pubsub.subscribe(DEFAULT_REDIS_CHANNEL) # Remove subscribe notification message sleep(0.1) redis_pubsub.get_message() process = darwin_start() try: with open(DEFAULT_STATS_FILE, "r") as statsfile: sleep(1) channel_message = redis_pubsub.get_message().get('data').decode( "utf-8") list_message = redis_probe.lpop(DEFAULT_REDIS_LIST).decode("utf-8") file_message = statsfile.readline() # File message has an additional '\n' if channel_message in file_message and list_message in file_message: if STAT_LOG_MATCH in file_message: ret = True else: logging.error( "file_and_redis_simple_report(): expected \"{}\", but got \"{}\"" .format(STAT_LOG_MATCH, file_message)) else: logging.error( "file_and_redis_simple_report(): messages are not identical" ) logging.error("file_and_redis_simple_report(): {}".format( channel_message)) logging.error( "file_and_redis_simple_report(): {}".format(list_message)) logging.error( "file_and_redis_simple_report(): {}".format(file_message)) except Exception as e: logging.error( "file_and_redis_simple_report(): could not open file -> {}".format( e)) try: os.remove(DEFAULT_STATS_FILE) except Exception: pass darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) return ret
def file_reports(): ret = False part = 1 files = [{ "name": "good-file", "conf": "\"filepath\": \"{}\"".format(DEFAULT_STATS_FILE), "result": True }, { "name": "wrong-file", "conf": "\"filepath\": \"/tmp/but/I/do/not/exist\"", "result": False }] permissions = [ { "name": "default", "conf": "", "result": True, "expected_mode": "-rw-r-----" }, { "name": "custom-correct", "conf": ",\"permissions\": 700", "result": True, "expected_mode": "-rwx------" }, { "name": "custom-wrong", "conf": ",\"permissions\": -66", "result": False, "expected_mode": "" }, ] for logfile in files: for permission in permissions: print("[{}/6]".format(part), end='', flush=True) file_conf = "\"file\": {{{file} {permission}}},".format( file=logfile['conf'], permission=permission['conf']) # print(file_conf) if logfile['result'] and permission['result']: expected_result = STAT_LOG_MATCH else: expected_result = "nothing" darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis="", conf_file=file_conf, proc_stats="")) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) process = darwin_start() logline = "nothing" try: with open(DEFAULT_STATS_FILE, "r") as test_file: file_stats = os.stat(DEFAULT_STATS_FILE) mode = stat.filemode(file_stats.st_mode) if mode != permission['expected_mode']: logging.error( "file_reports(): file mode should be '{}', but is '{}'" .format(permission['expected_mode'], mode)) ret = False # break else: sleep(1) logline = test_file.readline() ret = True if expected_result in logline else False if not ret: logging.error( "file_reports(): while trying '{}' with '{}', didn't find '{}' in '{}'" .format(logfile['conf'], permission['conf'], expected_result, logline)) except FileNotFoundError: if expected_result != "nothing": logging.error( "file_reports(): file is absent, but should have been created" ) ret = False try: os.remove(DEFAULT_STATS_FILE) except Exception: pass darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) # Don't judge me... part += 1 print("\x08\x08\x08\x08\x08", end='', flush=True) if not ret: break if not ret: break # Dirty much... But pretty printing! print(" ", end='', flush=True) print("\x08\x08\x08\x08\x08\x08", end='', flush=True) return ret
def redis_reports(): ret = False part = 1 connections = [{ "name": "ip", "conf": "\"ip\": \"127.0.0.1\", \"port\": 7890", "partial_result": "" }, { "name": "ip_wrong_port", "conf": "\"ip\": \"127.0.0.1\", \"port\": 7891", "partial_result": "nodata" }, { "name": "ip_wrong_ip", "conf": "\"ip\": \"127.0.0.2\", \"port\": 7890", "partial_result": "nodata" }, { "name": "unix", "conf": "\"unix_path\": \"{}\"".format(DEFAULT_REDIS_SOCKET), "partial_result": "" }, { "name": "unix_wrong", "conf": "\"unix_path\": \"wrong_path\"", "partial_result": "nodata" }] report_methods = [ { "name": "list", "conf": "\"list\": \"{}\"".format(DEFAULT_REDIS_LIST), "partial_result": STAT_LOG_MATCH }, { "name": "channel", "conf": "\"channel\": \"{}\"".format(DEFAULT_REDIS_CHANNEL), "partial_result": STAT_LOG_MATCH }, ] for method in report_methods: for connection in connections: print("[{}/10]".format(part), end='', flush=True) redis_conf = "\"redis\": {{{connection}, {method}}},".format( connection=connection['conf'], method=method['conf']) expected_result = method['partial_result'] if connection[ 'partial_result'] == "" else connection['partial_result'] darwin_configure( CONF_TEMPLATE.substitute(log_path=DEFAULT_FILTER_PATH, conf_path=PATH_CONF_FLOGS, conf_redis=redis_conf, conf_file="", proc_stats="")) darwin_configure(CONF_FLOGS, path=PATH_CONF_FLOGS) redis_server = RedisServer(unix_socket=DEFAULT_REDIS_SOCKET, address="127.0.0.1", port=7890) redis_probe = redis_server.connect() process = darwin_start() if method['name'] == "channel": redis_pubsub = redis_probe.pubsub( ignore_subscribe_messages=True) redis_pubsub.subscribe(DEFAULT_REDIS_CHANNEL) # Remove subscribe notification message sleep(0.1) redis_pubsub.get_message() sleep(1) message = redis_pubsub.get_message() try: message = str(message['data']) except Exception: message = "nodata" elif method['name'] == "list": sleep(1) message = redis_probe.lpop(DEFAULT_REDIS_LIST) if not message: message = "nodata" else: message = str(message) ret = True if expected_result in message else False if not ret: logging.error( "redis_reports(): while trying '{}' with '{}', didn't find '{}' in '{}'" .format(method['conf'], connection['conf'], expected_result, message)) del redis_server del message darwin_stop(process) darwin_remove_configuration() darwin_remove_configuration(path=PATH_CONF_FLOGS) # Don't judge me... part += 1 print("\x08\x08\x08\x08\x08\x08", end='', flush=True) if not ret: break if not ret: break # Dirty much... But pretty printing! if part == 11: print("\x08", end='', flush=True) print(" ", end='', flush=True) print("\x08\x08\x08\x08\x08\x08\x08", end='', flush=True) return ret