github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/integration-cli/docker_cli_v2_only_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/docker/docker/integration-cli/registry"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func makefile(contents string) (string, func(), error) {
    14  	cleanup := func() {
    15  
    16  	}
    17  
    18  	f, err := ioutil.TempFile(".", "tmp")
    19  	if err != nil {
    20  		return "", cleanup, err
    21  	}
    22  	err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm)
    23  	if err != nil {
    24  		return "", cleanup, err
    25  	}
    26  
    27  	cleanup = func() {
    28  		err := os.Remove(f.Name())
    29  		if err != nil {
    30  			fmt.Println("Error removing tmpfile")
    31  		}
    32  	}
    33  	return f.Name(), cleanup, nil
    34  
    35  }
    36  
    37  // TestV2Only ensures that a daemon in v2-only mode does not
    38  // attempt to contact any v1 registry endpoints.
    39  func (s *DockerRegistrySuite) TestV2Only(c *check.C) {
    40  	reg, err := registry.NewMock(c)
    41  	defer reg.Close()
    42  	c.Assert(err, check.IsNil)
    43  
    44  	reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
    45  		w.WriteHeader(404)
    46  	})
    47  
    48  	reg.RegisterHandler("/v1/.*", func(w http.ResponseWriter, r *http.Request) {
    49  		c.Fatal("V1 registry contacted")
    50  	})
    51  
    52  	repoName := fmt.Sprintf("%s/busybox", reg.URL())
    53  
    54  	s.d.Start(c, "--insecure-registry", reg.URL(), "--disable-legacy-registry=true")
    55  
    56  	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL()))
    57  	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
    58  	defer cleanup()
    59  
    60  	s.d.Cmd("build", "--file", dockerfileName, ".")
    61  
    62  	s.d.Cmd("run", repoName)
    63  	s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", reg.URL())
    64  	s.d.Cmd("tag", "busybox", repoName)
    65  	s.d.Cmd("push", repoName)
    66  	s.d.Cmd("pull", repoName)
    67  }
    68  
    69  // TestV1 starts a daemon in 'normal' mode
    70  // and ensure v1 endpoints are hit for the following operations:
    71  // login, push, pull, build & run
    72  func (s *DockerRegistrySuite) TestV1(c *check.C) {
    73  	reg, err := registry.NewMock(c)
    74  	defer reg.Close()
    75  	c.Assert(err, check.IsNil)
    76  
    77  	v2Pings := 0
    78  	reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
    79  		v2Pings++
    80  		// V2 ping 404 causes fallback to v1
    81  		w.WriteHeader(404)
    82  	})
    83  
    84  	v1Pings := 0
    85  	reg.RegisterHandler("/v1/_ping", func(w http.ResponseWriter, r *http.Request) {
    86  		v1Pings++
    87  	})
    88  
    89  	v1Logins := 0
    90  	reg.RegisterHandler("/v1/users/", func(w http.ResponseWriter, r *http.Request) {
    91  		v1Logins++
    92  	})
    93  
    94  	v1Repo := 0
    95  	reg.RegisterHandler("/v1/repositories/busybox/", func(w http.ResponseWriter, r *http.Request) {
    96  		v1Repo++
    97  	})
    98  
    99  	reg.RegisterHandler("/v1/repositories/busybox/images", func(w http.ResponseWriter, r *http.Request) {
   100  		v1Repo++
   101  	})
   102  
   103  	s.d.Start(c, "--insecure-registry", reg.URL(), "--disable-legacy-registry=false")
   104  
   105  	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL()))
   106  	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
   107  	defer cleanup()
   108  
   109  	s.d.Cmd("build", "--file", dockerfileName, ".")
   110  	c.Assert(v1Repo, check.Equals, 1, check.Commentf("Expected v1 repository access after build"))
   111  
   112  	repoName := fmt.Sprintf("%s/busybox", reg.URL())
   113  	s.d.Cmd("run", repoName)
   114  	c.Assert(v1Repo, check.Equals, 2, check.Commentf("Expected v1 repository access after run"))
   115  
   116  	s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
   117  	c.Assert(v1Logins, check.Equals, 1, check.Commentf("Expected v1 login attempt"))
   118  
   119  	s.d.Cmd("tag", "busybox", repoName)
   120  	s.d.Cmd("push", repoName)
   121  
   122  	c.Assert(v1Repo, check.Equals, 2)
   123  
   124  	s.d.Cmd("pull", repoName)
   125  	c.Assert(v1Repo, check.Equals, 3, check.Commentf("Expected v1 repository access after pull"))
   126  }