forked from TrueCloudLab/distribution
Merge pull request #724 from aaronlehmann/repository-name-regexp-tests
Add additional test coverage for the regexp contained in RepositoryNameRegexp
This commit is contained in:
commit
4703e9980c
2 changed files with 114 additions and 30 deletions
|
@ -6,10 +6,18 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestRepositoryNameRegexp(t *testing.T) {
|
||||
for _, testcase := range []struct {
|
||||
var (
|
||||
// regexpTestcases is a unified set of testcases for
|
||||
// TestValidateRepositoryName and TestRepositoryNameRegexp.
|
||||
// Some of them are valid inputs for one and not the other.
|
||||
regexpTestcases = []struct {
|
||||
// input is the repository name or name component testcase
|
||||
input string
|
||||
// err is the error expected from ValidateRepositoryName, or nil
|
||||
err error
|
||||
// invalid should be true if the testcase is *not* expected to
|
||||
// match RepositoryNameRegexp
|
||||
invalid bool
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
|
@ -39,10 +47,12 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
{
|
||||
input: "a/a/a/a/",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "a//a/a",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "a",
|
||||
|
@ -56,9 +66,27 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
{
|
||||
input: "a/aa/a",
|
||||
},
|
||||
{
|
||||
input: "foo.com/",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
// TODO: this testcase should be valid once we switch to
|
||||
// the reference package.
|
||||
input: "foo.com:8080/bar",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "foo.com/bar",
|
||||
},
|
||||
{
|
||||
input: "foo.com/bar/baz",
|
||||
},
|
||||
{
|
||||
input: "foo.com/bar/baz/quux",
|
||||
},
|
||||
{
|
||||
input: "blog.foo.com/bar/baz",
|
||||
},
|
||||
|
@ -68,6 +96,7 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
{
|
||||
input: "asdf$$^/aa",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "aa-a/aa",
|
||||
|
@ -81,6 +110,7 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
{
|
||||
input: "a-/a/a/a",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: strings.Repeat("a", 255),
|
||||
|
@ -92,40 +122,55 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
{
|
||||
input: "-foo/bar",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "foo/bar-",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "foo-/bar",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "foo/-bar",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "_foo/bar",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "foo/bar_",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "____/____",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "_docker/_docker",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
{
|
||||
input: "docker_/docker_",
|
||||
err: ErrRepositoryNameComponentInvalid,
|
||||
invalid: true,
|
||||
},
|
||||
} {
|
||||
}
|
||||
)
|
||||
|
||||
// TestValidateRepositoryName tests the ValidateRepositoryName function,
|
||||
// which uses RepositoryNameComponentAnchoredRegexp for validation
|
||||
func TestValidateRepositoryName(t *testing.T) {
|
||||
for _, testcase := range regexpTestcases {
|
||||
failf := func(format string, v ...interface{}) {
|
||||
t.Logf(strconv.Quote(testcase.input)+": "+format, v...)
|
||||
t.Fail()
|
||||
|
@ -149,3 +194,21 @@ func TestRepositoryNameRegexp(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepositoryNameRegexp(t *testing.T) {
|
||||
for _, testcase := range regexpTestcases {
|
||||
failf := func(format string, v ...interface{}) {
|
||||
t.Logf(strconv.Quote(testcase.input)+": "+format, v...)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
matches := RepositoryNameRegexp.FindString(testcase.input) == testcase.input
|
||||
if matches == testcase.invalid {
|
||||
if testcase.invalid {
|
||||
failf("expected invalid repository name %s", testcase.input)
|
||||
} else {
|
||||
failf("expected valid repository name %s", testcase.input)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,6 +66,27 @@ func TestRouter(t *testing.T) {
|
|||
"name": "foo/bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
RouteName: RouteNameTags,
|
||||
RequestURI: "/v2/docker.com/foo/tags/list",
|
||||
Vars: map[string]string{
|
||||
"name": "docker.com/foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
RouteName: RouteNameTags,
|
||||
RequestURI: "/v2/docker.com/foo/bar/tags/list",
|
||||
Vars: map[string]string{
|
||||
"name": "docker.com/foo/bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
RouteName: RouteNameTags,
|
||||
RequestURI: "/v2/docker.com/foo/bar/baz/tags/list",
|
||||
Vars: map[string]string{
|
||||
"name": "docker.com/foo/bar/baz",
|
||||
},
|
||||
},
|
||||
{
|
||||
RouteName: RouteNameBlob,
|
||||
RequestURI: "/v2/foo/bar/blobs/tarsum.dev+foo:abcdef0919234",
|
||||
|
|
Loading…
Reference in a new issue