def testWaitForServiceLive(self):
        """Test wait for service against a basic local web service.
        """
        # Get a free port to use for this test:
        port1 = net.get_free_port()

        web = webhelpers.BasicWeb(port=port1)
        self.stop_needed.append(web)

        # Run the web app and wait for ready should connect:
        web.start()
        result = net.wait_for_service('localhost', port1)
        self.assertEquals(result, True)

        result = net.wait_for_service('localhost', port1, retries=1)
        self.assertEquals(result, True)
    def testWaitForServiceLive(self):
        """Test wait for service against a basic local web service.
        """
        # Get a free port to use for this test:
        port1 = net.get_free_port()

        web = webhelpers.BasicWeb(port=port1)
        self.stop_needed.append(web)

        # Run the web app and wait for ready should connect:
        web.start()
        result = net.wait_for_service('localhost', port1)
        self.assertEquals(result, True)

        result = net.wait_for_service('localhost', port1, retries=1)
        self.assertEquals(result, True)
    def setUp(self):
        """Create the container and start the container it ready for testing.
        """
        log = get_log("DockerBase.setUp")

        kwargs = {}
        if self.entrypoint:
            kwargs['entrypoint'] = self.entrypoint

        port_bindings = {}
        interface = self.settings['interface']
        for entry in self.settings['export']['ports']:
            port_bindings[entry['port']] = (interface, entry['export_port'])
        log.debug("box: port binding '{}'".format(port_bindings))

        host_config = self.conn.create_host_config(port_bindings=port_bindings)

        box = self.conn.create_container(image=self.settings['image'],
                                         detach=True,
                                         ports=self.ports,
                                         host_config=host_config,
                                         **kwargs)
        log.debug("box: '{}'".format(box))
        self.containerId = box['Id']

        self.conn.start(self.containerId, )
        log.debug("box: container started '{}'".format(self.settings['image']))

        result = self.conn.inspect_container(self.containerId)
        if not result['State']['Running']:
            raise SystemError("Container failed to start '{}:{}'".format(
                self.settings['image'], self.containerId))

        log.info(
            "Container to running '{}:{}'. Checking it is ready...".format(
                self.settings['image'], self.containerId))

        # wait for the first port to respond to connections:
        interface = self.settings['interface']
        name = self.settings['export']['wait_for_port']
        ports = self.settings['export']['ports']
        port = [p['export_port'] for p in ports if p['name'] == name]
        port = port[0]

        is_ready = net.wait_for_service(interface, port, retries=self.retries)
        if not is_ready:
            log.error()
            raise SystemError()

        self.waitForReady()

        log.info("Container is ready for use '{}:{}'".format(
            self.settings['image'], self.containerId))
    def testNetTools(self):
        """Test as much of the net helper tools as I can.
        """
        # Recover a free port to construct a uri for a ficticious web servce
        # we can test the fail path of the wait_for_ready with:
        port = net.get_free_port()

        class XYZ(object):
            def __init__(self, port):
                self.port = port

            def freeport_range(self):
                return self.port

        fpr = XYZ(port)

        gport = net.get_free_port(fp=fpr.freeport_range)
        self.assertEquals(gport, port)

        # use the port
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('localhost', port))

        # Now exlucde this port and try again.
        self.assertRaises(net.NoFreePort,
                          net.get_free_port,
                          exclude_ports=[port],
                          fp=fpr.freeport_range)

        s.close()

        # Recover a free port to construct a uri for a ficticious web servce
        # we can test the fail path of the wait_for_ready with:
        port = net.get_free_port()
        ficticious_uri = "http://localhost:%d" % port

        # Use this made up uri and see it did indeed NOT manage to connect:
        result = net.wait_for_ready(ficticious_uri, retries=1)
        self.assertEquals(result, False)

        # Use this made up uri and see it did indeed NOT manage to connect:
        result = net.wait_for_service('localhost', port, retries=1)
        self.assertEquals(result, False)
    def testNetTools(self):
        """Test as much of the net helper tools as I can.
        """
        # Recover a free port to construct a uri for a ficticious web servce
        # we can test the fail path of the wait_for_ready with:
        port = net.get_free_port()

        class XYZ(object):
            def __init__(self, port):
                self.port = port
            def freeport_range(self):
                return self.port

        fpr = XYZ(port)

        gport = net.get_free_port(fp=fpr.freeport_range)
        self.assertEquals(gport, port)

        # use the port
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('localhost', port))

        # Now exlucde this port and try again.
        self.assertRaises(net.NoFreePort, net.get_free_port, exclude_ports=[port], fp=fpr.freeport_range)

        s.close()

        # Recover a free port to construct a uri for a ficticious web servce
        # we can test the fail path of the wait_for_ready with:
        port = net.get_free_port()
        ficticious_uri = "http://localhost:%d" % port

        # Use this made up uri and see it did indeed NOT manage to connect:
        result = net.wait_for_ready(ficticious_uri, retries=1)
        self.assertEquals(result, False)

        # Use this made up uri and see it did indeed NOT manage to connect:
        result = net.wait_for_service('localhost', port, retries=1)
        self.assertEquals(result, False)