summaryrefslogtreecommitdiffstats
path: root/tests/test_config_parser.py
blob: 3b6504b7f084f7cd6720311b59571ee54678222c (plain)
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
from ngxtop import config_parser


def test_get_log_formats():
    config = '''
        http {
            # ubuntu default, log_format on multiple lines
            log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                              "$status $body_bytes_sent '$http_referer' "
                              '"$http_user_agent" "$http_x_forwarded_for"';

            # name can also be quoted, and format don't always have to
            log_format  'te st'  $remote_addr;
        }
    '''
    formats = dict(config_parser.get_log_formats(config))
    assert 'main' in formats
    assert "'$http_referer'" in formats['main']
    assert 'te st' in formats


def test_get_access_logs_no_format():
    config = '''
        http {
            # ubuntu default
            access_log /var/log/nginx/access.log;

            # syslog is a valid access log, but we can't follow it
            access_log syslog:server=address combined;

            # commented
            # access_log commented;

            server {
                location / {
                    # has parameter with default format
                    access_log /path/to/log gzip=1;
                }
            }
        }
    '''
    logs = dict(config_parser.get_access_logs(config))
    assert len(logs) == 2
    assert logs['/var/log/nginx/access.log'] == 'combined'
    assert logs['/path/to/log'] == 'combined'


def test_access_logs_with_format_name():
    config = '''
        http {
            access_log /path/to/main.log main gzip=5 buffer=32k flush=1m;
            server {
                access_log /path/to/test.log 'te st';
            }
        }
    '''
    logs = dict(config_parser.get_access_logs(config))
    assert len(logs) == 2
    assert logs['/path/to/main.log'] == 'main'
    assert logs['/path/to/test.log'] == 'te st'