github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_v2_only.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  	if err != nil {
    41  		c.Fatal(err.Error())
    42  	}
    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.hostport)
    53  
    54  	err = s.d.Start("--insecure-registry", reg.hostport, "--disable-legacy-registry=true")
    55  	if err != nil {
    56  		c.Fatalf("Error starting daemon: %s", err.Error())
    57  	}
    58  
    59  	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.hostport))
    60  	if err != nil {
    61  		c.Fatalf("Unable to create test dockerfile")
    62  	}
    63  	defer cleanup()
    64  
    65  	s.d.Cmd("build", "--file", dockerfileName, ".")
    66  
    67  	s.d.Cmd("run", repoName)
    68  	s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", reg.hostport)
    69  	s.d.Cmd("tag", "busybox", repoName)
    70  	s.d.Cmd("push", repoName)
    71  	s.d.Cmd("pull", repoName)
    72  }
    73  
    74  // TestV1 starts a daemon in 'normal' mode
    75  // and ensure v1 endpoints are hit for the following operations:
    76  // login, push, pull, build & run
    77  func (s *DockerRegistrySuite) TestV1(c *check.C) {
    78  	reg, err := newTestRegistry(c)
    79  	if err != nil {
    80  		c.Fatal(err.Error())
    81  	}
    82  
    83  	v2Pings := 0
    84  	reg.registerHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
    85  		v2Pings++
    86  		// V2 ping 404 causes fallback to v1
    87  		w.WriteHeader(404)
    88  	})
    89  
    90  	v1Pings := 0
    91  	reg.registerHandler("/v1/_ping", func(w http.ResponseWriter, r *http.Request) {
    92  		v1Pings++
    93  	})
    94  
    95  	v1Logins := 0
    96  	reg.registerHandler("/v1/users/", func(w http.ResponseWriter, r *http.Request) {
    97  		v1Logins++
    98  	})
    99  
   100  	v1Repo := 0
   101  	reg.registerHandler("/v1/repositories/busybox/", func(w http.ResponseWriter, r *http.Request) {
   102  		v1Repo++
   103  	})
   104  
   105  	reg.registerHandler("/v1/repositories/busybox/images", func(w http.ResponseWriter, r *http.Request) {
   106  		v1Repo++
   107  	})
   108  
   109  	err = s.d.Start("--insecure-registry", reg.hostport, "--disable-legacy-registry=false")
   110  	if err != nil {
   111  		c.Fatalf("Error starting daemon: %s", err.Error())
   112  	}
   113  
   114  	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.hostport))
   115  	if err != nil {
   116  		c.Fatalf("Unable to create test dockerfile")
   117  	}
   118  	defer cleanup()
   119  
   120  	s.d.Cmd("build", "--file", dockerfileName, ".")
   121  	if v1Repo == 0 {
   122  		c.Errorf("Expected v1 repository access after build")
   123  	}
   124  
   125  	repoName := fmt.Sprintf("%s/busybox", reg.hostport)
   126  	s.d.Cmd("run", repoName)
   127  	if v1Repo == 1 {
   128  		c.Errorf("Expected v1 repository access after run")
   129  	}
   130  
   131  	s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", reg.hostport)
   132  	if v1Logins == 0 {
   133  		c.Errorf("Expected v1 login attempt")
   134  	}
   135  
   136  	s.d.Cmd("tag", "busybox", repoName)
   137  	s.d.Cmd("push", repoName)
   138  
   139  	if v1Repo != 2 || v1Pings != 1 {
   140  		c.Error("Not all endpoints contacted after push", v1Repo, v1Pings)
   141  	}
   142  
   143  	s.d.Cmd("pull", repoName)
   144  	if v1Repo != 3 {
   145  		c.Errorf("Expected v1 repository access after pull")
   146  	}
   147  
   148  }