Search in sources :

Example 16 with DockerApi

use of org.springframework.boot.buildpack.platform.docker.DockerApi in project spring-boot by spring-projects.

the class BuilderTests method buildInvokesBuilderWithTagsAndPublishesImageAndTags.

@Test
void buildInvokesBuilderWithTagsAndPublishesImageAndTags() throws Exception {
    TestPrintStream out = new TestPrintStream();
    DockerApi docker = mockDockerApi();
    Image builderImage = loadImage("image.json");
    Image runImage = loadImage("run-image.json");
    DockerConfiguration dockerConfiguration = new DockerConfiguration().withBuilderRegistryTokenAuthentication("builder token").withPublishRegistryTokenAuthentication("publish token");
    given(docker.image().pull(eq(ImageReference.of(BuildRequest.DEFAULT_BUILDER_IMAGE_NAME)), any(), eq(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()))).willAnswer(withPulledImage(builderImage));
    given(docker.image().pull(eq(ImageReference.of("docker.io/cloudfoundry/run:base-cnb")), any(), eq(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()))).willAnswer(withPulledImage(runImage));
    Builder builder = new Builder(BuildLog.to(out), docker, dockerConfiguration);
    BuildRequest request = getTestRequest().withPublish(true).withTags(ImageReference.of("my-application:1.2.3"));
    builder.build(request);
    assertThat(out.toString()).contains("Running creator");
    assertThat(out.toString()).contains("Successfully built image 'docker.io/library/my-application:latest'");
    assertThat(out.toString()).contains("Successfully created image tag 'docker.io/library/my-application:1.2.3'");
    then(docker.image()).should().pull(eq(ImageReference.of(BuildRequest.DEFAULT_BUILDER_IMAGE_NAME)), any(), eq(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()));
    then(docker.image()).should().pull(eq(ImageReference.of("docker.io/cloudfoundry/run:base-cnb")), any(), eq(dockerConfiguration.getBuilderRegistryAuthentication().getAuthHeader()));
    then(docker.image()).should().push(eq(request.getName()), any(), eq(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()));
    then(docker.image()).should().tag(eq(request.getName()), eq(ImageReference.of("my-application:1.2.3")));
    then(docker.image()).should().push(eq(ImageReference.of("my-application:1.2.3")), any(), eq(dockerConfiguration.getPublishRegistryAuthentication().getAuthHeader()));
    ArgumentCaptor<ImageArchive> archive = ArgumentCaptor.forClass(ImageArchive.class);
    then(docker.image()).should().load(archive.capture(), any());
    then(docker.image()).should().remove(archive.getValue().getTag(), true);
    then(docker.image()).shouldHaveNoMoreInteractions();
}
Also used : DockerConfiguration(org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) ImageArchive(org.springframework.boot.buildpack.platform.docker.type.ImageArchive) Image(org.springframework.boot.buildpack.platform.docker.type.Image) Test(org.junit.jupiter.api.Test)

Example 17 with DockerApi

use of org.springframework.boot.buildpack.platform.docker.DockerApi in project spring-boot by spring-projects.

the class BuilderTests method mockDockerApiLifecycleError.

private DockerApi mockDockerApiLifecycleError() throws IOException {
    ContainerApi containerApi = mock(ContainerApi.class);
    ContainerReference reference = ContainerReference.of("container-ref");
    given(containerApi.create(any(), any())).willReturn(reference);
    given(containerApi.wait(eq(reference))).willReturn(ContainerStatus.of(9, null));
    ImageApi imageApi = mock(ImageApi.class);
    VolumeApi volumeApi = mock(VolumeApi.class);
    DockerApi docker = mock(DockerApi.class);
    given(docker.image()).willReturn(imageApi);
    given(docker.container()).willReturn(containerApi);
    given(docker.volume()).willReturn(volumeApi);
    return docker;
}
Also used : ImageApi(org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi) VolumeApi(org.springframework.boot.buildpack.platform.docker.DockerApi.VolumeApi) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) ContainerApi(org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi) ContainerReference(org.springframework.boot.buildpack.platform.docker.type.ContainerReference)

Example 18 with DockerApi

use of org.springframework.boot.buildpack.platform.docker.DockerApi in project spring-boot by spring-projects.

