github.com/powellquiring/docker@v1.6.0-rc1/integration-cli/docker_cli_push_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
    13  )
    14  
    15  // pulling an image from the central registry should work
    16  func TestPushBusyboxImage(t *testing.T) {
    17  	defer setupRegistry(t)()
    18  
    19  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    20  	// tag the image to upload it to the private registry
    21  	tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
    22  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
    23  		t.Fatalf("image tagging failed: %s, %v", out, err)
    24  	}
    25  	defer deleteImages(repoName)
    26  
    27  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    28  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
    29  		t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
    30  	}
    31  	logDone("push - busybox to private registry")
    32  }
    33  
    34  // pushing an image without a prefix should throw an error
    35  func TestPushUnprefixedRepo(t *testing.T) {
    36  	pushCmd := exec.Command(dockerBinary, "push", "busybox")
    37  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    38  		t.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
    39  	}
    40  	logDone("push - unprefixed busybox repo must not pass")
    41  }
    42  
    43  func TestPushUntagged(t *testing.T) {
    44  	defer setupRegistry(t)()
    45  
    46  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    47  
    48  	expected := "Repository does not exist"
    49  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    50  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    51  		t.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
    52  	} else if !strings.Contains(out, expected) {
    53  		t.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
    54  	}
    55  	logDone("push - untagged image")
    56  }
    57  
    58  func TestPushBadTag(t *testing.T) {
    59  	defer setupRegistry(t)()
    60  
    61  	repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
    62  
    63  	expected := "does not exist"
    64  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    65  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    66  		t.Fatalf("pushing the image to the private registry should have failed: outuput %q", out)
    67  	} else if !strings.Contains(out, expected) {
    68  		t.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
    69  	}
    70  	logDone("push - image with bad tag")
    71  }
    72  
    73  func TestPushMultipleTags(t *testing.T) {
    74  	defer setupRegistry(t)()
    75  
    76  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    77  	repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
    78  	repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
    79  	// tag the image to upload it tot he private registry
    80  	tagCmd1 := exec.Command(dockerBinary, "tag", "busybox", repoTag1)
    81  	if out, _, err := runCommandWithOutput(tagCmd1); err != nil {
    82  		t.Fatalf("image tagging failed: %s, %v", out, err)
    83  	}
    84  	defer deleteImages(repoTag1)
    85  	tagCmd2 := exec.Command(dockerBinary, "tag", "busybox", repoTag2)
    86  	if out, _, err := runCommandWithOutput(tagCmd2); err != nil {
    87  		t.Fatalf("image tagging failed: %s, %v", out, err)
    88  	}
    89  	defer deleteImages(repoTag2)
    90  
    91  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    92  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
    93  		t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
    94  	}
    95  	logDone("push - multiple tags to private registry")
    96  }
    97  
    98  func TestPushInterrupt(t *testing.T) {
    99  	defer setupRegistry(t)()
   100  
   101  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
   102  	// tag the image to upload it tot he private registry
   103  	tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
   104  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   105  		t.Fatalf("image tagging failed: %s, %v", out, err)
   106  	}
   107  	defer deleteImages(repoName)
   108  
   109  	pushCmd := exec.Command(dockerBinary, "push", repoName)
   110  	if err := pushCmd.Start(); err != nil {
   111  		t.Fatalf("Failed to start pushing to private registry: %v", err)
   112  	}
   113  
   114  	// Interrupt push (yes, we have no idea at what point it will get killed).
   115  	time.Sleep(200 * time.Millisecond)
   116  	if err := pushCmd.Process.Kill(); err != nil {
   117  		t.Fatalf("Failed to kill push process: %v", err)
   118  	}
   119  	// Try agin
   120  	pushCmd = exec.Command(dockerBinary, "push", repoName)
   121  	if err := pushCmd.Start(); err != nil {
   122  		t.Fatalf("Failed to start pushing to private registry: %v", err)
   123  	}
   124  
   125  	logDone("push - interrupted")
   126  }
   127  
   128  func TestPushEmptyLayer(t *testing.T) {
   129  	defer setupRegistry(t)()
   130  	repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
   131  	emptyTarball, err := ioutil.TempFile("", "empty_tarball")
   132  	if err != nil {
   133  		t.Fatalf("Unable to create test file: %v", err)
   134  	}
   135  	tw := tar.NewWriter(emptyTarball)
   136  	err = tw.Close()
   137  	if err != nil {
   138  		t.Fatalf("Error creating empty tarball: %v", err)
   139  	}
   140  	freader, err := os.Open(emptyTarball.Name())
   141  	if err != nil {
   142  		t.Fatalf("Could not open test tarball: %v", err)
   143  	}
   144  
   145  	importCmd := exec.Command(dockerBinary, "import", "-", repoName)
   146  	importCmd.Stdin = freader
   147  	out, _, err := runCommandWithOutput(importCmd)
   148  	if err != nil {
   149  		t.Errorf("import failed with errors: %v, output: %q", err, out)
   150  	}
   151  
   152  	// Now verify we can push it
   153  	pushCmd := exec.Command(dockerBinary, "push", repoName)
   154  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
   155  		t.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
   156  	}
   157  	logDone("push - empty layer config to private registry")
   158  }