def test_arg_parser_start_invalid(self):
     """Check that if a 'start' argument is not valid an exception is
     raised.
     """
     arguments = ['-s', '/Dec/2013:14:15:16'] + self.default_arguments
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_delta_invalid(self):
     """Check that if an invalid delta argument is passed an exception is
     raised.
     """
     arguments = ['-d', 'invalid'] + self.default_arguments
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_start_invalid(self):
     """Check that if a 'start' argument is not valid an exception is
     raised.
     """
     arguments = ['-s', '/Dec/2013:14:15:16', ] + self.default_arguments
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_delta_invalid(self):
     """Check that if an invalid delta argument is passed an exception is
     raised.
     """
     arguments = ['-d', 'invalid', ] + self.default_arguments
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_log_file_invalid(self):
     """Check that if the log file passed does not exist an exception is
     raised.
     """
     arguments = ['-c', 'counter', '-l', 'non_existing.log']
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_filters_without_closing_bracket(self):
     """Test that trying to input an invalid filter expression fails."""
     arguments = [
         '--filter', 'ip],ssl',
         '-l', 'haproxy/tests/files/huge.log',
     ]
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_filters_invalid(self):
     """Test that trying to input non existing filters raises an
     exception.
     """
     arguments = ['--filter', 'non_existing_filter',
                  '-l', 'haproxy/tests/files/huge.log', ]
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_log_file_invalid(self):
     """Check that if the log file passed does not exist an exception is
     raised.
     """
     arguments = ['-c', 'counter',
                  '-l', 'non_existing.log', ]
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
 def test_arg_parser_commands_invalid(self):
     """Test that trying to input non existing commands raises an
     exception.
     """
     with self.assertRaises(ValueError):
         arguments = ['-c', 'non_existing_method',
                      '-l', 'haproxy/tests/files/huge.log', ]
         parse_arguments(self.parser.parse_args(arguments))
Example #10
0
def test_arguments_delta_invalid(delta):
    """Incorrectly formatted delta argument raises an exception."""
    parser = create_parser()
    with pytest.raises(ValueError) as exception_info:
        parse_arguments(parser.parse_args([
            '-d',
            delta,
        ]))
    assert '--delta argument is not valid' in str(exception_info)
Example #11
0
def test_arguments_date_invalid(start):
    """Incorrectly formatted start argument raises an exception."""
    parser = create_parser()
    with pytest.raises(ValueError) as exception_info:
        parse_arguments(parser.parse_args([
            '-s',
            start,
        ]))
    assert '--start argument is not valid' in str(exception_info)
Example #12
0
def test_log_argument(filename, is_valid):
    """Check that the argument parsing validates that the file exists."""
    parser = create_parser()
    if is_valid:
        data = parse_arguments(parser.parse_args(['-l', filename]))
        assert data['log'] == filename
    else:
        with pytest.raises(ValueError) as exception_info:
            parse_arguments(parser.parse_args(['-l', filename]))
        assert f'{filename} does not exist' in str(exception_info)
Example #13
0
 def test_arg_parser_filters_without_closing_bracket(self):
     """Test that trying to input an invalid filter expression fails."""
     arguments = [
         '--filter',
         'ip],ssl',
         '-l',
         'haproxy/tests/files/huge.log',
     ]
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
Example #14
0
    def test_arg_parser_negate_filter_output(self):
        """Check that if the negate filter argument is set, is actually used.
        """
        arguments = [
            '-c',
            'counter',
            '-l',
            'haproxy/tests/files/small.log',
            '-f',
            'server[instance3]',
            '-n',
        ]

        # with the negate argument set, there should be all but instance3 lines
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('7', output_text)

        # remove the negate argument, now only 2 lines should match
        arguments.pop()
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('2', output_text)

        # finally remove the filter, 9 lines should match
        arguments.pop()
        arguments.pop()  # this second pop() is because of the argument
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('9', output_text)
Example #15
0
 def test_arg_parser_filters_invalid(self):
     """Test that trying to input non existing filters raises an
     exception.
     """
     arguments = [
         '--filter',
         'non_existing_filter',
         '-l',
         'haproxy/tests/files/huge.log',
     ]
     with self.assertRaises(ValueError):
         parse_arguments(self.parser.parse_args(arguments))
