4545import org .mockito .junit .jupiter .MockitoExtension ;
4646
4747import org .springframework .boot .buildpack .platform .docker .DockerApi .ContainerApi ;
48+ import org .springframework .boot .buildpack .platform .docker .DockerApi .Feature ;
4849import org .springframework .boot .buildpack .platform .docker .DockerApi .ImageApi ;
4950import org .springframework .boot .buildpack .platform .docker .DockerApi .SystemApi ;
5051import org .springframework .boot .buildpack .platform .docker .DockerApi .VolumeApi ;
9091@ ExtendWith (MockitoExtension .class )
9192class DockerApiTests {
9293
93- private static final String API_URL = "/v" + DockerApi .API_VERSION ;
94+ private static final String API_URL = "/v" + DockerApi .PREFERRED_API_VERSION ;
9495
9596 public static final String PING_URL = "/_ping" ;
9697
9798 private static final String IMAGES_URL = API_URL + "/images" ;
9899
99- private static final String PLATFORM_IMAGES_URL = "/v" + DockerApi .PLATFORM_API_VERSION + "/images" ;
100-
101- private static final String PLATFORM_INSPECT_IMAGES_URL = "/v" + DockerApi .PLATFORM_INSPECT_API_VERSION + "/images" ;
102-
103100 private static final String CONTAINERS_URL = API_URL + "/containers" ;
104101
105- private static final String PLATFORM_CONTAINERS_URL = "/v" + DockerApi .PLATFORM_API_VERSION + "/containers" ;
106-
107102 private static final String VOLUMES_URL = API_URL + "/volumes" ;
108103
109104 private static final ImagePlatform LINUX_ARM64_PLATFORM = ImagePlatform .of ("linux/arm64/v1" );
@@ -176,6 +171,52 @@ void createDockerApi() {
176171 assertThat (api ).isNotNull ();
177172 }
178173
174+ @ Test
175+ void buildUrlWhenUnknownVersionUsesPreferredVersion () throws Exception {
176+ setVersion ("0.0" );
177+ assertThat (this .dockerApi .buildUrl (Feature .BASELINE , "/test" ))
178+ .isEqualTo (URI .create ("/v" + DockerApi .PREFERRED_API_VERSION + "/test" ));
179+ }
180+
181+ @ Test
182+ void buildUrlWhenVersionIsGreaterThanPreferredUsesPreferred () throws Exception {
183+ setVersion ("1000.0" );
184+ assertThat (this .dockerApi .buildUrl (Feature .BASELINE , "/test" ))
185+ .isEqualTo (URI .create ("/v" + DockerApi .PREFERRED_API_VERSION + "/test" ));
186+ }
187+
188+ @ Test
189+ void buildUrlWhenVersionIsEqualToPreferredUsesPreferred () throws Exception {
190+ setVersion (DockerApi .PREFERRED_API_VERSION .toString ());
191+ assertThat (this .dockerApi .buildUrl (Feature .BASELINE , "/test" ))
192+ .isEqualTo (URI .create ("/v" + DockerApi .PREFERRED_API_VERSION + "/test" ));
193+ }
194+
195+ @ Test
196+ void buildUrlWhenVersionIsLessThanPreferredAndGreaterThanMinimumUsesVersionVersion () throws Exception {
197+ setVersion ("1.48" );
198+ assertThat (this .dockerApi .buildUrl (Feature .BASELINE , "/test" )).isEqualTo (URI .create ("/v1.48/test" ));
199+ }
200+
201+ @ Test
202+ void buildUrlWhenVersionIsLessThanPreferredAndEqualToMinimumUsesVersionVersion () throws Exception {
203+ setVersion (Feature .BASELINE .minimumVersion ().toString ());
204+ assertThat (this .dockerApi .buildUrl (Feature .BASELINE , "/test" )).isEqualTo (URI .create ("/v1.24/test" ));
205+ }
206+
207+ @ Test
208+ void buildUrlWhenVersionIsLessThanMinimumThrowsException () throws Exception {
209+ setVersion ("1.23" );
210+ assertThatIllegalStateException ().isThrownBy (() -> this .dockerApi .buildUrl (Feature .BASELINE , "/test" ))
211+ .withMessage ("Docker API version must be at least 1.24 "
212+ + "to support this feature, but current API version is 1.23" );
213+ }
214+
215+ private void setVersion (String version ) throws IOException , URISyntaxException {
216+ given (http ().head (eq (new URI (PING_URL ))))
217+ .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , version )));
218+ }
219+
179220 @ Nested
180221 class ImageDockerApiTests {
181222
@@ -244,12 +285,11 @@ void pullWithRegistryAuthPullsImageAndProducesEvents() throws Exception {
244285 @ Test
245286 void pullWithPlatformPullsImageAndProducesEvents () throws Exception {
246287 ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
247- URI createUri = new URI (PLATFORM_IMAGES_URL
248- + " /create?fromImage=gcr.io%2Fpaketo-buildpacks%2Fbuilder%3Abase&platform=linux%2Farm64%2Fv1" );
249- URI imageUri = new URI (PLATFORM_INSPECT_IMAGES_URL + " /gcr.io/paketo-buildpacks/builder:base/json?platform="
288+ URI createUri = new URI (
289+ "/v1.49/images /create?fromImage=gcr.io%2Fpaketo-buildpacks%2Fbuilder%3Abase&platform=linux%2Farm64%2Fv1" );
290+ URI imageUri = new URI ("/v1.49/images /gcr.io/paketo-buildpacks/builder:base/json?platform="
250291 + ENCODED_LINUX_ARM64_PLATFORM_JSON );
251- given (http ().head (eq (new URI (PING_URL ))))
252- .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.49" )));
292+ setVersion ("1.49" );
253293 given (http ().post (eq (createUri ), isNull ())).willReturn (responseOf ("pull-stream.json" ));
254294 given (http ().get (imageUri )).willReturn (responseOf ("type/image.json" ));
255295 Image image = this .api .pull (reference , LINUX_ARM64_PLATFORM , this .pullListener );
@@ -264,8 +304,7 @@ void pullWithPlatformPullsImageAndProducesEvents() throws Exception {
264304 void pullWithPlatformAndInsufficientApiVersionThrowsException () throws Exception {
265305 ImageReference reference = ImageReference .of ("gcr.io/paketo-buildpacks/builder:base" );
266306 ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
267- given (http ().head (eq (new URI (PING_URL )))).willReturn (
268- responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , DockerApi .API_VERSION )));
307+ setVersion ("1.24" );
269308 assertThatIllegalStateException ().isThrownBy (() -> this .api .pull (reference , platform , this .pullListener ))
270309 .withMessageContaining ("must be at least 1.41" )
271310 .withMessageContaining ("current API version is 1.24" );
@@ -403,10 +442,9 @@ void inspectInspectImage() throws Exception {
403442 @ Test
404443 void inspectWithPlatformWhenSupportedVersionInspectImage () throws Exception {
405444 ImageReference reference = ImageReference .of ("docker.io/paketobuildpacks/builder:base" );
406- URI imageUri = new URI (PLATFORM_INSPECT_IMAGES_URL
407- + "/docker.io/paketobuildpacks/builder:base/json?platform=" + ENCODED_LINUX_ARM64_PLATFORM_JSON );
408- given (http ().head (eq (new URI (PING_URL )))).willReturn (responseWithHeaders (
409- new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , DockerApi .PLATFORM_INSPECT_API_VERSION )));
445+ URI imageUri = new URI ("/v1.49/images/docker.io/paketobuildpacks/builder:base/json?platform="
446+ + ENCODED_LINUX_ARM64_PLATFORM_JSON );
447+ setVersion ("1.49" );
410448 given (http ().get (imageUri )).willReturn (responseOf ("type/image-platform.json" ));
411449 Image image = this .api .inspect (reference , LINUX_ARM64_PLATFORM );
412450 assertThat (image .getArchitecture ()).isEqualTo ("arm64" );
@@ -416,9 +454,8 @@ void inspectWithPlatformWhenSupportedVersionInspectImage() throws Exception {
416454 @ Test
417455 void inspectWithPlatformWhenOldVersionInspectImage () throws Exception {
418456 ImageReference reference = ImageReference .of ("docker.io/paketobuildpacks/builder:base" );
419- URI imageUri = new URI (IMAGES_URL + "/docker.io/paketobuildpacks/builder:base/json" );
420- given (http ().head (eq (new URI (PING_URL )))).willReturn (responseWithHeaders (
421- new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , DockerApi .PLATFORM_API_VERSION )));
457+ URI imageUri = new URI ("/v1.48/images/docker.io/paketobuildpacks/builder:base/json" );
458+ setVersion ("1.48" );
422459 given (http ().get (imageUri )).willReturn (responseOf ("type/image.json" ));
423460 Image image = this .api .inspect (reference , LINUX_ARM64_PLATFORM );
424461 assertThat (image .getArchitecture ()).isEqualTo ("amd64" );
@@ -619,23 +656,27 @@ void createWhenHasContentContainerWithContent() throws Exception {
619656
620657 @ Test
621658 void createWithPlatformCreatesContainer () throws Exception {
622- createWithPlatform ("1.41" );
659+ ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
660+ ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
661+ ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
662+ setVersion ("1.41" );
663+ URI createUri = new URI ("/v1.41/containers/create?platform=linux%2Farm64%2Fv1" );
664+ given (http ().post (eq (createUri ), eq ("application/json" ), any ()))
665+ .willReturn (responseOf ("create-container-response.json" ));
666+ ContainerReference containerReference = this .api .create (config , platform );
667+ assertThat (containerReference ).hasToString ("e90e34656806" );
668+ then (http ()).should ().post (any (), any (), this .writer .capture ());
669+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
670+ this .writer .getValue ().accept (out );
671+ assertThat (out .toByteArray ()).hasSize (config .toString ().length ());
623672 }
624673
625674 @ Test
626675 void createWithPlatformAndUnknownApiVersionAttemptsCreate () throws Exception {
627- createWithPlatform (null );
628- }
629-
630- private void createWithPlatform (String apiVersion ) throws IOException , URISyntaxException {
631676 ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
632677 ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
633678 ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
634- if (apiVersion != null ) {
635- given (http ().head (eq (new URI (PING_URL ))))
636- .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , apiVersion )));
637- }
638- URI createUri = new URI (PLATFORM_CONTAINERS_URL + "/create?platform=linux%2Farm64%2Fv1" );
679+ URI createUri = new URI (CONTAINERS_URL + "/create?platform=linux%2Farm64%2Fv1" );
639680 given (http ().post (eq (createUri ), eq ("application/json" ), any ()))
640681 .willReturn (responseOf ("create-container-response.json" ));
641682 ContainerReference containerReference = this .api .create (config , platform );
@@ -651,8 +692,7 @@ void createWithPlatformAndKnownInsufficientApiVersionThrowsException() throws Ex
651692 ImageReference imageReference = ImageReference .of ("ubuntu:bionic" );
652693 ContainerConfig config = ContainerConfig .of (imageReference , (update ) -> update .withCommand ("/bin/bash" ));
653694 ImagePlatform platform = ImagePlatform .of ("linux/arm64/v1" );
654- given (http ().head (eq (new URI (PING_URL ))))
655- .willReturn (responseWithHeaders (new BasicHeader (DockerApi .API_VERSION_HEADER_NAME , "1.24" )));
695+ setVersion ("1.24" );
656696 assertThatIllegalStateException ().isThrownBy (() -> this .api .create (config , platform ))
657697 .withMessageContaining ("must be at least 1.41" )
658698 .withMessageContaining ("current API version is 1.24" );
0 commit comments