Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference: rewrite test to use sub-tests, add benchmark #3883

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions reference/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,35 +261,41 @@ func TestParseRepositoryInfo(t *testing.T) {
},
}

for _, tcase := range tcases {
refStrings := []string{tcase.FamiliarName, tcase.FullName}
if tcase.AmbiguousName != "" {
refStrings = append(refStrings, tcase.AmbiguousName)
for i, tc := range tcases {
tc := tc
refStrings := []string{tc.FamiliarName, tc.FullName}
if tc.AmbiguousName != "" {
refStrings = append(refStrings, tc.AmbiguousName)
}

var refs []Named
for _, r := range refStrings {
named, err := ParseNormalizedNamed(r)
if err != nil {
t.Fatal(err)
}
refs = append(refs, named)
}

for _, r := range refs {
if expected, actual := tcase.FamiliarName, FamiliarName(r); expected != actual {
t.Fatalf("Invalid normalized reference for %q. Expected %q, got %q", r, expected, actual)
}
if expected, actual := tcase.FullName, r.String(); expected != actual {
t.Fatalf("Invalid canonical reference for %q. Expected %q, got %q", r, expected, actual)
}
if expected, actual := tcase.Domain, Domain(r); expected != actual {
t.Fatalf("Invalid domain for %q. Expected %q, got %q", r, expected, actual)
}
if expected, actual := tcase.RemoteName, Path(r); expected != actual {
t.Fatalf("Invalid remoteName for %q. Expected %q, got %q", r, expected, actual)
}

t.Run(strconv.Itoa(i)+"/"+r, func(t *testing.T) {
t.Parallel()
named, err := ParseNormalizedNamed(r)
if err != nil {
t.Fatalf("ref=%s: %v", r, err)
}
t.Run("FamiliarName", func(t *testing.T) {
if expected, actual := tc.FamiliarName, FamiliarName(named); expected != actual {
t.Errorf("Invalid familiar name for %q. Expected %q, got %q", named, expected, actual)
}
})
t.Run("FullName", func(t *testing.T) {
if expected, actual := tc.FullName, named.String(); expected != actual {
t.Errorf("Invalid canonical reference for %q. Expected %q, got %q", named, expected, actual)
}
})
t.Run("Domain", func(t *testing.T) {
if expected, actual := tc.Domain, Domain(named); expected != actual {
t.Errorf("Invalid domain for %q. Expected %q, got %q", named, expected, actual)
}
})
t.Run("RemoteName", func(t *testing.T) {
if expected, actual := tc.RemoteName, Path(named); expected != actual {
t.Errorf("Invalid remoteName for %q. Expected %q, got %q", named, expected, actual)
}
})
})
}
}
}
Expand Down Expand Up @@ -677,6 +683,7 @@ func TestParseDockerRef(t *testing.T) {
},
}
for _, test := range testcases {
test := test
Copy link
Member

@milosgajdos milosgajdos Apr 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I would also love this var to (and the range var) to be called tc for consistency's sake but I appreciate making tests consistent would've been a proper effort

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, 💯. Both for the OCD side of me, but also because having a common pattern / naming helps with cognitive overload when reading code (when reading tc you don't have to "think" what it is).

Probably best to do so in a one-time pass over all existing tests and update them.

t.Run(test.name, func(t *testing.T) {
t.Parallel()
normalized, err := ParseDockerRef(test.input)
Expand Down
270 changes: 270 additions & 0 deletions reference/regexp_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
package reference

import (
"strings"
"testing"
)

func BenchmarkParse(b *testing.B) {
tests := []regexpMatch{
{
input: "",
match: false,
},
{
input: "short",
match: true,
},
{
input: "simple/name",
match: true,
},
{
input: "library/ubuntu",
match: true,
},
{
input: "docker/stevvooe/app",
match: true,
},
{
input: "aa/aa/aa/aa/aa/aa/aa/aa/aa/bb/bb/bb/bb/bb/bb",
match: true,
},
{
input: "aa/aa/bb/bb/bb",
match: true,
},
{
input: "a/a/a/a",
match: true,
},
{
input: "a/a/a/a/",
match: false,
},
{
input: "a//a/a",
match: false,
},
{
input: "a",
match: true,
},
{
input: "a/aa",
match: true,
},
{
input: "a/aa/a",
match: true,
},
{
input: "foo.com",
match: true,
},
{
input: "foo.com/",
match: false,
},
{
input: "foo.com:8080/bar",
match: true,
},
{
input: "foo.com:http/bar",
match: false,
},
{
input: "foo.com/bar",
match: true,
},
{
input: "foo.com/bar/baz",
match: true,
},
{
input: "localhost:8080/bar",
match: true,
},
{
input: "sub-dom1.foo.com/bar/baz/quux",
match: true,
},
{
input: "blog.foo.com/bar/baz",
match: true,
},
{
input: "a^a",
match: false,
},
{
input: "aa/asdf$$^/aa",
match: false,
},
{
input: "asdf$$^/aa",
match: false,
},
{
input: "aa-a/a",
match: true,
},
{
input: strings.Repeat("a/", 128) + "a",
match: true,
},
{
input: "a-/a/a/a",
match: false,
},
{
input: "foo.com/a-/a/a",
match: false,
},
{
input: "-foo/bar",
match: false,
},
{
input: "foo/bar-",
match: false,
},
{
input: "foo-/bar",
match: false,
},
{
input: "foo/-bar",
match: false,
},
{
input: "_foo/bar",
match: false,
},
{
input: "foo_bar",
match: true,
},
{
input: "foo_bar.com",
match: true,
},
{
input: "foo_bar.com:8080",
match: false,
},
{
input: "foo_bar.com:8080/app",
match: false,
},
{
input: "foo.com/foo_bar",
match: true,
},
{
input: "____/____",
match: false,
},
{
input: "_docker/_docker",
match: false,
},
{
input: "docker_/docker_",
match: false,
},
{
input: "b.gcr.io/test.example.com/my-app",
match: true,
},
{
input: "xn--n3h.com/myimage", // ☃.com in punycode
match: true,
},
{
input: "xn--7o8h.com/myimage", // 🐳.com in punycode
match: true,
},
{
input: "example.com/xn--7o8h.com/myimage", // 🐳.com in punycode
match: true,
},
{
input: "example.com/some_separator__underscore/myimage",
match: true,
},
{
input: "example.com/__underscore/myimage",
match: false,
},
{
input: "example.com/..dots/myimage",
match: false,
},
{
input: "example.com/.dots/myimage",
match: false,
},
{
input: "example.com/nodouble..dots/myimage",
match: false,
},
{
input: "example.com/nodouble..dots/myimage",
match: false,
},
{
input: "docker./docker",
match: false,
},
{
input: ".docker/docker",
match: false,
},
{
input: "docker-/docker",
match: false,
},
{
input: "-docker/docker",
match: false,
},
{
input: "do..cker/docker",
match: false,
},
{
input: "do__cker:8080/docker",
match: false,
},
{
input: "do__cker/docker",
match: true,
},
{
input: "b.gcr.io/test.example.com/my-app",
match: true,
},
{
input: "registry.io/foo/project--id.module--name.ver---sion--name",
match: true,
},
{
input: "Asdf.com/foo/bar", // uppercase character in hostname
match: true,
},
{
input: "Foo/FarB", // uppercase characters in remote name
match: false,
},
}

b.ReportAllocs()
for i := 0; i < b.N; i++ {
for _, tc := range tests {
_, _ = Parse(tc.input)
}
}
}
Loading