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