|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.crepecake.registry; |
| 18 | + |
| 19 | +import com.google.cloud.tools.crepecake.http.Authorization; |
| 20 | +import java.io.IOException; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +/** Integration tests for {@link DockerCredentialRetriever}. */ |
| 25 | +public class DockerCredentialRetrieverIntegrationTest { |
| 26 | + |
| 27 | + /** Tests retrieval via {@code docker-credential-gcr} CLI. */ |
| 28 | + @Test |
| 29 | + public void testRetrieveGCR() |
| 30 | + throws IOException, NonexistentServerUrlDockerCredentialHelperException, |
| 31 | + NonexistentDockerCredentialHelperException { |
| 32 | + DockerCredentialRetriever dockerCredentialRetriever = |
| 33 | + new DockerCredentialRetriever("gcr.io", "gcr"); |
| 34 | + |
| 35 | + Authorization authorization = dockerCredentialRetriever.retrieve(); |
| 36 | + |
| 37 | + // Checks that some token was received. |
| 38 | + Assert.assertTrue(0 < authorization.getToken().length()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testRetrieve_nonexistentCredentialHelper() |
| 43 | + throws IOException, NonexistentServerUrlDockerCredentialHelperException { |
| 44 | + try { |
| 45 | + DockerCredentialRetriever fakeDockerCredentialRetriever = |
| 46 | + new DockerCredentialRetriever("", "fake-cloud-provider"); |
| 47 | + |
| 48 | + fakeDockerCredentialRetriever.retrieve(); |
| 49 | + |
| 50 | + Assert.fail("Retrieve should have failed for nonexistent credential helper"); |
| 51 | + |
| 52 | + } catch (NonexistentDockerCredentialHelperException ex) { |
| 53 | + Assert.assertEquals( |
| 54 | + "The system does not have docker-credential-fake-cloud-provider CLI", ex.getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testRetrieve_nonexistentServerUrl() |
| 60 | + throws IOException, NonexistentDockerCredentialHelperException { |
| 61 | + try { |
| 62 | + DockerCredentialRetriever fakeDockerCredentialRetriever = |
| 63 | + new DockerCredentialRetriever("fake.server.url", "gcr"); |
| 64 | + |
| 65 | + fakeDockerCredentialRetriever.retrieve(); |
| 66 | + |
| 67 | + Assert.fail("Retrieve should have failed for nonexistent server URL"); |
| 68 | + |
| 69 | + } catch (NonexistentServerUrlDockerCredentialHelperException ex) { |
| 70 | + Assert.assertEquals( |
| 71 | + "The credential helper (docker-credential-gcr) has nothing for server URL: fake.server.url", |
| 72 | + ex.getMessage()); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments