buildkit/platform_test.go
buildkit/platform_test.goBrowse 1970 files
951 tokens
3,285 bytes
Token encoding: o200k_base
Snapshot 21a254f
← Back to SKILL.md
1package buildkit2 3import (4 "testing"5 6 specs "github.com/opencontainers/image-spec/specs-go/v1"7)8 9func TestValidatePlatform(t *testing.T) {10 tests := []struct {11 name string12 input string13 expected specs.Platform14 wantErr bool15 }{16 {17 name: "empty string returns Linux platform",18 input: "",19 // When no platform is specified, we default to a Linux platform that matches20 // the host architecture. This ensures container compatibility while preserving21 // performance characteristics of the host system.22 expected: func() specs.Platform {23 platform, _ := ParsePlatformWithDefaults("")24 return platform25 }(),26 wantErr: false,27 },28 {29 name: "linux/arm64/v8",30 input: "linux/arm64/v8",31 expected: specs.Platform{32 OS: "linux",33 Architecture: "arm64",34 Variant: "v8",35 },36 wantErr: false,37 },38 {39 name: "linux/amd64",40 input: "linux/amd64",41 expected: specs.Platform{42 OS: "linux",43 Architecture: "amd64",44 },45 wantErr: false,46 },47 {48 name: "invalid input",49 input: "invalid",50 wantErr: true,51 },52 {53 name: "multiple platforms not supported",54 input: "linux/amd64,linux/arm64",55 wantErr: true,56 },57 }58 59 for _, tt := range tests {60 t.Run(tt.name, func(t *testing.T) {61 got, err := ParsePlatformWithDefaults(tt.input)62 if tt.wantErr {63 if err == nil {64 t.Errorf("ParsePlatformWithDefaults() expected error but got none")65 }66 return67 }68 if err != nil {69 t.Errorf("ParsePlatformWithDefaults() error = %v", err)70 return71 }72 if got.OS != tt.expected.OS || got.Architecture != tt.expected.Architecture || got.Variant != tt.expected.Variant {73 t.Errorf("ParsePlatformWithDefaults() = %v, want %v", got, tt.expected)74 }75 })76 }77}78 79func TestValidatePlatformWithMultiplePlatforms(t *testing.T) {80 tests := []struct {81 name string82 input string83 expected specs.Platform84 wantErr bool85 }{86 {87 name: "empty string returns Linux platform",88 input: "",89 expected: func() specs.Platform {90 platform, _ := ParsePlatformWithDefaults("")91 return platform92 }(),93 wantErr: false,94 },95 {96 name: "linux/arm64/v8",97 input: "linux/arm64/v8",98 expected: specs.Platform{99 OS: "linux",100 Architecture: "arm64",101 Variant: "v8",102 },103 wantErr: false,104 },105 {106 name: "linux/amd64",107 input: "linux/amd64",108 expected: specs.Platform{109 OS: "linux",110 Architecture: "amd64",111 },112 wantErr: false,113 },114 {115 name: "invalid input",116 input: "invalid",117 wantErr: true,118 },119 {120 name: "multiple platforms not supported",121 input: "linux/amd64,linux/arm64",122 wantErr: true,123 },124 }125 126 for _, tt := range tests {127 t.Run(tt.name, func(t *testing.T) {128 opts := map[string]string{"platform": tt.input}129 got, err := validatePlatform(opts)130 if tt.wantErr {131 if err == nil {132 t.Errorf("validatePlatform() expected error but got none")133 }134 return135 }136 if err != nil {137 t.Errorf("validatePlatform() error = %v", err)138 return139 }140 if got.OS != tt.expected.OS || got.Architecture != tt.expected.Architecture || got.Variant != tt.expected.Variant {141 t.Errorf("validatePlatform() = %v, want %v", got, tt.expected)142 }143 })144 }145}146