Example #1
0
    def test_4_Autoreload(self):
        # If test_3 has not been executed, the server won't be stopped,
        # so we'll have to do it.
        if engine.state != engine.states.EXITING:
            engine.exit()

        # Start the demo script in a new process
        p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
        p.write_conf(extra='test_case_name: "test_4_Autoreload"')
        p.start(imports='cherrypy.test._test_states_demo')
        try:
            self.getPage('/start')
            start = float(self.body)

            # Give the autoreloader time to cache the file time.
            time.sleep(2)

            # Touch the file
            os.utime(os.path.join(thisdir, '_test_states_demo.py'), None)

            # Give the autoreloader time to re-exec the process
            time.sleep(2)
            host = cherrypy.server.socket_host
            port = cherrypy.server.socket_port
            portend.occupied(host, port, timeout=5)

            self.getPage('/start')
            if not (float(self.body) > start):
                raise AssertionError('start time %s not greater than %s' %
                                     (float(self.body), start))
        finally:
            # Shut down the spawned process
            self.getPage('/exit')
        p.join()
Example #2
0
    def start(self, modulename):
        mpconf = CONF_PATH
        if not os.path.isabs(mpconf):
            mpconf = os.path.join(curdir, mpconf)

        f = open(mpconf, 'wb')
        try:
            output = (self.template % {
                'port': self.port,
                'testmod': modulename,
                'curdir': curdir
            })
            f.write(output)
        finally:
            f.close()

        result = read_process(APACHE_PATH, '-k start -f %s' % mpconf)
        if result:
            print(result)

        # Make a request so mod_wsgi starts up our app.
        # If we don't, concurrent initial requests will 404.
        portend.occupied('127.0.0.1', self.port, timeout=5)
        webtest.openURL('/ihopetheresnodefault', port=self.port)
        time.sleep(1)
Example #3
0
    def test_4_Autoreload(self):
        # If test_3 has not been executed, the server won't be stopped,
        # so we'll have to do it.
        if engine.state != engine.states.EXITING:
            engine.exit()

        # Start the demo script in a new process
        p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'))
        p.write_conf(extra='test_case_name: "test_4_Autoreload"')
        p.start(imports='cherrypy.test._test_states_demo')
        try:
            self.getPage('/start')
            start = float(self.body)

            # Give the autoreloader time to cache the file time.
            time.sleep(2)

            # Touch the file
            os.utime(os.path.join(thisdir, '_test_states_demo.py'), None)

            # Give the autoreloader time to re-exec the process
            time.sleep(2)
            host = cherrypy.server.socket_host
            port = cherrypy.server.socket_port
            portend.occupied(host, port, timeout=5)

            self.getPage('/start')
            if not (float(self.body) > start):
                raise AssertionError('start time %s not greater than %s' %
                                     (float(self.body), start))
        finally:
            # Shut down the spawned process
            self.getPage('/exit')
        p.join()
Example #4
0
    def start(self, imports=None):
        """Start cherryd in a subprocess."""
        portend.free(self.host, self.port, timeout=1)

        args = [
            '-m',
            'cherrypy',
            '-c', self.config_file,
            '-p', self.pid_file,
        ]
        r"""
        Command for running cherryd server with autoreload enabled

        Using

        ```
        ['-c',
         "__requires__ = 'CherryPy'; \
         import pkg_resources, re, sys; \
         sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]); \
         sys.exit(\
            pkg_resources.load_entry_point(\
                'CherryPy', 'console_scripts', 'cherryd')())"]
        ```

        doesn't work as it's impossible to reconstruct the `-c`'s contents.
        Ref: https://github.com/cherrypy/cherrypy/issues/1545
        """

        if not isinstance(imports, (list, tuple)):
            imports = [imports]
        for i in imports:
            if i:
                args.append('-i')
                args.append(i)

        if self.daemonize:
            args.append('-d')

        env = os.environ.copy()
        # Make sure we import the cherrypy package in which this module is
        # defined.
        grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = os.pathsep.join(
                (grandparentdir, env['PYTHONPATH']))
        else:
            env['PYTHONPATH'] = grandparentdir
        self._proc = subprocess.Popen([sys.executable] + args, env=env)
        if self.wait:
            self.exit_code = self._proc.wait()
        else:
            portend.occupied(self.host, self.port, timeout=5)

        # Give the engine a wee bit more time to finish STARTING
        if self.daemonize:
            time.sleep(2)
        else:
            time.sleep(1)
