Clarify repository naming constraints for registry API

After discussion, it was found that one of the proposed regular expressions
incorrectly limited separator delimited compoonents to two characters. The
desired restriction is to have repository name components limited to two
characters minimum. This changeset accomplishes this by wrapping the regular
expressions in a validation function, returning detailed feedback on the
validation error.

With this change, the repository name regular expressions are no longer enough
to respond with 404s on invalid repo names. Changes to the router will need to
be added to support this.
This commit is contained in:
Stephen J Day 2014-11-17 11:28:32 -08:00
parent 8a64db69ec
commit a650f0f854
2 changed files with 108 additions and 20 deletions

View file

@ -7,56 +7,85 @@ import (
func TestRepositoryNameRegexp(t *testing.T) {
for _, testcase := range []struct {
input string
valid bool
err error
}{
{
input: "simple/name",
valid: true,
},
{
input: "library/ubuntu",
valid: true,
},
{
input: "docker/stevvooe/app",
valid: true,
},
{
input: "aa/aa/aa/aa/aa/aa/aa/aa/aa/bb/bb/bb/bb/bb/bb",
valid: true,
err: ErrRepositoryNameTooManyComponents,
},
{
input: "a/a/a/a/a/a/b/b/b/b",
valid: false,
input: "aa/aa/bb/bb/bb",
},
{
input: "a/a/a/b/b",
err: ErrRepositoryNameComponentShort,
},
{
input: "a/a/a/a/",
valid: false,
err: ErrRepositoryNameComponentShort,
},
{
input: "foo.com/bar/baz",
valid: true,
},
{
input: "blog.foo.com/bar/baz",
valid: true,
},
{
input: "asdf",
valid: false,
err: ErrRepositoryNameMissingComponents,
},
{
input: "asdf$$^/",
valid: false,
input: "asdf$$^/aa",
err: ErrRepositoryNameComponentInvalid,
},
{
input: "aa-a/aa",
},
{
input: "aa/aa",
},
{
input: "a-a/a-a",
},
{
input: "a",
err: ErrRepositoryNameMissingComponents,
},
{
input: "a-/a/a/a",
err: ErrRepositoryNameComponentInvalid,
},
} {
if RepositoryNameRegexp.MatchString(testcase.input) != testcase.valid {
status := "invalid"
if testcase.valid {
status = "valid"
}
t.Fatalf("expected %q to be %s repository name", testcase.input, status)
failf := func(format string, v ...interface{}) {
t.Logf(testcase.input+": "+format, v...)
t.Fail()
}
if err := ValidateRespositoryName(testcase.input); err != testcase.err {
if testcase.err != nil {
if err != nil {
failf("unexpected error for invalid repository: got %v, expected %v", err, testcase.err)
} else {
failf("expected invalid repository: %v", testcase.err)
}
} else {
if err != nil {
// Wrong error returned.
failf("unexpected error validating repository name: %v, expected %v", err, testcase.err)
} else {
failf("unexpected error validating repository name: %v", err)
}
}
}
}
}