2020import com .google .api .client .http .HttpStatusCodes ;
2121import com .google .cloud .tools .jib .builder .BuildConfiguration ;
2222import com .google .cloud .tools .jib .builder .BuildImageSteps ;
23+ import com .google .cloud .tools .jib .cache .CacheDirectoryNotOwnedException ;
2324import com .google .cloud .tools .jib .cache .CacheMetadataCorruptedException ;
2425import com .google .cloud .tools .jib .registry .RegistryUnauthorizedException ;
2526import java .io .IOException ;
27+ import java .nio .file .Path ;
28+ import java .nio .file .Paths ;
2629import java .util .Collections ;
2730import java .util .concurrent .ExecutionException ;
2831import org .apache .http .conn .HttpHostConnectException ;
@@ -60,8 +63,8 @@ public void testBuildImage_pass() throws MojoExecutionException {
6063
6164 @ Test
6265 public void testBuildImage_cacheMetadataCorruptedException ()
63- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
64- IOException {
66+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
67+ CacheDirectoryNotOwnedException {
6568 CacheMetadataCorruptedException mockCacheMetadataCorruptedException =
6669 Mockito .mock (CacheMetadataCorruptedException .class );
6770 Mockito .doThrow (mockCacheMetadataCorruptedException ).when (mockBuildImageSteps ).run ();
@@ -80,8 +83,8 @@ public void testBuildImage_cacheMetadataCorruptedException()
8083
8184 @ Test
8285 public void testBuildImage_executionException_httpHostConnectException ()
83- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
84- IOException {
86+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
87+ CacheDirectoryNotOwnedException {
8588 HttpHostConnectException mockHttpHostConnectException =
8689 Mockito .mock (HttpHostConnectException .class );
8790 Mockito .when (mockExecutionException .getCause ()).thenReturn (mockHttpHostConnectException );
@@ -101,8 +104,8 @@ public void testBuildImage_executionException_httpHostConnectException()
101104
102105 @ Test
103106 public void testBuildImage_executionException_registryUnauthorizedException_statusCodeForbidden ()
104- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
105- IOException {
107+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
108+ CacheDirectoryNotOwnedException {
106109 Mockito .when (mockRegistryUnauthorizedException .getHttpResponseException ())
107110 .thenReturn (mockHttpResponseException );
108111 Mockito .when (mockRegistryUnauthorizedException .getImageReference ())
@@ -127,8 +130,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_stat
127130
128131 @ Test
129132 public void testBuildImage_executionException_registryUnauthorizedException_noCredentials ()
130- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
131- IOException {
133+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
134+ CacheDirectoryNotOwnedException {
132135 Mockito .when (mockRegistryUnauthorizedException .getHttpResponseException ())
133136 .thenReturn (mockHttpResponseException );
134137 Mockito .when (mockRegistryUnauthorizedException .getRegistry ()).thenReturn ("someregistry" );
@@ -151,8 +154,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_noCr
151154
152155 @ Test
153156 public void testBuildImage_executionException_registryUnauthorizedException_other ()
154- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
155- IOException {
157+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
158+ CacheDirectoryNotOwnedException {
156159 Mockito .when (mockRegistryUnauthorizedException .getHttpResponseException ())
157160 .thenReturn (mockHttpResponseException );
158161 Mockito .when (mockRegistryUnauthorizedException .getRegistry ()).thenReturn ("someregistry" );
@@ -178,8 +181,8 @@ public void testBuildImage_executionException_registryUnauthorizedException_othe
178181
179182 @ Test
180183 public void testBuildImage_executionException_other ()
181- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
182- IOException {
184+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
185+ CacheDirectoryNotOwnedException {
183186 Throwable throwable = new Throwable ();
184187 Mockito .when (mockExecutionException .getCause ()).thenReturn (throwable );
185188 Mockito .doThrow (mockExecutionException ).when (mockBuildImageSteps ).run ();
@@ -196,8 +199,8 @@ public void testBuildImage_executionException_other()
196199
197200 @ Test
198201 public void testBuildImage_otherException ()
199- throws InterruptedException , ExecutionException , CacheMetadataCorruptedException ,
200- IOException {
202+ throws InterruptedException , ExecutionException , CacheMetadataCorruptedException , IOException ,
203+ CacheDirectoryNotOwnedException {
201204 IOException ioException = new IOException ();
202205 Mockito .doThrow (ioException ).when (mockBuildImageSteps ).run ();
203206
@@ -215,4 +218,30 @@ public void testBuildImage_otherException()
215218 Mockito .verify (mockLog ).error (ioException );
216219 }
217220 }
221+
222+ @ Test
223+ public void testBuildImage_cacheDirectoryNotOwnedException ()
224+ throws InterruptedException , ExecutionException , CacheDirectoryNotOwnedException ,
225+ CacheMetadataCorruptedException , IOException {
226+ Path expectedCacheDirectory = Paths .get ("some/path" );
227+
228+ CacheDirectoryNotOwnedException mockCacheDirectoryNotOwnedException =
229+ Mockito .mock (CacheDirectoryNotOwnedException .class );
230+ Mockito .when (mockCacheDirectoryNotOwnedException .getCacheDirectory ())
231+ .thenReturn (expectedCacheDirectory );
232+ Mockito .doThrow (mockCacheDirectoryNotOwnedException ).when (mockBuildImageSteps ).run ();
233+
234+ try {
235+ testBuildImageMojo .buildImage (mockBuildImageSteps );
236+ Assert .fail ("buildImage should have thrown an exception" );
237+
238+ } catch (MojoExecutionException ex ) {
239+ Assert .assertEquals (
240+ "Build image failed, perhaps you should check that '"
241+ + expectedCacheDirectory
242+ + "' is not used by another application or set the `useOnlyProjectCache` configuration" ,
243+ ex .getMessage ());
244+ Assert .assertEquals (mockCacheDirectoryNotOwnedException , ex .getCause ());
245+ }
246+ }
218247}
0 commit comments