|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All rights reserved. |
| 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.jib.gradle; |
| 18 | + |
| 19 | +import com.google.cloud.tools.jib.image.ImageReference; |
| 20 | +import com.google.cloud.tools.jib.image.InvalidImageReferenceException; |
| 21 | +import org.gradle.api.Project; |
| 22 | +import org.gradle.testfixtures.ProjectBuilder; |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.mockito.junit.MockitoJUnitRunner; |
| 30 | + |
| 31 | +@RunWith(MockitoJUnitRunner.class) |
| 32 | +public class BuildDockerTaskTest { |
| 33 | + |
| 34 | + @Mock private JibExtension mockJibExtension; |
| 35 | + @Mock private GradleBuildLogger mockBuildLogger; |
| 36 | + |
| 37 | + private BuildDockerTask task; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setup() { |
| 41 | + Project tempProject = ProjectBuilder.builder().withName("project-name").build(); |
| 42 | + tempProject.setVersion("project-version"); |
| 43 | + task = tempProject.getTasks().create("tempDockerTask", BuildDockerTask.class); |
| 44 | + task.setJibExtension(mockJibExtension); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testGetDockerTag_configured() throws InvalidImageReferenceException { |
| 49 | + Mockito.when(mockJibExtension.getTargetImage()).thenReturn("a/b:c"); |
| 50 | + ImageReference result = task.getDockerTag(mockBuildLogger); |
| 51 | + Assert.assertEquals("a/b", result.getRepository()); |
| 52 | + Assert.assertEquals("c", result.getTag()); |
| 53 | + Mockito.verify(mockBuildLogger, Mockito.never()).lifecycle(Mockito.any()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testGetDockerTag_notConfigured() throws InvalidImageReferenceException { |
| 58 | + Mockito.when(mockJibExtension.getTargetImage()).thenReturn(null); |
| 59 | + ImageReference result = task.getDockerTag(mockBuildLogger); |
| 60 | + Assert.assertEquals("project-name", result.getRepository()); |
| 61 | + Assert.assertEquals("project-version", result.getTag()); |
| 62 | + Mockito.verify(mockBuildLogger) |
| 63 | + .lifecycle( |
| 64 | + "Tagging image with generated image reference project-name:project-version. If you'd " |
| 65 | + + "like to specify a different tag, you can set the jib.to.image parameter in your " |
| 66 | + + "build.gradle, or use the --image=<MY IMAGE> commandline flag."); |
| 67 | + } |
| 68 | +} |
0 commit comments