Search in sources :

Example 1 with CloseConnectionInputStream

use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.

the class DockerConnector method getResource.

/**
     * Gets files from the specified container.
     *
     * @return stream of resources from the specified container filesystem, with retention connection
     * @throws IOException
     *          when a problem occurs with docker api calls
     * @apiNote this method implements 1.20 docker API and requires docker not less than 1.8.0 version
     */
public InputStream getResource(final GetResourceParams params) throws IOException {
    DockerConnection connection = null;
    try {
        connection = connectionFactory.openConnection(dockerDaemonUri).method("GET").path(apiVersionPathPrefix + "/containers/" + params.getContainer() + "/archive").query("path", params.getSourcePath());
        final DockerResponse response = connection.request();
        if (response.getStatus() != OK.getStatusCode()) {
            throw getDockerException(response);
        }
        return new CloseConnectionInputStream(response.getInputStream(), connection);
    } catch (IOException io) {
        connection.close();
        throw io;
    }
}
Also used : DockerConnection(org.eclipse.che.plugin.docker.client.connection.DockerConnection) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) DockerResponse(org.eclipse.che.plugin.docker.client.connection.DockerResponse) IOException(java.io.IOException)

Example 2 with CloseConnectionInputStream

use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.

the class DockerConnectorTest method shouldProduceErrorWhenGetsResourcesFromContainerIfResponseCodeIsNotSuccess.

@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = EXCEPTION_ERROR_MESSAGE)
public void shouldProduceErrorWhenGetsResourcesFromContainerIfResponseCodeIsNotSuccess() throws IOException {
    GetResourceParams getResourceParams = GetResourceParams.create(CONTAINER, PATH_TO_FILE);
    when(dockerResponse.getStatus()).thenReturn(RESPONSE_ERROR_CODE);
    when(dockerResponse.getInputStream()).thenReturn(new CloseConnectionInputStream(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()), dockerConnection));
    dockerConnector.getResource(getResourceParams);
    verify(dockerResponse).getStatus();
}
Also used : CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) GetResourceParams(org.eclipse.che.plugin.docker.client.params.GetResourceParams) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.testng.annotations.Test)

Example 3 with CloseConnectionInputStream

use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToGetResourcesFromContainer.

@Test
public void shouldBeAbleToGetResourcesFromContainer() throws IOException {
    GetResourceParams getResourceParams = GetResourceParams.create(CONTAINER, PATH_TO_FILE);
    when(dockerResponse.getInputStream()).thenReturn(new CloseConnectionInputStream(new ByteArrayInputStream(STREAM_DATA_BYTES), dockerConnection));
    String response = CharStreams.toString(new InputStreamReader(dockerConnector.getResource(getResourceParams)));
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_GET);
    verify(dockerConnection).path("/containers/" + getResourceParams.getContainer() + "/archive");
    verify(dockerConnection).query(eq("path"), eq(PATH_TO_FILE));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    assertEquals(response, STREAM_DATA);
}
Also used : CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) GetResourceParams(org.eclipse.che.plugin.docker.client.params.GetResourceParams) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 4 with CloseConnectionInputStream

use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.

the class DockerConnectorTest method shouldProduceErrorWhenPutsResourcesIntoContainerIfResponseCodeIsNotSuccess.

@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = EXCEPTION_ERROR_MESSAGE)
public void shouldProduceErrorWhenPutsResourcesIntoContainerIfResponseCodeIsNotSuccess() throws IOException {
    InputStream source = new CloseConnectionInputStream(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()), dockerConnection);
    PutResourceParams putResourceParams = PutResourceParams.create(CONTAINER, PATH_TO_FILE, source);
    when(dockerResponse.getStatus()).thenReturn(RESPONSE_ERROR_CODE);
    when(dockerResponse.getInputStream()).thenReturn(new ByteArrayInputStream(ERROR_MESSAGE.getBytes()));
    dockerConnector.putResource(putResourceParams);
    verify(dockerResponse).getStatus();
}
Also used : CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) PutResourceParams(org.eclipse.che.plugin.docker.client.params.PutResourceParams) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) InputStream(java.io.InputStream) Test(org.testng.annotations.Test)

Example 5 with CloseConnectionInputStream

use of org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToPutResourcesIntoContainer.

@Test
public void shouldBeAbleToPutResourcesIntoContainer() throws IOException {
    InputStream source = new CloseConnectionInputStream(new ByteArrayInputStream(STREAM_DATA_BYTES), dockerConnection);
    PutResourceParams putResourceParams = PutResourceParams.create(CONTAINER, PATH_TO_FILE, source);
    dockerConnector.putResource(putResourceParams);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_PUT);
    verify(dockerConnection).path("/containers/" + putResourceParams.getContainer() + "/archive");
    verify(dockerConnection).query(eq("path"), eq(PATH_TO_FILE));
    verify(dockerConnection).header("Content-Type", ExtMediaType.APPLICATION_X_TAR);
    verify(dockerConnection).header(eq("Content-Length"), anyInt());
    verify(dockerConnection).entity(any(InputStream.class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
}
Also used : CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) PutResourceParams(org.eclipse.che.plugin.docker.client.params.PutResourceParams) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) InputStream(java.io.InputStream) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

CloseConnectionInputStream (org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Test (org.testng.annotations.Test)4 InputStream (java.io.InputStream)2 URI (java.net.URI)2 GetResourceParams (org.eclipse.che.plugin.docker.client.params.GetResourceParams)2 PutResourceParams (org.eclipse.che.plugin.docker.client.params.PutResourceParams)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 DockerConnection (org.eclipse.che.plugin.docker.client.connection.DockerConnection)1 DockerResponse (org.eclipse.che.plugin.docker.client.connection.DockerResponse)1 Matchers.anyString (org.mockito.Matchers.anyString)1