Search in sources :

Example 1 with BuildImageParams

use of org.eclipse.che.plugin.docker.client.params.BuildImageParams in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToBuildImageWithAdditionalQueryParameters.

@Test
public void shouldBeAbleToBuildImageWithAdditionalQueryParameters() throws IOException, InterruptedException {
    AuthConfigs authConfigs = DtoFactory.newDto(AuthConfigs.class);
    AuthConfig authConfig = DtoFactory.newDto(AuthConfig.class);
    Map<String, AuthConfig> auth = new HashMap<>();
    auth.put("auth", authConfig);
    authConfigs.setConfigs(auth);
    final String imageId = "37a7da3b7edc";
    final String repository = "repo/name";
    final String tag = "tag";
    final boolean rm = true;
    final boolean forcerm = true;
    final long memory = 2147483648L;
    final long memswap = 3221225472L;
    final boolean pull = true;
    final String dockerfile = "path/Dockerfile";
    final boolean nocache = true;
    final boolean q = true;
    final String cpusetcpus = "4-5";
    final long cpuperiod = 10000L;
    final long cpuquota = 5000L;
    final Map<String, String> buildargs = new HashMap<>();
    buildargs.put("constraint:label.com!", "value");
    BuildImageParams buildImageParams = BuildImageParams.create(this.dockerfile).withAuthConfigs(authConfigs).withRepository(repository).withTag(tag).withRemoveIntermediateContainers(rm).withForceRemoveIntermediateContainers(forcerm).withMemoryLimit(memory).withMemorySwapLimit(memswap).withDoForcePull(pull).withDockerfile(dockerfile).withNoCache(nocache).withQuiet(q).withCpusetCpus(cpusetcpus).withCpuPeriod(cpuperiod).withCpuQuota(cpuquota).withBuildArgs(buildargs);
    doReturn(new ByteArrayInputStream(("{\"stream\":\"Successfully built " + imageId + "\"}").getBytes())).when(dockerResponse).getInputStream();
    String returnedImageId = dockerConnector.buildImage(buildImageParams, progressMonitor);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/build");
    verify(dockerConnection).header("Content-Type", "application/x-compressed-tar");
    verify(dockerConnection).header(eq("Content-Length"), anyInt());
    verify(dockerConnection).entity(any(InputStream.class));
    verify(dockerConnection).header(eq("X-Registry-Config"), any(byte[].class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    verify(dockerConnection).query(eq("rm"), eq(1));
    verify(dockerConnection).query(eq("forcerm"), eq(1));
    verify(dockerConnection).query(eq("memory"), eq(memory));
    verify(dockerConnection).query(eq("memswap"), eq(memswap));
    verify(dockerConnection).query(eq("pull"), eq(1));
    verify(dockerConnection).query(eq("dockerfile"), eq(dockerfile));
    verify(dockerConnection).query(eq("nocache"), eq(1));
    verify(dockerConnection).query(eq("q"), eq(1));
    verify(dockerConnection).query(eq("cpusetcpus"), eq(cpusetcpus));
    verify(dockerConnection).query(eq("cpuperiod"), eq(cpuperiod));
    verify(dockerConnection).query(eq("cpuquota"), eq(cpuquota));
    verify(dockerConnection).query(eq("t"), eq(repository + ':' + tag));
    verify(dockerConnection).query(eq("buildargs"), eq(URLEncoder.encode(GSON.toJson(buildargs), "UTF-8")));
    assertEquals(returnedImageId, imageId);
}
Also used : AuthConfigs(org.eclipse.che.plugin.docker.client.dto.AuthConfigs) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) InputStream(java.io.InputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) Matchers.anyString(org.mockito.Matchers.anyString) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 2 with BuildImageParams

use of org.eclipse.che.plugin.docker.client.params.BuildImageParams in project che by eclipse.

the class MachineProviderImpl method buildImage.

protected void buildImage(CheServiceImpl service, String machineImageName, boolean doForcePullOnBuild, ProgressMonitor progressMonitor) throws MachineException {
    File workDir = null;
    try {
        BuildImageParams buildImageParams;
        if (service.getBuild() != null && service.getBuild().getDockerfileContent() != null) {
            workDir = Files.createTempDirectory(null).toFile();
            final File dockerfileFile = new File(workDir, "Dockerfile");
            try (FileWriter output = new FileWriter(dockerfileFile)) {
                output.append(service.getBuild().getDockerfileContent());
            }
            buildImageParams = BuildImageParams.create(dockerfileFile);
        } else {
            buildImageParams = BuildImageParams.create(service.getBuild().getContext()).withDockerfile(service.getBuild().getDockerfilePath());
        }
        buildImageParams.withForceRemoveIntermediateContainers(true).withRepository(machineImageName).withAuthConfigs(dockerCredentials.getCredentials()).withDoForcePull(doForcePullOnBuild).withMemoryLimit(service.getMemLimit()).withMemorySwapLimit(-1).withCpusetCpus(cpusetCpus).withCpuPeriod(cpuPeriod).withCpuQuota(cpuQuota).withBuildArgs(service.getBuild().getArgs());
        docker.buildImage(buildImageParams, progressMonitor);
    } catch (IOException e) {
        throw new MachineException(e.getLocalizedMessage(), e);
    } finally {
        if (workDir != null) {
            FileCleaner.addFile(workDir);
        }
    }
}
Also used : FileWriter(java.io.FileWriter) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) File(java.io.File) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams)

