summaryrefslogtreecommitdiffhomepage
path: root/test/test_usr1.py
blob: ecb4d8fdf2b55f565c87283ea47472518a2aaef2 (plain) (blame)
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
import os
import signal
from pathlib import Path

from unit.applications.lang.python import ApplicationPython
from unit.log import Log
from unit.utils import waitforfiles

prerequisites = {'modules': {'python': 'any'}}

client = ApplicationPython()


def test_usr1_access_log(search_in_file, temp_dir, unit_pid, wait_for_record):
    client.load('empty')

    log = 'access.log'
    log_new = 'new.log'
    log_path = f'{temp_dir}/{log}'

    assert 'success' in client.conf(
        f'"{log_path}"', 'access_log'
    ), 'access log configure'

    assert waitforfiles(log_path), 'open'

    Path(log_path).rename(f'{temp_dir}/{log_new}')

    assert client.get()['status'] == 200

    assert (
        wait_for_record(r'"GET / HTTP/1.1" 200 0 "-" "-"', log_new) is not None
    ), 'rename new'
    assert not Path(log_path).is_file(), 'rename old'

    os.kill(unit_pid, signal.SIGUSR1)

    assert waitforfiles(log_path), 'reopen'

    assert client.get(url='/usr1')['status'] == 200

    assert (
        wait_for_record(r'"GET /usr1 HTTP/1.1" 200 0 "-" "-"', log) is not None
    ), 'reopen 2'
    assert search_in_file(r'/usr1', log_new) is None, 'rename new 2'


def test_usr1_unit_log(search_in_file, temp_dir, unit_pid, wait_for_record):
    client.load('log_body')

    log_new = 'new.log'
    log_path = f'{temp_dir}/unit.log'
    log_path_new = f'{temp_dir}/{log_new}'

    Path(log_path).rename(log_path_new)

    Log.swap(log_new)

    try:
        body = 'body_for_a_log_new\n'
        assert client.post(body=body)['status'] == 200

        assert wait_for_record(body, log_new) is not None, 'rename new'
        assert not Path(log_path).is_file(), 'rename old'

        os.kill(unit_pid, signal.SIGUSR1)

        assert waitforfiles(log_path), 'reopen'

        body = 'body_for_a_log_unit\n'
        assert client.post(body=body)['status'] == 200

        assert wait_for_record(body) is not None, 'rename new'
        assert search_in_file(body, log_new) is None, 'rename new 2'

    finally:
        # merge two log files into unit.log to check alerts

        path_log = Path(log_path)
        log = path_log.read_text(encoding='utf-8', errors='ignore') + Path(
            log_path_new
        ).read_text(encoding='utf-8', errors='ignore')
        path_log.write_text(log, encoding='utf-8', errors='ignore')

        Log.swap(log_new)