test_stats.py 5.88 KB
Newer Older
1
2
3
4
5
from sbws.util.parser import create_parser
from sbws.util.config import get_config
from sbws.lib.resultdump import ResultError
from sbws.lib.resultdump import ResultSuccess
from sbws.lib.resultdump import Result
6
from sbws.lib.resultdump import write_result_to_datadir
7
8
import sbws.core.init
import sbws.core.stats
9
from tests.unit.globals import monotonic_time
Matt Traudt's avatar
Matt Traudt committed
10
11
from unittest.mock import patch
from datetime import datetime
12
13
import os
import time
14
import logging
15
16


17
def init_directory(dname):
18
    p = create_parser()
Matt Traudt's avatar
Matt Traudt committed
19
    args = p.parse_args('-d {} --log-level DEBUG init'.format(dname).split())
20
21
    conf = get_config(args)
    sbws.core.init.main(args, conf)
22
23
24
25
26
27


def add_single_stale_result(dname):
    r = ResultError(
        Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'),
        ['DEADBEEF1111', 'BEADDEEF2222'],
Matt Traudt's avatar
Matt Traudt committed
28
        '127.0.1.1', 'SBWSscanner', t=19950216)
29
30
31
    dd = os.path.join(str(dname), 'datadir')
    os.makedirs(dd)
    write_result_to_datadir(r, dd)
32
33
34
35
36
37


def add_single_fresh_result(dname):
    r = ResultError(
        Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'),
        ['DEADBEEF1111', 'BEADDEEF2222'],
Matt Traudt's avatar
Matt Traudt committed
38
        '127.0.1.1', 'SBWSscanner', t=time.time())
39
40
41
    dd = os.path.join(str(dname), 'datadir')
    os.makedirs(dd)
    write_result_to_datadir(r, dd)
42
43


Matt Traudt's avatar
Matt Traudt committed
44
def add_two_fresh_results(dname, t):
45
46
47
    r1 = ResultError(
        Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'),
        ['DEADBEEF1111', 'BEADDEEF2222'],
Matt Traudt's avatar
Matt Traudt committed
48
        '127.0.1.1', 'SBWSscanner', t=t)
49
50
51
52
    r2 = ResultSuccess(
        [1, 2, 3], [{'amount': 100, 'duration': 1}],
        Result.Relay('DEADBEEF1111', 'CowSayWhat', '127.0.0.1'),
        ['DEADBEEF1111', 'BEADDEEF2222'],
Matt Traudt's avatar
Matt Traudt committed
53
        '127.0.1.1', 'SBWSscanner', t=t)
54
55
56
57
    dd = os.path.join(str(dname), 'datadir')
    os.makedirs(dd)
    write_result_to_datadir(r1, dd)
    write_result_to_datadir(r2, dd)
58
59


60
def test_stats_uninitted(tmpdir, caplog):
61
62
63
64
    '''
    An un-initialized .sbws directory should fail hard and exit immediately
    '''
    p = create_parser()
Matt Traudt's avatar
Matt Traudt committed
65
    args = p.parse_args('-d {} --log-level DEBUG stats'.format(tmpdir).split())
66
    conf = get_config(args)
67
    try:
68
        sbws.core.stats.main(args, conf)
69
70
71
72
    except SystemExit as e:
        assert e.code == 1
    else:
        assert None, 'Should have failed'
73
74
    assert 'Sbws isn\'t initialized. Try sbws init' == \
        caplog.records[-1].getMessage()
75
76


77
def test_stats_initted(tmpdir, caplog):
78
79
80
81
    '''
    An initialized but rather empty .sbws directory should fail about missing
    ~/.sbws/datadir
    '''
82
    init_directory(tmpdir)
83
    p = create_parser()
Matt Traudt's avatar
Matt Traudt committed
84
    args = p.parse_args('-d {} --log-level DEBUG stats'.format(tmpdir).split())
85
    conf = get_config(args)
86
    try:
87
        sbws.core.stats.main(args, conf)
88
89
90
91
    except SystemExit as e:
        assert e.code == 1
    else:
        assert None, 'Should have failed'