Example #16
0
 def test_arg_parser_commands_invalid(self):
     """Test that trying to input non existing commands raises an
     exception.
     """
     with self.assertRaises(ValueError):
         arguments = [
             '-c',
             'non_existing_method',
             '-l',
             'haproxy/tests/files/huge.log',
         ]
         parse_arguments(self.parser.parse_args(arguments))
    def test_arg_parser_negate_filter_output(self):
        """Check that if the negate filter argument is set, is actually used.
        """
        arguments = [
            '-c', 'counter',
            '-l', 'haproxy/tests/files/small.log',
            '-f', 'server[instance3]',
            '-n',
        ]

        # with the negate argument set, there should be all but instance3 lines
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('7', output_text)

        # remove the negate argument, now only 2 lines should match
        arguments.pop()
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('2', output_text)

        # finally remove the filter, 9 lines should match
        arguments.pop()
        arguments.pop()  # this second pop() is because of the argument
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('9', output_text)
 def test_arg_parser_negate_filter_parsed(self):
     """Check that if the negate filter argument is set, is parsed
     correctly.
     """
     arguments = ['-n'] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertTrue(data['negate_filter'])
 def test_arg_parser_negate_filter_parsed(self):
     """Check that if the negate filter argument is set, is parsed
     correctly.
     """
     arguments = ['-n', ] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertTrue(data['negate_filter'])
 def test_arg_parser_log_file_valid(self):
     """Check that any log file passed does exist before handling it
     further.
     """
     arguments = ['-c', 'counter', '-l', 'haproxy/tests/test_argparse.py']
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual('haproxy/tests/test_argparse.py', data['log'])
Example #21
0
def test_commands_arguments(cmds, is_valid):
    """Test that the commands are parsed, and an exception raised otherwise."""
    parser = create_parser()
    if not is_valid:
        with pytest.raises(ValueError) as exception_info:
            parse_arguments(parser.parse_args([
                '-c',
                cmds,
            ]))
        assert 'is not available. Use --list-commands' in str(exception_info)
    else:
        data = parse_arguments(parser.parse_args([
            '-c',
            cmds,
        ]))
        assert data['commands'] == cmds.split(',')
 def test_arg_parser_filters_valid_with_argument(self):
     """Test that valid filters with arguments are correctly parsed."""
     arguments = ['-f', 'ip[something],ssl',
                  '-l', 'haproxy/tests/files/huge.log', ]
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual([('ip', 'something'), ('ssl', None)],
                      data['filters'])
Example #23
0
def test_filters_arguments(filters_list, is_valid):
    """Test that the filters are parsed, and an exception raised otherwise."""
    parser = create_parser()
    if not is_valid:
        with pytest.raises(ValueError) as exception_info:
            parse_arguments(parser.parse_args([
                '-f',
                filters_list,
            ]))
        assert 'is not available. Use --list-filters' in str(exception_info)
    else:
        data = parse_arguments(parser.parse_args([
            '-f',
            filters_list,
        ]))
        assert data['filters'] == [(x, None) for x in filters_list.split(',')]
Example #24
0
def test_parser_boolean_arguments(argument, option):
    """Test that the argument parsing defaults works."""
    parser = create_parser()
    data = parse_arguments(parser.parse_args([
        argument,
    ]))
    assert data[option] is True
