Пример #1
0
 def setUp(self):
     super(TestRedirectorServer, self).setUp()
     # Allows us to watch what bytes are being sent to the client.
     self._channel = FakeServerChannel()
     # Allows us to write bytes to stdout, stderr without them going to the terminal.
     self._sys = FakeSys()
     self._server = RedirectorServer(self._channel, sys_impl=self._sys)
Пример #2
0
 def setUp(self):
     self._client_sys = FakeSys()
     self._server_sys = FakeSys()
     self._fake_clock = scalyr_util.FakeClock()
     self._client_channel = FakeClientChannel(self._fake_clock)
     self._server_channel = FakeServerChannel(self._client_channel)
     self._client = RedirectorClient(self._client_channel, sys_impl=self._client_sys, fake_clock=self._fake_clock)
     self._server = RedirectorServer(self._server_channel, sys_impl=self._server_sys)
     self._client.start()
     self._server.start()
Пример #3
0
class TestRedirectionService(ScalyrTestCase):
    """Tests both the RedirectorServer and the RedirectorClient communicating together.
    """
    def setUp(self):
        self._client_sys = FakeSys()
        self._server_sys = FakeSys()
        self._fake_clock = scalyr_util.FakeClock()
        self._client_channel = FakeClientChannel(self._fake_clock)
        self._server_channel = FakeServerChannel(self._client_channel)
        self._client = RedirectorClient(self._client_channel, sys_impl=self._client_sys, fake_clock=self._fake_clock)
        self._server = RedirectorServer(self._server_channel, sys_impl=self._server_sys)
        self._client.start()
        self._server.start()

    def test_end_to_end(self):
        self._server_sys.stdout.write('Test full')
        self._server.stop()
        self._client.stop()
Пример #4
0
class TestRedirectionService(ScalyrTestCase):
    """Tests both the RedirectorServer and the RedirectorClient communicating together.
    """
    def setUp(self):
        self._client_sys = FakeSys()
        self._server_sys = FakeSys()
        self._fake_clock = scalyr_util.FakeClock()
        self._client_channel = FakeClientChannel(self._fake_clock)
        self._server_channel = FakeServerChannel(self._client_channel)
        self._client = RedirectorClient(self._client_channel, sys_impl=self._client_sys, fake_clock=self._fake_clock)
        self._server = RedirectorServer(self._server_channel, sys_impl=self._server_sys)
        self._client.start()
        self._server.start()

    def test_end_to_end(self):
        self._server_sys.stdout.write('Test full')
        self._server.stop()
        self._client.stop()
Пример #5
0
 def setUp(self):
     self._client_sys = FakeSys()
     self._server_sys = FakeSys()
     self._fake_clock = scalyr_util.FakeClock()
     self._client_channel = FakeClientChannel(self._fake_clock)
     self._server_channel = FakeServerChannel(self._client_channel)
     self._client = RedirectorClient(self._client_channel, sys_impl=self._client_sys, fake_clock=self._fake_clock)
     self._server = RedirectorServer(self._server_channel, sys_impl=self._server_sys)
     self._client.start()
     self._server.start()
Пример #6
0
 def setUp(self):
     # Allows us to watch what bytes are being sent to the client.
     self._channel = FakeServerChannel()
     # Allows us to write bytes to stdout, stderr without them going to the terminal.
     self._sys = FakeSys()
     self._server = RedirectorServer(self._channel, sys_impl=self._sys)
Пример #7
0
class TestRedirectorServer(ScalyrTestCase):
    """Tests the RedirectorServer code using fakes for stdout, stderr and the channel.
    """
    def setUp(self):
        # Allows us to watch what bytes are being sent to the client.
        self._channel = FakeServerChannel()
        # Allows us to write bytes to stdout, stderr without them going to the terminal.
        self._sys = FakeSys()
        self._server = RedirectorServer(self._channel, sys_impl=self._sys)

    def test_sending_str(self):
        self._server.start()
        # Verify that the server told the channel to accept the next client connection.
        self.assertEquals(self._channel.accept_count, 1)
        # Simulate writing to stdout.
        self._sys.stdout.write('Testing')
        # Make sure we wrote a message to the channel
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, 'Testing')

    def test_sending_unicode(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stdout.write(u'caf\xe9')
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, u'caf\xe9')

    def test_sending_to_stderr(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stderr.write('Testing again')
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 1)
        self.assertEquals(content, 'Testing again')

    def test_connection_failure(self):
        # Set the channel to simulate a connection timeout.
        self._channel.timeout_connection = True
        caught_it = False
        try:
            # Make sure that we get an exception.
            self._server.start()
        except RedirectorError:
            caught_it = True
        self.assertTrue(caught_it)

    def _parse_sent_bytes(self, content):
        """Parses the stream id and the actual content from the encoded content string sent by the server.

        @param content: The string sent by the server.
        @type content: str

        @return: A tuple of the stream_id and the actual content encoded in the sent string.
        @rtype: (int,str)
        """
        prefix_code = content[0:4]
        code = struct.unpack('i', prefix_code)[0]
        stream_id = code % 2
        num_bytes = code >> 1

        self.assertEquals(len(content), num_bytes + 4)
        decoded_str = content[4:].decode('utf-8')

        return stream_id, decoded_str