Example #5
0
    def start(self, imports=None):
        """Start cherryd in a subprocess."""
        portend.free(self.host, self.port, timeout=1)

        args = [
            '-m',
            'cherrypy.__main__',  # __main__ is needed for `-m` in Python 2.6
            '-c', self.config_file,
            '-p', self.pid_file,
        ]
        """
        Command for running cherryd server with autoreload enabled

        Using

        ```
        ['-c',
         "__requires__ = 'CherryPy'; \
         import pkg_resources, re, sys; \
         sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]); \
         sys.exit(\
            pkg_resources.load_entry_point(\
                'CherryPy', 'console_scripts', 'cherryd')())"]
        ```

        doesn't work as it's impossible to reconstruct the `-c`'s contents.
        Ref: https://github.com/cherrypy/cherrypy/issues/1545
        """

        if not isinstance(imports, (list, tuple)):
            imports = [imports]
        for i in imports:
            if i:
                args.append('-i')
                args.append(i)

        if self.daemonize:
            args.append('-d')

        env = os.environ.copy()
        # Make sure we import the cherrypy package in which this module is
        # defined.
        grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = os.pathsep.join(
                (grandparentdir, env['PYTHONPATH']))
        else:
            env['PYTHONPATH'] = grandparentdir
        self._proc = subprocess.Popen([sys.executable] + args, env=env)
        if self.wait:
            self.exit_code = self._proc.wait()
        else:
            portend.occupied(self.host, self.port, timeout=5)

        # Give the engine a wee bit more time to finish STARTING
        if self.daemonize:
            time.sleep(2)
        else:
            time.sleep(1)
Example #6
0
 def start(self, port, *args, **config):
     config.update(self.config)
     config['server.socket_port'] = port
     portend.free('localhost', port)
     server = self[port] = subprocess.Popen((sys.executable, '-m', self.module, '-c', json.dumps(config)) + args)
     portend.occupied('localhost', port)
     server.started = time.time()
     assert not server.poll()
     return clients.Resource('http://localhost:{}'.format(port))
Example #7
0
def main(host, port: int):
	portend.occupied(host, port, timeout=10)
	data = "dparrot uptest"
	headers = {
		'Content-Type': 'text/plain',
	}
	resp = requests.post(f'http://{host}:{port}/', data=data, headers=headers)

	assert resp.headers['Access-Control-Allow-Origin'] == '*'
	new_url = resp.json()['url']
	assert requests.get(new_url).text == data
Example #8
0
def main(host, port: int):
    portend.occupied(host, port, timeout=10)
    data = "dparrot uptest"
    headers = {
        'Content-Type': 'text/plain',
    }
    resp = requests.post(f'http://{host}:{port}/', data=data, headers=headers)

    assert resp.headers['Access-Control-Allow-Origin'] == '*'
    new_url = resp.json()['url']
    assert requests.get(new_url).text == data
Example #9
0
    def wait(self):
        """Wait until the HTTP server is ready to receive requests."""
        while not getattr(self.httpserver, 'ready', False):
            if self.interrupt:
                raise self.interrupt
            time.sleep(.1)

        # Wait for port to be occupied
        if not os.environ.get('LISTEN_PID', None):
            # Wait for port to be occupied if not running via socket-activation
            # (for socket-activation the port will be managed by systemd )
            if isinstance(self.bind_addr, tuple):
                with _safe_wait(*self.bound_addr):
                    portend.occupied(*self.bound_addr, timeout=Timeouts.occupied)
Example #10
0
    def wait(self):
        """Wait until the HTTP server is ready to receive requests."""
        while not getattr(self.httpserver, 'ready', False):
            if self.interrupt:
                raise self.interrupt
            time.sleep(.1)

        # Wait for port to be occupied
        if not os.environ.get('LISTEN_PID', None):
            # Wait for port to be occupied if not running via socket-activation
            # (for socket-activation the port will be managed by systemd )
            if isinstance(self.bind_addr, tuple):
                with _safe_wait(*self.bound_addr):
                    portend.occupied(*self.bound_addr, timeout=Timeouts.occupied)
Example #11
0
 def start(self):
     super(MongoDBInstance, self).start()
     self.data_dir = self.data_dir or self.get_data_dir()
     if not hasattr(self, 'port') or not self.port:
         self.port = self.find_free_port()
     cmd = [
         self.find_binary(),
         '--dbpath', self.data_dir,
         '--port', str(self.port),
     ] + list(self.mongod_args)
     if hasattr(self, 'bind_ip') and not '--bind_ip' in cmd:
         cmd.extend(['--bind_ip', self.bind_ip])
     self.process = subprocess.Popen(cmd, stdout=self.get_log())
     portend.occupied('localhost', self.port, timeout=3)
     log.info('{self} listening on {self.port}'.format(**locals()))
Example #12
0
 def start_instance(self, number):
     port = self.find_free_port()
     data_dir = os.path.join(self.data_root, 'r{number}'.format(**locals()))
     os.mkdir(data_dir)
     cmd = [
         self.find_binary(),
         '--dbpath', data_dir,
         '--port', str(port),
         '--replSet', self.replica_set_name,
     ] + list(self.mongod_parameters)
     log_file = self.get_log(number)
     process = subprocess.Popen(cmd, stdout=log_file)
     portend.occupied('localhost', port, timeout=50)
     log.info('{self}:{number} listening on {port}'.format(**locals()))
     return InstanceInfo(data_dir, port, process, log_file)
Example #13
0
    def wait_for_http(self, host='localhost', timeout=15):
        timeout = datetime.timedelta(seconds=timeout)
        timer = Stopwatch()
        portend.occupied(host, self.port, timeout=timeout)

        url = self.build_url(self.status_path)
        while True:
            try:
                conn = urllib.request.urlopen(url)
                break
            # comment below workaround for PyCQA/pyflakes#376
            except urllib.error.HTTPError as err:  # noqa: F841
                if timer.split() > timeout:
                    raise ServiceNotRunningError(self.__err.format(**locals()))
                time.sleep(.5)
        return conn.read()
Example #14
0
 def start(self):
     super(MongoDBInstance, self).start()
     if not hasattr(self, 'port') or not self.port:
         self.port = portend.find_available_local_port()
     self.data_dir = tempfile.mkdtemp()
     cmd = [
         self.find_binary(),
         '--dbpath',
         self.data_dir,
         '--port',
         str(self.port),
     ] + list(self.mongod_args)
     if hasattr(self, 'bind_ip') and '--bind_ip' not in cmd:
         cmd.extend(['--bind_ip', self.bind_ip])
     self.process = subprocess.Popen(cmd, **self.process_kwargs)
     portend.occupied('localhost', self.port, timeout=3)
     log.info(f'{self} listening on {self.port}')
Example #15
0
 def start_instance(self, number):
     port = portend.find_available_local_port()
     data_dir = os.path.join(self.data_root, repr(number))
     os.mkdir(data_dir)
     cmd = [
         self.find_binary(),
         '--dbpath',
         data_dir,
         '--port',
         str(port),
         '--replSet',
         self.replica_set_name,
     ] + list(self.mongod_parameters)
     log_file = self.get_log(number)
     process = subprocess.Popen(cmd, stdout=log_file)
     portend.occupied('localhost', port, timeout=50)
     log.info(f'{self}:{number} listening on {port}')
     return InstanceInfo(data_dir, port, process, log_file)
Example #16
0
    def start(self, imports=None):
        """Start cherryd in a subprocess."""
        portend.free(self.host, self.port, timeout=1)

        args = [
            os.path.join(thisdir, '..', 'cherryd'),
            '-c',
            self.config_file,
            '-p',
            self.pid_file,
        ]

        if not isinstance(imports, (list, tuple)):
            imports = [imports]
        for i in imports:
            if i:
                args.append('-i')
                args.append(i)

        if self.daemonize:
            args.append('-d')

        env = os.environ.copy()
        # Make sure we import the cheroot package in which this module is
        # defined.
        grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = os.pathsep.join(
                (grandparentdir, env['PYTHONPATH']))
        else:
            env['PYTHONPATH'] = grandparentdir
        self._proc = subprocess.Popen([sys.executable] + args, env=env)
        if self.wait:
            self.exit_code = self._proc.wait()
        else:
            portend.occupied(self.host, self.port, timeout=5)

        # Give the engine a wee bit more time to finish STARTING
        if self.daemonize:
            time.sleep(2)
        else:
            time.sleep(1)
Example #17
0
    def test_0_NormalStateFlow(self):
        engine.stop()
        # Our db_connection should not be running
        self.assertEqual(db_connection.running, False)
        self.assertEqual(db_connection.startcount, 1)
        self.assertEqual(len(db_connection.threads), 0)

        # Test server start
        engine.start()
        self.assertEqual(engine.state, engine.states.STARTED)

        host = cherrypy.server.socket_host
        port = cherrypy.server.socket_port
        portend.occupied(host, port, timeout=0.1)

        # The db_connection should be running now
        self.assertEqual(db_connection.running, True)
        self.assertEqual(db_connection.startcount, 2)
        self.assertEqual(len(db_connection.threads), 0)

        self.getPage('/')
        self.assertBody('Hello World')
        self.assertEqual(len(db_connection.threads), 1)

        # Test engine stop. This will also stop the HTTP server.
        engine.stop()
        self.assertEqual(engine.state, engine.states.STOPPED)

        # Verify that our custom stop function was called
        self.assertEqual(db_connection.running, False)
        self.assertEqual(len(db_connection.threads), 0)

        # Block the main thread now and verify that exit() works.
        def exittest():
            self.getPage('/')
            self.assertBody('Hello World')
            engine.exit()

        cherrypy.server.start()
        engine.start_with_callback(exittest)
        engine.block()
        self.assertEqual(engine.state, engine.states.EXITING)
Example #18
0
    def test_0_NormalStateFlow(self):
        engine.stop()
        # Our db_connection should not be running
        self.assertEqual(db_connection.running, False)
        self.assertEqual(db_connection.startcount, 1)
        self.assertEqual(len(db_connection.threads), 0)

        # Test server start
        engine.start()
        self.assertEqual(engine.state, engine.states.STARTED)

        host = cherrypy.server.socket_host
        port = cherrypy.server.socket_port
        portend.occupied(host, port, timeout=0.1)

        # The db_connection should be running now
        self.assertEqual(db_connection.running, True)
        self.assertEqual(db_connection.startcount, 2)
        self.assertEqual(len(db_connection.threads), 0)

        self.getPage('/')
        self.assertBody('Hello World')
        self.assertEqual(len(db_connection.threads), 1)

        # Test engine stop. This will also stop the HTTP server.
        engine.stop()
        self.assertEqual(engine.state, engine.states.STOPPED)

        # Verify that our custom stop function was called
        self.assertEqual(db_connection.running, False)
        self.assertEqual(len(db_connection.threads), 0)

        # Block the main thread now and verify that exit() works.
        def exittest():
            self.getPage('/')
            self.assertBody('Hello World')
            engine.exit()
        cherrypy.server.start()
        engine.start_with_callback(exittest)
        engine.block()
        self.assertEqual(engine.state, engine.states.EXITING)
Example #19
0
    def start(self, imports=None):
        """Start cherryd in a subprocess."""
        portend.free(self.host, self.port, timeout=1)

        args = [
            os.path.join(thisdir, '..', 'cherryd'),
            '-c', self.config_file,
            '-p', self.pid_file,
        ]

        if not isinstance(imports, (list, tuple)):
            imports = [imports]
        for i in imports:
            if i:
                args.append('-i')
                args.append(i)

        if self.daemonize:
            args.append('-d')

        env = os.environ.copy()
        # Make sure we import the cheroot package in which this module is
        # defined.
        grandparentdir = os.path.abspath(os.path.join(thisdir, '..', '..'))
        if env.get('PYTHONPATH', ''):
            env['PYTHONPATH'] = os.pathsep.join(
                (grandparentdir, env['PYTHONPATH']))
        else:
            env['PYTHONPATH'] = grandparentdir
        self._proc = subprocess.Popen([sys.executable] + args, env=env)
        if self.wait:
            self.exit_code = self._proc.wait()
        else:
            portend.occupied(self.host, self.port, timeout=5)

        # Give the engine a wee bit more time to finish STARTING
        if self.daemonize:
            time.sleep(2)
        else:
            time.sleep(1)
Example #20
0
def test_safe_wait_INADDR_ANY():  # pylint: disable=invalid-name
    """
    Wait on INADDR_ANY should not raise IOError

    In cases where the loopback interface does not exist, CherryPy cannot
    effectively determine if a port binding to INADDR_ANY was effected.
    In this situation, CherryPy should assume that it failed to detect
    the binding (not that the binding failed) and only warn that it could
    not verify it.
    """
    # At such a time that CherryPy can reliably determine one or more
    #  viable IP addresses of the host, this test may be removed.

    # Simulate the behavior we observe when no loopback interface is
    #  present by: finding a port that's not occupied, then wait on it.

    free_port = portend.find_available_local_port()

    servers = cherrypy.process.servers

    inaddr_any = '0.0.0.0'

    # Wait on the free port that's unbound
    with pytest.warns(
            UserWarning,
            match='Unable to verify that the server is bound on ',
    ) as warnings:
        # pylint: disable=protected-access
        with servers._safe_wait(inaddr_any, free_port):
            portend.occupied(inaddr_any, free_port, timeout=1)
    assert len(warnings) == 1

    # The wait should still raise an IO error if INADDR_ANY was
    #  not supplied.
    with pytest.raises(IOError):
        # pylint: disable=protected-access
        with servers._safe_wait('127.0.0.1', free_port):
            portend.occupied('127.0.0.1', free_port, timeout=1)
Example #21
0
    def start(self, modulename):
        mpconf = CONF_PATH
        if not os.path.isabs(mpconf):
            mpconf = os.path.join(curdir, mpconf)

        f = open(mpconf, 'wb')
        try:
            output = (self.template %
                      {'port': self.port, 'testmod': modulename,
                       'curdir': curdir})
            f.write(output)
        finally:
            f.close()

        result = read_process(APACHE_PATH, '-k start -f %s' % mpconf)
        if result:
            print(result)

        # Make a request so mod_wsgi starts up our app.
        # If we don't, concurrent initial requests will 404.
        portend.occupied('127.0.0.1', self.port, timeout=5)
        webtest.openURL('/ihopetheresnodefault', port=self.port)
        time.sleep(1)
Example #22
0
    def test_safe_wait_INADDR_ANY(self):
        """
        Wait on INADDR_ANY should not raise IOError

        In cases where the loopback interface does not exist, CherryPy cannot
        effectively determine if a port binding to INADDR_ANY was effected.
        In this situation, CherryPy should assume that it failed to detect
        the binding (not that the binding failed) and only warn that it could
        not verify it.
        """
        # At such a time that CherryPy can reliably determine one or more
        #  viable IP addresses of the host, this test may be removed.

        # Simulate the behavior we observe when no loopback interface is
        #  present by: finding a port that's not occupied, then wait on it.

        free_port = portend.find_available_local_port()

        servers = cherrypy.process.servers

        inaddr_any = '0.0.0.0'

        # Wait on the free port that's unbound
        with warnings.catch_warnings(record=True) as w:
            with servers._safe_wait(inaddr_any, free_port):
                portend.occupied(inaddr_any, free_port, timeout=1)
            self.assertEqual(len(w), 1)
            self.assertTrue(isinstance(w[0], warnings.WarningMessage))
            self.assertTrue(
                'Unable to verify that the server is bound on ' in str(w[0]))

        # The wait should still raise an IO error if INADDR_ANY was
        #  not supplied.
        with pytest.raises(IOError):
            with servers._safe_wait('127.0.0.1', free_port):
                portend.occupied('127.0.0.1', free_port, timeout=1)
Example #23
0
    def test_safe_wait_INADDR_ANY(self):
        """
        Wait on INADDR_ANY should not raise IOError

        In cases where the loopback interface does not exist, CherryPy cannot
        effectively determine if a port binding to INADDR_ANY was effected.
        In this situation, CherryPy should assume that it failed to detect
        the binding (not that the binding failed) and only warn that it could
        not verify it.
        """
        # At such a time that CherryPy can reliably determine one or more
        #  viable IP addresses of the host, this test may be removed.

        # Simulate the behavior we observe when no loopback interface is
        #  present by: finding a port that's not occupied, then wait on it.

        free_port = portend.find_available_local_port()

        servers = cherrypy.process.servers

        inaddr_any = '0.0.0.0'

        # Wait on the free port that's unbound
        with warnings.catch_warnings(record=True) as w:
            with servers._safe_wait(inaddr_any, free_port):
                portend.occupied(inaddr_any, free_port, timeout=1)
            self.assertEqual(len(w), 1)
            self.assertTrue(isinstance(w[0], warnings.WarningMessage))
            self.assertTrue(
                'Unable to verify that the server is bound on ' in str(w[0]))

        # The wait should still raise an IO error if INADDR_ANY was
        #  not supplied.
        with pytest.raises(IOError):
            with servers._safe_wait('127.0.0.1', free_port):
                portend.occupied('127.0.0.1', free_port, timeout=1)
Example #24
0
 def test_occupied_with_immediate_timeout(self, listening_addr,
                                          immediate_timeout):
     host, port = listening_addr[:2]
     portend.occupied(host, port, timeout=1.0)
Example #25
0
def main(host, port: int):
	portend.occupied(host, port, timeout=10)
	resp = requests.get(f'http://{host}:{port}/')
	# parrot returns 404 for /
	assert resp.status_code == 404