@@ -186,6 +186,8 @@ func Test_filesMap_extractFile(t *testing.T) {
186
186
}
187
187
188
188
func Test_filesMap_Get (t * testing.T ) {
189
+ testDataRoot := makeTestData (t )
190
+
189
191
type fields struct {
190
192
root string
191
193
fileMap map [string ]* fileMetadata
@@ -224,7 +226,7 @@ func Test_filesMap_Get(t *testing.T) {
224
226
{
225
227
name : "when file is in map and is extractable, should return with contents" ,
226
228
fields : fields {
227
- root : filepath .Join ("testdata" , "rootfs-foo" ),
229
+ root : filepath .Join (testDataRoot , "rootfs-foo" ),
228
230
fileMap : map [string ]* fileMetadata {
229
231
"etc/redhat-release" : {
230
232
isExecutable : false ,
@@ -242,7 +244,7 @@ func Test_filesMap_Get(t *testing.T) {
242
244
{
243
245
name : "when file is in map and is extractable but does not exist, then keep error" ,
244
246
fields : fields {
245
- root : filepath .Join ("testdata" , "rootfs-foo" ),
247
+ root : filepath .Join (testDataRoot , "rootfs-foo" ),
246
248
fileMap : map [string ]* fileMetadata {
247
249
"foo/does/not/exist" : {
248
250
isExecutable : false ,
@@ -259,7 +261,7 @@ func Test_filesMap_Get(t *testing.T) {
259
261
{
260
262
name : "when file is in map, is extractable, and is an absolute symlink, should return with contents" ,
261
263
fields : fields {
262
- root : filepath .Join ("testdata" , "rootfs-rhcos" ),
264
+ root : filepath .Join (testDataRoot , "rootfs-rhcos" ),
263
265
fileMap : map [string ]* fileMetadata {
264
266
"etc/redhat-release" : {
265
267
isExecutable : false ,
@@ -278,7 +280,7 @@ func Test_filesMap_Get(t *testing.T) {
278
280
{
279
281
name : "when file is in map, is extractable, and is a relative symlink, should return with contents" ,
280
282
fields : fields {
281
- root : filepath .Join ("testdata" , "rootfs-rhcos-rel-symlink" ),
283
+ root : filepath .Join (testDataRoot , "rootfs-rhcos-rel-symlink" ),
282
284
fileMap : map [string ]* fileMetadata {
283
285
"etc/redhat-release" : {
284
286
isExecutable : false ,
@@ -348,15 +350,11 @@ func Test_filesMap_ReadErr(t *testing.T) {
348
350
}
349
351
350
352
func Test_extractFilesFromDirectory (t * testing.T ) {
351
- fooRoot := filepath .Join ("testdata" , "rootfs-foo" )
352
- fooRootAbs , err := filepath .Abs (fooRoot )
353
- require .NoError (t , err )
354
- rhcosRoot := filepath .Join ("testdata" , "rootfs-rhcos" )
355
- rhcosRootAbs , err := filepath .Abs (rhcosRoot )
356
- require .NoError (t , err )
357
- symlinkRelRoot := filepath .Join ("testdata" , "rootfs-rhcos-rel-symlink" )
358
- symlinkRelRootAbs , err := filepath .Abs (symlinkRelRoot )
359
- require .NoError (t , err )
353
+ testDataRoot := makeTestData (t )
354
+
355
+ fooRoot := filepath .Join (testDataRoot , "rootfs-foo" )
356
+ rhcosRoot := filepath .Join (testDataRoot , "rootfs-rhcos" )
357
+ symlinkRelRoot := filepath .Join (testDataRoot , "rootfs-rhcos-rel-symlink" )
360
358
361
359
testcases := []struct {
362
360
name string
@@ -367,7 +365,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
367
365
name : "etc/redhat-release no symlink" ,
368
366
root : fooRoot ,
369
367
expectedFilesMap : & filesMap {
370
- root : fooRootAbs ,
368
+ root : fooRoot ,
371
369
files : map [string ]* fileMetadata {
372
370
"etc/redhat-release" : {
373
371
isExecutable : false ,
@@ -382,7 +380,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
382
380
name : "etc/redhat-release with absolute symlink" ,
383
381
root : rhcosRoot ,
384
382
expectedFilesMap : & filesMap {
385
- root : rhcosRootAbs ,
383
+ root : rhcosRoot ,
386
384
files : map [string ]* fileMetadata {
387
385
"etc/redhat-release" : {
388
386
isExecutable : false ,
@@ -397,7 +395,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
397
395
name : "etc/redhat-release with relative symlink" ,
398
396
root : symlinkRelRoot ,
399
397
expectedFilesMap : & filesMap {
400
- root : symlinkRelRootAbs ,
398
+ root : symlinkRelRoot ,
401
399
files : map [string ]* fileMetadata {
402
400
"etc/redhat-release" : {
403
401
isExecutable : false ,
@@ -427,3 +425,51 @@ func Test_extractFilesFromDirectory(t *testing.T) {
427
425
})
428
426
}
429
427
}
428
+
429
+ func makeTestData (t * testing.T ) string {
430
+ mkdirs := func (file string ) {
431
+ require .NoError (t , os .MkdirAll (filepath .Dir (file ), 0755 ))
432
+ }
433
+ write := func (file , data string ) {
434
+ require .NoError (t , os .WriteFile (file , []byte (data ), 0644 ))
435
+ }
436
+
437
+ root := t .TempDir ()
438
+
439
+ fooRedhatRelease := filepath .Join (root , "rootfs-foo/etc/redhat-release" )
440
+ mkdirs (fooRedhatRelease )
441
+ write (fooRedhatRelease , "Some random red hat release that does not exist (X.Y) (foo bar)\n " )
442
+
443
+ // This directory structure mirrors what we have found in RHCOS 4.11.
444
+ //
445
+ // * `usr/lib/system-release` contains the distro identification data.
446
+ // * `etc/redhat-release` is a symlink to `/usr/lib/system-release`
447
+ //
448
+ // $ ls -l etc/redhat-release
449
+ // total 0
450
+ // lrwxr-xr-x 1 <OWNER> <GROUP> <DATE> redhat-release -> /usr/lib/system-release
451
+ //
452
+ rhcosRedhatRelease := filepath .Join (root , "rootfs-rhcos/etc/redhat-release" )
453
+ mkdirs (rhcosRedhatRelease )
454
+ require .NoError (t , os .Symlink ("/usr/lib/system-release" , rhcosRedhatRelease ))
455
+ rhcosSystemRelease := filepath .Join (root , "rootfs-rhcos/usr/lib/system-release" )
456
+ mkdirs (rhcosSystemRelease )
457
+ write (rhcosSystemRelease , "Red Hat Enterprise Linux CoreOS release 4.11\n " )
458
+
459
+ // This directory structure mirrors what we have found in RHCOS 4.11
460
+ // except it uses a symlink to a relative path.
461
+ //
462
+ // * `release` contains the data.
463
+ // * `etc/redhat-release` is a symlink to `release`
464
+ //
465
+ // $ ls -l etc/redhat-release
466
+ // total 0
467
+ // lrwxr-xr-x 1 <OWNER> <GROUP> <DATE> redhat-release -> ../release
468
+ //
469
+ relSymlinkRedhatRelease := filepath .Join (root , "rootfs-rhcos-rel-symlink/etc/redhat-release" )
470
+ mkdirs (relSymlinkRedhatRelease )
471
+ require .NoError (t , os .Symlink ("../release" , relSymlinkRedhatRelease ))
472
+ write (filepath .Join (root , "rootfs-rhcos-rel-symlink/release" ), "Hello" )
473
+
474
+ return root
475
+ }
0 commit comments