Search in sources :

Example 6 with RegistryConfigs

use of com.spotify.docker.client.messages.RegistryConfigs in project docker-client by spotify.

the class ContainerRegistryAuthSupplierTest method testAuthForBuild_ExceptionOnRefresh.

@Test
public void testAuthForBuild_ExceptionOnRefresh() throws Exception {
    when(clock.currentTimeMillis()).thenReturn(expiration.minusSeconds(minimumExpirationSecs - 1).getMillis());
    doThrow(new IOException("failure!!")).when(refresher).refresh(credentials);
    final RegistryConfigs configs = supplier.authForBuild();
    assertThat(configs.configs().values(), is(empty()));
}
Also used : RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with RegistryConfigs

use of com.spotify.docker.client.messages.RegistryConfigs in project docker-client by spotify.

the class DefaultDockerClient method build.

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
@Override
public String build(final Path directory, final String name, final String dockerfile, final ProgressHandler handler, final BuildParam... params) throws DockerException, InterruptedException, IOException {
    checkNotNull(handler, "handler");
    WebTarget resource = noTimeoutResource().path("build");
    for (final BuildParam param : params) {
        resource = resource.queryParam(param.name(), param.value());
    }
    if (name != null) {
        resource = resource.queryParam("t", name);
    }
    if (dockerfile != null) {
        resource = resource.queryParam("dockerfile", dockerfile);
    }
    // Convert auth to X-Registry-Config format
    final RegistryConfigs registryConfigs = registryAuthSupplier.authForBuild();
    try (final CompressedDirectory compressedDirectory = CompressedDirectory.create(directory);
        final InputStream fileStream = Files.newInputStream(compressedDirectory.file());
        final ProgressStream build = request(POST, ProgressStream.class, resource, resource.request(APPLICATION_JSON_TYPE).header("X-Registry-Config", authRegistryHeader(registryConfigs)), Entity.entity(fileStream, "application/tar"))) {
        String imageId = null;
        while (build.hasNextMessage(POST, resource.getUri())) {
            final ProgressMessage message = build.nextMessage(POST, resource.getUri());
            final String id = message.buildImageId();
            if (id != null) {
                imageId = id;
            }
            handler.progress(message);
        }
        return imageId;
    }
}
Also used : ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) InputStream(java.io.InputStream) WebTarget(javax.ws.rs.client.WebTarget) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 8 with RegistryConfigs

use of com.spotify.docker.client.messages.RegistryConfigs in project docker-client by spotify.

the class ContainerRegistryAuthSupplierTest method testAuthForBuild_RefreshNeeded.

@Test
public void testAuthForBuild_RefreshNeeded() throws Exception {
    when(clock.currentTimeMillis()).thenReturn(expiration.minusSeconds(minimumExpirationSecs - 1).getMillis());
    final RegistryConfigs configs = supplier.authForBuild();
    assertThat(configs.configs().values(), is(not(empty())));
    assertThat(configs.configs().values(), everyItem(matchesAccessToken(accessToken)));
    verify(refresher).refresh(credentials);
}
Also used : RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) Test(org.junit.Test)

Example 9 with RegistryConfigs

use of com.spotify.docker.client.messages.RegistryConfigs in project docker-client by spotify.

the class DefaultDockerClientUnitTest method testBuildPassesMultipleRegistryConfigs.

@Test
public void testBuildPassesMultipleRegistryConfigs() throws Exception {
    final RegistryConfigs registryConfigs = RegistryConfigs.create(ImmutableMap.of("server1", RegistryAuth.builder().serverAddress("server1").username("u1").password("p1").email("e1").build(), "server2", RegistryAuth.builder().serverAddress("server2").username("u2").password("p2").email("e2").build()));
    final RegistryAuthSupplier authSupplier = mock(RegistryAuthSupplier.class);
    when(authSupplier.authForBuild()).thenReturn(registryConfigs);
    final DefaultDockerClient client = builder.registryAuthSupplier(authSupplier).build();
    // build() calls /version to check what format of header to send
    enqueueServerApiVersion("1.20");
    // TODO (mbrown): what to return for build response?
    server.enqueue(new MockResponse().setResponseCode(200));
    final Path path = Paths.get(Resources.getResource("dockerDirectory").toURI());
    client.build(path);
    final RecordedRequest versionRequest = takeRequestImmediately();
    assertThat(versionRequest.getMethod(), is("GET"));
    assertThat(versionRequest.getPath(), is("/version"));
    final RecordedRequest buildRequest = takeRequestImmediately();
    assertThat(buildRequest.getMethod(), is("POST"));
    assertThat(buildRequest.getPath(), is("/build"));
    final String registryConfigHeader = buildRequest.getHeader("X-Registry-Config");
    assertThat(registryConfigHeader, is(not(nullValue())));
    // check that the JSON in the header is equivalent to what we mocked out above from
    // the registryAuthSupplier
    final JsonNode headerJsonNode = toJson(BaseEncoding.base64().decode(registryConfigHeader));
    assertThat(headerJsonNode, is(toJson(registryConfigs.configs())));
}
Also used : Path(java.nio.file.Path) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) JsonNode(com.fasterxml.jackson.databind.JsonNode) Matchers.containsString(org.hamcrest.Matchers.containsString) RegistryAuthSupplier(com.spotify.docker.client.auth.RegistryAuthSupplier) Test(org.junit.Test)

Example 10 with RegistryConfigs

use of com.spotify.docker.client.messages.RegistryConfigs in project docker-client by spotify.

the class MultiRegistryAuthSupplierTest method testAuthForBuild_ReturnsNull.

/**
 * Test what happens if one of the Suppliers returns null for authForBuild().
 */
@Test
public void testAuthForBuild_ReturnsNull() throws Exception {
    when(supplier1.authForBuild()).thenReturn(null);
    final RegistryConfigs registryConfigs = RegistryConfigs.create(ImmutableMap.of("a", RegistryAuth.builder().username("1").serverAddress("a").build(), "b", RegistryAuth.builder().username("2").serverAddress("b").build()));
    when(supplier2.authForBuild()).thenReturn(registryConfigs);
    assertThat(multiSupplier.authForBuild(), is(registryConfigs));
}
Also used : RegistryConfigs(com.spotify.docker.client.messages.RegistryConfigs) Test(org.junit.Test)

Aggregations

RegistryConfigs (com.spotify.docker.client.messages.RegistryConfigs)10 Test (org.junit.Test)9 Path (java.nio.file.Path)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 AccessToken (com.google.auth.oauth2.AccessToken)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 RegistryAuthSupplier (com.spotify.docker.client.auth.RegistryAuthSupplier)1 ProgressMessage (com.spotify.docker.client.messages.ProgressMessage)1 RegistryAuth (com.spotify.docker.client.messages.RegistryAuth)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 WebTarget (javax.ws.rs.client.WebTarget)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1