github.com/sevki/docker@v1.7.1/integration-cli/docker_cli_push_test.go (about)

     1  package main
     2  
     3  import (
     4  	"archive/tar"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"os/exec"
     9  	"strings"
    10  	"time"
    11  
    12  	"github.com/go-check/check"
    13  )
    14  
    15  // pulling an image from the central registry should work
    16  func (s *DockerRegistrySuite) TestPushBusyboxImage(c *check.C) {
    17  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    18  	// tag the image to upload it to the private registry
    19  	tagCmd := exec.Command(dockerBinary, "tag", "busybox", repoName)
    20  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
    21  		c.Fatalf("image tagging failed: %s, %v", out, err)
    22  	}
    23  
    24  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    25  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
    26  		c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
    27  	}
    28  }
    29  
    30  // pushing an image without a prefix should throw an error
    31  func (s *DockerSuite) TestPushUnprefixedRepo(c *check.C) {
    32  	pushCmd := exec.Command(dockerBinary, "push", "busybox")
    33  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    34  		c.Fatalf("pushing an unprefixed repo didn't result in a non-zero exit status: %s", out)
    35  	}
    36  }
    37  
    38  func (s *DockerRegistrySuite) TestPushUntagged(c *check.C) {
    39  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    40  
    41  	expected := "Repository does not exist"
    42  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    43  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    44  		c.Fatalf("pushing the image to the private registry should have failed: output %q", out)
    45  	} else if !strings.Contains(out, expected) {
    46  		c.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
    47  	}
    48  }
    49  
    50  func (s *DockerRegistrySuite) TestPushBadTag(c *check.C) {
    51  	repoName := fmt.Sprintf("%v/dockercli/busybox:latest", privateRegistryURL)
    52  
    53  	expected := "does not exist"
    54  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    55  	if out, _, err := runCommandWithOutput(pushCmd); err == nil {
    56  		c.Fatalf("pushing the image to the private registry should have failed: output %q", out)
    57  	} else if !strings.Contains(out, expected) {
    58  		c.Fatalf("pushing the image failed with an unexpected message: expected %q, got %q", expected, out)
    59  	}
    60  }
    61  
    62  func (s *DockerRegistrySuite) TestPushMultipleTags(c *check.C) {
    63  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    64  	repoTag1 := fmt.Sprintf("%v/dockercli/busybox:t1", privateRegistryURL)
    65  	repoTag2 := fmt.Sprintf("%v/dockercli/busybox:t2", privateRegistryURL)
    66  	// tag the image and upload it to the private registry
    67  	tagCmd1 := exec.Command(dockerBinary, "tag", "busybox", repoTag1)
    68  	if out, _, err := runCommandWithOutput(tagCmd1); err != nil {
    69  		c.Fatalf("image tagging failed: %s, %v", out, err)
    70  	}
    71  	tagCmd2 := exec.Command(dockerBinary, "tag", "busybox", repoTag2)
    72  	if out, _, err := runCommandWithOutput(tagCmd2); err != nil {
    73  		c.Fatalf("image tagging failed: %s, %v", out, err)
    74  	}
    75  
    76  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    77  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
    78  		c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
    79  	}
    80  }
    81  
    82  func (s *DockerRegistrySuite) TestPushInterrupt(c *check.C) {
    83  	repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
    84  	// tag the image and upload it to the private registry
    85  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", "busybox", repoName)); err != nil {
    86  		c.Fatalf("image tagging failed: %s, %v", out, err)
    87  	}
    88  
    89  	pushCmd := exec.Command(dockerBinary, "push", repoName)
    90  	if err := pushCmd.Start(); err != nil {
    91  		c.Fatalf("Failed to start pushing to private registry: %v", err)
    92  	}
    93  
    94  	// Interrupt push (yes, we have no idea at what point it will get killed).
    95  	time.Sleep(200 * time.Millisecond)
    96  	if err := pushCmd.Process.Kill(); err != nil {
    97  		c.Fatalf("Failed to kill push process: %v", err)
    98  	}
    99  	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "push", repoName)); err == nil {
   100  		str := string(out)
   101  		if !strings.Contains(str, "already in progress") {
   102  			c.Fatalf("Push should be continued on daemon side, but seems ok: %v, %s", err, out)
   103  		}
   104  	}
   105  	// now wait until all this pushes will complete
   106  	// if it failed with timeout - there would be some error,
   107  	// so no logic about it here
   108  	for exec.Command(dockerBinary, "push", repoName).Run() != nil {
   109  	}
   110  }
   111  
   112  func (s *DockerRegistrySuite) TestPushEmptyLayer(c *check.C) {
   113  	repoName := fmt.Sprintf("%v/dockercli/emptylayer", privateRegistryURL)
   114  	emptyTarball, err := ioutil.TempFile("", "empty_tarball")
   115  	if err != nil {
   116  		c.Fatalf("Unable to create test file: %v", err)
   117  	}
   118  	tw := tar.NewWriter(emptyTarball)
   119  	err = tw.Close()
   120  	if err != nil {
   121  		c.Fatalf("Error creating empty tarball: %v", err)
   122  	}
   123  	freader, err := os.Open(emptyTarball.Name())
   124  	if err != nil {
   125  		c.Fatalf("Could not open test tarball: %v", err)
   126  	}
   127  
   128  	importCmd := exec.Command(dockerBinary, "import", "-", repoName)
   129  	importCmd.Stdin = freader
   130  	out, _, err := runCommandWithOutput(importCmd)
   131  	if err != nil {
   132  		c.Errorf("import failed with errors: %v, output: %q", err, out)
   133  	}
   134  
   135  	// Now verify we can push it
   136  	pushCmd := exec.Command(dockerBinary, "push", repoName)
   137  	if out, _, err := runCommandWithOutput(pushCmd); err != nil {
   138  		c.Fatalf("pushing the image to the private registry has failed: %s, %v", out, err)
   139  	}
   140  }