Search in sources :

Example 1 with PullParams

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

the class DockerConnectorTest method shouldBeAbleToPullImageFromRepository.

@Test
public void shouldBeAbleToPullImageFromRepository() throws IOException, InterruptedException {
    PullParams pullParams = PullParams.create(IMAGE);
    when(dockerResponse.getInputStream()).thenReturn(new ByteArrayInputStream(STREAM_DATA_BYTES));
    dockerConnector.pull(pullParams, progressMonitor);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/images/create");
    verify(dockerConnection).query("fromImage", pullParams.getImage());
    verify(dockerConnection).header(eq("X-Registry-Auth"), any(AuthConfig.class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
}
Also used : PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 2 with PullParams

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

the class MachineProviderImpl method pullImage.

/**
     * Pulls docker image for container creation.
     *
     * @param service
     *         service that provides description of image that should be pulled
     * @param machineImageName
     *         name of the image that should be assigned on pull
     * @param progressMonitor
     *         consumer of output
     * @throws SourceNotFoundException
     *         if image for pulling not found
     * @throws MachineException
     *         if any other error occurs
     */
protected void pullImage(CheServiceImpl service, String machineImageName, ProgressMonitor progressMonitor) throws MachineException {
    DockerMachineSource dockerMachineSource = new DockerMachineSource(new MachineSourceImpl("image").setLocation(service.getImage()));
    if (dockerMachineSource.getRepository() == null) {
        throw new MachineException(format("Machine creation failed. Machine source is invalid. No repository is defined. Found '%s'.", dockerMachineSource));
    }
    try {
        boolean isSnapshot = SNAPSHOT_LOCATION_PATTERN.matcher(dockerMachineSource.getLocation()).matches();
        if (!isSnapshot || snapshotUseRegistry) {
            PullParams pullParams = PullParams.create(dockerMachineSource.getRepository()).withTag(MoreObjects.firstNonNull(dockerMachineSource.getTag(), LATEST_TAG)).withRegistry(dockerMachineSource.getRegistry()).withAuthConfigs(dockerCredentials.getCredentials());
            docker.pull(pullParams, progressMonitor);
        }
        String fullNameOfPulledImage = dockerMachineSource.getLocation(false);
        try {
            // tag image with generated name to allow sysadmin recognize it
            docker.tag(TagParams.create(fullNameOfPulledImage, machineImageName));
        } catch (ImageNotFoundException nfEx) {
            throw new SourceNotFoundException(nfEx.getLocalizedMessage(), nfEx);
        }
        // remove unneeded tag if restoring snapshot from registry
        if (isSnapshot && snapshotUseRegistry) {
            docker.removeImage(RemoveImageParams.create(fullNameOfPulledImage).withForce(false));
        }
    } catch (IOException e) {
        throw new MachineException("Can't create machine from image. Cause: " + e.getLocalizedMessage(), e);
    }
}
Also used : SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) IOException(java.io.IOException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)

Example 3 with PullParams

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

the class MachineProviderImplTest method shouldPullDockerImageOnInstanceCreationFromSnapshotFromRegistry.

@Test
public void shouldPullDockerImageOnInstanceCreationFromSnapshotFromRegistry() throws Exception {
    String repo = MACHINE_SNAPSHOT_PREFIX + "repo";
    String tag = "latest";
    String registry = "localhost:1234";
    createInstanceFromSnapshot(repo, tag, registry);
    PullParams pullParams = PullParams.create(repo).withRegistry(registry).withTag(tag);
    verify(dockerConnector).pull(eq(pullParams), any(ProgressMonitor.class));
}
Also used : ProgressMonitor(org.eclipse.che.plugin.docker.client.ProgressMonitor) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 4 with PullParams

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

the class DockerConnectorTest method shouldBeAbleToPullImageCreateRegistryRepository.

@Test
public void shouldBeAbleToPullImageCreateRegistryRepository() throws IOException, InterruptedException {
    PullParams pullParams = PullParams.create(IMAGE).withRegistry(REGISTRY);
    when(dockerResponse.getInputStream()).thenReturn(new ByteArrayInputStream(STREAM_DATA_BYTES));
    dockerConnector.pull(pullParams, progressMonitor);
    verify(dockerConnectionFactory).openConnection(any(URI.class));
    verify(dockerConnection).method(REQUEST_METHOD_POST);
    verify(dockerConnection).path("/images/create");
    verify(dockerConnection).query("fromImage", pullParams.getRegistry() + '/' + pullParams.getImage());
    verify(dockerConnection).header(eq("X-Registry-Auth"), any(AuthConfig.class));
    verify(dockerConnection).request();
    verify(dockerResponse).getStatus();
    verify(dockerResponse).getInputStream();
}
Also used : PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) ByteArrayInputStream(java.io.ByteArrayInputStream) AuthConfig(org.eclipse.che.plugin.docker.client.dto.AuthConfig) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 5 with PullParams

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

the class DockerConnectorTest method shouldThrowDockerExceptionWhilePullingImageIfResponseCodeIsNotSuccess.

@Test(expectedExceptions = DockerException.class, expectedExceptionsMessageRegExp = EXCEPTION_ERROR_MESSAGE)
public void shouldThrowDockerExceptionWhilePullingImageIfResponseCodeIsNotSuccess() throws IOException, InterruptedException {
    PullParams pullParams = PullParams.create(IMAGE);
    when(dockerResponse.getStatus()).thenReturn(RESPONSE_ERROR_CODE);
    dockerConnector.pull(pullParams, progressMonitor);
    verify(dockerResponse).getStatus();
}
Also used : PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) Test(org.testng.annotations.Test)

Aggregations

PullParams (org.eclipse.che.plugin.docker.client.params.PullParams)5 Test (org.testng.annotations.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 URI (java.net.URI)2 AuthConfig (org.eclipse.che.plugin.docker.client.dto.AuthConfig)2 IOException (java.io.IOException)1 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)1 SourceNotFoundException (org.eclipse.che.api.machine.server.exception.SourceNotFoundException)1 MachineSourceImpl (org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl)1 ProgressMonitor (org.eclipse.che.plugin.docker.client.ProgressMonitor)1 ImageNotFoundException (org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)1 Matchers.anyString (org.mockito.Matchers.anyString)1