github.com/sams1990/dockerrepo@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_registry_user_agent_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "os" 8 "regexp" 9 10 "github.com/docker/docker/integration-cli/registry" 11 "github.com/go-check/check" 12 ) 13 14 // unescapeBackslashSemicolonParens unescapes \;() 15 func unescapeBackslashSemicolonParens(s string) string { 16 re := regexp.MustCompile(`\\;`) 17 ret := re.ReplaceAll([]byte(s), []byte(";")) 18 19 re = regexp.MustCompile(`\\\(`) 20 ret = re.ReplaceAll([]byte(ret), []byte("(")) 21 22 re = regexp.MustCompile(`\\\)`) 23 ret = re.ReplaceAll([]byte(ret), []byte(")")) 24 25 re = regexp.MustCompile(`\\\\`) 26 ret = re.ReplaceAll([]byte(ret), []byte(`\`)) 27 28 return string(ret) 29 } 30 31 func regexpCheckUA(c *check.C, ua string) { 32 re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)") 33 substrArr := re.FindStringSubmatch(ua) 34 35 c.Assert(substrArr, check.HasLen, 3, check.Commentf("Expected 'UpstreamClient()' with upstream client UA")) 36 dockerUA := substrArr[1] 37 upstreamUAEscaped := substrArr[2] 38 39 // check dockerUA looks correct 40 reDockerUA := regexp.MustCompile("^docker/[0-9A-Za-z+]") 41 bMatchDockerUA := reDockerUA.MatchString(dockerUA) 42 c.Assert(bMatchDockerUA, check.Equals, true, check.Commentf("Docker Engine User-Agent malformed")) 43 44 // check upstreamUA looks correct 45 // Expecting something like: Docker-Client/1.11.0-dev (linux) 46 upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped) 47 reUpstreamUA := regexp.MustCompile("^\\(Docker-Client/[0-9A-Za-z+]") 48 bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA) 49 c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed")) 50 } 51 52 // registerUserAgentHandler registers a handler for the `/v2/*` endpoint. 53 // Note that a 404 is returned to prevent the client to proceed. 54 // We are only checking if the client sent a valid User Agent string along 55 // with the request. 56 func registerUserAgentHandler(reg *registry.Mock, result *string) { 57 reg.RegisterHandler("/v2/", func(w http.ResponseWriter, r *http.Request) { 58 w.WriteHeader(404) 59 w.Write([]byte(`{"errors":[{"code": "UNSUPPORTED","message": "this is a mock registry"}]}`)) 60 var ua string 61 for k, v := range r.Header { 62 if k == "User-Agent" { 63 ua = v[0] 64 } 65 } 66 *result = ua 67 }) 68 } 69 70 // TestUserAgentPassThrough verifies that when an image is pulled from 71 // a registry, the registry should see a User-Agent string of the form 72 // [docker engine UA] UpstreamClientSTREAM-CLIENT([client UA]) 73 func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) { 74 var ua string 75 76 reg, err := registry.NewMock(c) 77 defer reg.Close() 78 c.Assert(err, check.IsNil) 79 registerUserAgentHandler(reg, &ua) 80 repoName := fmt.Sprintf("%s/busybox", reg.URL()) 81 82 s.d.StartWithBusybox(c, "--insecure-registry", reg.URL()) 83 84 tmp, err := ioutil.TempDir("", "integration-cli-") 85 c.Assert(err, check.IsNil) 86 defer os.RemoveAll(tmp) 87 88 dockerfile, err := makefile(tmp, fmt.Sprintf("FROM %s", repoName)) 89 c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) 90 91 s.d.Cmd("build", "--file", dockerfile, tmp) 92 regexpCheckUA(c, ua) 93 94 s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL()) 95 regexpCheckUA(c, ua) 96 97 s.d.Cmd("pull", repoName) 98 regexpCheckUA(c, ua) 99 100 s.d.Cmd("tag", "busybox", repoName) 101 s.d.Cmd("push", repoName) 102 regexpCheckUA(c, ua) 103 }