@@ -2,6 +2,7 @@ package extensions
22
33import (
44 "os"
5+ "runtime"
56 "testing"
67
78 "github.com/stretchr/testify/assert"
@@ -97,18 +98,8 @@ func TestFilterExtensionBinariesByTags(t *testing.T) {
9798
9899 for _ , tt := range tests {
99100 t .Run (tt .name , func (t * testing.T ) {
100- // Set environment variables
101- if tt .excludeTags != "" {
102- os .Setenv ("EXTENSION_BINARY_OVERRIDE_EXCLUDE_TAGS" , tt .excludeTags )
103- } else {
104- os .Unsetenv ("EXTENSION_BINARY_OVERRIDE_EXCLUDE_TAGS" )
105- }
106-
107- if tt .includeTags != "" {
108- os .Setenv ("EXTENSION_BINARY_OVERRIDE_INCLUDE_TAGS" , tt .includeTags )
109- } else {
110- os .Unsetenv ("EXTENSION_BINARY_OVERRIDE_INCLUDE_TAGS" )
111- }
101+ t .Setenv ("EXTENSION_BINARY_OVERRIDE_EXCLUDE_TAGS" , tt .excludeTags )
102+ t .Setenv ("EXTENSION_BINARY_OVERRIDE_INCLUDE_TAGS" , tt .includeTags )
112103
113104 // Call the function
114105 result := filterExtensionBinariesByTags (testBinaries )
@@ -123,10 +114,123 @@ func TestFilterExtensionBinariesByTags(t *testing.T) {
123114 }
124115
125116 assert .ElementsMatch (t , tt .expectedImageTags , actualImageTags , "Expected image tags %v, got %v" , tt .expectedImageTags , actualImageTags )
117+ })
118+ }
119+ }
120+
121+ func TestExtensionBinaryArchitecture (t * testing.T ) {
122+ tests := []struct {
123+ name string
124+ setOCPArchitecture bool
125+ ocpArchitecture string
126+ runtimeArchitecture string
127+ wantArchitecture string
128+ }{
129+ {
130+ name : "arm64 target overrides amd64 runtime" ,
131+ setOCPArchitecture : true ,
132+ ocpArchitecture : "arm64" ,
133+ runtimeArchitecture : "amd64" ,
134+ wantArchitecture : "arm64" ,
135+ },
136+ {
137+ name : "amd64 target is honored" ,
138+ setOCPArchitecture : true ,
139+ ocpArchitecture : "amd64" ,
140+ runtimeArchitecture : "arm64" ,
141+ wantArchitecture : "amd64" ,
142+ },
143+ {
144+ name : "unset target uses runtime architecture" ,
145+ runtimeArchitecture : runtime .GOARCH ,
146+ wantArchitecture : runtime .GOARCH ,
147+ },
148+ }
149+
150+ for _ , tt := range tests {
151+ t .Run (tt .name , func (t * testing.T ) {
152+ t .Setenv ("OCP_ARCH" , tt .ocpArchitecture )
153+ if ! tt .setOCPArchitecture {
154+ assert .NoError (t , os .Unsetenv ("OCP_ARCH" ))
155+ }
156+
157+ assert .Equal (t , tt .wantArchitecture , extensionBinaryArchitecture (tt .runtimeArchitecture ))
158+ })
159+ }
160+ }
161+
162+ func TestFilterExtensionBinariesByArchitecture (t * testing.T ) {
163+ tests := []struct {
164+ name string
165+ architectures []string
166+ architecture string
167+ wantIncluded bool
168+ }{
169+ {
170+ name : "empty allowlist is available on all architectures" ,
171+ architecture : "arm64" ,
172+ wantIncluded : true ,
173+ },
174+ {
175+ name : "matching architecture is included" ,
176+ architectures : []string {"amd64" , "arm64" },
177+ architecture : "arm64" ,
178+ wantIncluded : true ,
179+ },
180+ {
181+ name : "non-matching architecture is omitted" ,
182+ architectures : []string {"amd64" },
183+ architecture : "arm64" ,
184+ wantIncluded : false ,
185+ },
186+ }
187+
188+ for _ , tt := range tests {
189+ t .Run (tt .name , func (t * testing.T ) {
190+ binary := TestBinary {
191+ imageTag : "test-extension" ,
192+ binaryPath : "/usr/bin/test-extension" ,
193+ architectures : tt .architectures ,
194+ }
195+
196+ filtered := filterExtensionBinariesByArchitecture ([]TestBinary {binary }, tt .architecture )
197+
198+ assert .Equal (t , tt .wantIncluded , len (filtered ) == 1 )
199+ })
200+ }
201+ }
202+
203+ func TestVSphereExtensionBinaryArchitectures (t * testing.T ) {
204+ tests := []struct {
205+ name string
206+ architecture string
207+ wantIncluded bool
208+ }{
209+ {
210+ name : "included on amd64" ,
211+ architecture : "amd64" ,
212+ wantIncluded : true ,
213+ },
214+ {
215+ name : "omitted on arm64" ,
216+ architecture : "arm64" ,
217+ wantIncluded : false ,
218+ },
219+ }
220+
221+ for _ , tt := range tests {
222+ t .Run (tt .name , func (t * testing.T ) {
223+ filtered := filterExtensionBinariesByArchitecture (extensionBinaries , tt .architecture )
224+ included := false
225+ for _ , binary := range filtered {
226+ if binary .imageTag == "vsphere-csi-driver-operator" {
227+ included = true
228+ assert .Equal (t , []string {"amd64" }, binary .architectures )
229+ break
230+ }
231+ }
126232
127- // Clean up environment variables
128- os .Unsetenv ("EXTENSION_BINARY_OVERRIDE_EXCLUDE_TAGS" )
129- os .Unsetenv ("EXTENSION_BINARY_OVERRIDE_INCLUDE_TAGS" )
233+ assert .Equal (t , tt .wantIncluded , included )
130234 })
131235 }
132236}
0 commit comments