Example 3 with BuildImageParams

use of org.eclipse.che.plugin.docker.client.params.BuildImageParams in project che by eclipse.

the class DockerConnectorTest method shouldThrowIOExceptionWhenBuildImageButNoSuccessMessageInResponse.

@Test(expectedExceptions = IOException.class, expectedExceptionsMessageRegExp = "Docker image build failed. Image id not found in build output.")
public void shouldThrowIOExceptionWhenBuildImageButNoSuccessMessageInResponse() throws IOException, InterruptedException {
    AuthConfigs authConfigs = DtoFactory.newDto(AuthConfigs.class);
    AuthConfig authConfig = DtoFactory.newDto(AuthConfig.class);
    Map<String, AuthConfig> auth = new HashMap<>();
    auth.put("auth", authConfig);
    authConfigs.setConfigs(auth);
    BuildImageParams getEventsParams = BuildImageParams.create(dockerfile).withAuthConfigs(authConfigs);
    doReturn(new ByteArrayInputStream("c96d378b4911: Already exists".getBytes())).when(dockerResponse).getInputStream();
    dockerConnector.buildImage(getEventsParams, progressMonitor);
    verify(dockerResponse).getInputStream();
}
Also used : AuthConfigs(org.eclipse.che.plugin.docker.client.dto.AuthConfigs) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) Matchers.anyString(org.mockito.Matchers.anyString) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) Test(org.testng.annotations.Test)

Example 4 with BuildImageParams

use of org.eclipse.che.plugin.docker.client.params.BuildImageParams in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToBuildImage.

@Test
public void shouldBeAbleToBuildImage() throws IOException, InterruptedException {
    AuthConfigs authConfigs = DtoFactory.newDto(AuthConfigs.class);
    AuthConfig authConfig = DtoFactory.newDto(AuthConfig.class);
    Map<String, AuthConfig> auth = new HashMap<>();
    auth.put("auth", authConfig);
    authConfigs.setConfigs(auth);
    final String imageId = "37a7da3b7edc";
    BuildImageParams buildImageParams = BuildImageParams.create(dockerfile).withAuthConfigs(authConfigs);
    doReturn(new ByteArrayInputStream(("{\"stream\":\"Successfully built " + imageId + "\"}").getBytes())).when(dockerResponse).getInputStream();
    String returnedImageId = dockerConnector.buildImage(buildImageParams, progressMonitor);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/build");
    verify(dockerConnection).header("Content-Type", "application/x-compressed-tar");
    verify(dockerConnection).header(eq("Content-Length"), anyInt());
    verify(dockerConnection).entity(any(InputStream.class));
    verify(dockerConnection, never()).header(eq("remote"), anyString());
    verify(dockerConnection).header(eq("X-Registry-Config"), any(byte[].class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    assertEquals(returnedImageId, imageId);
}
Also used : AuthConfigs(org.eclipse.che.plugin.docker.client.dto.AuthConfigs) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) InputStream(java.io.InputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) Matchers.anyString(org.mockito.Matchers.anyString) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 5 with BuildImageParams

use of org.eclipse.che.plugin.docker.client.params.BuildImageParams in project che by eclipse.

the class DockerConnectorTest method shouldBeAbleToBuildImageWithRemoteContext.

@Test
public void shouldBeAbleToBuildImageWithRemoteContext() throws IOException, InterruptedException {
    AuthConfigs authConfigs = DtoFactory.newDto(AuthConfigs.class);
    AuthConfig authConfig = DtoFactory.newDto(AuthConfig.class);
    Map<String, AuthConfig> auth = new HashMap<>();
    auth.put("auth", authConfig);
    authConfigs.setConfigs(auth);
    final String imageId = "37a7da3b7edc";
    final String remote = "https://some.host.com/path/tarball.tar";
    BuildImageParams buildImageParams = BuildImageParams.create(remote).withAuthConfigs(authConfigs);
    doReturn(new ByteArrayInputStream(("{\"stream\":\"Successfully built " + imageId + "\"}").getBytes())).when(dockerResponse).getInputStream();
    String returnedImageId = dockerConnector.buildImage(buildImageParams, progressMonitor);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/build");
    verify(dockerConnection).query(eq("remote"), eq(remote));
    verify(dockerConnection, never()).header("Content-Type", "application/x-compressed-tar");
    verify(dockerConnection, never()).header(eq("Content-Length"), anyInt());
    verify(dockerConnection, never()).entity(any(InputStream.class));
    verify(dockerConnection).header(eq("X-Registry-Config"), any(byte[].class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
    assertEquals(returnedImageId, imageId);
}
Also used : AuthConfigs(org.eclipse.che.plugin.docker.client.dto.AuthConfigs) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseConnectionInputStream(org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream) InputStream(java.io.InputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) Matchers.anyString(org.mockito.Matchers.anyString) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams) URI(java.net.URI) Test(org.testng.annotations.Test)

Aggregations

BuildImageParams (org.eclipse.che.plugin.docker.client.params.BuildImageParams)6 HashMap (java.util.HashMap)5 AuthConfig (org.eclipse.che.plugin.docker.client.dto.AuthConfig)5 AuthConfigs (org.eclipse.che.plugin.docker.client.dto.AuthConfigs)5 Matchers.anyString (org.mockito.Matchers.anyString)5 Test (org.testng.annotations.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 CloseConnectionInputStream (org.eclipse.che.plugin.docker.client.connection.CloseConnectionInputStream)3 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)1