github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_registry_user_agent_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"regexp"
     7  
     8  	"github.com/go-check/check"
     9  )
    10  
    11  // unescapeBackslashSemicolonParens unescapes \;()
    12  func unescapeBackslashSemicolonParens(s string) string {
    13  	re := regexp.MustCompile(`\\;`)
    14  	ret := re.ReplaceAll([]byte(s), []byte(";"))
    15  
    16  	re = regexp.MustCompile(`\\\(`)
    17  	ret = re.ReplaceAll([]byte(ret), []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  	return string(ret)
    26  }
    27  
    28  func regexpCheckUA(c *check.C, ua string) {
    29  	re := regexp.MustCompile("(?P<dockerUA>.+) UpstreamClient(?P<upstreamUA>.+)")
    30  	substrArr := re.FindStringSubmatch(ua)
    31  
    32  	c.Assert(substrArr, check.HasLen, 3, check.Commentf("Expected 'UpstreamClient()' with upstream client UA"))
    33  	dockerUA := substrArr[1]
    34  	upstreamUAEscaped := substrArr[2]
    35  
    36  	// check dockerUA looks correct
    37  	reDockerUA := regexp.MustCompile("^docker/[0-9A-Za-z+]")
    38  	bMatchDockerUA := reDockerUA.MatchString(dockerUA)
    39  	c.Assert(bMatchDockerUA, check.Equals, true, check.Commentf("Docker Engine User-Agent malformed"))
    40  
    41  	// check upstreamUA looks correct
    42  	// Expecting something like:  Docker-Client/1.11.0-dev (linux)
    43  	upstreamUA := unescapeBackslashSemicolonParens(upstreamUAEscaped)
    44  	reUpstreamUA := regexp.MustCompile("^\\(Docker-Client/[0-9A-Za-z+]")
    45  	bMatchUpstreamUA := reUpstreamUA.MatchString(upstreamUA)
    46  	c.Assert(bMatchUpstreamUA, check.Equals, true, check.Commentf("(Upstream) Docker Client User-Agent malformed"))
    47  }
    48  
    49  func registerUserAgentHandler(reg *testRegistry, result *string) {
    50  	reg.registerHandler("/v2/", func(w http.ResponseWriter, r *http.Request) {
    51  		w.WriteHeader(404)
    52  		var ua string
    53  		for k, v := range r.Header {
    54  			if k == "User-Agent" {
    55  				ua = v[0]
    56  			}
    57  		}
    58  		*result = ua
    59  	})
    60  }
    61  
    62  // TestUserAgentPassThroughOnPull verifies that when an image is pulled from
    63  // a registry, the registry should see a User-Agent string of the form
    64  //   [docker engine UA] UptreamClientSTREAM-CLIENT([client UA])
    65  func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
    66  	var (
    67  		buildUA string
    68  		pullUA  string
    69  		pushUA  string
    70  		loginUA string
    71  	)
    72  
    73  	buildReg, err := newTestRegistry(c)
    74  	c.Assert(err, check.IsNil)
    75  	registerUserAgentHandler(buildReg, &buildUA)
    76  	buildRepoName := fmt.Sprintf("%s/busybox", buildReg.hostport)
    77  
    78  	pullReg, err := newTestRegistry(c)
    79  	c.Assert(err, check.IsNil)
    80  	registerUserAgentHandler(pullReg, &pullUA)
    81  	pullRepoName := fmt.Sprintf("%s/busybox", pullReg.hostport)
    82  
    83  	pushReg, err := newTestRegistry(c)
    84  	c.Assert(err, check.IsNil)
    85  	registerUserAgentHandler(pushReg, &pushUA)
    86  	pushRepoName := fmt.Sprintf("%s/busybox", pushReg.hostport)
    87  
    88  	loginReg, err := newTestRegistry(c)
    89  	c.Assert(err, check.IsNil)
    90  	registerUserAgentHandler(loginReg, &loginUA)
    91  
    92  	err = s.d.Start(
    93  		"--insecure-registry", buildReg.hostport,
    94  		"--insecure-registry", pullReg.hostport,
    95  		"--insecure-registry", pushReg.hostport,
    96  		"--insecure-registry", loginReg.hostport,
    97  		"--disable-legacy-registry=true")
    98  	c.Assert(err, check.IsNil)
    99  
   100  	dockerfileName, cleanup1, err := makefile(fmt.Sprintf("FROM %s", buildRepoName))
   101  	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
   102  	defer cleanup1()
   103  	s.d.Cmd("build", "--file", dockerfileName, ".")
   104  	regexpCheckUA(c, buildUA)
   105  
   106  	s.d.Cmd("login", "-u", "richard", "-p", "testtest", "-e", "testuser@testdomain.com", loginReg.hostport)
   107  	regexpCheckUA(c, loginUA)
   108  
   109  	s.d.Cmd("pull", pullRepoName)
   110  	regexpCheckUA(c, pullUA)
   111  
   112  	dockerfileName, cleanup2, err := makefile(`FROM scratch
   113  	ENV foo bar`)
   114  	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
   115  	defer cleanup2()
   116  	s.d.Cmd("build", "-t", pushRepoName, "--file", dockerfileName, ".")
   117  
   118  	s.d.Cmd("push", pushRepoName)
   119  	regexpCheckUA(c, pushUA)
   120  }