Skip to content

Commit

Permalink
Write local ipfs node backend and remove http reqs from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Jul 2, 2018
1 parent a99925d commit 7335fb9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
30 changes: 26 additions & 4 deletions ethpm/backends/ipfs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from eth_utils import (
to_bytes,
)
import ipfsapi
import requests

from ethpm import (
Expand All @@ -13,6 +14,9 @@
INFURA_GATEWAY_PREFIX,
IPFS_GATEWAY_PREFIX,
)
from ethpm.exceptions import (
IPFSConnectionError,
)
from ethpm.utils.ipfs import (
extract_ipfs_path_from_uri,
is_ipfs_uri,
Expand Down Expand Up @@ -92,10 +96,28 @@ def base_uri(self) -> str:
class LocalIPFSBackend(BaseIPFSBackend):
"""
Backend class for all IPFS URIs served through a locally running IPFS node.
TODO - implement
Default IPFS port = 5001
Default IPFS Gateway port = 8080 (read-only)
"""
def can_handle_uri(self, ipfs_uri: str) -> bool:
return False
def __init__(self, host: str, port: str) -> None:
self.host = host
self.port = port

def fetch_uri_contents(self, ipfs_uri: str) -> bytes:
pass
try:
client = ipfsapi.Client(self.host, self.port)
except ipfsapi.exceptions.ConnectionError:
raise IPFSConnectionError(
'Cannot connect to local IPFS daemon running at host:{0}, port:{1}.'.format(
self.host,
self.port,
)
)
ipfs_hash = extract_ipfs_path_from_uri(ipfs_uri)
contents = client.cat(ipfs_hash)
return contents

@property
def base_uri(self) -> str:
uri = '{0}:{1}'.format(self.host, self.port)
return uri
7 changes: 7 additions & 0 deletions ethpm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ class UriNotSupportedError(ValidationError):
Raised when URI scheme does not conform to the registry URI scheme.
"""
pass


class IPFSConnectionError(PyEthPMError):
"""
Raised when unable to connect to a locally running IPFS daemon.
"""
pass
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
extras_require={
'test': [
'pytest>=3.2.1,<4',
'requests-mock>=1.5.0,<2',
'tox>=1.8.0,<2',
],
'lint': [
Expand Down Expand Up @@ -56,6 +57,7 @@
'eth-keys>=0.2.0b3,<1',
'eth-tester==0.1.0b26',
'eth-utils>=1.0.2,<2',
'ipfsapi>=0.4.3,<1',
'jsonschema>=2.6.0,<3',
'py-evm==0.2.0a18',
'py-solc>=2.1.0,<3',
Expand Down
28 changes: 26 additions & 2 deletions tests/ethpm/backends/test_ipfs_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@
to_text,
)
import pytest
import requests_mock

from ethpm.backends.ipfs import (
DummyIPFSBackend,
InfuraIPFSBackend,
IPFSGatewayBackend,
LocalIPFSBackend,
)


@pytest.mark.parametrize(
'uri',
(
'ipfs:Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV',
'ipfs:/Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV',
'ipfs://Qme4otpS88NV8yQi8TfTP89EsQC5bko3F5N1yhRoi6cwGV',
)
)
def test_local_ipfs_backend(uri):
base_uri = 'localhost:8080'
backend = LocalIPFSBackend('localhost', '8080')
assert backend.base_uri == base_uri
assert backend.can_handle_uri(uri) is True
with requests_mock.Mocker() as m:
m.get(requests_mock.ANY, text='pragma')
assert backend.fetch_uri_contents(uri).startswith(b'pragma')


@pytest.mark.parametrize(
'uri',
(
Expand All @@ -25,7 +45,9 @@ def test_ipfs_gateway_backend(uri):
backend = IPFSGatewayBackend()
assert backend.base_uri == base_uri
assert backend.can_handle_uri(uri) is True
assert backend.fetch_uri_contents(uri).startswith(b'pragma')
with requests_mock.Mocker() as m:
m.get(requests_mock.ANY, text='pragma')
assert backend.fetch_uri_contents(uri).startswith(b'pragma')


@pytest.mark.parametrize(
Expand All @@ -41,7 +63,9 @@ def test_infura_ipfs_gateway_backend(uri):
backend = InfuraIPFSBackend()
assert backend.base_uri == base_uri
assert backend.can_handle_uri(uri) is True
assert backend.fetch_uri_contents(uri).startswith(b'pragma')
with requests_mock.Mocker() as m:
m.get(requests_mock.ANY, text='pragma')
assert backend.fetch_uri_contents(uri).startswith(b'pragma')


def test_dummy_ipfs_backend():
Expand Down
8 changes: 6 additions & 2 deletions tests/ethpm/test_package_init_from_registry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os

import pytest
import requests_mock
from solc import (
compile_source,
)
Expand Down Expand Up @@ -40,11 +42,13 @@ def w3_with_registry(w3):
return w3, address, registry


def test_package_init_from_registry(w3_with_registry):
def test_package_init_from_registry(w3_with_registry, safe_math_manifest):
os.environ['URI_BACKEND_CLASS'] = 'ethpm.backends.ipfs.IPFSGatewayBackend'
w3, address, registry = w3_with_registry
valid_registry_uri = 'ercXXX://%s/safe-math-lib?version=1.0.0' % address
pkg = Package.from_registry(valid_registry_uri, w3)
with requests_mock.Mocker() as m:
m.get(requests_mock.ANY, text=json.dumps(safe_math_manifest))
pkg = Package.from_registry(valid_registry_uri, w3)
assert isinstance(pkg, Package)
assert pkg.name == 'safe-math-lib'
os.environ.pop('URI_BACKEND_CLASS')
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ envlist=
combine_as_imports=True
force_sort_within_sections=True
include_trailing_comma=True
known_third_party=pytest
known_third_party=pytest,requests_mock
known_first_party=ethpm
line_length=21
multi_line_output=3
Expand Down

0 comments on commit 7335fb9

Please sign in to comment.