2323import com .google .cloud .tools .jib .cache .Caches ;
2424import com .google .cloud .tools .jib .frontend .ExposedPortsParser ;
2525import com .google .cloud .tools .jib .image .ImageReference ;
26+ import com .google .cloud .tools .jib .image .InvalidImageReferenceException ;
2627import com .google .cloud .tools .jib .registry .LocalRegistry ;
2728import java .io .IOException ;
2829import java .net .URISyntaxException ;
3233import java .util .concurrent .ExecutionException ;
3334import org .hamcrest .CoreMatchers ;
3435import org .junit .Assert ;
36+ import org .junit .Before ;
3537import org .junit .ClassRule ;
3638import org .junit .Rule ;
3739import org .junit .Test ;
@@ -46,28 +48,28 @@ public class BuildStepsIntegrationTest {
4648
4749 @ Rule public TemporaryFolder temporaryCacheDirectory = new TemporaryFolder ();
4850
51+ private SourceFilesConfiguration sourceFilesConfiguration ;
52+
53+ @ Before
54+ public void setUp () throws IOException , URISyntaxException {
55+ sourceFilesConfiguration = new TestSourceFilesConfiguration ();
56+ }
57+
4958 @ Test
5059 public void testSteps_forBuildToDockerRegistry ()
51- throws IOException , URISyntaxException , InterruptedException , CacheMetadataCorruptedException ,
52- ExecutionException , CacheDirectoryNotOwnedException , CacheDirectoryCreationException {
53- SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration ();
54- BuildConfiguration buildConfiguration =
55- BuildConfiguration .builder (logger )
56- .setBaseImage (ImageReference .of ("gcr.io" , "distroless/java" , "latest" ))
57- .setTargetImage (ImageReference .of ("localhost:5000" , "testimage" , "testtag" ))
58- .setMainClass ("HelloWorld" )
59- .setJavaArguments (Collections .singletonList ("An argument." ))
60- .setExposedPorts (
61- ExposedPortsParser .parse (Arrays .asList ("1000" , "2000-2002/tcp" , "3000/udp" )))
62- .setAllowHttp (true )
63- .build ();
64-
65- Path cacheDirectory = temporaryCacheDirectory .newFolder ().toPath ();
60+ throws IOException , InterruptedException , CacheMetadataCorruptedException , ExecutionException ,
61+ CacheDirectoryNotOwnedException , CacheDirectoryCreationException {
6662 BuildSteps buildImageSteps =
67- BuildSteps .forBuildToDockerRegistry (
68- buildConfiguration ,
69- sourceFilesConfiguration ,
70- new Caches .Initializer (cacheDirectory , false , cacheDirectory , false ));
63+ getBuildSteps (
64+ BuildConfiguration .builder (logger )
65+ .setBaseImage (ImageReference .of ("gcr.io" , "distroless/java" , "latest" ))
66+ .setTargetImage (ImageReference .of ("localhost:5000" , "testimage" , "testtag" ))
67+ .setMainClass ("HelloWorld" )
68+ .setJavaArguments (Collections .singletonList ("An argument." ))
69+ .setExposedPorts (
70+ ExposedPortsParser .parse (Arrays .asList ("1000" , "2000-2002/tcp" , "3000/udp" )))
71+ .setAllowHttp (true )
72+ .build ());
7173
7274 long lastTime = System .nanoTime ();
7375 buildImageSteps .run ();
@@ -91,11 +93,31 @@ public void testSteps_forBuildToDockerRegistry()
9193 "Hello, world. An argument.\n " , new Command ("docker" , "run" , imageReference ).run ());
9294 }
9395
96+ @ Test
97+ public void testSteps_forBuildToDockerRegistry_dockerHubBaseImage ()
98+ throws InvalidImageReferenceException , IOException , InterruptedException , ExecutionException ,
99+ CacheDirectoryCreationException , CacheMetadataCorruptedException ,
100+ CacheDirectoryNotOwnedException {
101+ getBuildSteps (
102+ BuildConfiguration .builder (logger )
103+ .setBaseImage (ImageReference .parse ("openjdk:8-jre-alpine" ))
104+ .setTargetImage (ImageReference .of ("localhost:5000" , "testimage" , "testtag" ))
105+ .setMainClass ("HelloWorld" )
106+ .setJavaArguments (Collections .singletonList ("An argument." ))
107+ .setAllowHttp (true )
108+ .build ())
109+ .run ();
110+
111+ String imageReference = "localhost:5000/testimage:testtag" ;
112+ new Command ("docker" , "pull" , imageReference ).run ();
113+ Assert .assertEquals (
114+ "Hello, world. An argument.\n " , new Command ("docker" , "run" , imageReference ).run ());
115+ }
116+
94117 @ Test
95118 public void testSteps_forBuildToDockerDaemon ()
96- throws IOException , URISyntaxException , InterruptedException , CacheMetadataCorruptedException ,
97- ExecutionException , CacheDirectoryNotOwnedException , CacheDirectoryCreationException {
98- SourceFilesConfiguration sourceFilesConfiguration = new TestSourceFilesConfiguration ();
119+ throws IOException , InterruptedException , CacheMetadataCorruptedException , ExecutionException ,
120+ CacheDirectoryNotOwnedException , CacheDirectoryCreationException {
99121 BuildConfiguration buildConfiguration =
100122 BuildConfiguration .builder (logger )
101123 .setBaseImage (ImageReference .of ("gcr.io" , "distroless/java" , "latest" ))
@@ -107,13 +129,12 @@ public void testSteps_forBuildToDockerDaemon()
107129 .build ();
108130
109131 Path cacheDirectory = temporaryCacheDirectory .newFolder ().toPath ();
110- BuildSteps buildDockerSteps =
111- BuildSteps .forBuildToDockerDaemon (
132+ BuildSteps .forBuildToDockerDaemon (
112133 buildConfiguration ,
113134 sourceFilesConfiguration ,
114- new Caches .Initializer (cacheDirectory , false , cacheDirectory , false ));
135+ new Caches .Initializer (cacheDirectory , false , cacheDirectory , false ))
136+ .run ();
115137
116- buildDockerSteps .run ();
117138 Assert .assertThat (
118139 new Command ("docker" , "inspect" , "testdocker" ).run (),
119140 CoreMatchers .containsString (
@@ -126,4 +147,12 @@ public void testSteps_forBuildToDockerDaemon()
126147 Assert .assertEquals (
127148 "Hello, world. An argument.\n " , new Command ("docker" , "run" , "testdocker" ).run ());
128149 }
150+
151+ private BuildSteps getBuildSteps (BuildConfiguration buildConfiguration ) throws IOException {
152+ Path cacheDirectory = temporaryCacheDirectory .newFolder ().toPath ();
153+ return BuildSteps .forBuildToDockerRegistry (
154+ buildConfiguration ,
155+ sourceFilesConfiguration ,
156+ new Caches .Initializer (cacheDirectory , false , cacheDirectory , false ));
157+ }
129158}
0 commit comments