22
22
import java .io .InputStream ;
23
23
import java .io .OutputStream ;
24
24
import java .net .URI ;
25
+ import java .net .URISyntaxException ;
25
26
import java .nio .file .Path ;
26
27
import java .util .ArrayList ;
27
28
import java .util .Arrays ;
87
88
@ ExtendWith (MockitoExtension .class )
88
89
class DockerApiTests {
89
90
90
- private static final String API_URL = "/v" + DockerApi .MINIMUM_API_VERSION ;
91
+ private static final String API_URL = "/v" + DockerApi .API_VERSION ;
92
+
93
+ private static final String PLATFORM_API_URL = "/v" + DockerApi .PLATFORM_API_VERSION ;
91
94
92
95
public static final String PING_URL = "/_ping" ;
93
96
94
97
private static final String IMAGES_URL = API_URL + "/images" ;
95
98
96
- private static final String IMAGES_1_41_URL = "/v" + ApiVersion . of ( 1 , 41 ) + "/images" ;
99
+ private static final String PLATFORM_IMAGES_URL = PLATFORM_API_URL + "/images" ;
97
100
98
101
private static final String CONTAINERS_URL = API_URL + "/containers" ;
99
102
100
- private static final String CONTAINERS_1_41_URL = "/v" + ApiVersion . of ( 1 , 41 ) + "/containers" ;
103
+ private static final String PLATFORM_CONTAINERS_URL = PLATFORM_API_URL + "/containers" ;
101
104
102
105
private static final String VOLUMES_URL = API_URL + "/volumes" ;
103
106
@@ -235,9 +238,9 @@ void pullWithRegistryAuthPullsImageAndProducesEvents() throws Exception {
235
238
void pullWithPlatformPullsImageAndProducesEvents () throws Exception {
236
239
ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
237
240
ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
238
- URI createUri = new URI (IMAGES_1_41_URL
241
+ URI createUri = new URI (PLATFORM_IMAGES_URL
239
242
+ "/create?fromImage=gcr.io%2Fpaketo-buildpacks%2Fbuilder%3Abase&platform=linux%2Farm64%2Fv1" );
240
- URI imageUri = new URI (IMAGES_1_41_URL + "/gcr.io/paketo-buildpacks/builder:base/json" );
243
+ URI imageUri = new URI (PLATFORM_IMAGES_URL + "/gcr.io/paketo-buildpacks/builder:base/json" );
241
244
given (http ().head (eq (new URI (PING_URL ))))
242
245
.willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.41" )));
243
246
given (http ().post (eq (createUri ), isNull ())).willReturn (responseOf ("pull-stream.json" ));
@@ -254,9 +257,9 @@ void pullWithPlatformPullsImageAndProducesEvents() throws Exception {
254
257
void pullWithPlatformAndInsufficientApiVersionThrowsException () throws Exception {
255
258
ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
256
259
ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
257
- given (http ().head (eq (new URI (PING_URL )))).willReturn (responseWithHeaders (
258
- new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , DockerApi .MINIMUM_API_VERSION )));
259
- assertThatIllegalArgumentException ().isThrownBy (() -> this .api .pull (reference , platform , this .pullListener ))
260
+ given (http ().head (eq (new URI (PING_URL )))).willReturn (
261
+ responseWithHeaders ( new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , DockerApi .API_VERSION )));
262
+ assertThatIllegalStateException ().isThrownBy (() -> this .api .pull (reference , platform , this .pullListener ))
260
263
.withMessageContaining ("must be at least 1.41" )
261
264
.withMessageContaining ("current API version is 1.24" );
262
265
}
@@ -583,12 +586,23 @@ void createWhenHasContentContainerWithContent() throws Exception {
583
586
584
587
@ Test
585
588
void createWithPlatformCreatesContainer () throws Exception {
589
+ createWithPlatform ("1.41" );
590
+ }
591
+
592
+ @ Test
593
+ void createWithPlatformAndUnknownApiVersionAttemptsCreate () throws Exception {
594
+ createWithPlatform (null );
595
+ }
596
+
597
+ private void createWithPlatform (String apiVersion ) throws IOException , URISyntaxException {
586
598
ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
587
599
ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
588
600
ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
589
- given (http ().head (eq (new URI (PING_URL ))))
590
- .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.41" )));
591
- URI createUri = new URI (CONTAINERS_1_41_URL + "/create?platform=linux%2Farm64%2Fv1" );
601
+ if (apiVersion != null ) {
602
+ given (http ().head (eq (new URI (PING_URL ))))
603
+ .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , apiVersion )));
604
+ }
605
+ URI createUri = new URI (PLATFORM_CONTAINERS_URL + "/create?platform=linux%2Farm64%2Fv1" );
592
606
given (http ().post (eq (createUri ), eq ("application/json" ), any ()))
593
607
.willReturn (responseOf ("create-container-response.json" ));
594
608
ContainerReference containerReference = this .api .create (config , platform );
@@ -600,11 +614,13 @@ void createWithPlatformCreatesContainer() throws Exception {
600
614
}
601
615
602
616
@ Test
603
- void createWithPlatformAndInsufficientApiVersionThrowsException () {
617
+ void createWithPlatformAndKnownInsufficientApiVersionThrowsException () throws Exception {
604
618
ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
605
619
ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
606
620
ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
607
- assertThatIllegalArgumentException ().isThrownBy (() -> this .api .create (config , platform ))
621
+ given (http ().head (eq (new URI (PING_URL ))))
622
+ .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.24" )));
623
+ assertThatIllegalStateException ().isThrownBy (() -> this .api .create (config , platform ))
608
624
.withMessageContaining ("must be at least 1.41" )
609
625
.withMessageContaining ("current API version is 1.24" );
610
626
}
@@ -744,22 +760,22 @@ void getApiVersionWithVersionHeaderReturnsVersion() throws Exception {
744
760
}
745
761
746
762
@ Test
747
- void getApiVersionWithEmptyVersionHeaderReturnsDefaultVersion () throws Exception {
763
+ void getApiVersionWithEmptyVersionHeaderReturnsUnknownVersion () throws Exception {
748
764
given (http ().head (eq (new URI (PING_URL ))))
749
765
.willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "" )));
750
- assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .MINIMUM_API_VERSION );
766
+ assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .UNKNOWN_API_VERSION );
751
767
}
752
768
753
769
@ Test
754
- void getApiVersionWithNoVersionHeaderReturnsDefaultVersion () throws Exception {
770
+ void getApiVersionWithNoVersionHeaderReturnsUnknownVersion () throws Exception {
755
771
given (http ().head (eq (new URI (PING_URL )))).willReturn (emptyResponse ());
756
- assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .MINIMUM_API_VERSION );
772
+ assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .UNKNOWN_API_VERSION );
757
773
}
758
774
759
775
@ Test
760
- void getApiVersionWithExceptionReturnsDefaultVersion () throws Exception {
776
+ void getApiVersionWithExceptionReturnsUnknownVersion () throws Exception {
761
777
given (http ().head (eq (new URI (PING_URL )))).willThrow (new IOException ("simulated error" ));
762
- assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .MINIMUM_API_VERSION );
778
+ assertThat (this .api .getApiVersion ()).isEqualTo (DockerApi .UNKNOWN_API_VERSION );
763
779
}
764
780
765
781
}
0 commit comments