docker_cli_v2_only_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "testing"
  8. "github.com/docker/docker/testutil/registry"
  9. "gotest.tools/assert"
  10. )
  11. func makefile(path string, contents string) (string, error) {
  12. f, err := ioutil.TempFile(path, "tmp")
  13. if err != nil {
  14. return "", err
  15. }
  16. err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm)
  17. if err != nil {
  18. return "", err
  19. }
  20. return f.Name(), nil
  21. }
  22. // TestV2Only ensures that a daemon does not
  23. // attempt to contact any v1 registry endpoints.
  24. func (s *DockerRegistrySuite) TestV2Only(c *testing.T) {
  25. reg, err := registry.NewMock(c)
  26. defer reg.Close()
  27. assert.NilError(c, err)
  28. reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
  29. w.WriteHeader(404)
  30. })
  31. reg.RegisterHandler("/v1/.*", func(w http.ResponseWriter, r *http.Request) {
  32. c.Fatal("V1 registry contacted")
  33. })
  34. repoName := fmt.Sprintf("%s/busybox", reg.URL())
  35. s.d.Start(c, "--insecure-registry", reg.URL())
  36. tmp, err := ioutil.TempDir("", "integration-cli-")
  37. assert.NilError(c, err)
  38. defer os.RemoveAll(tmp)
  39. dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL()))
  40. assert.NilError(c, err, "Unable to create test dockerfile")
  41. s.d.Cmd("build", "--file", dockerfileName, tmp)
  42. s.d.Cmd("run", repoName)
  43. s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
  44. s.d.Cmd("tag", "busybox", repoName)
  45. s.d.Cmd("push", repoName)
  46. s.d.Cmd("pull", repoName)
  47. }