92
93
    assert '{}/datadir does not exist'.format(tmpdir) == \
        caplog.records[-1].getMessage()
94
95


96
def test_stats_stale_result(tmpdir, caplog):
97
98
99
100
    '''
    An initialized .sbws directory with no fresh results should say so and
    exit cleanly
    '''
101
    init_directory(tmpdir)
102
103
    add_single_stale_result(tmpdir)
    p = create_parser()
Matt Traudt's avatar
Matt Traudt committed
104
    args = p.parse_args('-d {} --log-level DEBUG stats'.format(tmpdir).split())
105
106
107
    conf = get_config(args)
    sbws.core.stats.main(args, conf)
    assert 'No fresh results' == caplog.records[-1].getMessage()
108
109


110
def test_stats_fresh_result(tmpdir, capsys, caplog):
111
112
113
114
    '''
    An initialized .sbws directory with a fresh error result should have some
    boring stats and exit cleanly
    '''
115
116
    caplog.set_level(logging.DEBUG)
    init_directory(tmpdir)
117
118
119
    add_single_fresh_result(tmpdir)
    p = create_parser()
    args = p.parse_args(
Matt Traudt's avatar
Matt Traudt committed
120
        '-d {} --log-level DEBUG stats --error-types'.format(tmpdir).split())
121
122
    conf = get_config(args)
    sbws.core.stats.main(args, conf)
123
124
    captured = capsys.readouterr()
    lines = captured.out.strip().split('\n')
125
    needed_output_lines = [
126
        '1 relays have recent results',
127
        'Mean 0.00 successful measurements per relay',
128
129
        '0 success results and 1 error results',
    ]
130
131
    for needed_line in needed_output_lines:
        assert needed_line in lines
132
    lines = [l.getMessage() for l in caplog.records]
133
    needed_log_lines = [
134
        'Keeping 1/1 read lines from {}/{}/{}.txt'.format(
Matt Traudt's avatar
Matt Traudt committed
135
            tmpdir, 'datadir', datetime.utcfromtimestamp(time.time()).date()),
136
        'Keeping 1/1 results after removing old ones',
137
138
    ]
    for needed_line in needed_log_lines:
139
140
141
        assert needed_line in lines


Matt Traudt's avatar
Matt Traudt committed
142
143
@patch('time.time')
def test_stats_fresh_results(time_mock, tmpdir, capsys, caplog):
144
145
146
147
    '''
    An initialized .sbws directory with a fresh error and fresh success should
    have some exciting stats and exit cleanly
    '''
148
149
    caplog.set_level(logging.DEBUG)
    init_directory(tmpdir)
Matt Traudt's avatar
Matt Traudt committed
150
151
152
    start = 1524769441
    time_mock.side_effect = monotonic_time(start=start)
    add_two_fresh_results(tmpdir, start-1)
153
154
    p = create_parser()
    args = p.parse_args(
Matt Traudt's avatar
Matt Traudt committed
155
        '-d {} --log-level DEBUG stats --error-types'.format(tmpdir).split())
156
157
    conf = get_config(args)
    sbws.core.stats.main(args, conf)
158
159
160
    needed_output_lines = [
        '1 relays have recent results',
        '1 success results and 1 error results',
161
        'Mean 1.00 successful measurements per relay',
162
163
        '1/2 (50.00%) results were error-misc',
    ]
164
165
    captured = capsys.readouterr()
    lines = captured.out.strip().split('\n')
166
167
    for needed_line in needed_output_lines:
        assert needed_line in lines
168
    lines = [l.getMessage() for l in caplog.records]
169
    needed_log_lines = [
170
        'Keeping 2/2 read lines from {}/{}/{}.txt'.format(
Matt Traudt's avatar
Matt Traudt committed
171
            tmpdir, 'datadir', datetime.utcfromtimestamp(time.time()).date()),
172
        'Keeping 2/2 results after removing old ones',
173
174
175
        'Found a _ResultType.Error for the first time',
        'Found a _ResultType.Success for the first time',
    ]
176
    for needed_line in needed_log_lines:
177
        assert needed_line in lines