-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathconftest.py
More file actions
148 lines (103 loc) · 3.47 KB
/
conftest.py
File metadata and controls
148 lines (103 loc) · 3.47 KB
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
# Note that this file is special in that py.test will automatically import this file and gather
# its list of fixtures even if it is not directly imported into the corresponding test case.
import os
import pathlib
import pytest
import sys
import typing as ty
from multiaddr import Multiaddr
import ipfshttpclient
TEST_DIR: pathlib.Path = pathlib.Path(__file__).parent
def _running_in_linux() -> bool:
return sys.platform == 'linux'
def _running_in_travis_ci() -> bool:
return '/home/travis/build' in os.getenv('PATH')
@pytest.fixture(scope='session')
def fake_dir() -> pathlib.Path:
return TEST_DIR.joinpath('fake_dir')
@pytest.fixture(scope='session')
def docker_compose_file() -> str:
"""
Override the location of the file used by pytest-docker.
The fixture name must be docker_compose_file and return str.
"""
if _running_in_travis_ci():
pytest.skip('Docker hub reports rate limit errors on pulls from Travis CI servers')
elif not _running_in_linux():
pytest.skip("No IPFS server build for Windows; Travis doesn't support Docker on mac")
return str(TEST_DIR.joinpath('docker-compose.yml'))
@pytest.fixture(scope='session')
def ipfs_service_address(docker_ip, docker_services, ipfs_service_auth) -> Multiaddr:
port = docker_services.port_for('proxy', 80)
address = Multiaddr(f'/ip4/{docker_ip}/tcp/{port}')
print(f'Will connect to {address}')
def is_responsive() -> bool:
try:
with ipfshttpclient.connect(address, auth=ipfs_service_auth):
pass
except ipfshttpclient.exceptions.Error:
return False
else:
return True
docker_services.wait_until_responsive(
timeout=20, # Pulling the docker image is not included in this timeout
pause=0.5,
check=is_responsive
)
return address
@pytest.fixture(scope='session')
def ipfs_service_auth() -> ty.Tuple[str, str]:
return 'TheUser', 'ThePassword'
@pytest.fixture(scope="function")
def ipfs_service_client(ipfs_service_address, ipfs_service_auth):
with ipfshttpclient.connect(
addr=ipfs_service_address,
auth=ipfs_service_auth
) as client:
yield client
@pytest.fixture(scope='session')
def ipfs_is_available() -> bool:
"""
Return whether the IPFS daemon is reachable or not
"""
try:
with ipfshttpclient.connect():
pass
except ipfshttpclient.exceptions.Error as e:
print('\nFailed to connect to IPFS client', file=sys.stderr)
print(e, file=sys.stderr)
return False
else:
return True
def sort_by_key(items, key="Name"):
return sorted(items, key=lambda x: x[key])
def _generate_client(
ipfs_is_available: bool,
offline: bool
) -> ty.Generator[ipfshttpclient.Client, None, None]:
if ipfs_is_available:
with ipfshttpclient.Client(offline=offline) as client:
yield client
else:
pytest.skip("Running IPFS node required")
@pytest.fixture(scope="function")
def client(ipfs_is_available: bool):
yield from _generate_client(ipfs_is_available, False)
@pytest.fixture(scope="function")
def offline_client(ipfs_is_available: bool):
yield from _generate_client(ipfs_is_available, True)
@pytest.fixture(scope="module")
def module_offline_client(ipfs_is_available: bool):
yield from _generate_client(ipfs_is_available, True)
@pytest.fixture
def cleanup_pins(client):
pinned = set(client.pin.ls(type="recursive")["Keys"])
yield
for multihash in client.pin.ls(type="recursive")["Keys"]:
if multihash not in pinned:
client.pin.rm(multihash)
@pytest.fixture
def daemon():
"""Result replaced by plugin in `run-tests.py` with the subprocess object of
the spawned daemon."""
return None