def test_function_result_is_available_in_stdout_and_logs_in_stderr(self): # This is the JSON result from Lambda function # Convert to proper binary type to be compatible with Python 2 & 3 expected_output = b'{"a":"b"}' expected_stderr = b"**This string is printed from Lambda function**" layer_downloader = LayerDownloader("./", "./") image_builder = LambdaImage(layer_downloader, False, False) container = LambdaContainer(self.runtime, self.handler, self.code_dir, self.layers, image_builder) stdout_stream = io.BytesIO() stderr_stream = io.BytesIO() stdout_stream_writer = StreamWriter(stdout_stream) stderr_stream_writer = StreamWriter(stderr_stream) with self._create(container): container.start() container.wait_for_logs(stdout=stdout_stream_writer, stderr=stderr_stream_writer) function_output = stdout_stream.getvalue() function_stderr = stderr_stream.getvalue() self.assertEquals(function_output.strip(), expected_output) self.assertIn(expected_stderr, function_stderr)
def test_function_result_is_available_in_stdout_and_logs_in_stderr(self): # This is the JSON result from Lambda function # Convert to proper binary type to be compatible with Python 2 & 3 expected_output = b'{"a":"b"}' expected_stderr = b"**This string is printed from Lambda function**" layer_downloader = LayerDownloader("./", "./") image_builder = LambdaImage(layer_downloader, False, False) container = LambdaContainer(self.runtime, self.handler, self.code_dir, self.layers, image_builder) stdout_stream = io.BytesIO() stderr_stream = io.BytesIO() stdout_stream_writer = StreamWriter(stdout_stream) stderr_stream_writer = StreamWriter(stderr_stream) with self._create(container): container.start() container.wait_for_logs(stdout=stdout_stream_writer, stderr=stderr_stream_writer) function_output = stdout_stream.getvalue() function_stderr = stderr_stream.getvalue() self.assertEqual(function_output.strip(), expected_output) self.assertIn(expected_stderr, function_stderr)
def test_debug_port_is_created_on_host(self): layer_downloader = LayerDownloader("./", "./") image_builder = LambdaImage(layer_downloader, False) container = LambdaContainer(self.runtime, self.handler, self.code_dir, self.layers, image_builder, debug_options=self.debug_context) with self._create(container): container.start() # After container is started, query the container to make sure it is bound to the right ports port_binding = self.docker_client.api.port(container.id, self.debug_port) self.assertIsNotNone( port_binding, "Container must be bound to a port on host machine") self.assertEquals(1, len(port_binding), "Only one port must be bound to the container") self.assertEquals(port_binding[0]["HostPort"], str(self.debug_port))
def test_debug_port_is_created_on_host(self): layer_downloader = LayerDownloader("./", "./") image_builder = LambdaImage(layer_downloader, False, False) container = LambdaContainer(self.runtime, self.handler, self.code_dir, self.layers, image_builder, debug_options=self.debug_context) with self._create(container): container.start() # After container is started, query the container to make sure it is bound to the right ports port_binding = self.docker_client.api.port(container.id, self.debug_port) self.assertIsNotNone(port_binding, "Container must be bound to a port on host machine") self.assertEquals(1, len(port_binding), "Only one port must be bound to the container") self.assertEquals(port_binding[0]["HostPort"], str(self.debug_port))
def test_container_is_attached_to_network(self): container = LambdaContainer(self.runtime, self.handler, self.code_dir) with self._network_create() as network: # Ask the container to attach to the network container.network_id = network.id with self._create(container): container.start() # Now that the container has been created, it would be connected to the network # Fetch the latest information about this network from server network.reload() self.assertEquals(1, len(network.containers)) self.assertEquals(container.id, network.containers[0].id)
def test_container_is_attached_to_network(self): layer_downloader = LayerDownloader("./", "./") image_builder = LambdaImage(layer_downloader, False, False) container = LambdaContainer(self.runtime, self.handler, self.code_dir, self.layers, image_builder) with self._network_create() as network: # Ask the container to attach to the network container.network_id = network.id with self._create(container): container.start() # Now that the container has been created, it would be connected to the network # Fetch the latest information about this network from server network.reload() self.assertEquals(1, len(network.containers)) self.assertEquals(container.id, network.containers[0].id)
def test_function_result_is_available_in_stdout_and_logs_in_stderr(self): # This is the JSON result from Lambda function # Convert to proper binary type to be compatible with Python 2 & 3 expected_output = six.binary_type('{"a":"b"}'.encode('utf-8')) expected_stderr = six.binary_type( "**This string is printed from Lambda function**".encode("utf-8")) container = LambdaContainer(self.runtime, self.handler, self.code_dir) stdout_stream = io.BytesIO() stderr_stream = io.BytesIO() with self._create(container): container.start() container.wait_for_logs(stdout=stdout_stream, stderr=stderr_stream) function_output = stdout_stream.getvalue() function_stderr = stderr_stream.getvalue() self.assertEquals(function_output.strip(), expected_output) self.assertIn(expected_stderr, function_stderr)