docker_cli_v2_only_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "github.com/go-check/check"
  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. c.Assert(err, check.IsNil)
  33. reg.registerHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
  34. w.WriteHeader(404)
  35. })
  36. reg.registerHandler("/v1/.*", func(w http.ResponseWriter, r *http.Request) {
  37. c.Fatal("V1 registry contacted")
  38. })
  39. repoName := fmt.Sprintf("%s/busybox", reg.hostport)
  40. err = s.d.Start("--insecure-registry", reg.hostport, "--disable-legacy-registry=true")
  41. c.Assert(err, check.IsNil)
  42. dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.hostport))
  43. c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
  44. defer cleanup()
  45. s.d.Cmd("build", "--file", dockerfileName, ".")
  46. s.d.Cmd("run", repoName)
  47. s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", reg.hostport)
  48. s.d.Cmd("tag", "busybox", repoName)
  49. s.d.Cmd("push", repoName)
  50. s.d.Cmd("pull", repoName)
  51. }
  52. // TestV1 starts a daemon in 'normal' mode
  53. // and ensure v1 endpoints are hit for the following operations:
  54. // login, push, pull, build & run
  55. func (s *DockerRegistrySuite) TestV1(c *check.C) {
  56. reg, err := newTestRegistry(c)
  57. c.Assert(err, check.IsNil)
  58. v2Pings := 0
  59. reg.registerHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
  60. v2Pings++
  61. // V2 ping 404 causes fallback to v1
  62. w.WriteHeader(404)
  63. })
  64. v1Pings := 0
  65. reg.registerHandler("/v1/_ping", func(w http.ResponseWriter, r *http.Request) {
  66. v1Pings++
  67. })
  68. v1Logins := 0
  69. reg.registerHandler("/v1/users/", func(w http.ResponseWriter, r *http.Request) {
  70. v1Logins++
  71. })
  72. v1Repo := 0
  73. reg.registerHandler("/v1/repositories/busybox/", func(w http.ResponseWriter, r *http.Request) {
  74. v1Repo++
  75. })
  76. reg.registerHandler("/v1/repositories/busybox/images", func(w http.ResponseWriter, r *http.Request) {
  77. v1Repo++
  78. })
  79. err = s.d.Start("--insecure-registry", reg.hostport, "--disable-legacy-registry=false")
  80. c.Assert(err, check.IsNil)
  81. dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.hostport))
  82. c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
  83. defer cleanup()
  84. s.d.Cmd("build", "--file", dockerfileName, ".")
  85. c.Assert(v1Repo, check.Equals, 1, check.Commentf("Expected v1 repository access after build"))
  86. repoName := fmt.Sprintf("%s/busybox", reg.hostport)
  87. s.d.Cmd("run", repoName)
  88. c.Assert(v1Repo, check.Equals, 2, check.Commentf("Expected v1 repository access after run"))
  89. s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.hostport)
  90. c.Assert(v1Logins, check.Equals, 1, check.Commentf("Expected v1 login attempt"))
  91. s.d.Cmd("tag", "busybox", repoName)
  92. s.d.Cmd("push", repoName)
  93. c.Assert(v1Repo, check.Equals, 2)
  94. s.d.Cmd("pull", repoName)
  95. c.Assert(v1Repo, check.Equals, 3, check.Commentf("Expected v1 repository access after pull"))
  96. }