Simplify DockerRegistrySuite.TestUserAgentPassThrough()

This patch simplifies the test by;

- re-using the registry-mock / handler
- skipping the last `docker build`, which was only
  used to make sure a local image was present. Instead,
  the daemon is started with a `busybox` image loaded.

Also added a comment, explaining why the mock always
returns a 404 (hence, error/output-string should not
be checked in the test), and made the mock return a
valid/correctly formatted error response.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2017-06-12 15:02:51 +02:00
parent ebe66b1d0f
commit 5d04fe73bf
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -49,9 +49,14 @@ func regexpCheckUA(c *check.C, ua string) {
c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed")) c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed"))
} }
// registerUserAgentHandler registers a handler for the `/v2/*` endpoint.
// Note that a 404 is returned to prevent the client to proceed.
// We are only checking if the client sent a valid User Agent string along
// with the request.
func registerUserAgentHandler(reg *registry.Mock, result *string) { func registerUserAgentHandler(reg *registry.Mock, result *string) {
reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) { reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404) w.WriteHeader(404)
w.Write([]byte(`{"errors":[{"code": "UNSUPPORTED","message": "this is a mock registry"}]}`))
var ua string var ua string
for k, v := range r.Header { for k, v := range r.Header {
if k == "User-Agent" { if k == "User-Agent" {
@ -66,63 +71,33 @@ func registerUserAgentHandler(reg *registry.Mock, result *string) {
// a registry, the registry should see a User-Agent string of the form // a registry, the registry should see a User-Agent string of the form
// [docker engine UA] UpstreamClientSTREAM-CLIENT([client UA]) // [docker engine UA] UpstreamClientSTREAM-CLIENT([client UA])
func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) { func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
var ( var ua string
buildUA string
pullUA string
pushUA string
loginUA string
)
buildReg, err := registry.NewMock(c) reg, err := registry.NewMock(c)
defer buildReg.Close() defer reg.Close()
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
registerUserAgentHandler(buildReg, &buildUA) registerUserAgentHandler(reg, &ua)
buildRepoName := fmt.Sprintf("%s/busybox", buildReg.URL()) repoName := fmt.Sprintf("%s/busybox", reg.URL())
pullReg, err := registry.NewMock(c) s.d.StartWithBusybox(c, "--insecure-registry", reg.URL())
defer pullReg.Close()
c.Assert(err, check.IsNil)
registerUserAgentHandler(pullReg, &pullUA)
pullRepoName := fmt.Sprintf("%s/busybox", pullReg.URL())
pushReg, err := registry.NewMock(c)
defer pushReg.Close()
c.Assert(err, check.IsNil)
registerUserAgentHandler(pushReg, &pushUA)
pushRepoName := fmt.Sprintf("%s/busybox", pushReg.URL())
loginReg, err := registry.NewMock(c)
defer loginReg.Close()
c.Assert(err, check.IsNil)
registerUserAgentHandler(loginReg, &loginUA)
s.d.Start(c,
"--insecure-registry", buildReg.URL(),
"--insecure-registry", pullReg.URL(),
"--insecure-registry", pushReg.URL(),
"--insecure-registry", loginReg.URL())
tmp, err := ioutil.TempDir("", "integration-cli-") tmp, err := ioutil.TempDir("", "integration-cli-")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
defer os.RemoveAll(tmp) defer os.RemoveAll(tmp)
dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s", buildRepoName)) dockerfile, err := makefile(tmp, fmt.Sprintf("FROM %s", repoName))
c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
s.d.Cmd("build", "--file", dockerfileName, tmp) s.d.Cmd("build", "--file", dockerfile, tmp)
regexpCheckUA(c, buildUA) regexpCheckUA(c, ua)
s.d.Cmd("login", "-u", "richard", "-p", "testtest", loginReg.URL()) s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
regexpCheckUA(c, loginUA) regexpCheckUA(c, ua)
s.d.Cmd("pull", pullRepoName) s.d.Cmd("pull", repoName)
regexpCheckUA(c, pullUA) regexpCheckUA(c, ua)
dockerfileName, err = makefile(tmp, `FROM scratch s.d.Cmd("tag", "busybox", repoName)
ENV foo bar`) s.d.Cmd("push", repoName)
c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) regexpCheckUA(c, ua)
s.d.Cmd("build", "-t", pushRepoName, "--file", dockerfileName, ".")
s.d.Cmd("push", pushRepoName)
regexpCheckUA(c, pushUA)
} }