docker_cli_v2_only.go 3.5 KB

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