|
1 | 1 | # Note that this file is special in that py.test will automatically import this file and gather
|
2 | 2 | # its list of fixtures even if it is not directly imported into the corresponding test case.
|
3 | 3 |
|
| 4 | +import os |
4 | 5 | import pathlib
|
5 | 6 | import pytest
|
6 | 7 | import sys
|
7 | 8 | import typing as ty
|
| 9 | +from multiaddr import Multiaddr |
8 | 10 |
|
9 | 11 | import ipfshttpclient
|
10 | 12 |
|
11 | 13 |
|
12 | 14 | TEST_DIR: pathlib.Path = pathlib.Path(__file__).parent
|
13 | 15 |
|
14 | 16 |
|
| 17 | +def _running_in_linux() -> bool: |
| 18 | + return sys.platform == 'linux' |
| 19 | + |
| 20 | + |
| 21 | +def _running_in_travis_ci() -> bool: |
| 22 | + return '/home/travis/build' in os.getenv('PATH') |
| 23 | + |
| 24 | + |
15 | 25 | @pytest.fixture(scope='session')
|
16 | 26 | def fake_dir() -> pathlib.Path:
|
17 | 27 | return TEST_DIR.joinpath('fake_dir')
|
18 | 28 |
|
19 | 29 |
|
| 30 | +@pytest.fixture(scope='session') |
| 31 | +def docker_compose_file() -> str: |
| 32 | + """ |
| 33 | + Override the location of the file used by pytest-docker. |
| 34 | +
|
| 35 | + The fixture name must be docker_compose_file and return str. |
| 36 | + """ |
| 37 | + |
| 38 | + if _running_in_travis_ci(): |
| 39 | + pytest.skip('Docker hub reports rate limit errors on pulls from Travis CI servers') |
| 40 | + elif not _running_in_linux(): |
| 41 | + pytest.skip("No IPFS server build for Windows; Travis doesn't support Docker on mac") |
| 42 | + |
| 43 | + return str(TEST_DIR.joinpath('docker-compose.yml')) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope='session') |
| 47 | +def ipfs_service_address(docker_ip, docker_services, ipfs_service_auth) -> Multiaddr: |
| 48 | + port = docker_services.port_for('proxy', 80) |
| 49 | + address = Multiaddr(f'/ip4/{docker_ip}/tcp/{port}') |
| 50 | + |
| 51 | + print(f'Will connect to {address}') |
| 52 | + |
| 53 | + def is_responsive() -> bool: |
| 54 | + try: |
| 55 | + with ipfshttpclient.connect(address, auth=ipfs_service_auth): |
| 56 | + pass |
| 57 | + except ipfshttpclient.exceptions.Error: |
| 58 | + return False |
| 59 | + else: |
| 60 | + return True |
| 61 | + |
| 62 | + docker_services.wait_until_responsive( |
| 63 | + timeout=20, # Pulling the docker image is not included in this timeout |
| 64 | + pause=0.5, |
| 65 | + check=is_responsive |
| 66 | + ) |
| 67 | + |
| 68 | + return address |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture(scope='session') |
| 72 | +def ipfs_service_auth() -> ty.Tuple[str, str]: |
| 73 | + return 'TheUser', 'ThePassword' |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture(scope="function") |
| 77 | +def ipfs_service_client(ipfs_service_address, ipfs_service_auth): |
| 78 | + with ipfshttpclient.connect( |
| 79 | + addr=ipfs_service_address, |
| 80 | + auth=ipfs_service_auth |
| 81 | + ) as client: |
| 82 | + yield client |
| 83 | + |
| 84 | + |
20 | 85 | @pytest.fixture(scope='session')
|
21 | 86 | def ipfs_is_available() -> bool:
|
22 | 87 | """
|
|
0 commit comments