@@ -17,6 +17,9 @@ limitations under the License.
17
17
package repository
18
18
19
19
import (
20
+ "io/ioutil"
21
+ "os"
22
+ "path/filepath"
20
23
"testing"
21
24
22
25
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
@@ -124,7 +127,7 @@ func Test_localRepository_newLocalRepository(t *testing.T) {
124
127
t .Run (tt .name , func (t * testing.T ) {
125
128
got , err := newLocalRepository (tt .fields .provider , tt .fields .configVariablesClient )
126
129
if (err != nil ) != tt .wantErr {
127
- t .Errorf ("Get () error = %v, wantErr %v" , err , tt .wantErr )
130
+ t .Errorf ("newLocalRepository () error = %v, wantErr %v" , err , tt .wantErr )
128
131
return
129
132
}
130
133
@@ -152,3 +155,173 @@ func Test_localRepository_newLocalRepository(t *testing.T) {
152
155
})
153
156
}
154
157
}
158
+
159
+ func tempDir (t * testing.T ) string {
160
+ dir , err := ioutil .TempDir ("" , "cc" )
161
+ if err != nil {
162
+ t .Fatalf ("err: %s" , err )
163
+ }
164
+ if err := os .RemoveAll (dir ); err != nil {
165
+ t .Fatalf ("err: %s" , err )
166
+ }
167
+
168
+ return dir
169
+ }
170
+
171
+ func tempTestLayout (t * testing.T , tmpDir , path , msg string ) string {
172
+
173
+ dst := filepath .Join (tmpDir , path )
174
+ // Create all directories in the standard layout
175
+ if err := os .MkdirAll (filepath .Dir (dst ), 0755 ); err != nil {
176
+ t .Fatalf ("err: %s" , err )
177
+ }
178
+
179
+ err := ioutil .WriteFile (dst , []byte (msg ), 0644 )
180
+ if err != nil {
181
+ t .Fatalf ("err: %s" , err )
182
+ }
183
+ return dst
184
+ }
185
+
186
+ func Test_localRepository_newLocalRepository_Latest (t * testing.T ) {
187
+ tmpDir := tempDir (t )
188
+ defer os .RemoveAll (filepath .Dir (tmpDir ))
189
+
190
+ // Create several release directories
191
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/v1.0.0/infrastructure-components.yaml" , "foo: bar" )
192
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/v1.0.1/infrastructure-components.yaml" , "foo: bar" )
193
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/Foo.Bar/infrastructure-components.yaml" , "foo: bar" )
194
+ // Provider URL for the latest release
195
+ p2URLLatest := "owner-foo/provider-2/releases/latest/infrastructure-components.yaml"
196
+ p2URLLatestAbs := filepath .Join (tmpDir , p2URLLatest )
197
+ p2 := config .NewProvider ("provider-2" , p2URLLatestAbs , clusterctlv1 .BootstrapProviderType )
198
+
199
+ t .Run ("Pass: select latest release from multiple release directories" , func (t * testing.T ) {
200
+ got , err := newLocalRepository (p2 , test .NewFakeVariableClient ())
201
+ if err != nil {
202
+ t .Errorf ("newLocalRepository() got error %v" , err )
203
+ return
204
+ }
205
+
206
+ if got .basepath != tmpDir {
207
+ t .Errorf ("basepath() got = %v, want = %v " , got .basepath , tmpDir )
208
+ }
209
+ if got .owner != "owner-foo" {
210
+ t .Errorf ("owner() got = %v, want = owner-foo " , got .owner )
211
+ }
212
+ if got .providerName != "provider-2" {
213
+ t .Errorf ("providerName() got = %v, want = provider-2 " , got .providerName )
214
+ }
215
+ if got .DefaultVersion () != "v1.0.1" {
216
+ t .Errorf ("DefaultVersion() got = %v, want = v1.0.1 " , got .DefaultVersion ())
217
+ }
218
+ if got .RootPath () != "" {
219
+ t .Errorf ("RootPath() got = %v, want = \" \" " , got .RootPath ())
220
+ }
221
+ if got .ComponentsPath () != "infrastructure-components.yaml" {
222
+ t .Errorf ("ComponentsPath() got = %v, want = infrastructure-components.yaml " , got .ComponentsPath ())
223
+ }
224
+ })
225
+ }
226
+
227
+ func Test_localRepository_GetFile (t * testing.T ) {
228
+ tmpDir := tempDir (t )
229
+ defer os .RemoveAll (filepath .Dir (tmpDir ))
230
+
231
+ // Provider 1: URL is for the only release available
232
+ dst1 := tempTestLayout (t , tmpDir , "owner-foo/provider-1/releases/v1.0.0/infrastructure-components.yaml" , "foo: bar" )
233
+ p1 := config .NewProvider ("provider-1" , dst1 , clusterctlv1 .BootstrapProviderType )
234
+
235
+ // Provider 2: URL is for the latest release
236
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/v1.0.0/infrastructure-components.yaml" , "version: v1.0.0" )
237
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/v1.0.1/infrastructure-components.yaml" , "version: v1.0.1" )
238
+ tempTestLayout (t , tmpDir , "owner-foo/provider-2/releases/Foo.Bar/infrastructure-components.yaml" , "version: Foo.Bar" )
239
+ p2URLLatest := "owner-foo/provider-2/releases/latest/infrastructure-components.yaml"
240
+ p2URLLatestAbs := filepath .Join (tmpDir , p2URLLatest )
241
+ p2 := config .NewProvider ("provider-2" , p2URLLatestAbs , clusterctlv1 .BootstrapProviderType )
242
+
243
+ type fields struct {
244
+ provider config.Provider
245
+ configVariablesClient config.VariablesClient
246
+ }
247
+ type args struct {
248
+ version string
249
+ fileName string
250
+ }
251
+ type want struct {
252
+ contents string
253
+ }
254
+ tests := []struct {
255
+ name string
256
+ fields fields
257
+ args args
258
+ want want
259
+ wantErr bool
260
+ }{
261
+ {
262
+ name : "Get file from release directory" ,
263
+ fields : fields {
264
+ provider : p1 ,
265
+ configVariablesClient : test .NewFakeVariableClient (),
266
+ },
267
+ args : args {
268
+ version : "v1.0.0" ,
269
+ fileName : "infrastructure-components.yaml" ,
270
+ },
271
+ want : want {
272
+ contents : "foo: bar" ,
273
+ },
274
+ wantErr : false ,
275
+ },
276
+ {
277
+ name : "Get file from latest release directory" ,
278
+ fields : fields {
279
+ provider : p2 ,
280
+ configVariablesClient : test .NewFakeVariableClient (),
281
+ },
282
+ args : args {
283
+ version : "latest" ,
284
+ fileName : "infrastructure-components.yaml" ,
285
+ },
286
+ want : want {
287
+ contents : "version: v1.0.1" , // We use the file contents to determine data was read from latest release
288
+ },
289
+ wantErr : false ,
290
+ },
291
+ {
292
+ name : "Get file from default version release directory" ,
293
+ fields : fields {
294
+ provider : p2 ,
295
+ configVariablesClient : test .NewFakeVariableClient (),
296
+ },
297
+ args : args {
298
+ version : "" ,
299
+ fileName : "infrastructure-components.yaml" ,
300
+ },
301
+ want : want {
302
+ contents : "version: v1.0.1" , // We use the file contents to determine data was read from latest release
303
+ },
304
+ wantErr : false ,
305
+ },
306
+ }
307
+ for _ , tt := range tests {
308
+ t .Run (tt .name , func (t * testing.T ) {
309
+ r , err := newLocalRepository (tt .fields .provider , tt .fields .configVariablesClient )
310
+ if err != nil {
311
+ t .Errorf ("newLocalRepository() threw unexpected error: %v" , err )
312
+ return
313
+ }
314
+ got , err := r .GetFile (tt .args .version , tt .args .fileName )
315
+ if (err != nil ) != tt .wantErr {
316
+ t .Errorf ("GetFile() error = %v, wantErr %v" , err , tt .wantErr )
317
+ return
318
+ }
319
+ if tt .wantErr {
320
+ return
321
+ }
322
+ if string (got ) != tt .want .contents {
323
+ t .Errorf ("GetFile() returned %s expected %s" , got , tt .want .contents )
324
+ }
325
+ })
326
+ }
327
+ }
0 commit comments