|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 7 | + "gotest.tools/v3/assert" |
| 8 | + is "gotest.tools/v3/assert/cmp" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMergePlatforms(t *testing.T) { |
| 12 | + defaultPlatform := ocispec.Platform{OS: "linux", Architecture: "amd64"} |
| 13 | + otherPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"} |
| 14 | + thirdPlatform := ocispec.Platform{OS: "darwin", Architecture: "arm64"} |
| 15 | + |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + defined []ocispec.Platform |
| 19 | + supported []ocispec.Platform |
| 20 | + contains []ocispec.Platform |
| 21 | + wantLen int |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "AllUnique", |
| 25 | + defined: []ocispec.Platform{defaultPlatform}, |
| 26 | + supported: []ocispec.Platform{otherPlatform, thirdPlatform}, |
| 27 | + contains: []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform}, |
| 28 | + wantLen: 3, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "SomeOverlap", |
| 32 | + defined: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 33 | + supported: []ocispec.Platform{otherPlatform, thirdPlatform}, |
| 34 | + contains: []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform}, |
| 35 | + wantLen: 3, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "AllOverlap", |
| 39 | + defined: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 40 | + supported: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 41 | + contains: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 42 | + wantLen: 2, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "EmptySupported", |
| 46 | + defined: []ocispec.Platform{defaultPlatform}, |
| 47 | + supported: []ocispec.Platform{}, |
| 48 | + contains: []ocispec.Platform{defaultPlatform}, |
| 49 | + wantLen: 1, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "EmptyDefined", |
| 53 | + defined: []ocispec.Platform{}, |
| 54 | + supported: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 55 | + contains: []ocispec.Platform{defaultPlatform, otherPlatform}, |
| 56 | + wantLen: 2, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "BothEmpty", |
| 60 | + defined: []ocispec.Platform{}, |
| 61 | + supported: []ocispec.Platform{}, |
| 62 | + contains: []ocispec.Platform{}, |
| 63 | + wantLen: 0, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tc := range tests { |
| 68 | + t.Run(tc.name, func(t *testing.T) { |
| 69 | + got := mergePlatforms(tc.defined, tc.supported) |
| 70 | + assert.Equal(t, len(got), tc.wantLen) |
| 71 | + for _, p := range tc.contains { |
| 72 | + assert.Assert(t, is.Contains(got, p)) |
| 73 | + } |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments