Пример #1
0
    def test_binding(self):
        """
        If a specific port is requested, and a server is already started in the
        requested port, then the layer setup fails.
        """
        s = socket.socket()
        try:
            s.bind((socket.gethostbyname(""), 4225))
        except socket.error:
            s.close()
            s = None
            raise

        if "YETI_SERVER" in os.environ:
            del os.environ["YETI_SERVER"]
        os.environ["YETI_PORT"] = "4225"
        try:
            try:
                YetiLayer.setUp()
            except ValueError, e:
                msg = str(e)
                self.assertIn(
                    "Failed to execute Yeti server on port 4225", msg)
                self.assertIn(
                    "Address already in use", msg)
            else:
Пример #2
0
    def test_server_timeout(self):
        """
        If we don't see that the server is started before the timeout, a
        ValueError is raised, even if the process is still running.
        """
        timeout = 1
        os.environ["YETI_CAPTURE_TIMEOUT"] = "%s" % timeout
        os.environ["YETI_BROWSER"] = ""
        if "YETI_SERVER" in os.environ:
            del os.environ["YETI_SERVER"]
        os.environ["YETI_PORT"] = "4225"

        mock_proc = self.mock_popen()
        mock_file = self.mock_builtin_open()

        with self.mocker.order():
            mock_time = self.mocker.replace("time.time")
            # The first time is to initialize the start time.
            mock_time()
            start_time = 0
            self.mocker.result(start_time)

            # The second time is to check if the timeout is exceeded in
            # the while loop.
            mock_time()
            self.mocker.result(start_time)
            # Go one iteration of the while loop, reporting the server
            # is starting up.
            mock_proc.poll()
            self.mocker.result(None)
            mock_file.readline()
            self.mocker.result("not yeti?")

            # Trigger a timeout.
            mock_time()
            self.mocker.result(start_time + timeout + 1)

            # The opened file is closed.
            mock_file.close()
            self.mocker.result(None)

            # Last check whether the server is still running.
            mock_proc.poll()
            self.mocker.result(None)

            # Since the server is running, it gets terminated.
            mock_proc.terminate()
            self.mocker.result(None)
            mock_proc.wait()
            self.mocker.result(None)

        self.mocker.replay()

        try:
            YetiLayer.setUp()
        except ValueError, e:
            msg = str(e)
            self.assertIn(
                "Failed to execute Yeti server in 1 seconds"
                " on port 4225", msg)
Пример #3
0
    def test_server_fail(self):
        """
        If we a poll of the process returns a non-None value while we
        are waiting, we report that server couldn't be started.
        """
        mock_proc = self.mock_popen()
        mock_file = self.mock_builtin_open()

        with self.mocker.order():
            mock_time = self.mocker.replace("time.time")
            # The first time is to initialize the start time.
            mock_time()
            start_time = 0
            self.mocker.result(start_time)

            # The second time is to check if the timeout is exceeded in
            # the while loop.
            mock_time()
            self.mocker.result(start_time)
            # Go one iteration of the while loop, reporting the server
            # is starting up.
            mock_proc.poll()
            self.mocker.result(None)
            mock_file.readline()
            self.mocker.result("not yeti?")

            # Go another iteration of the while loop, reporting the
            # server failed to start up.
            mock_time()
            self.mocker.result(start_time)
            mock_proc.poll()
            self.mocker.result(1)

            # The opened file is closed.
            mock_file.close()
            self.mocker.result(None)

        self.mocker.replay()

        if "YETI_SERVER" in os.environ:
            del os.environ["YETI_SERVER"]
        os.environ["YETI_PORT"] = "4225"

        try:
            YetiLayer.setUp()
        except ValueError, e:
            msg = str(e)
            self.assertIn(
                "Failed to execute Yeti server on port 4225", msg)
Пример #4
0
    def test_wait_for_server_startup(self):
        """
        Even if we don't wait for the browser to be captured, we wait
        for the server to start up.
        """
        mock_proc = self.mock_popen()
        mock_file = self.mock_builtin_open()

        with self.mocker.order():
            mock_time = self.mocker.replace("time.time")
            # The first time is to initialize the start time.
            mock_time()
            start_time = 0
            self.mocker.result(start_time)

            # The second time is to check if the timeout is exceeded in
            # the while loop.
            mock_time()
            self.mocker.result(start_time)
            # Go one iteration of the while loop, reporting the server
            # has started.
            mock_proc.poll()
            self.mocker.result(None)
            mock_file.readline()
            self.mocker.result("to run and report the results")

            # The opened file is closed.
            mock_file.close()
            self.mocker.result(None)

            # Last check to make sure the server is running ok.
            mock_proc.poll()
            self.mocker.result(None)

        self.mocker.replay()

        os.environ["YETI_BROWSER"] = ""
        if "YETI_SERVER" in os.environ:
            del os.environ["YETI_SERVER"]
        os.environ["YETI_PORT"] = "4225"

        YetiLayer.setUp()
        self.assertEqual(
            "http://localhost:4225", os.environ["YETI_SERVER"])
Пример #5
0
        if "YETI_SERVER" in os.environ:
            del os.environ["YETI_SERVER"]
        os.environ["YETI_PORT"] = "4225"
        try:
            try:
                YetiLayer.setUp()
            except ValueError, e:
                msg = str(e)
                self.assertIn(
                    "Failed to execute Yeti server on port 4225", msg)
                self.assertIn(
                    "Address already in use", msg)
            else:
                self.fail("ValueError not raised")
        finally:
            YetiLayer.tearDown()
            s.close()

    def mock_popen(self):
        """Replace subprocess.Popen and make it return a mock process.

        The mock process is returned.
        """
        mock_Popen = self.mocker.replace("subprocess.Popen")
        self.mock_proc = self.mocker.mock()
        mock_Popen(ARGS, KWARGS)
        self.mocker.result(self.mock_proc)
        return self.mock_proc

    def mock_builtin_open(self):
        """Replace built-in open and make it return a mock file.