Esempio n. 1
0
def test_json_output(capsys, default_arguments):
    """Check that the JSON switch is used and JSON output is printed."""
    default_arguments['json'] = True
    main(default_arguments)
    output_text = capsys.readouterr().out
    assert 'COUNTER\n=======\n9' not in output_text
    assert '{"COUNTER": 9}' in output_text
Esempio n. 2
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)
Esempio n. 3
0
def test_main_with_filter(capsys, default_arguments):
    """Check that the filters are applied as expected."""
    default_arguments['filters'] = [
        ('server', 'instance1'),
    ]
    main(default_arguments)
    output_text = capsys.readouterr().out
    assert 'COUNTER\n=======\n4' in output_text
Esempio n. 4
0
def test_main_negate_filter(capsys, default_arguments):
    """Check that filters can be reversed."""
    default_arguments['filters'] = [
        ('server', 'instance1'),
    ]
    default_arguments['negate_filter'] = True
    main(default_arguments)
    output_text = capsys.readouterr().out
    assert 'COUNTER\n=======\n5' in output_text
Esempio n. 5
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
Esempio n. 6
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)
    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)
    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)
Esempio n. 10
0
    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)
Esempio n. 11
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_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)
Esempio n. 13
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)
Esempio n. 14
0
    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)
Esempio n. 15
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)
Esempio n. 17
0
    def test_from_main(self):
        log_path = 'haproxy/tests/files/small.log'
        data = {
            'start': None,
            'delta': None,
            'log': log_path,
            'commands': ['counter', ],
            'negate_filter': None,
            'filters': None,
            'list_commands': False,
            'list_filters': False,
        }
        logfile = main(data)

        self.assertEqual(logfile.logfile, log_path)
Esempio n. 18
0
    def test_haproxy_log_file_from_main(self):
        log_path = 'haproxy/tests/files/small.log'
        data = {
            'start': None,
            'delta': None,
            'log': log_path,
            'commands': ['counter', ],
            'negate_filter': None,
            'filters': None,
            'list_commands': False,
            'list_filters': False,
        }
        logfile = main(data)

        self.assertEqual(logfile.logfile, log_path)
Esempio n. 19
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)
    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)
Esempio n. 21
0
def test_main(capsys, default_arguments):
    """Check that the main function works as expected with default arguments."""
    main(default_arguments)
    output_text = capsys.readouterr().out
    assert 'COUNTER\n=======\n9' in output_text
Esempio n. 22
0
def test_print_no_output(capsys, default_arguments):
    """Check that the print header is not shown."""
    default_arguments['commands'] = ['print']
    main(default_arguments)
    output_text = capsys.readouterr().out
    assert 'PRINT\n=====' not in output_text