2222import java .io .InputStream ;
2323import java .io .OutputStream ;
2424import java .net .URI ;
25+ import java .net .URISyntaxException ;
2526import java .nio .file .Path ;
2627import java .util .ArrayList ;
2728import java .util .Arrays ;
8788@ ExtendWith (MockitoExtension .class )
8889class DockerApiTests {
8990
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 ;
9194
9295 public static final String PING_URL = "/_ping" ;
9396
9497 private static final String IMAGES_URL = API_URL + "/images" ;
9598
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" ;
97100
98101 private static final String CONTAINERS_URL = API_URL + "/containers" ;
99102
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" ;
101104
102105 private static final String VOLUMES_URL = API_URL + "/volumes" ;
103106
@@ -235,9 +238,9 @@ void pullWithRegistryAuthPullsImageAndProducesEvents() throws Exception {
235238 void pullWithPlatformPullsImageAndProducesEvents () throws Exception {
236239 ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
237240 ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
238- URI createUri = new URI (IMAGES_1_41_URL
241+ URI createUri = new URI (PLATFORM_IMAGES_URL
239242 + "/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" );
241244 given (http ().head (eq (new URI (PING_URL ))))
242245 .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.41" )));
243246 given (http ().post (eq (createUri ), isNull ())).willReturn (responseOf ("pull-stream.json" ));
@@ -254,9 +257,9 @@ void pullWithPlatformPullsImageAndProducesEvents() throws Exception {
254257 void pullWithPlatformAndInsufficientApiVersionThrowsException () throws Exception {
255258 ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
256259 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 ))
260263 .withMessageContaining ("must be at least 1.41" )
261264 .withMessageContaining ("current API version is 1.24" );
262265 }
@@ -583,12 +586,23 @@ void createWhenHasContentContainerWithContent() throws Exception {
583586
584587 @ Test
585588 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 {
586598 ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
587599 ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
588600 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" );
592606 given (http ().post (eq (createUri ), eq ("application/json" ), any ()))
593607 .willReturn (responseOf ("create-container-response.json" ));
594608 ContainerReference containerReference = this .api .create (config , platform );
@@ -600,11 +614,13 @@ void createWithPlatformCreatesContainer() throws Exception {
600614 }
601615
602616 @ Test
603- void createWithPlatformAndInsufficientApiVersionThrowsException () {
617+ void createWithPlatformAndKnownInsufficientApiVersionThrowsException () throws Exception {
604618 ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
605619 ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
606620 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 ))
608624 .withMessageContaining ("must be at least 1.41" )
609625 .withMessageContaining ("current API version is 1.24" );
610626 }
@@ -744,22 +760,22 @@ void getApiVersionWithVersionHeaderReturnsVersion() throws Exception {
744760 }
745761
746762 @ Test
747- void getApiVersionWithEmptyVersionHeaderReturnsDefaultVersion () throws Exception {
763+ void getApiVersionWithEmptyVersionHeaderReturnsUnknownVersion () throws Exception {
748764 given (http ().head (eq (new URI (PING_URL ))))
749765 .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 );
751767 }
752768
753769 @ Test
754- void getApiVersionWithNoVersionHeaderReturnsDefaultVersion () throws Exception {
770+ void getApiVersionWithNoVersionHeaderReturnsUnknownVersion () throws Exception {
755771 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 );
757773 }
758774
759775 @ Test
760- void getApiVersionWithExceptionReturnsDefaultVersion () throws Exception {
776+ void getApiVersionWithExceptionReturnsUnknownVersion () throws Exception {
761777 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 );
763779 }
764780
765781 }
0 commit comments