From f58c50f383f7cada4fc8cf4c278b8b1e1a10a801 Mon Sep 17 00:00:00 2001 From: David Boucher Date: Wed, 28 Feb 2024 10:53:07 +0100 Subject: [PATCH] fix(tests): Coding style of Engine improved --- tests/resources/Broker.py | 323 +++++------ tests/resources/Engine.py | 1150 +++++++++++++++++++------------------ 2 files changed, 741 insertions(+), 732 deletions(-) diff --git a/tests/resources/Broker.py b/tests/resources/Broker.py index 591da4fd771..c92480ac4b7 100755 --- a/tests/resources/Broker.py +++ b/tests/resources/Broker.py @@ -1,16 +1,12 @@ import signal from os import setsid from os import makedirs -from os.path import exists, dirname +from os.path import exists import pymysql.cursors import time import re import shutil import psutil -import socket -import sys -import time -from datetime import datetime from subprocess import getoutput import subprocess as subp from robot.api import logger @@ -22,7 +18,6 @@ import broker_pb2_grpc from google.protobuf import empty_pb2 from google.protobuf.json_format import MessageToJson -from robot.libraries.BuiltIn import BuiltIn from Common import DB_NAME_STORAGE, DB_NAME_CONF, DB_USER, DB_PASS, DB_HOST, DB_PORT, VAR_ROOT, ETC_ROOT, TESTS_PARAMS TIMEOUT = 30 @@ -414,7 +409,7 @@ def _apply_conf(name, callback): def config_broker(name: str, poller_inst: int = 1): """ - + config_broker Configure broker instances for test. Write the configuration files. @@ -424,8 +419,8 @@ def config_broker(name: str, poller_inst: int = 1): poller_inst (int, optional): Defaults to 1. Returns: create conf broker with the chosen name - - """ + + """ makedirs(ETC_ROOT, mode=0o777, exist_ok=True) makedirs(VAR_ROOT, mode=0o777, exist_ok=True) makedirs(ETC_ROOT + "/centreon-broker", mode=0o777, exist_ok=True) @@ -500,7 +495,7 @@ def config_broker(name: str, poller_inst: int = 1): def change_broker_tcp_output_to_grpc(name: str): """ - + change_broker_tcp_output_to_grpc Update broker configuration to use a gRPC output instead of a TCP one. @@ -509,8 +504,8 @@ def change_broker_tcp_output_to_grpc(name: str): name (str): name of the conf broker wanted to be changed Returns: N/A - - """ + + """ def output_to_grpc(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -532,8 +527,8 @@ def add_path_to_rrd_output(name: str, path: str): path (str): path to the rrd output (/tmp/rrd) Returns: N/A - - """ + + """ def rrd_output(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -552,8 +547,8 @@ def change_broker_tcp_input_to_grpc(name: str): name (str): input name Returns: N/A - - """ + + """ def input_to_grpc(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -568,27 +563,26 @@ def _add_broker_crypto(json_dict, add_cert: bool, only_ca_cert: bool): json_dict["encryption"] = "yes" if (add_cert): json_dict["ca_certificate"] = "/tmp/ca_1234.crt" - if (only_ca_cert == False): + if only_ca_cert is False: json_dict["public_cert"] = "/tmp/server_1234.crt" json_dict["private_key"] = "/tmp/server_1234.key" def add_broker_tcp_input_grpc_crypto(name: str, add_cert: bool, reversed: bool): - """ - add_broker_tcp_input_grpc_crypto + """ + add_broker_tcp_input_grpc_crypto Add grpc crypto to broker tcp input Args: - name (str): - add_cert (bool): - reversed (bool): + name (str): + add_cert (bool): + reversed (bool): Example: | Add Broker Tcp Input Grpc Crypto | central | ${True} | ${False} | Returns: N/A - - """ + """ def _crypto_modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -612,8 +606,8 @@ def add_broker_tcp_output_grpc_crypto(name: str, add_cert: bool, reversed: bool) | Add Broker Tcp Output Grpc Crypto | module0 | ${True} | ${False} | Returns: N/A - - """ + + """ def _crypto_modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -624,7 +618,7 @@ def _crypto_modifier(conf): def add_host_to_broker_output(name: str, output_name: str, host_ip: str): """ - + add_host_to_broker_output Add host to broker output @@ -638,8 +632,8 @@ def add_host_to_broker_output(name: str, output_name: str, host_ip: str): | Add Host To Broker Output | module0 | central-module-master-output | localhost | Returns: N/A - - """ + + """ def modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -650,7 +644,7 @@ def modifier(conf): def add_host_to_broker_input(name: str, input_name: str, host_ip: str): """ - + add_host_to_broker_input Add host to broker input @@ -664,8 +658,8 @@ def add_host_to_broker_input(name: str, input_name: str, host_ip: str): | Add Host To Broker Input | central | central-broker-master-input | localhost | Returns: N/A - - """ + + """ def modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -676,7 +670,7 @@ def modifier(conf): def remove_host_from_broker_output(name: str, output_name: str): """ - + remove_host_from_broker_output Remove host from broker output @@ -689,8 +683,8 @@ def remove_host_from_broker_output(name: str, output_name: str): | Remove Host From Broker Output | module0 | central-module-master-output | Returns: N/A - - """ + + """ def modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -701,7 +695,7 @@ def modifier(conf): def remove_host_from_broker_input(name: str, input_name: str): """ - + remove_host_from_broker_input Remove host from broker input @@ -714,8 +708,8 @@ def remove_host_from_broker_input(name: str, input_name: str): | Remove Host From Broker Input | central | central-broker-master-input | Returns: N/A - - """ + + """ def modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -726,7 +720,7 @@ def modifier(conf): def change_broker_compression_output(config_name: str, output_name: str, compression_value: str): """ - + change_broker_compression_output Update broker configuration compression output @@ -740,8 +734,8 @@ def change_broker_compression_output(config_name: str, output_name: str, compres | Change Broker Compression Output | module0 | central-module-master-output | yes | Returns: N/A - - """ + + """ def compression_modifier(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -752,7 +746,7 @@ def compression_modifier(conf): def change_broker_compression_input(config_name: str, input_name: str, compression_value: str): """ - + change_broker_compression_input Update broker configuration compression input @@ -766,8 +760,8 @@ def change_broker_compression_input(config_name: str, input_name: str, compressi | Change Broker Compression Input | central | central-broker-master-input | yes | Returns: N/A - - """ + + """ def compression_modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -778,7 +772,7 @@ def compression_modifier(conf): def config_broker_remove_rrd_output(name): """ - + config_broker_remove_rrd_output Remove rrd output from broker configuration @@ -790,8 +784,8 @@ def config_broker_remove_rrd_output(name): | Config Broker Remove Rrd Output | central | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -814,7 +808,7 @@ def config_broker_remove_rrd_output(name): def config_broker_bbdo_input(name, stream, port, proto, host=None): """ - + config_broker_bbdo_input Configure broker bbdo input @@ -831,8 +825,8 @@ def config_broker_bbdo_input(name, stream, port, proto, host=None): | Config Broker Bbdo Input | rrd | bbdo_client | 5670 | tcp | localhost | Returns: N/A - - """ + + """ if stream != "bbdo_server" and stream != "bbdo_client": raise Exception( "config_broker_bbdo_input_output() function only accepts stream in ('bbdo_server', 'bbdo_client')") @@ -872,7 +866,7 @@ def config_broker_bbdo_input(name, stream, port, proto, host=None): def config_broker_bbdo_output(name, stream, port, proto, host=None): """ - + config_broker_bbdo_output Configure broker bbdo output @@ -888,8 +882,8 @@ def config_broker_bbdo_output(name, stream, port, proto, host=None): | Config Broker Bbdo Output | central | bbdo_server | 5670 | tcp | localhost | Returns: N/A - - """ + + """ if stream != "bbdo_server" and stream != "bbdo_client": raise Exception( "config_broker_bbdo_output() function only accepts stream in ('bbdo_server', 'bbdo_client')") @@ -930,7 +924,7 @@ def config_broker_bbdo_output(name, stream, port, proto, host=None): def config_broker_sql_output(name, output, queries_per_transaction: int = 20000): """ - + config_broker_sql_output Configure broker sql output @@ -941,8 +935,8 @@ def config_broker_sql_output(name, output, queries_per_transaction: int = 20000) queries_per_transaction (int, optional): Defaults to 20000. Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1023,7 +1017,7 @@ def config_broker_sql_output(name, output, queries_per_transaction: int = 20000) def broker_config_clear_outputs_except(name, ex: list): """ - + broker_config_clear_outputs_except Configure broker to clear outputs except the one in the list @@ -1031,13 +1025,13 @@ def broker_config_clear_outputs_except(name, ex: list): Args: name (_type_): ex (list): - + Example: | Broker Config Clear Outputs Except | central | ["sql", "storage"] | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1059,7 +1053,7 @@ def broker_config_clear_outputs_except(name, ex: list): def config_broker_victoria_output(): """ - + config_broker_victoria_output Configure broker to add victoria output @@ -1068,8 +1062,8 @@ def config_broker_victoria_output(): | Config Broker Victoria Output | Returns: N/A - - """ + + """ filename = "central-broker.json" with open(ETC_ROOT + "/centreon-broker/{}".format(filename), "r") as f: @@ -1094,7 +1088,7 @@ def config_broker_victoria_output(): def broker_config_add_item(name, key, value): """ - + broker_config_add_item Add item to broker configuration @@ -1108,8 +1102,8 @@ def broker_config_add_item(name, key, value): | Broker Config Add Item | module0 | bbdo_version | 3.0.1 | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name == 'rrd': @@ -1129,7 +1123,7 @@ def broker_config_add_item(name, key, value): def broker_config_remove_item(name, key): """ - + broker_config_remove_item Remove item from broker configuration @@ -1142,7 +1136,7 @@ def broker_config_remove_item(name, key): | Broker Config Remove Item | module0 | bbdo_version | Returns: N/A - + """ if name == 'central': filename = "central-broker.json" @@ -1163,7 +1157,7 @@ def broker_config_remove_item(name, key): def broker_config_add_lua_output(name, output, luafile): """ - + broker_config_add_lua_output Add lua output to broker configuration @@ -1177,8 +1171,8 @@ def broker_config_add_lua_output(name, output, luafile): | `Broker Config Add Lua Output` | central | test-protobuf | /tmp/lua.lua | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1203,7 +1197,7 @@ def broker_config_add_lua_output(name, output, luafile): def broker_config_output_set(name, output, key, value): """ - + broker_config_output_set Configure broker output set. @@ -1218,8 +1212,8 @@ def broker_config_output_set(name, output, key, value): | Broker Config Output Set | central | central-broker-master-sql | host | localhost | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1239,7 +1233,7 @@ def broker_config_output_set(name, output, key, value): def broker_config_output_set_json(name, output, key, value): """ - + broker_config_output_set_json Configure broker output set json. @@ -1254,8 +1248,8 @@ def broker_config_output_set_json(name, output, key, value): | Broker Config Output Set Json | central | central-broker-master-sql | filters | {"category": ["neb", "foo", "bar"]} | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1277,7 +1271,7 @@ def broker_config_output_set_json(name, output, key, value): def broker_config_output_remove(name, output, key): """ - + broker_config_output_remove Configure broker output remove. @@ -1291,8 +1285,8 @@ def broker_config_output_remove(name, output, key): | Broker Config Output Remove | central | centreon-broker-master-rrd | host | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1315,7 +1309,7 @@ def broker_config_output_remove(name, output, key): def broker_config_input_set(name, inp, key, value): """ - + broker_config_input_set Configure broker input set. @@ -1330,8 +1324,8 @@ def broker_config_input_set(name, inp, key, value): | Broker Config Input Set | rrd | rrd-broker-master-input | encryption | yes | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1373,7 +1367,7 @@ def broker_config_input_remove(name, inp, key): def broker_config_log(name, key, value): """ - + broker_config_log Configure broker log level. @@ -1387,8 +1381,8 @@ def broker_config_log(name, key, value): | Broker Config Log | central | bam | trace | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1408,7 +1402,7 @@ def broker_config_log(name, key, value): def broker_config_flush_log(name, value): """ - + broker_config_flush_log Flush broker configuration log. @@ -1421,8 +1415,8 @@ def broker_config_flush_log(name, value): | Broker Config Flush Log | central | 1 | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1442,7 +1436,7 @@ def broker_config_flush_log(name, value): def broker_config_source_log(name, value): """ - + broker_config_source_log Configure the log source. @@ -1455,8 +1449,8 @@ def broker_config_source_log(name, value): | Broker Config Source Log | central | 1 | Returns: N/A - - """ + + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -1476,7 +1470,7 @@ def broker_config_source_log(name, value): def check_broker_stats_exist(name, key1, key2, timeout=TIMEOUT): """ - + check_broker_stats_exist Return True if the stats key exists. @@ -1493,8 +1487,8 @@ def check_broker_stats_exist(name, key1, key2, timeout=TIMEOUT): | Should Be True | ${exist} | Returns: N/A - - """ + + """ limit = time.time() + timeout while time.time() < limit: if name == 'central': @@ -1522,7 +1516,7 @@ def check_broker_stats_exist(name, key1, key2, timeout=TIMEOUT): def get_broker_stats_size(name, key, timeout=TIMEOUT): """ - + get_broker_stats_size Return the size of the stats key. @@ -1536,8 +1530,8 @@ def get_broker_stats_size(name, key, timeout=TIMEOUT): | ${size} | Get Broker Stats Size | central | poller | # 2 | Returns: N/A - - """ + + """ limit = time.time() + timeout retval = 0 while time.time() < limit: @@ -1568,7 +1562,8 @@ def get_broker_stats_size(name, key, timeout=TIMEOUT): time.sleep(5) return retval -def get_broker_stats(name: str, expected:str, timeout: int, *keys): + +def get_broker_stats(name: str, expected: str, timeout: int, *keys): """! read a value from broker stats @param name central, module or rrd @@ -1581,7 +1576,7 @@ def get_broker_stats(name: str, expected:str, timeout: int, *keys): def json_get(json_dict, keys: tuple, index: int): try: key = keys[index] - if index == len(keys) -1: + if index == len(keys) - 1: return json_dict[key] else: return json_get(json_dict[key], keys, index + 1) @@ -1618,7 +1613,7 @@ def json_get(json_dict, keys: tuple, index: int): def get_not_existing_indexes(count: int): """ - + get_not_existing_indexes Gets count indexes that does not exist in index_data. @@ -1632,8 +1627,8 @@ def get_not_existing_indexes(count: int): | Log To Console | @{indexes} | Returns: a list of index ids. - - """ + + """ # Connect to the database connection = pymysql.connect(host=DB_HOST, user=DB_USER, @@ -1666,7 +1661,7 @@ def get_not_existing_indexes(count: int): def get_indexes_to_delete(count: int): """ - + get_indexes_to_delete Gets count indexes from available ones. Return a list of indexes that does not exist in data_index. @@ -1681,7 +1676,7 @@ def get_indexes_to_delete(count: int): Returns: LIST: a list of index ids. - """ + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1725,7 +1720,7 @@ def delete_all_rrd_metrics(): def check_rrd_info(metric_id: int, key: str, value, timeout: int = 60): """ - + check_rrd_info Execute rrdtool info and check one value of the returned informations @@ -1741,8 +1736,8 @@ def check_rrd_info(metric_id: int, key: str, value, timeout: int = 60): | Should Be True | ${result} | Returns: N/A - - """ + + """ limit = time.time() + timeout while time.time() < limit: res = getoutput( @@ -1759,7 +1754,7 @@ def check_rrd_info(metric_id: int, key: str, value, timeout: int = 60): def get_metrics_for_service(service_id: int, metric_name: str = "%", timeout: int = 60): """ - + get_metrics_for_service scan data base every 5s to extract metric ids for a service @@ -1773,8 +1768,8 @@ def get_metrics_for_service(service_id: int, metric_name: str = "%", timeout: in | ${metrics} | Get Metrics For Service | 1 | % | Returns: N/A - - """ + + """ limit = time.time() + timeout select_request = f"SELECT metric_id FROM metrics JOIN index_data ON index_id=id WHERE service_id={service_id} and metric_name like '{metric_name}'" @@ -1802,7 +1797,7 @@ def get_metrics_for_service(service_id: int, metric_name: str = "%", timeout: in def get_not_existing_metrics(count: int): """ - + get_not_existing_metrics Return a list of metrics that does not exist. @@ -1815,8 +1810,8 @@ def get_not_existing_metrics(count: int): | Log To Console | @{metrics} | Returns: N/A - - """ + + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1841,7 +1836,7 @@ def get_not_existing_metrics(count: int): index = 1 retval = [] while len(retval) < count: - if not index in inter: + if index not in inter: retval.append(index) index += 1 return retval @@ -2027,7 +2022,7 @@ def stop_map(): def get_indexes_to_rebuild(count: int, nb_day=180): """ - + get_indexes_to_rebuild Get count indexes that are available to rebuild them. @@ -2037,8 +2032,8 @@ def get_indexes_to_rebuild(count: int, nb_day=180): nb_day (int, optional): Defaults to 180. Returns: a list of indexes - - """ + + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -2086,7 +2081,7 @@ def get_indexes_to_rebuild(count: int, nb_day=180): def add_duplicate_metrics(): """ - + add_duplicate_metrics add a value at the mid of the first day of each metric @@ -2094,7 +2089,7 @@ def add_duplicate_metrics(): Returns: list : a list of indexes of pair