2014-02-25 16:17:48 +00:00
package main
import (
2023-07-14 18:02:38 +00:00
"context"
2015-09-03 00:56:01 +00:00
"regexp"
2014-10-07 01:54:52 +00:00
"strings"
2019-09-09 21:06:12 +00:00
"testing"
2015-07-22 16:14:48 +00:00
"time"
2015-04-18 16:46:47 +00:00
2022-03-04 13:49:42 +00:00
"github.com/opencontainers/go-digest"
2020-02-07 13:39:24 +00:00
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
2023-10-12 13:47:48 +00:00
"gotest.tools/v3/skip"
2014-02-25 16:17:48 +00:00
)
2022-06-16 21:32:10 +00:00
type DockerCLIPullSuite struct {
ds * DockerSuite
}
2023-07-14 18:02:38 +00:00
func ( s * DockerCLIPullSuite ) TearDownTest ( ctx context . Context , c * testing . T ) {
s . ds . TearDownTest ( ctx , c )
2022-06-16 21:32:10 +00:00
}
func ( s * DockerCLIPullSuite ) OnTimeout ( c * testing . T ) {
s . ds . OnTimeout ( c )
}
2015-09-03 00:56:01 +00:00
// TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
// prints all expected output.
2019-09-09 21:05:55 +00:00
func ( s * DockerHubPullSuite ) TestPullFromCentralRegistry ( c * testing . T ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-03 00:56:01 +00:00
out := s . Cmd ( c , "pull" , "hello-world" )
defer deleteImages ( "hello-world" )
2019-04-04 13:23:19 +00:00
assert . Assert ( c , strings . Contains ( out , "Using default tag: latest" ) , "expected the 'latest' tag to be automatically assumed" )
assert . Assert ( c , strings . Contains ( out , "Pulling from library/hello-world" ) , "expected the 'library/' prefix to be automatically assumed" )
assert . Assert ( c , strings . Contains ( out , "Downloaded newer image for hello-world:latest" ) )
2015-09-03 00:56:01 +00:00
matches := regexp . MustCompile ( ` Digest: (.+)\n ` ) . FindAllStringSubmatch ( out , - 1 )
2019-04-04 13:23:19 +00:00
assert . Equal ( c , len ( matches ) , 1 , "expected exactly one image digest in the output" )
assert . Equal ( c , len ( matches [ 0 ] ) , 2 , "unexpected number of submatches for the digest" )
2017-01-07 01:23:18 +00:00
_ , err := digest . Parse ( matches [ 0 ] [ 1 ] )
2019-04-04 13:23:19 +00:00
assert . NilError ( c , err , "invalid digest %q in output" , matches [ 0 ] [ 1 ] )
2015-09-03 00:56:01 +00:00
// We should have a single entry in images.
img := strings . TrimSpace ( s . Cmd ( c , "images" ) )
2015-12-16 16:20:56 +00:00
splitImg := strings . Split ( img , "\n" )
2019-04-04 13:23:19 +00:00
assert . Equal ( c , len ( splitImg ) , 2 )
match , _ := regexp . MatchString ( ` hello-world\s+latest.*? ` , splitImg [ 1 ] )
assert . Assert ( c , match , "invalid output for `docker images` (expected image and tag name)" )
2015-01-13 23:19:44 +00:00
}
2014-09-23 22:58:05 +00:00
2015-09-03 00:56:01 +00:00
// TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
2016-12-21 03:03:39 +00:00
// that pulling the same image with different combinations of implicit elements of the image
2015-09-03 00:56:01 +00:00
// reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
// multiple images.
2019-09-09 21:05:55 +00:00
func ( s * DockerHubPullSuite ) TestPullFromCentralRegistryImplicitRefParts ( c * testing . T ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-03 00:56:01 +00:00
2016-03-02 01:02:06 +00:00
// Pull hello-world from v2
pullFromV2 := func ( ref string ) ( int , string ) {
out := s . Cmd ( c , "pull" , "hello-world" )
2016-02-17 21:05:47 +00:00
v1Retries := 0
for strings . Contains ( out , "this image was pulled from a legacy registry" ) {
// Some network errors may cause fallbacks to the v1
// protocol, which would violate the test's assumption
// that it will get the same images. To make the test
// more robust against these network glitches, allow a
// few retries if we end up with a v1 pull.
if v1Retries > 2 {
2016-03-02 01:02:06 +00:00
c . Fatalf ( "too many v1 fallback incidents when pulling %s" , ref )
2016-02-17 21:05:47 +00:00
}
2016-03-02 01:02:06 +00:00
s . Cmd ( c , "rmi" , ref )
out = s . Cmd ( c , "pull" , ref )
2016-02-17 21:05:47 +00:00
v1Retries ++
}
2016-03-02 01:02:06 +00:00
return v1Retries , out
}
pullFromV2 ( "hello-world" )
defer deleteImages ( "hello-world" )
s . Cmd ( c , "tag" , "hello-world" , "hello-world-backup" )
for _ , ref := range [ ] string {
"hello-world" ,
"hello-world:latest" ,
"library/hello-world" ,
"library/hello-world:latest" ,
"docker.io/library/hello-world" ,
"index.docker.io/library/hello-world" ,
} {
var out string
for {
var v1Retries int
v1Retries , out = pullFromV2 ( ref )
// Keep repeating the test case until we don't hit a v1
// fallback case. We won't get the right "Image is up
// to date" message if the local image was replaced
// with one pulled from v1.
if v1Retries == 0 {
break
}
s . Cmd ( c , "rmi" , ref )
s . Cmd ( c , "tag" , "hello-world-backup" , "hello-world" )
}
2019-04-04 13:23:19 +00:00
assert . Assert ( c , strings . Contains ( out , "Image is up to date for hello-world:latest" ) )
2015-07-23 12:12:36 +00:00
}
2016-03-02 01:02:06 +00:00
s . Cmd ( c , "rmi" , "hello-world-backup" )
2015-09-03 00:56:01 +00:00
// We should have a single entry in images.
img := strings . TrimSpace ( s . Cmd ( c , "images" ) )
2015-12-16 16:20:56 +00:00
splitImg := strings . Split ( img , "\n" )
2019-04-04 13:23:19 +00:00
assert . Equal ( c , len ( splitImg ) , 2 )
match , _ := regexp . MatchString ( ` hello-world\s+latest.*? ` , splitImg [ 1 ] )
assert . Assert ( c , match , "invalid output for `docker images` (expected image and tag name)" )
2015-07-23 12:12:36 +00:00
}
2015-09-03 00:56:01 +00:00
// TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
2019-09-09 21:05:55 +00:00
func ( s * DockerHubPullSuite ) TestPullScratchNotAllowed ( c * testing . T ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-09-03 00:56:01 +00:00
out , err := s . CmdWithError ( "pull" , "scratch" )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , "expected pull of scratch to fail" )
assert . Assert ( c , strings . Contains ( out , "'scratch' is a reserved name" ) )
assert . Assert ( c , ! strings . Contains ( out , "Pulling repository scratch" ) )
2015-07-20 05:56:10 +00:00
}
2015-07-22 16:14:48 +00:00
2015-09-03 00:56:01 +00:00
// TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
// results in more images than a naked pull.
2019-09-09 21:05:55 +00:00
func ( s * DockerHubPullSuite ) TestPullAllTagsFromCentralRegistry ( c * testing . T ) {
2023-10-12 13:47:48 +00:00
// See https://github.com/moby/moby/issues/46632
skip . If ( c , testEnv . UsingSnapshotter , "The image dockercore/engine-pull-all-test-fixture is a hand-made image that contains an error in the manifest, the size is reported as 424 but its real size is 524, containerd fails to pull it because it checks that the sizes reported are right" )
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2017-09-13 15:30:44 +00:00
s . Cmd ( c , "pull" , "dockercore/engine-pull-all-test-fixture" )
outImageCmd := s . Cmd ( c , "images" , "dockercore/engine-pull-all-test-fixture" )
2015-09-03 00:56:01 +00:00
splitOutImageCmd := strings . Split ( strings . TrimSpace ( outImageCmd ) , "\n" )
2019-04-04 13:23:19 +00:00
assert . Equal ( c , len ( splitOutImageCmd ) , 2 )
2015-09-03 00:56:01 +00:00
2017-09-13 15:30:44 +00:00
s . Cmd ( c , "pull" , "--all-tags=true" , "dockercore/engine-pull-all-test-fixture" )
outImageAllTagCmd := s . Cmd ( c , "images" , "dockercore/engine-pull-all-test-fixture" )
2015-12-16 16:20:56 +00:00
linesCount := strings . Count ( outImageAllTagCmd , "\n" )
2019-04-04 13:23:19 +00:00
assert . Assert ( c , linesCount > 2 , "pulling all tags should provide more than two images, got %s" , outImageAllTagCmd )
2015-09-03 00:56:01 +00:00
2017-09-13 15:30:44 +00:00
// Verify that the line for 'dockercore/engine-pull-all-test-fixture:latest' is left unchanged.
2015-09-03 00:56:01 +00:00
var latestLine string
for _ , line := range strings . Split ( outImageAllTagCmd , "\n" ) {
2017-09-13 15:30:44 +00:00
if strings . HasPrefix ( line , "dockercore/engine-pull-all-test-fixture" ) && strings . Contains ( line , "latest" ) {
2015-09-03 00:56:01 +00:00
latestLine = line
break
2015-07-22 16:14:48 +00:00
}
2015-07-22 23:10:25 +00:00
}
2019-04-04 13:23:19 +00:00
assert . Assert ( c , latestLine != "" , "no entry for dockercore/engine-pull-all-test-fixture:latest found after pulling all tags" )
2017-09-13 02:28:21 +00:00
2015-09-03 00:56:01 +00:00
splitLatest := strings . Fields ( latestLine )
splitCurrent := strings . Fields ( splitOutImageCmd [ 1 ] )
Fix flaky test TestPullAllTagsFromCentralRegistry
This test was directly comparing lines of output from "docker images".
Sometimes, when busybox had been pushed to the hub recently, the
relative creation times would differ like this:
... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
Fixing by removing the time-since-creation fields from the comparison.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2015-12-08 19:23:04 +00:00
// Clear relative creation times, since these can easily change between
// two invocations of "docker images". Without this, the test can fail
// like this:
// ... obtained []string = []string{"busybox", "latest", "d9551b4026f0", "27", "minutes", "ago", "1.113", "MB"}
// ... expected []string = []string{"busybox", "latest", "d9551b4026f0", "26", "minutes", "ago", "1.113", "MB"}
splitLatest [ 3 ] = ""
splitLatest [ 4 ] = ""
splitLatest [ 5 ] = ""
splitCurrent [ 3 ] = ""
splitCurrent [ 4 ] = ""
splitCurrent [ 5 ] = ""
2019-04-04 13:23:19 +00:00
assert . Assert ( c , is . DeepEqual ( splitLatest , splitCurrent ) , "dockercore/engine-pull-all-test-fixture:latest was changed after pulling all tags" )
2015-07-22 23:10:25 +00:00
}
2015-07-23 01:46:59 +00:00
2015-09-03 00:56:01 +00:00
// TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
2015-11-14 00:59:01 +00:00
// gets cancelled.
2015-09-03 00:56:01 +00:00
//
// Ref: docker/docker#15589
2019-09-09 21:05:55 +00:00
func ( s * DockerHubPullSuite ) TestPullClientDisconnect ( c * testing . T ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2023-07-27 18:01:09 +00:00
const imgRepo = "hello-world:latest"
2015-08-17 21:11:40 +00:00
2023-07-27 18:01:09 +00:00
pullCmd := s . MakeCmd ( "pull" , imgRepo )
2015-08-17 21:11:40 +00:00
stdout , err := pullCmd . StdoutPipe ( )
2019-04-04 13:23:19 +00:00
assert . NilError ( c , err )
2015-08-17 21:11:40 +00:00
err = pullCmd . Start ( )
2019-04-04 13:23:19 +00:00
assert . NilError ( c , err )
2017-09-07 17:11:25 +00:00
go pullCmd . Wait ( )
2015-08-17 21:11:40 +00:00
2015-09-03 00:56:01 +00:00
// Cancel as soon as we get some output.
2015-08-17 21:11:40 +00:00
buf := make ( [ ] byte , 10 )
_ , err = stdout . Read ( buf )
2019-04-04 13:23:19 +00:00
assert . NilError ( c , err )
2015-08-17 21:11:40 +00:00
err = pullCmd . Process . Kill ( )
2019-04-04 13:23:19 +00:00
assert . NilError ( c , err )
2015-08-17 21:11:40 +00:00
2015-11-14 00:59:01 +00:00
time . Sleep ( 2 * time . Second )
2023-07-27 18:01:09 +00:00
_ , err = s . CmdWithError ( "inspect" , imgRepo )
2019-04-04 13:23:19 +00:00
assert . ErrorContains ( c , err , "" , "image was pulled after client disconnected" )
2015-08-17 21:11:40 +00:00
}
2016-03-12 19:34:07 +00:00
2016-09-08 23:28:23 +00:00
// Regression test for https://github.com/docker/docker/issues/26429
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIPullSuite ) TestPullLinuxImageFailsOnWindows ( c * testing . T ) {
2016-09-08 23:28:23 +00:00
testRequires ( c , DaemonIsWindows , Network )
_ , _ , err := dockerCmdWithError ( "pull" , "ubuntu" )
2024-01-18 15:12:06 +00:00
errorMessage := "no matching manifest for windows"
if testEnv . UsingSnapshotter ( ) {
errorMessage = "no match for platform in manifest"
}
assert . ErrorContains ( c , err , errorMessage )
2016-09-08 23:28:23 +00:00
}
2017-01-31 01:04:14 +00:00
// Regression test for https://github.com/docker/docker/issues/28892
2022-06-16 21:32:10 +00:00
func ( s * DockerCLIPullSuite ) TestPullWindowsImageFailsOnLinux ( c * testing . T ) {
2017-01-31 01:04:14 +00:00
testRequires ( c , DaemonIsLinux , Network )
2022-08-26 02:09:48 +00:00
_ , _ , err := dockerCmdWithError ( "pull" , "mcr.microsoft.com/windows/servercore:ltsc2022" )
2023-09-20 12:47:51 +00:00
errorMessage := "no matching manifest for linux"
if testEnv . UsingSnapshotter ( ) {
errorMessage = "no match for platform in manifest"
}
assert . ErrorContains ( c , err , errorMessage )
2017-01-31 01:04:14 +00:00
}