test_v3bwfile.py 6.87 KB
Newer Older
juga's avatar
juga committed
1
2
# -*- coding: utf-8 -*-
"""Test generation of bandwidth measurements document (v3bw)"""
juga's avatar
juga committed
3
4
import json

5
from sbws import __version__ as version
6
from sbws.globals import SPEC_VERSION, RESULT_VERSION
juga's avatar
juga committed
7
8
from sbws.lib.resultdump import Result, load_result_file
from sbws.lib.v3bwfile import (V3BwHeader, V3BWLine, TERMINATOR, LINE_SEP,
juga's avatar
juga committed
9
10
                               KEYVALUE_SEP_V110, num_results_of_type,
                               V3BwFile)
juga's avatar
juga committed
11

12
timestamp = 1523974147
13
timestamp_l = str(timestamp)
14
15
16
version_l = KEYVALUE_SEP_V110.join(['version', SPEC_VERSION])
software_l = KEYVALUE_SEP_V110.join(['software', 'sbws'])
software_version_l = KEYVALUE_SEP_V110.join(['software_version', version])
17
file_created = '2018-04-25T13:10:57'
18
file_created_l = KEYVALUE_SEP_V110.join(['file_created', file_created])
juga's avatar
juga committed
19
20
21
22
latest_bandwidth = '2018-04-17T14:09:07'
latest_bandwidth_l = KEYVALUE_SEP_V110.join(['latest_bandwidth',
                                             latest_bandwidth])
header_ls = [timestamp_l, version_l, file_created_l, latest_bandwidth_l,
23
24
             software_l, software_version_l, TERMINATOR]
header_str = LINE_SEP.join(header_ls) + LINE_SEP
25
earliest_bandwidth = '2018-04-16T14:09:07'
26
27
earliest_bandwidth_l = KEYVALUE_SEP_V110.join(['earliest_bandwidth',
                                               earliest_bandwidth])
28
generator_started = '2018-04-16T14:09:05'
29
30
generator_started_l = KEYVALUE_SEP_V110.join(['generator_started',
                                              generator_started])
31
header_extra_ls = [timestamp_l, version_l,
32
                   earliest_bandwidth_l, file_created_l, generator_started_l,
juga's avatar
juga committed
33
                   latest_bandwidth_l,
34
                   software_l, software_version_l, TERMINATOR]
35
36
header_extra_str = LINE_SEP.join(header_extra_ls) + LINE_SEP

37
bwl_str = "bw=54 error_circ=0 error_misc=0 error_stream=1 " \
38
    "master_key_ed25519=g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s " \
39
    "nick=A " \
40
    "node_id=$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA rtt=456 success=1 " \
41
    "time=2018-04-17T14:09:07\n"
juga's avatar
juga committed
42
43
44
45
46
47
48
49
50
51

v3bw_str = header_extra_str + bwl_str

RESULT_ERROR_STREAM_DICT = {
    "fingerprint": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    "address": "111.111.111.111",
    "dest_url": "http://y.z",
    "time": 1526894062.6408398,
    "circ": ["AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
             "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"],
52
    "version": RESULT_VERSION,
juga's avatar
juga committed
53
54
55
    "scanner": "IDidntEditTheSBWSConfig",
    "type": "error-stream",
    "msg": "Something bad happened while measuring bandwidth",
56
    "nickname": "A",
juga's avatar
juga committed
57
    "master_key_ed25519": "g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s"
juga's avatar
juga committed
58
59
60
61
62
63
64
65
66
67
68
69
70
}

RESULT_SUCCESS_DICT = {
    "fingerprint": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    "address": "111.111.111.111",
    "dest_url": "http://y.z",
    "time": 1526894062.6408398,
    "rtts": [0.4596822261810303, 0.44872617721557617, 0.4563450813293457,
             0.44872212409973145, 0.4561030864715576, 0.4765200614929199,
             0.4495084285736084, 0.45711588859558105, 0.45520496368408203,
             0.4635589122772217],
    "circ": ["AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
             "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"],
71
    "version": RESULT_VERSION,
juga's avatar
juga committed
72
73
74
75
76
77
78
79
    "scanner": "IDidntEditTheSBWSConfig",
    "type": "success",
    "downloads": [
        {"amount": 590009, "duration": 6.1014368534088135},
        {"amount": 590009, "duration": 8.391342878341675},
        {"amount": 321663, "duration": 7.064587831497192},
        {"amount": 321663, "duration": 8.266003131866455},
        {"amount": 321663, "duration": 5.779450178146362}],
80
    "nickname": "A",
juga's avatar
juga committed
81
    "master_key_ed25519": "g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s"
juga's avatar
juga committed
82
83
84
85
}
RESULT_SUCCESS_STR = str(RESULT_SUCCESS_DICT)
RESULT_ERROR_STREAM_STR = str(RESULT_ERROR_STREAM_DICT)

