moby/integration-cli/docker_cli_v2_only_test.go
Sebastiaan van Stijn 5bba06e082
integration-cli: SA5001: should check returned error before deferring reg.Close() (staticcheck)
```
integration-cli/docker_cli_registry_user_agent_test.go:78:2: SA5001: should check returned error before deferring reg.Close() (staticcheck)
integration-cli/docker_cli_v2_only_test.go:30:2:             SA5001: should check returned error before deferring reg.Close() (staticcheck)
integration-cli/docker_api_containers_test.go:392:3:         SA5001: should check returned error before deferring resp.Body.Close() (staticcheck)
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-09-18 12:57:03 +02:00

58 lines
1.4 KiB
Go

package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/docker/docker/testutil/registry"
"gotest.tools/assert"
)
func makefile(path string, contents string) (string, error) {
f, err := ioutil.TempFile(path, "tmp")
if err != nil {
return "", err
}
err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm)
if err != nil {
return "", err
}
return f.Name(), nil
}
// TestV2Only ensures that a daemon does not
// attempt to contact any v1 registry endpoints.
func (s *DockerRegistrySuite) TestV2Only(c *testing.T) {
reg, err := registry.NewMock(c)
assert.NilError(c, err)
defer reg.Close()
reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
reg.RegisterHandler("/v1/.*", func(w http.ResponseWriter, r *http.Request) {
c.Fatal("V1 registry contacted")
})
repoName := fmt.Sprintf("%s/busybox", reg.URL())
s.d.Start(c, "--insecure-registry", reg.URL())
tmp, err := ioutil.TempDir("", "integration-cli-")
assert.NilError(c, err)
defer os.RemoveAll(tmp)
dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL()))
assert.NilError(c, err, "Unable to create test dockerfile")
_, _ = s.d.Cmd("build", "--file", dockerfileName, tmp)
_, _ = s.d.Cmd("run", repoName)
_, _ = s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
_, _ = s.d.Cmd("tag", "busybox", repoName)
_, _ = s.d.Cmd("push", repoName)
_, _ = s.d.Cmd("pull", repoName)
}