Search in sources :

Example 6 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.

the class UpdateCheckerTest method testPerformUpdateCheck_failSilently.

@Test
public void testPerformUpdateCheck_failSilently() throws InterruptedException, GeneralSecurityException, URISyntaxException, IOException {
    String response = "HTTP/1.1 400 Bad Request\nContent-Length: 0\n\n";
    try (TestWebServer badServer = new TestWebServer(false, Collections.singletonList(response), 1)) {
        Optional<String> message = UpdateChecker.performUpdateCheck(configDir, "1.0.2", badServer.getEndpoint(), "tool-name", logEvent -> {
            assertThat(logEvent.getLevel()).isEqualTo(LogEvent.Level.DEBUG);
            assertThat(logEvent.getMessage()).contains("Update check failed; ");
        });
        assertThat(message).isEmpty();
    }
}
Also used : TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 7 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.

the class RegistryClientTest method testPullManifest_manifestList.

@Test
public void testPullManifest_manifestList() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String manifestResponse = "HTTP/1.1 200 OK\nContent-Length: 403\n\n{\n" + "  \"schemaVersion\": 2,\n" + "  \"mediaType\": \"application/vnd.docker.distribution.manifest.list.v2+json\",\n" + "  \"manifests\": [\n" + "    {\n" + "      \"mediaType\": \"application/vnd.docker.distribution.manifest.v2+json\",\n" + "      \"size\": 7143,\n" + "      \"digest\": \"sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f\",\n" + "      \"platform\": {\n" + "        \"architecture\": \"amd64\",\n" + "        \"os\": \"linux\"\n" + "      }\n" + "    }\n" + "  ]\n" + "}";
    registry = new TestWebServer(false, Arrays.asList(manifestResponse), 1);
    RegistryClient registryClient = createRegistryClient(null);
    ManifestAndDigest<?> manifestAndDigest = registryClient.pullManifest("image-tag");
    Assert.assertEquals("sha256:a340fa38667f765f8cfd79d4bc684ec8a6f48cdd63abfe36e109f4125ee38488", manifestAndDigest.getDigest().toString());
    Assert.assertTrue(manifestAndDigest.getManifest() instanceof V22ManifestListTemplate);
    V22ManifestListTemplate manifestList = (V22ManifestListTemplate) manifestAndDigest.getManifest();
    Assert.assertEquals(2, manifestList.getSchemaVersion());
    Assert.assertEquals(Arrays.asList("sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f"), manifestList.getDigestsForPlatform("amd64", "linux"));
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.containsString("GET /v2/foo/bar/manifests/image-tag "));
}
Also used : V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 8 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.

the class RegistryClientTest method testAutomaticTokenRefresh_refreshLimit.

@Test
public void testAutomaticTokenRefresh_refreshLimit() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String tokenResponse = "HTTP/1.1 200 OK\nContent-Length: 26\n\n{\"token\":\"awesome-token!\"}";
    authServer = new TestWebServer(false, Arrays.asList(tokenResponse), 5);
    String bearerAuth = "HTTP/1.1 401 Unauthorized\nContent-Length: 0\nWWW-Authenticate: Bearer realm=\"" + authServer.getEndpoint() + "\"\n\n";
    String unauthorized = "HTTP/1.1 401 Unauthorized\nContent-Length: 0\n\n";
    List<String> responses = Arrays.asList(bearerAuth, unauthorized, unauthorized, unauthorized, unauthorized, unauthorized);
    registry = new TestWebServer(false, responses, responses.size(), true);
    RegistryClient registryClient = createRegistryClient(null);
    Assert.assertTrue(registryClient.doPushBearerAuth());
    try {
        registryClient.checkBlob(digest);
        Assert.fail("Should have given up refreshing after 4 attempts");
    } catch (RegistryUnauthorizedException ex) {
        Assert.assertEquals(401, ex.getHttpResponseException().getStatusCode());
        Assert.assertEquals(5, authServer.getTotalResponsesServed());
        // 1 response asking to do bearer auth + 4 unauth responses for 4 refresh attempts + 1 final
        // unauth response propagated as RegistryUnauthorizedException here
        Assert.assertEquals(6, registry.getTotalResponsesServed());
    }
}
Also used : RegistryUnauthorizedException(com.google.cloud.tools.jib.api.RegistryUnauthorizedException) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 9 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.

the class RegistryClientTest method testAuthPullByWwwAuthenticate_bearerAuth.

@Test
public void testAuthPullByWwwAuthenticate_bearerAuth() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String tokenResponse = "HTTP/1.1 200 OK\nContent-Length: 26\n\n{\"token\":\"awesome-token!\"}";
    authServer = new TestWebServer(false, Arrays.asList(tokenResponse), 1);
    String blobResponse = "HTTP/1.1 200 OK\nContent-Length: 5678\n\n";
    registry = new TestWebServer(false, Arrays.asList(blobResponse), 1);
    RegistryClient registryClient = createRegistryClient(Credential.from("user", "pass"));
    registryClient.authPullByWwwAuthenticate("Bearer realm=\"" + authServer.getEndpoint() + "\"");
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());
    Mockito.verify(eventHandlers).dispatch(logContains("bearer auth succeeded"));
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 10 with TestWebServer

use of com.google.cloud.tools.jib.http.TestWebServer in project jib by google.

the class RegistryClientTest method testAuthPullByWwwAuthenticate_basicAuthRequestedButOAuth2Credential.

@Test
public void testAuthPullByWwwAuthenticate_basicAuthRequestedButOAuth2Credential() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryException {
    String blobResponse = "HTTP/1.1 200 OK\nContent-Length: 5678\n\n";
    registry = new TestWebServer(false, Arrays.asList(blobResponse), 1);
    Credential credential = Credential.from(Credential.OAUTH2_TOKEN_USER_NAME, "pass");
    Assert.assertTrue(credential.isOAuth2RefreshToken());
    RegistryClient registryClient = createRegistryClient(credential);
    registryClient.authPullByWwwAuthenticate("Basic foo");
    Optional<BlobDescriptor> digestAndSize = registryClient.checkBlob(digest);
    Assert.assertEquals(5678, digestAndSize.get().getSize());
    MatcherAssert.assertThat(registry.getInputRead(), CoreMatchers.not(CoreMatchers.containsString("Authorization:")));
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Aggregations

TestWebServer (com.google.cloud.tools.jib.http.TestWebServer)30 Test (org.junit.Test)26 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)12 Credential (com.google.cloud.tools.jib.api.Credential)2 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 RegistryUnauthorizedException (com.google.cloud.tools.jib.api.RegistryUnauthorizedException)2 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)2 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2