@@ -29,7 +29,6 @@ import (
29
29
"os"
30
30
"os/exec"
31
31
"regexp"
32
- "sort"
33
32
"strings"
34
33
"testing"
35
34
"testing/fstest"
@@ -38,6 +37,7 @@ import (
38
37
"github.com/containerd/containerd/remotes"
39
38
"github.com/containerd/containerd/remotes/docker"
40
39
"github.com/docker/distribution/reference"
40
+ "github.com/google/go-cmp/cmp"
41
41
42
42
"github.com/moby/buildkit/client"
43
43
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -346,30 +346,13 @@ func TestProjectChunk_test_integration(t *testing.T) {
346
346
return
347
347
}
348
348
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\n want:%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
+ })
373
356
}
374
357
375
358
// Individually check each chunk to ensure it doesn't rebuild
@@ -433,34 +416,36 @@ func TestProjectChunk_test_integration(t *testing.T) {
433
416
t .Errorf ("TestProjectChunk_test_integration() could not get decode tags from registry: %v" , err )
434
417
return
435
418
}
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\n want:%d\n got:%d\n \n tags:%s\n err:%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
463
444
}
464
445
}
465
446
}
447
+
448
+ if diff := cmp .Diff (act , expectation ); len (diff ) != 0 {
449
+ t .Errorf ("expected tags: %s\n but got %v from\n \t %s" , diff , act , strings .Join (tags , "\n \t " ))
450
+ }
466
451
}
0 commit comments