the class LifecycleTests method mockDockerApi.

private DockerApi mockDockerApi() {
    DockerApi docker = mock(DockerApi.class);
    ImageApi imageApi = mock(ImageApi.class);
    ContainerApi containerApi = mock(ContainerApi.class);
    VolumeApi volumeApi = mock(VolumeApi.class);
    given(docker.image()).willReturn(imageApi);
    given(docker.container()).willReturn(containerApi);
    given(docker.volume()).willReturn(volumeApi);
    return docker;
}
Also used : ImageApi(org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi) VolumeApi(org.springframework.boot.buildpack.platform.docker.DockerApi.VolumeApi) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) ContainerApi(org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi)

Example 19 with DockerApi

use of org.springframework.boot.buildpack.platform.docker.DockerApi in project spring-boot by spring-projects.

the class BootBuildImageRegistryIntegrationTests method buildsImageAndPublishesToRegistry.

@TestTemplate
void buildsImageAndPublishesToRegistry() throws IOException {
    writeMainClass();
    String repoName = "test-image";
    String imageName = this.registryAddress + "/" + repoName;
    BuildResult result = this.gradleBuild.build("bootBuildImage", "--imageName=" + imageName);
    assertThat(result.task(":bootBuildImage").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
    assertThat(result.getOutput()).contains("Building image").contains("Successfully built image").contains("Pushing image '" + imageName + ":latest" + "'").contains("Pushed image '" + imageName + ":latest" + "'");
    ImageReference imageReference = ImageReference.of(imageName);
    Image pulledImage = new DockerApi().image().pull(imageReference, UpdateListener.none());
    assertThat(pulledImage).isNotNull();
    new DockerApi().image().remove(imageReference, false);
}
Also used : BuildResult(org.gradle.testkit.runner.BuildResult) ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) Image(org.springframework.boot.buildpack.platform.docker.type.Image) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 20 with DockerApi

use of org.springframework.boot.buildpack.platform.docker.DockerApi in project spring-boot by spring-projects.

the class BuildImageRegistryIntegrationTests method whenBuildImageIsInvokedWithPublish.

@TestTemplate
void whenBuildImageIsInvokedWithPublish(MavenBuild mavenBuild) {
    String repoName = "test-image";
    String imageName = this.registryAddress + "/" + repoName;
    mavenBuild.project("build-image-publish").goals("package").systemProperty("spring-boot.build-image.imageName", imageName).execute((project) -> {
        assertThat(buildLog(project)).contains("Building image").contains("Successfully built image").contains("Pushing image '" + imageName + ":latest" + "'").contains("Pushed image '" + imageName + ":latest" + "'");
        ImageReference imageReference = ImageReference.of(imageName);
        DockerApi.ImageApi imageApi = new DockerApi().image();
        Image pulledImage = imageApi.pull(imageReference, UpdateListener.none());
        assertThat(pulledImage).isNotNull();
        imageApi.remove(imageReference, false);
    });
}
Also used : ImageReference(org.springframework.boot.buildpack.platform.docker.type.ImageReference) DockerApi(org.springframework.boot.buildpack.platform.docker.DockerApi) Image(org.springframework.boot.buildpack.platform.docker.type.Image) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

DockerApi (org.springframework.boot.buildpack.platform.docker.DockerApi)21 Image (org.springframework.boot.buildpack.platform.docker.type.Image)17 Test (org.junit.jupiter.api.Test)15 ImageArchive (org.springframework.boot.buildpack.platform.docker.type.ImageArchive)10 DockerConfiguration (org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration)4 ContainerApi (org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi)3 ImageApi (org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi)3 VolumeApi (org.springframework.boot.buildpack.platform.docker.DockerApi.VolumeApi)3 TestTemplate (org.junit.jupiter.api.TestTemplate)2 ContainerReference (org.springframework.boot.buildpack.platform.docker.type.ContainerReference)2 ImageReference (org.springframework.boot.buildpack.platform.docker.type.ImageReference)2 URI (java.net.URI)1 BuildResult (org.gradle.testkit.runner.BuildResult)1 DockerEngineException (org.springframework.boot.buildpack.platform.docker.transport.DockerEngineException)1 Layer (org.springframework.boot.buildpack.platform.docker.type.Layer)1