Example #25
0
    def test_arg_parser_filters_start_and_delta(self):
        """Check that the filter_time is applied on the log file if a start
        and delta arguments are given.
        """
        arguments = [
            '-s',
            '11/Dec/2015:11',
            '-d',
            '3h',
            '-c',
            'counter',
            '-l',
            'haproxy/tests/files/filters.log',
        ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('2', output_text)
 def test_arg_parser_commands_valid(self):
     """Test that valid commands are correctly parsed."""
     arguments = [
         '-c', 'http_methods',
         '-l', 'haproxy/tests/files/huge.log',
     ]
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual(['http_methods', ], data['commands'])
 def test_arg_parser_log_file_valid(self):
     """Check that any log file passed does exist before handling it
     further.
     """
     arguments = ['-c', 'counter',
                  '-l', 'haproxy/tests/test_argparse.py', ]
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual('haproxy/tests/test_argparse.py', data['log'])
Example #28
0
def test_show_help(capsys):
    """Check that the help is shown if no arguments are given."""
    parser = create_parser()
    data = parse_arguments(parser.parse_args([]))
    main(data)
    output_text = capsys.readouterr().out
    assert 'optional arguments:' in output_text
    assert '--list-filters ' in output_text
    assert '--list-commands ' in output_text
Example #29
0
 def test_arg_parser_delta_valid(self):
     """Check that if a 'delta' argument is valid is stored."""
     delta = '4d'
     arguments = [
         '-d',
         delta,
     ] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual(delta, data['delta'])
Example #30
0
 def test_arg_parser_start_valid(self):
     """Check that if a 'start' argument is valid is stored."""
     start = '12/Dec/2013:14:15:16'
     arguments = [
         '-s',
         start,
     ] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual(start, data['start'])
    def test_arg_parser_list_commands(self):
        """Test that list commands argument is parsed."""
        arguments = ['--list-commands']
        data = parse_arguments(self.parser.parse_args(arguments))

        for arg in data:
            if arg == 'list_commands':
                self.assertTrue(data['list_commands'])
            else:
                self.assertEqual(data[arg], None)
    def test_arg_parser_list_commands(self):
        """Test that list commands argument is parsed."""
        arguments = ['--list-commands', ]
        data = parse_arguments(self.parser.parse_args(arguments))

        for arg in data:
            if arg == 'list_commands':
                self.assertTrue(data['list_commands'])
            else:
                self.assertEqual(data[arg], None)
Example #33
0
 def test_arg_parser_filters_valid_with_argument(self):
     """Test that valid filters with arguments are correctly parsed."""
     arguments = [
         '-f',
         'ip[something],ssl',
         '-l',
         'haproxy/tests/files/huge.log',
     ]
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual([('ip', 'something'), ('ssl', None)], data['filters'])
Example #34
0
 def test_arg_parser_commands_valid(self):
     """Test that valid commands are correctly parsed."""
     arguments = [
         '-c',
         'http_methods',
         '-l',
         'haproxy/tests/files/huge.log',
     ]
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual([
         'http_methods',
     ], data['commands'])
Example #35
0
    def test_arg_parser_help_output(self):
        """Test that when no arguments are given the help is shown."""
        data = parse_arguments(self.parser.parse_args([]))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for keyword in ('LOG', 'START', 'DELTA', 'COMMAND'):
                self.assertIn(keyword, output_text)
    def test_arg_parser_help_output(self):
        """Test that when no arguments are given the help is shown."""
        data = parse_arguments(self.parser.parse_args([]))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for keyword in ('LOG', 'START', 'DELTA', 'COMMAND'):
                self.assertIn(keyword, output_text)
Example #37
0
def test_list_filters_and_commands(capsys, switch, listing):
    """Test that one can request the filters/commands to be listed."""
    parser = create_parser()
    data = parse_arguments(parser.parse_args([f'--{switch}',]))
    argument = switch.replace('-', '_')
    for key in data:
        expected = None
        if key == argument:
            expected = True
        assert data[key] is expected
    main(data)
    output_text = capsys.readouterr().out
    for name in listing:
        assert f'{name}: ' in output_text
    def test_arg_parser_list_filters_output(self):
        """Test that list filters argument outputs what's expected."""
        arguments = ['--list-filters', ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for filter_name in VALID_FILTERS:
                self.assertIn(filter_name[7:], output_text)
Example #39
0
def test_arguments_dates(start, delta):
    """Check that properly formatted start and delta arguments are processed fine.

    Thus they are extracted and stored for later use.
    """
    parser = create_parser()
    data = parse_arguments(parser.parse_args([
        '-s',
        start,
        '-d',
        delta,
    ]))
    assert data['start'] == start
    assert data['delta'] == delta
    def test_arg_parser_list_commands_output(self):
        """Test that list commands argument outputs what's expected."""
        arguments = ['--list-commands', ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for cmd in Log.commands():
                self.assertIn(cmd, output_text)
    def test_arg_parser_list_commands_output(self):
        """Test that list commands argument outputs what's expected."""
        arguments = ['--list-commands']
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for cmd in Log.commands():
                self.assertIn(cmd, output_text)
    def test_arg_parser_list_filters_output(self):
        """Test that list filters argument outputs what's expected."""
        arguments = ['--list-filters']
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            for filter_name in VALID_FILTERS:
                self.assertIn(filter_name[7:], output_text)
Example #43
0
def test_parser_arguments_defaults():
    """Test that the argument parsing defaults works."""
    parser = create_parser()
    data = parse_arguments(parser.parse_args([]))
    assert data == {
        'start': None,
        'delta': None,
        'commands': None,
        'filters': None,
        'negate_filter': None,
        'log': None,
        'list_commands': None,
        'list_filters': None,
        'json': False,
        'invalid_lines': False,
    }
    def test_arg_parser_filters(self):
        """Check that the filter logic on haproxy.main.main works as expected.
        """
        arguments = ['-f', 'ssl,ip[1.2.3.4]',
                     '-c', 'counter',
                     '-l', 'haproxy/tests/files/filters.log', ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('2', output_text)
    def test_arg_parser_filters_start(self):
        """Check that the filter_time is applied on the log file if a start
        argument is given.
        """
        arguments = ['-s', '12/Dec/2015',
                     '-c', 'counter',
                     '-l', 'haproxy/tests/files/filters.log', ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('4', output_text)
Example #46
0
    def test_arg_parser_filters(self):
        """Check that the filter logic on haproxy.main.main works as expected.
        """
        arguments = [
            '-f',
            'ssl,ip[1.2.3.4]',
            '-c',
            'counter',
            '-l',
            'haproxy/tests/files/filters.log',
        ]
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)

        with RedirectStdout(stdout=test_output):
            main(data)

        with open(test_output.name, 'r') as output_file:
            output_text = output_file.read()

            self.assertIn('counter', output_text)
            self.assertIn('2', output_text)
    def test_arg_parser_json(self):
        """Test that we really output json."""
        arguments = ['-l', 'haproxy/tests/files/small.log', '--json', '-c', 'counter']
        data = parse_arguments(self.parser.parse_args(arguments))
        test_output = NamedTemporaryFile(mode='w', delete=False)
        test_result = True

        with RedirectStdout(stdout=test_output):
            main(data)

        # Since python 3.5, json.load returns a JSONDecodeError instead of a
        # ValueError
        try:
            json_parse_exception = json.decoder.JSONDecodeError
        except AttributeError:
            json_parse_exception = ValueError

        try:
            with open(test_output.name) as json_file:
                json.load(json_file)
        except json_parse_exception:
            test_result = False

        self.assertTrue(test_result)
 def test_arg_parser_negate_filter_not_set(self):
     """Check that if the negate filter argument is not set, the default
     value is kept.
     """
     data = parse_arguments(self.parser.parse_args(self.default_arguments))
     self.assertFalse(data['negate_filter'])
 def test_arg_parser_delta_valid(self):
     """Check that if a 'delta' argument is valid is stored."""
     delta = '4d'
     arguments = ['-d', delta, ] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual(delta, data['delta'])
 def test_arg_parser_start_valid(self):
     """Check that if a 'start' argument is valid is stored."""
     start = '12/Dec/2013:14:15:16'
     arguments = ['-s', start, ] + self.default_arguments
     data = parse_arguments(self.parser.parse_args(arguments))
     self.assertEqual(start, data['start'])