File: conftest.py

package info (click to toggle)
sbws 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,084 kB
  • sloc: python: 10,432; sh: 146; makefile: 38
file content (358 lines) | stat: -rw-r--r-- 9,109 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""pytest configuration for unit tests."""

import argparse
import os
from datetime import datetime

try:
    from datetime import UTC
except ImportError:
    from datetime import timezone

    UTC = timezone.utc

import pytest

from sbws.globals import RESULT_VERSION
from sbws.lib.resultdump import (
    Result,
    ResultErrorStream,
    ResultSuccess,
    write_result_to_datadir,
)
from sbws.util.config import _get_default_config

TIME1 = 1529232277.9028733
TIME2 = datetime.now(UTC).timestamp()
FP1 = "A" * 40
FP2 = "B" * 40
ED25519 = "g+Shk00y9Md0hg1S6ptnuc/wWKbADBgdjT0Kg+TSF3s"
CIRC12 = [FP1, FP2]
CIRC21 = [FP2, FP1]
DEST_URL = "http://example.com/sbws.bin"
NICK1 = "A"
NICK2 = "B"
IP1 = "169.254.100.1"
IP2 = "169.254.100.2"
RTTS1 = [
    0.2943556308746338,
    0.2885427474975586,
    0.2802879810333252,
    0.28124427795410156,
    0.2827129364013672,
    0.2901294231414795,
    0.2784590721130371,
    0.2838578224182129,
    0.2842121124267578,
    0.28656768798828125,
]
RTTS2 = [
    0.9097037315368652,
    0.9293286800384521,
    2.3764255046844482,
    0.869133710861206,
    0.8188621997833252,
    0.9046516418457031,
    1.3477752208709717,
    0.8118226528167725,
    0.8821918964385986,
    0.8746812343597412,
]
RTTS3 = [
    0.510988712310791,
    0.4889242649078369,
    0.5003941059112549,
    0.49333715438842773,
    0.5000274181365967,
    0.5426476001739502,
    0.5190870761871338,
    0.4908745288848877,
    0.5516133308410645,
    0.4995298385620117,
]
DOWNLOADS1 = [
    {"duration": 28.01000952720642, "amount": 25916542},
    {"duration": 28.203476428985596, "amount": 25916542},
    {"duration": 27.897520780563354, "amount": 25916542},
    {"duration": 29.330559492111206, "amount": 25916542},
    {"duration": 27.93175745010376, "amount": 25916542},
]
DOWNLOADS2 = [
    {"duration": 23.68175435066223, "amount": 81920},
    {"duration": 27.667736768722534, "amount": 81920},
    {"duration": 31.022956371307373, "amount": 81920},
    {"duration": 33.020694971084595, "amount": 81920},
    {"duration": 33.59471535682678, "amount": 81920},
]
DOWNLOADS3 = [
    {"duration": 30.008347988128662, "amount": 644411},
    {"duration": 30.73241639137268, "amount": 644411},
    {"duration": 31.845987796783447, "amount": 644411},
    {"duration": 29.703084230422974, "amount": 644411},
    {"duration": 30.438726663589478, "amount": 644411},
]
SCANNER = "test"
AVG_BW = 966080
BUR_BW = 1048576
OBS_BW = 524288
BW = 600000
UNMEASURED = False

now = datetime.now(UTC)

RELAY1 = Result.Relay(
    FP1,
    NICK1,
    IP1,
    ED25519,
    average_bandwidth=AVG_BW,
    burst_bandwidth=BUR_BW,
    observed_bandwidth=OBS_BW,
    consensus_bandwidth=BW,
    consensus_bandwidth_is_unmeasured=UNMEASURED,
    relay_in_recent_consensus=[now],
    relay_recent_measurement_attempt=[now],
    relay_recent_priority_list=[now],
)
RELAY2 = Result.Relay(FP2, NICK2, IP2, ED25519)

RESULT = Result(RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1)
RESULT_SUCCESS1 = ResultSuccess(
    RTTS1, DOWNLOADS1, RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1
)
RESULT_SUCCESS2 = ResultSuccess(
    RTTS2, DOWNLOADS2, RELAY2, CIRC21, DEST_URL, SCANNER, t=TIME2
)
RESULT_ERROR_STREAM = ResultErrorStream(
    RELAY1, CIRC12, DEST_URL, SCANNER, t=TIME1, msg="Something bad"
)

RESULTDICT_IP_CHANGED = {FP1: [RESULT_SUCCESS1, RESULT_SUCCESS2]}
RESULTDICT_IP_NOT_CHANGED = {FP1: [RESULT_SUCCESS1, RESULT_SUCCESS1]}

RELAY_DICT = {
    "fingerprint": FP1,
    "address": IP1,
    "nickname": NICK1,
    "master_key_ed25519": ED25519,
    "relay_average_bandwidth": AVG_BW,
    "relay_burst_bandwidth": BUR_BW,
    "relay_observed_bandwidth": OBS_BW,
    "consensus_bandwidth": BW,
    "consensus_bandwidth_is_unmeasured": UNMEASURED,
    "relay_in_recent_consensus": [now],
    "relay_recent_measurement_attempt": [now],
    "relay_recent_priority_list": [now],
}

BASE_RESULT_NO_RELAY_DICT = {
    "dest_url": DEST_URL,
    "time": TIME1,
    "circ": CIRC12,
    "version": RESULT_VERSION,
    "scanner": SCANNER,
}

BASE_RESULT_DICT = RELAY_DICT.copy()
BASE_RESULT_DICT.update(BASE_RESULT_NO_RELAY_DICT)

RESULT_ERROR_STREAM_DICT = BASE_RESULT_DICT.copy()
RESULT_ERROR_STREAM_DICT.update(
    {
        "type": "error-stream",
        "msg": "Something bad",
    }
)

RESULT_SUCCESS_DICT = BASE_RESULT_DICT.copy()
RESULT_SUCCESS_DICT.update(
    {
        "rtts": RTTS1,
        "type": "success",
        "downloads": DOWNLOADS1,
    }
)


class _PseudoArguments(argparse.Namespace):
    """Just enough of the argparse.Namespace (what you get when you do
    args = parser.parse_args()) to make get_config() happy

    >>> args = _PseudoArguments(directory='/home/matt/.sbws')
    >>> args.directory
    '/home/matt/.sbws'

    """

    def __init__(self, **kw):
        for key in kw:
            setattr(self, key, kw[key])


@pytest.fixture(scope="function")
def tmpdir(tmpdir_factory, request):
    """Create a tmp dir for the tests"""
    base = str(hash(request.node.nodeid))[:3]
    bn = tmpdir_factory.mktemp(base)
    return bn


@pytest.fixture(scope="function")
def sbwshome_empty(tmpdir):
    """Create sbws home inside of the tests tmp dir without initializing."""
    home = tmpdir.join(".sbws")
    os.makedirs(home.strpath, mode=0o700, exist_ok=True)
    return home.strpath


@pytest.fixture(scope="function")
def sbwshome_only_datadir(sbwshome_empty):
    """Create sbws home inside of the tests tmp dir with only datadir."""
    os.makedirs(
        os.path.join(sbwshome_empty, "datadir"), mode=0o700, exist_ok=True
    )
    return sbwshome_empty


@pytest.fixture(scope="function")
def test_config_path(tmpdir):
    config = tmpdir.join(".sbws.ini")
    return config


@pytest.fixture(scope="function")
def args(sbwshome_empty, parser, test_config_path):
    """Args with sbws home in the tests tmp dir."""
    args = _PseudoArguments(
        config=test_config_path,
        output=sbwshome_empty,
        scale=False,
        log_level="debug",
    )
    return args


@pytest.fixture(scope="function")
def conf(sbwshome_empty, tmpdir):
    """Default configuration with sbws home in the tmp test dir."""
    conf = _get_default_config()
    conf["paths"]["sbws_home"] = sbwshome_empty
    conf["paths"]["state_fpath"] = str(tmpdir.join(".sbws", "state.dat"))
    conf["destinations"]["local"] = "on"
    conf["destinations.local"] = {
        "url": "https://127.0.0.1:28888",
        "verify": False,
        "country": "ZZ",
    }

    return conf


@pytest.fixture(scope="function")
def conf_results(sbwshome_success_result_two_relays, conf):
    """Minimal configuration having a datadir

    So that `ResultDump` does not raise AssertionError.

    """
    conf["paths"]["sbws_home"] = sbwshome_success_result_two_relays
    return conf


@pytest.fixture()
def result():
    return RESULT


@pytest.fixture()
def result_success():
    return RESULT_SUCCESS1


@pytest.fixture()
def result_success_dict():
    return RESULT_SUCCESS_DICT


@pytest.fixture()
def result_error_stream_dict():
    return RESULT_ERROR_STREAM_DICT


@pytest.fixture()
def result_error_stream():
    return RESULT_ERROR_STREAM


@pytest.fixture()
def resultdict_ip_changed():
    return RESULTDICT_IP_CHANGED


@pytest.fixture()
def resultdict_ip_not_changed():
    return RESULTDICT_IP_NOT_CHANGED


@pytest.fixture()
def resultdict_ip_changed_trimmed():
    return {FP1: [RESULT_SUCCESS2]}


@pytest.fixture(scope="function")
def sbwshome_error_result(sbwshome_only_datadir, conf):
    """
    Creates an ~/.sbws with a single fresh ResultError in it
    """
    dd = conf.getpath("paths", "datadir")
    write_result_to_datadir(RESULT_ERROR_STREAM, dd)
    return sbwshome_only_datadir


@pytest.fixture(scope="function")
def sbwshome_success_result(sbwshome_only_datadir, conf):
    """
    Creates an ~/.sbws with a single fresh ResultSuccess in it
    """
    dd = conf.getpath("paths", "datadir")
    write_result_to_datadir(RESULT_SUCCESS1, dd)
    return sbwshome_only_datadir


@pytest.fixture(scope="function")
def sbwshome_success_result_one_relay(sbwshome_only_datadir, conf):
    """
    Creates an ~/.sbws with a a couple of fresh ResultSuccess for one relay
    """
    dd = conf.getpath("paths", "datadir")
    write_result_to_datadir(RESULT_SUCCESS1, dd)
    write_result_to_datadir(RESULT_SUCCESS1, dd)
    return sbwshome_only_datadir


@pytest.fixture(scope="function")
def sbwshome_success_result_two_relays(sbwshome_only_datadir, conf):
    """
    Creates an ~/.sbws with a a couple of fresh ResultSuccess for a couple or
    relays
    """
    dd = conf.getpath("paths", "datadir")
    write_result_to_datadir(RESULT_SUCCESS1, dd)
    write_result_to_datadir(RESULT_SUCCESS1, dd)
    write_result_to_datadir(RESULT_SUCCESS2, dd)
    write_result_to_datadir(RESULT_SUCCESS2, dd)
    return sbwshome_only_datadir


@pytest.fixture(scope="function")
def end_event():
    import threading

    return threading.Event()


@pytest.fixture(scope="function")
def rd(args, conf_results):
    from sbws.lib.resultdump import ResultDump

    return ResultDump(args, conf_results)