From 023c7381609bd8cdff4f177c6161abd0720a0c0b Mon Sep 17 00:00:00 2001 From: Alexander Schlarb Date: Mon, 15 Aug 2016 18:05:39 +0200 Subject: [PATCH] Make unit tests fail with a descriptive error message if local `ipfs daemon` node is not running --- test/functional/tests.py | 46 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/functional/tests.py b/test/functional/tests.py index a82997be..251f45bd 100644 --- a/test/functional/tests.py +++ b/test/functional/tests.py @@ -1,14 +1,52 @@ # _*_ coding: utf-8 -*- import os -import shutil import json +import shutil +import socket +import sys import unittest +import logging import ipfsApi +__is_available = NotImplemented +def is_available(): + """ + Return whether the IPFS daemon is reachable or not + """ + global __is_available + + if not isinstance(__is_available, bool): + s = socket.socket() + try: + s.connect((ipfsApi.default_host, ipfsApi.default_port)) + except IOError: + __is_available = False + else: + __is_available = True + finally: + s.close() + + return __is_available + + +def skipIfOffline(): + if is_available(): + return lambda func: func + else: + return unittest.skip("IPFS node is not available") + + +def test_ipfs_node_available(): + addr = "[{0}]:{1}".format(ipfsApi.default_host, ipfsApi.default_port) + assert is_available(), "Functional tests require an IPFS node to be available at: " + addr + + + HERE = os.path.dirname(os.path.abspath(__file__)) +@skipIfOffline() class IpfsApiTest(unittest.TestCase): api = ipfsApi.Client() @@ -177,6 +215,7 @@ def test_cat_single_file_str(self): self.assertEqual("dsadsad\n", res) +@skipIfOffline() class IpfsApiLogTest(unittest.TestCase): def setUp(self): @@ -215,6 +254,7 @@ def test_log_tail(self): self.assertTrue(type(log) is dict) +@skipIfOffline() class IpfsApiPinTest(unittest.TestCase): fake_dir_hash = 'QmYqqgRahxbZvudnzDu2ZzUS1vFSNEuCrxghM8hgT8uBFY' @@ -288,6 +328,7 @@ def test_pin_ls_add_rm_directory(self): pins_after_rm[self.fake_dir_hash]['Type'] == 'recursive') +@skipIfOffline() class IpfsApiMFSTest(unittest.TestCase): test_files = { @@ -328,6 +369,7 @@ def test_write_stat_read_delete(self): self.api.files_rm(target) +@skipIfOffline() class TestBlockFunctions(unittest.TestCase): def setUp(self): self.api = ipfsApi.Client() @@ -354,6 +396,7 @@ def test_block_put(self): self.assertEqual(res['Key'], expected_block_multihash) +@skipIfOffline() class IpfsApiRepoTest(unittest.TestCase): def setUp(self): @@ -381,6 +424,7 @@ def test_repo_gc(self): self.assertTrue(garbage in keys) +@skipIfOffline() class IpfsApiObjectTest(unittest.TestCase): def setUp(self):