github.com/raychaser/docker@v1.5.0/integration-cli/docker_cli_pull_test.go (about)

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