Esempio n. 1
0
 def setUp(self):
     self.db_fd, self.db_filename = tempfile.mkstemp()
     config = {'DATABASE': 'sqlite://{}'.format(self.db_filename),
               'TESTING': True}
     self.node = Node('test', config)
     self.client = self.node.app.test_client()
Esempio n. 2
0
class NodeTests(unittest.TestCase):

    # When tests need to create "real" nodes that listen on a web interface,
    # this is the first port number to use (more will be created consecutively).
    TEST_PORT = 5001

    def setUp(self):
        self.db_fd, self.db_filename = tempfile.mkstemp()
        config = {'DATABASE': 'sqlite://{}'.format(self.db_filename),
                  'TESTING': True}
        self.node = Node('test', config)
        self.client = self.node.app.test_client()


    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(self.db_filename)


    def test_ping(self):
        """
        The test node responds to a HEAD request.
        """
        response = self.client.head('/')
        assert response.status_code == 200


    def test_add_peer(self):
        """
        A node can be told about a peer.
        """

        # Tell our test node about the peer.
        peer_info = {'url': 'http://localhost:{}'.format(self.TEST_PORT + 1)}
        response = self.client.post('/peer', data=json.dumps(peer_info))
        assert response.status_code == 201

        # From the POST response, get the id that our test node used to store
        # information about the peer.  Retrieve the newly created record from
        # our test node using this id.
        post_response = json.loads(response.data)
        response = self.client.get('/peer/{}'.format(post_response['id']))
        assert response.status_code == 200

        # Verify that the retrieved information is correct.
        get_response = json.loads(response.data)
        assert get_response['url'] == peer_info['url']


    def test_is_me(self):
        """
        A node can determine that a purported peer is, in fact, itself.
        """
        try:
            peer, peer_proc, peer_url = start_node('test_01', self.TEST_PORT)
            assert peer.is_me(peer_url)
        finally:
            peer_proc.terminate()


    def test_check_peer(self):
        """
        A node can check on an existing peer.
        """
        try:
            peer, peer_proc, peer_url = start_node('test_01', self.TEST_PORT)
            status = self.node.check_peer(peer_url)
            assert status.active

            # Check again -- the second time should (transparently to us)
            # cause our node to find an existing record and merely update it.
            status = self.node.check_peer(peer_url)
            assert status.active
        finally:
            peer_proc.terminate()


    def test_peer_lists(self):
        """
        Nodes can get lists of peers from one another.
        """
        PEER_COUNT = 5
        peers = []
        try:
            # Create some other nodes and tell our node about them.
            for n in xrange(1, PEER_COUNT + 1):
                peer, peer_proc, peer_url = start_node('test_{}'.format(n),
                                                       self.TEST_PORT + n)
                peers.append((peer, peer_proc, peer_url))
                self.client.post('/peer', data=json.dumps({'url': peer_url}))

            # Verify that our node has the list of the 10 others.
            logger.debug('Verifying list of peers from test node.')
            response = self.client.get('/peer')
            get_response = json.loads(response.data)
            assert len(get_response['objects']) == PEER_COUNT

            # Now tell each node to talk to our node and get its list of peers.
            # (For this we have to start our own node's app.)
            _, node_proc, node_url = start_node_app(self.node, self.TEST_PORT)
            for peer, _, peer_url in peers:
                logger.debug("Asking peer at {} to get test node's list "
                             "of peers.".format(peer_url))
                peer.get_peer_list_from(node_url)

                # Now verify that the peer has the full list of *PEER_COUNT - 1*
                # peers (it should have excluded itself).
                response = requests.get('{}/peer'.format(peer_url))
                get_response = json.loads(response.content)
                assert len(get_response['objects']) == PEER_COUNT - 1

        finally:
            logger.debug('Terminating test node and peers.')
            node_proc.terminate()
            for _, peer_proc, _ in peers:
                peer_proc.terminate()