github.com/lmars/docker@v1.6.0-rc2/integration-cli/docker_cli_tag_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  // tagging a named image in a new unprefixed repo should work
    11  func TestTagUnprefixedRepoByName(t *testing.T) {
    12  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    13  		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    14  	}
    15  
    16  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "testfoobarbaz")
    17  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
    18  		t.Fatal(out, err)
    19  	}
    20  
    21  	deleteImages("testfoobarbaz")
    22  
    23  	logDone("tag - busybox -> testfoobarbaz")
    24  }
    25  
    26  // tagging an image by ID in a new unprefixed repo should work
    27  func TestTagUnprefixedRepoByID(t *testing.T) {
    28  	getIDCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox")
    29  	out, _, err := runCommandWithOutput(getIDCmd)
    30  	if err != nil {
    31  		t.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
    32  	}
    33  
    34  	cleanedImageID := stripTrailingCharacters(out)
    35  	tagCmd := exec.Command(dockerBinary, "tag", cleanedImageID, "testfoobarbaz")
    36  	if out, _, err = runCommandWithOutput(tagCmd); err != nil {
    37  		t.Fatal(out, err)
    38  	}
    39  
    40  	deleteImages("testfoobarbaz")
    41  
    42  	logDone("tag - busybox's image ID -> testfoobarbaz")
    43  }
    44  
    45  // ensure we don't allow the use of invalid repository names; these tag operations should fail
    46  func TestTagInvalidUnprefixedRepo(t *testing.T) {
    47  
    48  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"}
    49  
    50  	for _, repo := range invalidRepos {
    51  		tagCmd := exec.Command(dockerBinary, "tag", "busybox", repo)
    52  		_, _, err := runCommandWithOutput(tagCmd)
    53  		if err == nil {
    54  			t.Fatalf("tag busybox %v should have failed", repo)
    55  		}
    56  	}
    57  	logDone("tag - busybox invalid repo names --> must not work")
    58  }
    59  
    60  // ensure we don't allow the use of invalid tags; these tag operations should fail
    61  func TestTagInvalidPrefixedRepo(t *testing.T) {
    62  	long_tag := makeRandomString(121)
    63  
    64  	invalidTags := []string{"repo:fo$z$", "repo:Foo@3cc", "repo:Foo$3", "repo:Foo*3", "repo:Fo^3", "repo:Foo!3", "repo:%goodbye", "repo:#hashtagit", "repo:F)xcz(", "repo:-foo", "repo:..", long_tag}
    65  
    66  	for _, repotag := range invalidTags {
    67  		tagCmd := exec.Command(dockerBinary, "tag", "busybox", repotag)
    68  		_, _, err := runCommandWithOutput(tagCmd)
    69  		if err == nil {
    70  			t.Fatalf("tag busybox %v should have failed", repotag)
    71  		}
    72  	}
    73  	logDone("tag - busybox with invalid repo:tagnames --> must not work")
    74  }
    75  
    76  // ensure we allow the use of valid tags
    77  func TestTagValidPrefixedRepo(t *testing.T) {
    78  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    79  		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    80  	}
    81  
    82  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"}
    83  
    84  	for _, repo := range validRepos {
    85  		tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", repo)
    86  		_, _, err := runCommandWithOutput(tagCmd)
    87  		if err != nil {
    88  			t.Errorf("tag busybox %v should have worked: %s", repo, err)
    89  			continue
    90  		}
    91  		deleteImages(repo)
    92  		logMessage := fmt.Sprintf("tag - busybox %v", repo)
    93  		logDone(logMessage)
    94  	}
    95  }
    96  
    97  // tag an image with an existed tag name without -f option should fail
    98  func TestTagExistedNameWithoutForce(t *testing.T) {
    99  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
   100  		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   101  	}
   102  
   103  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
   104  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   105  		t.Fatal(out, err)
   106  	}
   107  	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
   108  	out, _, err := runCommandWithOutput(tagCmd)
   109  	if err == nil || !strings.Contains(out, "Conflict: Tag test is already set to image") {
   110  		t.Fatal("tag busybox busybox:test should have failed,because busybox:test is existed")
   111  	}
   112  	deleteImages("busybox:test")
   113  
   114  	logDone("tag - busybox with an existed tag name without -f option --> must not work")
   115  }
   116  
   117  // tag an image with an existed tag name with -f option should work
   118  func TestTagExistedNameWithForce(t *testing.T) {
   119  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
   120  		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   121  	}
   122  
   123  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
   124  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   125  		t.Fatal(out, err)
   126  	}
   127  	tagCmd = exec.Command(dockerBinary, "tag", "-f", "busybox:latest", "busybox:test")
   128  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   129  		t.Fatal(out, err)
   130  	}
   131  	deleteImages("busybox:test")
   132  
   133  	logDone("tag - busybox with an existed tag name with -f option work")
   134  }
   135  
   136  // ensure tagging using official names works
   137  // ensure all tags result in the same name
   138  func TestTagOfficialNames(t *testing.T) {
   139  	names := []string{
   140  		"docker.io/busybox",
   141  		"index.docker.io/busybox",
   142  		"library/busybox",
   143  		"docker.io/library/busybox",
   144  		"index.docker.io/library/busybox",
   145  	}
   146  
   147  	for _, name := range names {
   148  		tagCmd := exec.Command(dockerBinary, "tag", "-f", "busybox:latest", name+":latest")
   149  		out, exitCode, err := runCommandWithOutput(tagCmd)
   150  		if err != nil || exitCode != 0 {
   151  			t.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
   152  			continue
   153  		}
   154  
   155  		// ensure we don't have multiple tag names.
   156  		imagesCmd := exec.Command(dockerBinary, "images")
   157  		out, _, err = runCommandWithOutput(imagesCmd)
   158  		if err != nil {
   159  			t.Errorf("listing images failed with errors: %v, %s", err, out)
   160  		} else if strings.Contains(out, name) {
   161  			t.Errorf("images should not have listed '%s'", name)
   162  			deleteImages(name + ":latest")
   163  		} else {
   164  			logMessage := fmt.Sprintf("tag official name - busybox %v", name)
   165  			logDone(logMessage)
   166  		}
   167  	}
   168  
   169  	for _, name := range names {
   170  		tagCmd := exec.Command(dockerBinary, "tag", "-f", name+":latest", "fooo/bar:latest")
   171  		_, exitCode, err := runCommandWithOutput(tagCmd)
   172  		if err != nil || exitCode != 0 {
   173  			t.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   174  			continue
   175  		}
   176  		deleteImages("fooo/bar:latest")
   177  		logMessage := fmt.Sprintf("tag official name - %v fooo/bar", name)
   178  		logDone(logMessage)
   179  	}
   180  }