def check_ignore_binaries_valid(cfg):
    """Check if the Wazuh runs correctly with the specified ignore_binaries field value.

    Ensure logcollector allows the specified ignore_binaries attribute. Also, in the case of the manager instance,
    check if the API answer for localfile block coincides.

    Args:
        cfg (dict): Dictionary with the localfile configuration.

    Raises:
        TimeoutError: In the case of Windows system, the callback for an invalid location pattern is not generated.
        AssertError: In the case of a server instance, the API response is different than the real configuration.
    """
    wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)

    if sys.platform == 'win32':
        log_callback = logcollector.callback_invalid_location_pattern(
            cfg['location'], prefix=prefix)
        wazuh_log_monitor.start(
            timeout=5,
            callback=log_callback,
            error_message=logcollector.GENERIC_CALLBACK_ERROR_INVALID_LOCATION)

    if wazuh_component == 'wazuh-manager':
        real_configuration = cfg.copy()
        real_configuration.pop('valid_value')
        api.wait_until_api_ready()
        api.compare_config_api_response([real_configuration], 'localfile')
    else:
        if sys.platform == 'win32':
            assert get_process_cmd('wazuh-agent.exe') != 'None'
        else:
            assert check_if_process_is_running('wazuh-logcollector')
def test_configuration_command(get_local_internal_options,
                               configure_local_internal_options,
                               get_configuration, configure_environment,
                               restart_logcollector):
    """Check if the Wazuh run correctly with the specified command monitoring configuration.

    Ensure command monitoring allow the specified attributes. Also, in the case of the manager instance, check if the API
    answer for localfile block coincides.

    Raises:
        TimeoutError: If the command monitoring callback is not generated.
        AssertError: In the case of a server instance, the API response is different that the real configuration.
    """
    cfg = get_configuration['metadata']

    log_callback = logcollector.callback_monitoring_command(cfg['log_format'],
                                                            cfg['command'],
                                                            prefix=prefix)
    wazuh_log_monitor.start(
        timeout=5,
        callback=log_callback,
        error_message=logcollector.GENERIC_CALLBACK_ERROR_COMMAND_MONITORING)

    if wazuh_component == 'wazuh-manager':
        api.wait_until_api_ready()
        api.compare_config_api_response([cfg], 'localfile')
def check_configuration_age_valid(cfg):
    """Check if the Wazuh module runs correctly and analyze the desired file.

    Ensure logcollector is running with the specified configuration, analyzing the designated file and,
    in the case of the Wazuh server, check if the API answer for localfile configuration block coincides
    the selected configuration.

    Args:
        cfg (dict): Dictionary with the localfile configuration.

    Raises:
        TimeoutError: If the "Analyzing file" callback is not generated.
        AssertError: In the case of a server instance, the API response is different from real configuration.
    """
    wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)

    log_callback = logcollector.callback_analyzing_file(cfg['location'],
                                                        prefix=prefix)
    wazuh_log_monitor.start(
        timeout=5,
        callback=log_callback,
        error_message=logcollector.GENERIC_CALLBACK_ERROR_ANALYZING_FILE)
    if wazuh_component == 'wazuh-manager':
        real_configuration = cfg.copy()
        real_configuration.pop('valid_value')
        api.wait_until_api_ready()
        api.compare_config_api_response([real_configuration], 'localfile')
def check_configuration_out_format_valid(cfg):
    """Check if the Wazuh run correctly with the specified out format field.

    Ensure logcollector allows the specified out format attribute.

    Args:
        cfg (dict): Dictionary with the localfile configuration.

    Raises:
        TimeoutError: If the callback for the socket target is not generated.
        AssertError: In the case of a server instance, the API response is different than the real configuration.
    """
    log_callback = logcollector.callback_socket_target(cfg['location'],
                                                       cfg['target'],
                                                       prefix=prefix)
    wazuh_log_monitor.start(
        timeout=5,
        callback=log_callback,
        error_message=logcollector.GENERIC_CALLBACK_ERROR_TARGET_SOCKET)

    if wazuh_component == 'wazuh-manager':
        real_configuration = dict(
            (key, cfg[key]) for key in ('location', 'target', 'log_format'))
        real_configuration['out_format'] = {
            'target': cfg['target_out_format'],
            'item': cfg['out_format']
        }
        api.wait_until_api_ready()
        api.compare_config_api_response([real_configuration], 'localfile')
def test_configuration_location(get_configuration, configure_environment,
                                restart_logcollector):
    """Check if Wazuh runs correctly with the specified location field value.

    Ensure logcollector allows the specified locations. Also, in the case of the manager instance, check if the API
    answer for localfile block coincides.

    Raises:
        TimeoutError: If the "Analyzing file" callback is not generated.
        AssertError: In the case of a server instance, the API response is different that the real configuration.
    """
    cfg = get_configuration['metadata']

    if wazuh_component == 'wazuh-manager':
        api.wait_until_api_ready()
        api.compare_config_api_response([cfg], 'localfile')
    else:
        if sys.platform == 'win32':
            assert get_process_cmd('wazuh-agent.exe') != 'None'
        else:
            assert check_if_process_is_running('wazuh-logcollector')
def check_log_format_valid(cfg):
    """Check if Wazuh run correctly with the specified log formats.

    Ensure logcollector allows the specified log formats. Also, in the case of the manager instance, check if the API
    answer for localfile block coincides.

    Raises:
        TimeoutError: If the "Analyzing file" callback is not generated.
        AssertError: In the case of a server instance, the API response is different that the real configuration.
    """
    wazuh_log_monitor = FileMonitor(LOG_FILE_PATH)

    if cfg['log_format'] not in log_format_not_print_analyzing_info:
        log_callback = logcollector.callback_analyzing_file(cfg['location'],
                                                            prefix=prefix)
        wazuh_log_monitor.start(
            timeout=5,
            callback=log_callback,
            error_message=logcollector.GENERIC_CALLBACK_ERROR_ANALYZING_FILE)
    elif 'command' in cfg['log_format']:
        log_callback = logcollector.callback_monitoring_command(
            cfg['log_format'], cfg['command'], prefix=prefix)
        wazuh_log_monitor.start(timeout=5,
                                callback=log_callback,
                                error_message=logcollector.
                                GENERIC_CALLBACK_ERROR_COMMAND_MONITORING)
    elif cfg['log_format'] == 'djb-multilog':
        log_callback = logcollector.callback_monitoring_djb_multilog(
            cfg['location'], prefix=prefix)
        wazuh_log_monitor.start(
            timeout=5,
            callback=log_callback,
            error_message="The expected multilog djb log has not been produced"
        )

    if wazuh_component == 'wazuh-manager':
        real_configuration = cfg.copy()
        real_configuration.pop('valid_value')
        api.wait_until_api_ready()
        api.compare_config_api_response([real_configuration], 'localfile')