Exemplo n.º 1
0
def test_return_raw_outputs_from_log_with_raw_response_flag(
    mocker,
    set_environment_variables,
):
    """
    Validates that the raw outputs of a log file is extracted correctly while using the raw_output parameter,
     even if the file has a context part

    """
    file_path = f'{git_path()}/demisto_sdk/commands/run_cmd/tests/test_data/kl-get-component.txt'
    expected_output = [{
        'ID': 10082,
        'Name': 'Projects',
        'ShortName': 'Projects',
        'SystemName': 'Projects'
    }, {
        'ID': 10077,
        'Name': 'Universe',
        'ShortName': 'Universe',
        'SystemName': 'Universe'
    }]
    mocker.patch.object(DefaultApi, 'download_file', return_value=file_path)
    runner = Runner('Query', json_to_outputs=True, raw_response=True)
    temp = runner._return_context_dict_from_log(['123'])
    assert temp == expected_output
Exemplo n.º 2
0
def execute_command(command_example, insecure: bool):
    errors = []
    context = {}
    md_example: str = ''
    cmd = command_example
    try:
        runner = Runner('', insecure=insecure)
        res, raw_context = runner.execute_command(command_example)
        if not res:
            raise RuntimeError('something went wrong with your command: {}'.format(command_example))

        for entry in res:
            if is_error(entry):
                raise RuntimeError('something went wrong with your command: {}'.format(command_example))

            if raw_context:
                context = {k.split('(')[0]: v for k, v in raw_context.items()}

            if entry.contents:
                content: str = entry.contents
                if isinstance(content, STRING_TYPES):
                    md_example = format_md(content)
                else:
                    md_example = f'```\n{json.dumps(content, sort_keys=True, indent=4)}\n```'

    except RuntimeError:
        errors.append('The provided example for cmd {} has failed...'.format(cmd))

    except Exception as e:
        errors.append(
            'Error encountered in the processing of command {}, error was: {}. '.format(cmd, str(e)) +
            '. Please check your command inputs and outputs')

    cmd = cmd.split(' ')[0][1:]
    return cmd, md_example, context, errors
Exemplo n.º 3
0
def test_return_raw_outputs_from_log(mocker, set_environment_variables,
                                     file_path, expected_output):
    """
    Validates that the context of a log file is extracted correctly.

    """
    mocker.patch.object(DefaultApi, 'download_file', return_value=file_path)
    runner = Runner('Query', json_to_outputs=True)
    temp = runner._return_context_dict_from_log(['123'])
    assert temp == expected_output
Exemplo n.º 4
0
def test_return_raw_outputs_from_log_also_write_log(mocker,
                                                    set_environment_variables,
                                                    file_path,
                                                    expected_output):
    """
    Validates that the context of a log file is extracted correctly and that the log file is saved correctly in
    the expected output path.

    """
    mocker.patch.object(DefaultApi, 'download_file', return_value=file_path)
    temp_file = tempfile.NamedTemporaryFile()
    runner = Runner('Query', debug_path=temp_file.name, json_to_outputs=True)
    temp = runner._return_context_dict_from_log(['123'])
    assert temp == expected_output
    assert filecmp.cmp(file_path, temp_file.name)
    temp_file.close()
Exemplo n.º 5
0
def run(**kwargs):
    runner = Runner(**kwargs)
    return runner.run()