2014-02-25 11:17:48 -05:00
package main
import (
2015-12-21 18:42:04 -05:00
"fmt"
2015-09-02 20:56:01 -04:00
"regexp"
2014-10-06 21:54:52 -04:00
"strings"
2016-03-03 09:18:10 -05:00
"sync"
2015-07-22 12:14:48 -04:00
"time"
2015-04-18 12:46:47 -04:00
2016-12-30 12:23:00 -05:00
"github.com/docker/docker/integration-cli/checker"
2015-07-22 21:46:59 -04:00
"github.com/go-check/check"
2017-01-06 20:23:18 -05:00
"github.com/opencontainers/go-digest"
2014-02-25 11:17:48 -05:00
)
2015-09-02 20:56:01 -04:00
// TestPullFromCentralRegistry pulls an image from the central registry and verifies that the client
// prints all expected output.
func ( s * DockerHubPullSuite ) TestPullFromCentralRegistry ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-09-02 20:56:01 -04:00
out := s . Cmd ( c , "pull" , "hello-world" )
defer deleteImages ( "hello-world" )
c . Assert ( out , checker . Contains , "Using default tag: latest" , check . Commentf ( "expected the 'latest' tag to be automatically assumed" ) )
c . Assert ( out , checker . Contains , "Pulling from library/hello-world" , check . Commentf ( "expected the 'library/' prefix to be automatically assumed" ) )
c . Assert ( out , checker . Contains , "Downloaded newer image for hello-world:latest" )
matches := regexp . MustCompile ( ` Digest: (.+)\n ` ) . FindAllStringSubmatch ( out , - 1 )
c . Assert ( len ( matches ) , checker . Equals , 1 , check . Commentf ( "expected exactly one image digest in the output" ) )
c . Assert ( len ( matches [ 0 ] ) , checker . Equals , 2 , check . Commentf ( "unexpected number of submatches for the digest" ) )
2017-01-06 20:23:18 -05:00
_ , err := digest . Parse ( matches [ 0 ] [ 1 ] )
2015-09-02 20:56:01 -04:00
c . Check ( err , checker . IsNil , check . Commentf ( "invalid digest %q in output" , matches [ 0 ] [ 1 ] ) )
// We should have a single entry in images.
img := strings . TrimSpace ( s . Cmd ( c , "images" ) )
2015-12-16 11:20:56 -05:00
splitImg := strings . Split ( img , "\n" )
c . Assert ( splitImg , checker . HasLen , 2 )
c . Assert ( splitImg [ 1 ] , checker . Matches , ` hello-world\s+latest.*? ` , check . Commentf ( "invalid output for `docker images` (expected image and tag name" ) )
2015-01-13 18:19:44 -05:00
}
2014-09-23 18:58:05 -04:00
2015-09-02 20:56:01 -04:00
// TestPullNonExistingImage pulls non-existing images from the central registry, with different
// combinations of implicit tag and library prefix.
func ( s * DockerHubPullSuite ) TestPullNonExistingImage ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2016-03-03 09:18:10 -05:00
type entry struct {
2016-03-28 20:16:56 -04:00
repo string
alias string
tag string
2016-03-03 09:18:10 -05:00
}
entries := [ ] entry {
2016-11-10 18:14:33 -05:00
{ "asdfasdf" , "asdfasdf" , "foobar" } ,
{ "asdfasdf" , "library/asdfasdf" , "foobar" } ,
{ "asdfasdf" , "asdfasdf" , "" } ,
{ "asdfasdf" , "asdfasdf" , "latest" } ,
{ "asdfasdf" , "library/asdfasdf" , "" } ,
{ "asdfasdf" , "library/asdfasdf" , "latest" } ,
2016-03-03 09:18:10 -05:00
}
// The option field indicates "-a" or not.
type record struct {
e entry
option string
out string
err error
}
2015-12-23 18:21:43 -05:00
2016-03-03 09:18:10 -05:00
// Execute 'docker pull' in parallel, pass results (out, err) and
// necessary information ("-a" or not, and the image name) to channel.
var group sync . WaitGroup
recordChan := make ( chan record , len ( entries ) * 2 )
for _ , e := range entries {
group . Add ( 1 )
go func ( e entry ) {
defer group . Done ( )
2016-03-28 20:16:56 -04:00
repoName := e . alias
if e . tag != "" {
repoName += ":" + e . tag
}
out , err := s . CmdWithError ( "pull" , repoName )
2016-03-03 09:18:10 -05:00
recordChan <- record { e , "" , out , err }
} ( e )
2016-03-28 20:16:56 -04:00
if e . tag == "" {
2016-03-03 09:18:10 -05:00
// pull -a on a nonexistent registry should fall back as well
group . Add ( 1 )
go func ( e entry ) {
defer group . Done ( )
2016-03-28 20:16:56 -04:00
out , err := s . CmdWithError ( "pull" , "-a" , e . alias )
2016-03-03 09:18:10 -05:00
recordChan <- record { e , "-a" , out , err }
} ( e )
}
}
// Wait for completion
group . Wait ( )
close ( recordChan )
// Process the results (out, err).
for record := range recordChan {
if len ( record . option ) == 0 {
c . Assert ( record . err , checker . NotNil , check . Commentf ( "expected non-zero exit status when pulling non-existing image: %s" , record . out ) )
2017-05-03 17:58:45 -04:00
c . Assert ( record . out , checker . Contains , fmt . Sprintf ( "pull access denied for %s, repository does not exist or may require 'docker login'" , record . e . repo ) , check . Commentf ( "expected image not found error messages" ) )
2016-03-03 09:18:10 -05:00
} else {
// pull -a on a nonexistent registry should fall back as well
c . Assert ( record . err , checker . NotNil , check . Commentf ( "expected non-zero exit status when pulling non-existing image: %s" , record . out ) )
2017-05-03 17:58:45 -04:00
c . Assert ( record . out , checker . Contains , fmt . Sprintf ( "pull access denied for %s, repository does not exist or may require 'docker login'" , record . e . repo ) , check . Commentf ( "expected image not found error messages" ) )
2016-03-03 09:18:10 -05:00
c . Assert ( record . out , checker . Not ( checker . Contains ) , "unauthorized" , check . Commentf ( ` message should not contain "unauthorized" ` ) )
2015-12-23 18:21:43 -05:00
}
2014-02-25 11:17:48 -05:00
}
2015-12-23 18:21:43 -05:00
2014-02-25 11:17:48 -05:00
}
2014-10-06 21:54:52 -04:00
2015-09-02 20:56:01 -04:00
// TestPullFromCentralRegistryImplicitRefParts pulls an image from the central registry and verifies
2016-12-20 22:03:39 -05:00
// that pulling the same image with different combinations of implicit elements of the image
2015-09-02 20:56:01 -04:00
// reference (tag, repository, central registry url, ...) doesn't trigger a new pull nor leads to
// multiple images.
func ( s * DockerHubPullSuite ) TestPullFromCentralRegistryImplicitRefParts ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-09-02 20:56:01 -04:00
2016-03-01 20:02:06 -05:00
// Pull hello-world from v2
pullFromV2 := func ( ref string ) ( int , string ) {
out := s . Cmd ( c , "pull" , "hello-world" )
2016-02-17 16:05:47 -05: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-01 20:02:06 -05:00
c . Fatalf ( "too many v1 fallback incidents when pulling %s" , ref )
2016-02-17 16:05:47 -05:00
}
2016-03-01 20:02:06 -05:00
s . Cmd ( c , "rmi" , ref )
out = s . Cmd ( c , "pull" , ref )
2016-02-17 16:05:47 -05:00
v1Retries ++
}
2016-03-01 20:02:06 -05: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" )
}
2015-09-02 20:56:01 -04:00
c . Assert ( out , checker . Contains , "Image is up to date for hello-world:latest" )
2015-07-23 08:12:36 -04:00
}
2016-03-01 20:02:06 -05:00
s . Cmd ( c , "rmi" , "hello-world-backup" )
2015-09-02 20:56:01 -04:00
// We should have a single entry in images.
img := strings . TrimSpace ( s . Cmd ( c , "images" ) )
2015-12-16 11:20:56 -05:00
splitImg := strings . Split ( img , "\n" )
c . Assert ( splitImg , checker . HasLen , 2 )
c . Assert ( splitImg [ 1 ] , checker . Matches , ` hello-world\s+latest.*? ` , check . Commentf ( "invalid output for `docker images` (expected image and tag name" ) )
2015-07-23 08:12:36 -04:00
}
2015-09-02 20:56:01 -04:00
// TestPullScratchNotAllowed verifies that pulling 'scratch' is rejected.
func ( s * DockerHubPullSuite ) TestPullScratchNotAllowed ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-09-02 20:56:01 -04:00
out , err := s . CmdWithError ( "pull" , "scratch" )
c . Assert ( err , checker . NotNil , check . Commentf ( "expected pull of scratch to fail" ) )
c . Assert ( out , checker . Contains , "'scratch' is a reserved name" )
c . Assert ( out , checker . Not ( checker . Contains ) , "Pulling repository scratch" )
2015-07-20 01:56:10 -04:00
}
2015-07-22 12:14:48 -04:00
2015-09-02 20:56:01 -04:00
// TestPullAllTagsFromCentralRegistry pulls using `all-tags` for a given image and verifies that it
// results in more images than a naked pull.
func ( s * DockerHubPullSuite ) TestPullAllTagsFromCentralRegistry ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-09-02 20:56:01 -04:00
s . Cmd ( c , "pull" , "busybox" )
outImageCmd := s . Cmd ( c , "images" , "busybox" )
splitOutImageCmd := strings . Split ( strings . TrimSpace ( outImageCmd ) , "\n" )
2015-12-16 11:20:56 -05:00
c . Assert ( splitOutImageCmd , checker . HasLen , 2 )
2015-09-02 20:56:01 -04:00
s . Cmd ( c , "pull" , "--all-tags=true" , "busybox" )
outImageAllTagCmd := s . Cmd ( c , "images" , "busybox" )
2015-12-16 11:20:56 -05:00
linesCount := strings . Count ( outImageAllTagCmd , "\n" )
c . Assert ( linesCount , checker . GreaterThan , 2 , check . Commentf ( "pulling all tags should provide more than two images, got %s" , outImageAllTagCmd ) )
2015-09-02 20:56:01 -04:00
// Verify that the line for 'busybox:latest' is left unchanged.
var latestLine string
for _ , line := range strings . Split ( outImageAllTagCmd , "\n" ) {
if strings . HasPrefix ( line , "busybox" ) && strings . Contains ( line , "latest" ) {
latestLine = line
break
2015-07-22 12:14:48 -04:00
}
2015-07-22 19:10:25 -04:00
}
2015-09-02 20:56:01 -04:00
c . Assert ( latestLine , checker . Not ( checker . Equals ) , "" , check . Commentf ( "no entry for busybox:latest found after pulling all tags" ) )
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 14:23:04 -05: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 ] = ""
2015-09-02 20:56:01 -04:00
c . Assert ( splitLatest , checker . DeepEquals , splitCurrent , check . Commentf ( "busybox:latest was changed after pulling all tags" ) )
2015-07-22 19:10:25 -04:00
}
2015-07-22 21:46:59 -04:00
2015-09-02 20:56:01 -04:00
// TestPullClientDisconnect kills the client during a pull operation and verifies that the operation
2015-11-13 19:59:01 -05:00
// gets cancelled.
2015-09-02 20:56:01 -04:00
//
// Ref: docker/docker#15589
func ( s * DockerHubPullSuite ) TestPullClientDisconnect ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-08-17 17:11:40 -04:00
repoName := "hello-world:latest"
2015-09-02 20:56:01 -04:00
pullCmd := s . MakeCmd ( "pull" , repoName )
2015-08-17 17:11:40 -04:00
stdout , err := pullCmd . StdoutPipe ( )
2015-09-02 20:56:01 -04:00
c . Assert ( err , checker . IsNil )
2015-08-17 17:11:40 -04:00
err = pullCmd . Start ( )
2015-09-02 20:56:01 -04:00
c . Assert ( err , checker . IsNil )
2015-08-17 17:11:40 -04:00
2015-09-02 20:56:01 -04:00
// Cancel as soon as we get some output.
2015-08-17 17:11:40 -04:00
buf := make ( [ ] byte , 10 )
_ , err = stdout . Read ( buf )
2015-09-02 20:56:01 -04:00
c . Assert ( err , checker . IsNil )
2015-08-17 17:11:40 -04:00
err = pullCmd . Process . Kill ( )
2015-09-02 20:56:01 -04:00
c . Assert ( err , checker . IsNil )
2015-08-17 17:11:40 -04:00
2015-11-13 19:59:01 -05:00
time . Sleep ( 2 * time . Second )
2015-12-16 11:20:56 -05:00
_ , err = s . CmdWithError ( "inspect" , repoName )
c . Assert ( err , checker . NotNil , check . Commentf ( "image was pulled after client disconnected" ) )
2015-08-17 17:11:40 -04:00
}
2016-03-12 14:34:07 -05:00
2016-03-14 16:11:35 -04:00
func ( s * DockerRegistryAuthHtpasswdSuite ) TestPullNoCredentialsNotFound ( c * check . C ) {
2017-06-11 08:39:28 -04:00
// @TODO TestPullNoCredentialsNotFound expects docker to fall back to a v1 registry, so has to be updated for v17.12, when v1 registries are no longer supported
s . d . StartWithBusybox ( c , "--disable-legacy-registry=false" )
2016-03-12 14:34:07 -05:00
// we don't care about the actual image, we just want to see image not found
// because that means v2 call returned 401 and we fell back to v1 which usually
// gives a 404 (in this case the test registry doesn't handle v1 at all)
2017-06-11 08:39:28 -04:00
out , err := s . d . Cmd ( "pull" , privateRegistryURL + "/busybox" )
2016-03-12 14:34:07 -05:00
c . Assert ( err , check . NotNil , check . Commentf ( out ) )
2016-03-28 20:16:56 -04:00
c . Assert ( out , checker . Contains , "Error: image busybox:latest not found" )
2016-03-12 14:34:07 -05:00
}
2016-09-08 19:28:23 -04:00
// Regression test for https://github.com/docker/docker/issues/26429
func ( s * DockerSuite ) TestPullLinuxImageFailsOnWindows ( c * check . C ) {
testRequires ( c , DaemonIsWindows , Network )
_ , _ , err := dockerCmdWithError ( "pull" , "ubuntu" )
c . Assert ( err . Error ( ) , checker . Contains , "cannot be used on this platform" )
}
2017-01-30 20:04:14 -05:00
// Regression test for https://github.com/docker/docker/issues/28892
func ( s * DockerSuite ) TestPullWindowsImageFailsOnLinux ( c * check . C ) {
testRequires ( c , DaemonIsLinux , Network )
_ , _ , err := dockerCmdWithError ( "pull" , "microsoft/nanoserver" )
c . Assert ( err . Error ( ) , checker . Contains , "cannot be used on this platform" )
}