|
| 1 | +// SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package artifact |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// testArchARM is the 32-bit ARM architecture identifier used in test platform specs. |
| 15 | +const testArchARM = "arm" |
| 16 | + |
| 17 | +func TestParsePlatform(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + input string |
| 23 | + want ocispec.Platform |
| 24 | + wantErr bool |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "os/arch", |
| 28 | + input: "linux/amd64", |
| 29 | + want: ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "os/arch/variant", |
| 33 | + input: "linux/arm/v7", |
| 34 | + want: ocispec.Platform{OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "fewer than 2 parts (no slash)", |
| 38 | + input: "linuxamd64", |
| 39 | + wantErr: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "more than 3 parts", |
| 43 | + input: "linux/amd64/v8/extra", |
| 44 | + wantErr: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "empty os", |
| 48 | + input: "/amd64", |
| 49 | + wantErr: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "empty arch", |
| 53 | + input: "linux/", |
| 54 | + wantErr: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "empty variant", |
| 58 | + input: "linux/arm/", |
| 59 | + wantErr: true, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + got, err := ParsePlatform(tt.input) |
| 68 | + if tt.wantErr { |
| 69 | + require.Error(t, err) |
| 70 | + return |
| 71 | + } |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, tt.want, got) |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestPlatformString(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + |
| 81 | + tests := []struct { |
| 82 | + name string |
| 83 | + platform ocispec.Platform |
| 84 | + want string |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "os/arch", |
| 88 | + platform: ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, |
| 89 | + want: "linux/amd64", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "os/arch/variant", |
| 93 | + platform: ocispec.Platform{OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, |
| 94 | + want: "linux/arm/v7", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tt := range tests { |
| 99 | + t.Run(tt.name, func(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + assert.Equal(t, tt.want, PlatformString(tt.platform)) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestParsePlatform_PlatformString_Roundtrip(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + platforms := []ocispec.Platform{ |
| 110 | + {OS: OSLinux, Architecture: ArchAMD64}, |
| 111 | + {OS: OSLinux, Architecture: ArchARM64}, |
| 112 | + {OS: OSLinux, Architecture: testArchARM, Variant: "v7"}, |
| 113 | + } |
| 114 | + |
| 115 | + for _, p := range platforms { |
| 116 | + parsed, err := ParsePlatform(PlatformString(p)) |
| 117 | + require.NoError(t, err) |
| 118 | + assert.Equal(t, p, parsed) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func TestDefaultPlatforms(t *testing.T) { |
| 123 | + t.Parallel() |
| 124 | + |
| 125 | + require.Len(t, DefaultPlatforms, 2) |
| 126 | + assert.Equal(t, ocispec.Platform{OS: OSLinux, Architecture: ArchAMD64}, DefaultPlatforms[0]) |
| 127 | + assert.Equal(t, ocispec.Platform{OS: OSLinux, Architecture: ArchARM64}, DefaultPlatforms[1]) |
| 128 | +} |
0 commit comments