Пример #8
0
 def __init__(self, pipe_name):
     self.__full_pipe_name = r"\\.\pipe\%s" % pipe_name
     RedirectorServer.__init__(
         self, PipeRedirectorServer.ServerChannel(self.__full_pipe_name))
Пример #9
0
class TestRedirectorServer(ScalyrTestCase):
    """Tests the RedirectorServer code using fakes for stdout, stderr and the channel."""
    def setUp(self):
        super(TestRedirectorServer, self).setUp()
        # Allows us to watch what bytes are being sent to the client.
        self._channel = FakeServerChannel()
        # Allows us to write bytes to stdout, stderr without them going to the terminal.
        self._sys = FakeSys()
        self._server = RedirectorServer(self._channel, sys_impl=self._sys)

    def test_sending_str(self):
        self._server.start()
        # Verify that the server told the channel to accept the next client connection.
        self.assertEquals(self._channel.accept_count, 1)
        # Simulate writing to stdout.
        self._sys.stdout.write("Testing")
        # Make sure we wrote a message to the channel
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, "Testing")

    def test_sending_unicode(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stdout.write("caf\xe9")
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, "caf\xe9")

    def test_sending_to_stderr(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stderr.write("Testing again")
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 1)
        self.assertEquals(content, "Testing again")

    def test_connection_failure(self):
        # Set the channel to simulate a connection timeout.
        self._channel.timeout_connection = True
        caught_it = False
        try:
            # Make sure that we get an exception.
            self._server.start()
        except RedirectorError:
            caught_it = True
        self.assertTrue(caught_it)

    def _parse_sent_bytes(self, content):
        """Parses the stream id and the actual content from the encoded content string sent by the server.

        @param content: The string sent by the server.
        @type content: six.binary_type

        @return: A tuple of the stream_id and the actual content encoded in the sent string.
        @rtype: (int,six.text_type)
        """
        prefix_code = content[0:4]
        # 2->TODO struct.pack|unpack in python < 2.7.7 does not allow unicode format string.
        code = compat.struct_unpack_unicode("i", prefix_code)[0]
        stream_id = code % 2
        num_bytes = code >> 1

        self.assertEquals(len(content), num_bytes + 4)
        decoded_str = content[4:].decode("utf-8")

        return stream_id, decoded_str
Пример #10
0
class TestRedirectorServer(unittest.TestCase):
    """Tests the RedirectorServer code using fakes for stdout, stderr and the channel.
    """
    def setUp(self):
        # Allows us to watch what bytes are being sent to the client.
        self._channel = FakeServerChannel()
        # Allows us to write bytes to stdout, stderr without them going to the terminal.
        self._sys = FakeSys()
        self._server = RedirectorServer(self._channel, sys_impl=self._sys)

    def test_sending_str(self):
        self._server.start()
        # Verify that the server told the channel to accept the next client connection.
        self.assertEquals(self._channel.accept_count, 1)
        # Simulate writing to stdout.
        self._sys.stdout.write('Testing')
        # Make sure we wrote a message to the channel
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, 'Testing')

    def test_sending_unicode(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stdout.write(u'caf\xe9')
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 0)
        self.assertEquals(content, u'caf\xe9')

    def test_sending_to_stderr(self):
        self._server.start()
        self.assertEquals(self._channel.accept_count, 1)
        self._sys.stderr.write('Testing again')
        self.assertEquals(self._channel.write_count, 1)
        (stream_id, content) = self._parse_sent_bytes(self._channel.last_write)

        self.assertEquals(stream_id, 1)
        self.assertEquals(content, 'Testing again')

    def test_connection_failure(self):
        # Set the channel to simulate a connection timeout.
        self._channel.timeout_connection = True
        caught_it = False
        try:
            # Make sure that we get an exception.
            self._server.start()
        except RedirectorError:
            caught_it = True
        self.assertTrue(caught_it)

    def _parse_sent_bytes(self, content):
        """Parses the stream id and the actual content from the encoded content string sent by the server.

        @param content: The string sent by the server.
        @type content: str

        @return: A tuple of the stream_id and the actual content encoded in the sent string.
        @rtype: (int,str)
        """
        prefix_code = content[0:4]
        code = struct.unpack('i', prefix_code)[0]
        stream_id = code % 2
        num_bytes = code >> 1

        self.assertEquals(len(content), num_bytes + 4)
        decoded_str = content[4:].decode('utf-8')

        return stream_id, decoded_str