Skip to content

Make unit tests fail with a descriptive error message if local ipfs daemon node is not running #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion test/functional/tests.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I follow that this is a decorator, but not how it works when you're decorating the test classes. Could you explain the interaction to me? How does this work since skipIfOffline doesn't accept parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decorators (in Python) are actually just pretty syntax around the magic __call__(*args, **kwargs) method.

Therefor:

@skipIfOffline()
def test_somehting():
    …

is the same as:

def test_something():
   …
test_something = skipIfOffline()(test_something)

were the skipIfOffline() part invokes the skipIfOffline function and (temporarily) stores the result. The result returned by skipIfOffline is in also a callable, that then gets invoked with the actual method as parameter. This second callable will then return another callable, that is later invoked as actual unit test by the test harness.

Definitely ask for clarification if this is hard to grasp for you (layers, layers, LAYERS). 😉

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()
Expand Down Expand Up @@ -177,6 +215,7 @@ def test_cat_single_file_str(self):
self.assertEqual("dsadsad\n", res)


@skipIfOffline()
class IpfsApiLogTest(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -215,6 +254,7 @@ def test_log_tail(self):
self.assertTrue(type(log) is dict)


@skipIfOffline()
class IpfsApiPinTest(unittest.TestCase):

fake_dir_hash = 'QmYqqgRahxbZvudnzDu2ZzUS1vFSNEuCrxghM8hgT8uBFY'
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()
Expand All @@ -354,6 +396,7 @@ def test_block_put(self):
self.assertEqual(res['Key'], expected_block_multihash)


@skipIfOffline()
class IpfsApiRepoTest(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -381,6 +424,7 @@ def test_repo_gc(self):
self.assertTrue(garbage in keys)


@skipIfOffline()
class IpfsApiObjectTest(unittest.TestCase):

def setUp(self):
Expand Down