test_v3bwfile.py 6.55 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
juga's avatar
juga committed
6
7
8
from sbws.globals import SPEC_VERSION
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
38
39
40
bwl_str = "bw=54 error_circ=0 error_misc=0 error_stream=1 " \
    "nick=A " \
    "node_id=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA rtt=456 success=1 " \
    "time=2018-04-17T14:09:07\n"
juga's avatar
juga committed
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

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"],
    "version": 2,
    "scanner": "IDidntEditTheSBWSConfig",
    "type": "error-stream",
    "msg": "Something bad happened while measuring bandwidth",
    "nickname": "A"
}

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"],
    "version": 2,
    "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}],
    "nickname": "A"
}
RESULT_SUCCESS_STR = str(RESULT_SUCCESS_DICT)
RESULT_ERROR_STREAM_STR = str(RESULT_ERROR_STREAM_DICT)

juga's avatar
juga committed
83
84
85

def test_v3bwheader_str():
    """Test header str"""
86
    header = V3BwHeader(timestamp_l, file_created=file_created)
87
88
89
90
    assert header_str == str(header)


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


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


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


juga's avatar
juga committed
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
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)
    text = datadir.read('v3bw.txt')
    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
155
    """Test generate v3bw file (including relay_lines)."""
juga's avatar
juga committed
156
157
158
159
160
161
162
163
164
165
    v3bw = datadir.read('v3bw.txt')
    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)
    # f = V3BwFile.from_results(None, str(tmpdir.join("v3bw.txt")), results)
    assert v3bw == str(f)