Skip to content

Commit db6afe1

Browse files
authored
Merge pull request #57 from alexander255/master
Make unit tests fail with a descriptive error message if local `ipfs daemon` node is not running
2 parents ff53f6a + 023c738 commit db6afe1

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

test/functional/tests.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,52 @@
11
# _*_ coding: utf-8 -*-
22
import os
3-
import shutil
43
import json
4+
import shutil
5+
import socket
6+
import sys
57
import unittest
8+
import logging
69

710
import ipfsApi
811

912

13+
__is_available = NotImplemented
14+
def is_available():
15+
"""
16+
Return whether the IPFS daemon is reachable or not
17+
"""
18+
global __is_available
19+
20+
if not isinstance(__is_available, bool):
21+
s = socket.socket()
22+
try:
23+
s.connect((ipfsApi.default_host, ipfsApi.default_port))
24+
except IOError:
25+
__is_available = False
26+
else:
27+
__is_available = True
28+
finally:
29+
s.close()
30+
31+
return __is_available
32+
33+
34+
def skipIfOffline():
35+
if is_available():
36+
return lambda func: func
37+
else:
38+
return unittest.skip("IPFS node is not available")
39+
40+
41+
def test_ipfs_node_available():
42+
addr = "[{0}]:{1}".format(ipfsApi.default_host, ipfsApi.default_port)
43+
assert is_available(), "Functional tests require an IPFS node to be available at: " + addr
44+
45+
46+
1047
HERE = os.path.dirname(os.path.abspath(__file__))
1148

49+
@skipIfOffline()
1250
class IpfsApiTest(unittest.TestCase):
1351

1452
api = ipfsApi.Client()
@@ -177,6 +215,7 @@ def test_cat_single_file_str(self):
177215
self.assertEqual("dsadsad\n", res)
178216

179217

218+
@skipIfOffline()
180219
class IpfsApiLogTest(unittest.TestCase):
181220

182221
def setUp(self):
@@ -215,6 +254,7 @@ def test_log_tail(self):
215254
self.assertTrue(type(log) is dict)
216255

217256

257+
@skipIfOffline()
218258
class IpfsApiPinTest(unittest.TestCase):
219259

220260
fake_dir_hash = 'QmYqqgRahxbZvudnzDu2ZzUS1vFSNEuCrxghM8hgT8uBFY'
@@ -288,6 +328,7 @@ def test_pin_ls_add_rm_directory(self):
288328
pins_after_rm[self.fake_dir_hash]['Type'] == 'recursive')
289329

290330

331+
@skipIfOffline()
291332
class IpfsApiMFSTest(unittest.TestCase):
292333

293334
test_files = {
@@ -328,6 +369,7 @@ def test_write_stat_read_delete(self):
328369
self.api.files_rm(target)
329370

330371

372+
@skipIfOffline()
331373
class TestBlockFunctions(unittest.TestCase):
332374
def setUp(self):
333375
self.api = ipfsApi.Client()
@@ -354,6 +396,7 @@ def test_block_put(self):
354396
self.assertEqual(res['Key'], expected_block_multihash)
355397

356398

399+
@skipIfOffline()
357400
class IpfsApiRepoTest(unittest.TestCase):
358401

359402
def setUp(self):
@@ -381,6 +424,7 @@ def test_repo_gc(self):
381424
self.assertTrue(garbage in keys)
382425

383426

427+
@skipIfOffline()
384428
class IpfsApiObjectTest(unittest.TestCase):
385429

386430
def setUp(self):

0 commit comments

Comments
 (0)