juga's avatar
juga committed
86
87
88

def test_v3bwheader_str():
    """Test header str"""
89
    header = V3BwHeader(timestamp_l, file_created=file_created)
90
91
92
93
    assert header_str == str(header)


def test_v3bwheader_extra_str():
94
    """Test header str with additional headers"""
95
    header = V3BwHeader(timestamp_l,
96
97
                        file_created=file_created,
                        generator_started=generator_started,
98
99
100
101
102
103
                        earliest_bandwidth=earliest_bandwidth)
    assert header_extra_str == str(header)


def test_v3bwheader_from_lines():
    """"""
104
    header_obj = V3BwHeader(timestamp_l,
105
106
                            file_created=file_created,
                            generator_started=generator_started,
107
                            earliest_bandwidth=earliest_bandwidth)
108
    header, _ = V3BwHeader.from_lines_v110(header_extra_ls)
109
110
111
112
113
    assert str(header_obj) == str(header)


def test_v3bwheader_from_text():
    """"""
114
    header_obj = V3BwHeader(timestamp_l,
115
116
                            file_created=file_created,
                            generator_started=generator_started,
117
                            earliest_bandwidth=earliest_bandwidth)
118
    header, _ = V3BwHeader.from_text_v110(header_extra_str)
119
    assert str(header_obj) == str(header)
juga's avatar
juga committed
120
121


juga's avatar
juga committed
122
123
124
125
126
127
def test_v3bwheader_from_file(datadir):
    """Test header str with additional headers"""
    header = V3BwHeader(timestamp_l,
                        file_created=file_created,
                        generator_started=generator_started,
                        earliest_bandwidth=earliest_bandwidth)
128
129
    # at some point this should be read from conftest
    text = datadir.read('v3bw/20180425_131057.v3bw')
juga's avatar
juga committed
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
    h, _ = V3BwHeader.from_text_v110(text)
    assert str(h) == str(header)


def test_num_results_of_type():
    assert num_results_of_type([Result.from_dict(RESULT_SUCCESS_DICT)],
                               'success') == 1
    assert num_results_of_type([Result.from_dict(RESULT_ERROR_STREAM_DICT)],
                               'success') == 0
    assert num_results_of_type([Result.from_dict(RESULT_SUCCESS_DICT)],
                               'error-stream') == 0
    assert num_results_of_type([Result.from_dict(RESULT_ERROR_STREAM_DICT)],
                               'error-stream') == 1


def test_v3bwline_from_results_file(datadir):
    lines = datadir.readlines('results.txt')
    d = dict()
    for line in lines:
        r = Result.from_dict(json.loads(line.strip()))
        fp = r.fingerprint
        if fp not in d:
            d[fp] = []
        d[fp].append(r)
    bwl = V3BWLine.from_data(d, fp)
    assert bwl_str == str(bwl)


def test_v3bwfile(datadir, tmpdir):
juga's avatar
juga committed
159
    """Test generate v3bw file (including relay_lines)."""
160
161
    # at some point this should be obtained from conftest
    v3bw = datadir.read('v3bw/20180425_131057.v3bw')
juga's avatar
juga committed
162
163
164
165
166
167
168
169
    results = load_result_file(str(datadir.join("results.txt")))
    header = V3BwHeader(timestamp_l,
                        file_created=file_created,
                        generator_started=generator_started,
                        earliest_bandwidth=earliest_bandwidth)
    bwls = [V3BWLine.from_results(results[fp]) for fp in results]
    f = V3BwFile(header, bwls)
    assert v3bw == str(f)