This file is indexed.

/usr/lib/python2.7/dist-packages/haproxy/tests/test_argparse.py is in python-haproxy-log-analysis 2.0~b0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
from haproxy import filters
from haproxy.logfile import Log
from haproxy.main import create_parser
from haproxy.main import main
from haproxy.main import parse_arguments
from haproxy.main import VALID_FILTERS
from tempfile import NamedTemporaryFile

import os
import sys
import unittest


class RedirectStdout(object):
    """Context manager class that redirects standard output to a file.

    This helps analyzing standard output (print() function output) on tests
    without having to do any change on the code, just on tests.
    """

    def __init__(self, stdout=None):
        self._stdout = stdout or sys.stdout
        self.old_stdout = None

    def __enter__(self):
        self.old_stdout = sys.stdout
        self.old_stdout.flush()
        sys.stdout = self._stdout

    def __exit__(self, exc_type, exc_value, traceback):
        self._stdout.flush()
        sys.stdout = self.old_stdout


class ArgumentParsingTest(unittest.TestCase):

    def setUp(self):
        self.parser = create_parser()
        self.default_arguments = [
            '-c', 'counter', '-l', 'haproxy/tests/files/huge.log',
        ]

    def tearDown(self):
        """Be sure to remove all pickle files so to not keep stale files
        around.
        """
        path = 'haproxy/tests/files'
        for filename in os.listdir(path):
            if filename.endswith('.pickle'):
                os.remove('{0}/{1}'.format(path, filename))

    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_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_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_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_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'])

    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_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_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_filters_valid(self):
        """Test that valid filters are correctly parsed."""
        arguments = ['-f', 'ssl',
                     '-l', 'haproxy/tests/files/huge.log', ]
        data = parse_arguments(self.parser.parse_args(arguments))
        self.assertEqual([('ssl', None)], data['filters'])

    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'])

    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_filters_invalid_argument(self):
        """Test that trying to input an invalid filter expression fails."""
        arguments = ['--filter', 'ip_with_error],ssl',
                     '-l', 'haproxy/tests/files/huge.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_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_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_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_only_log_file(self):
        """Test that when no arguments are given the help is shown."""
        arguments = ['-l', 'haproxy/tests/files/queue.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()

            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_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)

    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_valid_filters(self):
        """Ensure that all but time_frame filters are available."""
        methods = dir(filters)
        for valid_filter in VALID_FILTERS:
            filter_name = 'filter_{0}'.format(valid_filter)
            self.assertTrue(filter_name in methods)

        self.assertFalse('time_frame' in VALID_FILTERS)

    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_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_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)