github.com/SophiaGitHub/hello@v1.7.1-rc3/integration-cli/docker_cli_pull_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  
     8  	"github.com/go-check/check"
     9  )
    10  
    11  // See issue docker/docker#8141
    12  func (s *DockerRegistrySuite) TestPullImageWithAliases(c *check.C) {
    13  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    14  
    15  	repos := []string{}
    16  	for _, tag := range []string{"recent", "fresh"} {
    17  		repos = append(repos, fmt.Sprintf("%v:%v", repoName, tag))
    18  	}
    19  
    20  	// Tag and push the same image multiple times.
    21  	for _, repo := range repos {
    22  		if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repo)); err != nil {
    23  			c.Fatalf("Failed to tag image %v: error %v, output %q", repos, err, out)
    24  		}
    25  		if out, err := exec.Command(dockerBinary, "push", repo).CombinedOutput(); err != nil {
    26  			c.Fatalf("Failed to push image %v: error %v, output %q", repo, err, string(out))
    27  		}
    28  	}
    29  
    30  	// Clear local images store.
    31  	args := append([]string{"rmi"}, repos...)
    32  	if out, err := exec.Command(dockerBinary, args...).CombinedOutput(); err != nil {
    33  		c.Fatalf("Failed to clean images: error %v, output %q", err, string(out))
    34  	}
    35  
    36  	// Pull a single tag and verify it doesn't bring down all aliases.
    37  	pullCmd := exec.Command(dockerBinary, "pull", repos[0])
    38  	if out, _, err := runCommandWithOutput(pullCmd); err != nil {
    39  		c.Fatalf("Failed to pull %v: error %v, output %q", repoName, err, out)
    40  	}
    41  	if err := exec.Command(dockerBinary, "inspect", repos[0]).Run(); err != nil {
    42  		c.Fatalf("Image %v was not pulled down", repos[0])
    43  	}
    44  	for _, repo := range repos[1:] {
    45  		if err := exec.Command(dockerBinary, "inspect", repo).Run(); err == nil {
    46  			c.Fatalf("Image %v shouldn't have been pulled down", repo)
    47  		}
    48  	}
    49  }
    50  
    51  // pulling library/hello-world should show verified message
    52  func (s *DockerSuite) TestPullVerified(c *check.C) {
    53  	c.Skip("Skipping hub dependent test")
    54  
    55  	// Image must be pulled from central repository to get verified message
    56  	// unless keychain is manually updated to contain the daemon's sign key.
    57  
    58  	verifiedName := "hello-world"
    59  
    60  	// pull it
    61  	expected := "The image you are pulling has been verified"
    62  	pullCmd := exec.Command(dockerBinary, "pull", verifiedName)
    63  	if out, exitCode, err := runCommandWithOutput(pullCmd); err != nil || !strings.Contains(out, expected) {
    64  		if err != nil || exitCode != 0 {
    65  			c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
    66  		}
    67  		c.Fatalf("pulling a verified image failed. expected: %s\ngot: %s, %v", expected, out, err)
    68  	}
    69  
    70  	// pull it again
    71  	pullCmd = exec.Command(dockerBinary, "pull", verifiedName)
    72  	if out, exitCode, err := runCommandWithOutput(pullCmd); err != nil || strings.Contains(out, expected) {
    73  		if err != nil || exitCode != 0 {
    74  			c.Skip(fmt.Sprintf("pulling the '%s' image from the registry has failed: %v", verifiedName, err))
    75  		}
    76  		c.Fatalf("pulling a verified image failed. unexpected verify message\ngot: %s, %v", out, err)
    77  	}
    78  
    79  }
    80  
    81  // pulling an image from the central registry should work
    82  func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
    83  	testRequires(c, Network)
    84  
    85  	pullCmd := exec.Command(dockerBinary, "pull", "hello-world")
    86  	if out, _, err := runCommandWithOutput(pullCmd); err != nil {
    87  		c.Fatalf("pulling the hello-world image from the registry has failed: %s, %v", out, err)
    88  	}
    89  }
    90  
    91  // pulling a non-existing image from the central registry should return a non-zero exit code
    92  func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
    93  	testRequires(c, Network)
    94  
    95  	name := "sadfsadfasdf"
    96  	pullCmd := exec.Command(dockerBinary, "pull", name)
    97  	out, _, err := runCommandWithOutput(pullCmd)
    98  
    99  	if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
   100  		c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
   101  	}
   102  }
   103  
   104  // pulling an image from the central registry using official names should work
   105  // ensure all pulls result in the same image
   106  func (s *DockerSuite) TestPullImageOfficialNames(c *check.C) {
   107  	testRequires(c, Network)
   108  
   109  	names := []string{
   110  		"docker.io/hello-world",
   111  		"index.docker.io/hello-world",
   112  		"library/hello-world",
   113  		"docker.io/library/hello-world",
   114  		"index.docker.io/library/hello-world",
   115  	}
   116  	for _, name := range names {
   117  		pullCmd := exec.Command(dockerBinary, "pull", name)
   118  		out, exitCode, err := runCommandWithOutput(pullCmd)
   119  		if err != nil || exitCode != 0 {
   120  			c.Errorf("pulling the '%s' image from the registry has failed: %s", name, err)
   121  			continue
   122  		}
   123  
   124  		// ensure we don't have multiple image names.
   125  		imagesCmd := exec.Command(dockerBinary, "images")
   126  		out, _, err = runCommandWithOutput(imagesCmd)
   127  		if err != nil {
   128  			c.Errorf("listing images failed with errors: %v", err)
   129  		} else if strings.Contains(out, name) {
   130  			c.Errorf("images should not have listed '%s'", name)
   131  		}
   132  	}
   133  }
   134  
   135  func (s *DockerSuite) TestPullScratchNotAllowed(c *check.C) {
   136  	testRequires(c, Network)
   137  
   138  	pullCmd := exec.Command(dockerBinary, "pull", "scratch")
   139  	out, exitCode, err := runCommandWithOutput(pullCmd)
   140  	if err == nil {
   141  		c.Fatal("expected pull of scratch to fail, but it didn't")
   142  	}
   143  	if exitCode != 1 {
   144  		c.Fatalf("pulling scratch expected exit code 1, got %d", exitCode)
   145  	}
   146  	if strings.Contains(out, "Pulling repository scratch") {
   147  		c.Fatalf("pulling scratch should not have begun: %s", out)
   148  	}
   149  	if !strings.Contains(out, "'scratch' is a reserved name") {
   150  		c.Fatalf("unexpected output pulling scratch: %s", out)
   151  	}
   152  }