Skip to content

Commit e8668a1

Browse files
committed
Fix build integration tests
1 parent 949a695 commit e8668a1

File tree

1 file changed

+37
-52
lines changed

1 file changed

+37
-52
lines changed

pkg/dazzle/build_test.go

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"os"
3030
"os/exec"
3131
"regexp"
32-
"sort"
3332
"strings"
3433
"testing"
3534
"testing/fstest"
@@ -38,6 +37,7 @@ import (
3837
"github.com/containerd/containerd/remotes"
3938
"github.com/containerd/containerd/remotes/docker"
4039
"github.com/docker/distribution/reference"
40+
"github.com/google/go-cmp/cmp"
4141

4242
"github.com/moby/buildkit/client"
4343
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -346,30 +346,13 @@ func TestProjectChunk_test_integration(t *testing.T) {
346346
return
347347
}
348348

349-
// regexes for tags we expect
350-
// NOTE: order is important
351-
expectedTagsRE := []string{
352-
`base--[[:alnum:]]+$`,
353-
`basic--[[:alnum:]]+--chunked$`,
354-
`basic--[[:alnum:]]+--full$`,
355-
`basic--[[:alnum:]]+--test$`,
356-
`basic--[[:alnum:]]+--test-result$`,
357-
}
358-
359-
// Should match 'as-is'
360-
if len(expectedTagsRE) != len(tagResp.Tags) {
361-
t.Errorf("mismatched tag lengths, got:%s, want%d", tagResp.Tags, len(expectedTagsRE))
362-
return
363-
}
364-
sort.Strings(tagResp.Tags)
365-
for i, tag := range tagResp.Tags {
366-
re := expectedTagsRE[i]
367-
matched, err := regexp.MatchString(re, tag)
368-
if !matched || err != nil {
369-
t.Errorf("tags mismatch\nwant:%s\n got:%s\n err:%s)", re, tag, err)
370-
return
371-
}
372-
}
349+
expectAllTags(t, tagResp.Tags, map[string]int{
350+
`base--[[:alnum:]]+$`: 1,
351+
`basic--[[:alnum:]]+--chunked$`: 1,
352+
`basic--[[:alnum:]]+--full$`: 1,
353+
`basic--[[:alnum:]]+--test$`: 1,
354+
`basic--[[:alnum:]]+--test-result$`: 1,
355+
})
373356
}
374357

375358
// Individually check each chunk to ensure it doesn't rebuild
@@ -433,34 +416,36 @@ func TestProjectChunk_test_integration(t *testing.T) {
433416
t.Errorf("TestProjectChunk_test_integration() could not get decode tags from registry: %v", err)
434417
return
435418
}
436-
sort.Strings(tagResp.Tags)
437-
// NOTE: add another \n for consistency since the hashes can change the order
438-
allTags := strings.Join(tagResp.Tags, "\n") + "\n"
439-
if 7 != len(tagResp.Tags) {
440-
t.Errorf("mismatched tag lengths, got:%s, want:7", allTags)
441-
return
442-
}
443-
// regexes with counts for tags we expect
444-
// NOTE: order is important
445-
type expectedTag struct {
446-
Regex string
447-
Count int
448-
}
449-
expectedTags := []expectedTag{
450-
{`base--[[:alnum:]]+\n`, 1},
451-
{`basic--[[:alnum:]]+--chunked\n`, 1},
452-
{`basic--[[:alnum:]]+--full\n`, 2},
453-
{`basic--[[:alnum:]]+--test\n`, 2},
454-
// NOTE: since tests pass the result is unchanged
455-
{`basic--[[:alnum:]]+--test-result\n`, 1},
456-
}
457-
for _, expectedTag := range expectedTags {
458-
re := regexp.MustCompile(expectedTag.Regex)
459-
cnt := expectedTag.Count
460-
matches := re.FindAllStringIndex(allTags, -1)
461-
if cnt != len(matches) {
462-
t.Errorf("tags mismatch for %s\nwant:%d\n got:%d\n \ntags:%s\nerr:%s)", expectedTag.Regex, cnt, len(matches), allTags, err)
419+
expectAllTags(t, tagResp.Tags, map[string]int{
420+
`base--[[:alnum:]]+$`: 1,
421+
`basic--[[:alnum:]]+--chunked$`: 1,
422+
`basic--[[:alnum:]]+--full$`: 1,
423+
`basic--[[:alnum:]]+--test$`: 2,
424+
`basic--[[:alnum:]]+--test-result$`: 1,
425+
})
426+
}
427+
}
428+
429+
func expectAllTags(t *testing.T, tags []string, expectation map[string]int) {
430+
// regexes for tags we expect
431+
// NOTE: order is important
432+
act := make(map[string]int)
433+
434+
// Should match 'as-is'
435+
for pattern := range expectation {
436+
for _, tag := range tags {
437+
matched, err := regexp.MatchString(pattern, tag)
438+
if err != nil {
439+
t.Error(err)
440+
continue
441+
}
442+
if matched {
443+
act[pattern] = act[pattern] + 1
463444
}
464445
}
465446
}
447+
448+
if diff := cmp.Diff(act, expectation); len(diff) != 0 {
449+
t.Errorf("expected tags: %s\nbut got %v from\n\t%s", diff, act, strings.Join(tags, "\n\t"))
450+
}
466451
}

0 commit comments

Comments
 (0)