Skip to content

Commit a0e7c00

Browse files
authored
ROX-14082: Create test files and symlinks from code (#1039)
1 parent 3744365 commit a0e7c00

File tree

8 files changed

+62
-44
lines changed

8 files changed

+62
-44
lines changed

pkg/analyzer/nodes/node_test.go

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ func Test_filesMap_extractFile(t *testing.T) {
186186
}
187187

188188
func Test_filesMap_Get(t *testing.T) {
189+
testDataRoot := makeTestData(t)
190+
189191
type fields struct {
190192
root string
191193
fileMap map[string]*fileMetadata
@@ -224,7 +226,7 @@ func Test_filesMap_Get(t *testing.T) {
224226
{
225227
name: "when file is in map and is extractable, should return with contents",
226228
fields: fields{
227-
root: filepath.Join("testdata", "rootfs-foo"),
229+
root: filepath.Join(testDataRoot, "rootfs-foo"),
228230
fileMap: map[string]*fileMetadata{
229231
"etc/redhat-release": {
230232
isExecutable: false,
@@ -242,7 +244,7 @@ func Test_filesMap_Get(t *testing.T) {
242244
{
243245
name: "when file is in map and is extractable but does not exist, then keep error",
244246
fields: fields{
245-
root: filepath.Join("testdata", "rootfs-foo"),
247+
root: filepath.Join(testDataRoot, "rootfs-foo"),
246248
fileMap: map[string]*fileMetadata{
247249
"foo/does/not/exist": {
248250
isExecutable: false,
@@ -259,7 +261,7 @@ func Test_filesMap_Get(t *testing.T) {
259261
{
260262
name: "when file is in map, is extractable, and is an absolute symlink, should return with contents",
261263
fields: fields{
262-
root: filepath.Join("testdata", "rootfs-rhcos"),
264+
root: filepath.Join(testDataRoot, "rootfs-rhcos"),
263265
fileMap: map[string]*fileMetadata{
264266
"etc/redhat-release": {
265267
isExecutable: false,
@@ -278,7 +280,7 @@ func Test_filesMap_Get(t *testing.T) {
278280
{
279281
name: "when file is in map, is extractable, and is a relative symlink, should return with contents",
280282
fields: fields{
281-
root: filepath.Join("testdata", "rootfs-rhcos-rel-symlink"),
283+
root: filepath.Join(testDataRoot, "rootfs-rhcos-rel-symlink"),
282284
fileMap: map[string]*fileMetadata{
283285
"etc/redhat-release": {
284286
isExecutable: false,
@@ -348,15 +350,11 @@ func Test_filesMap_ReadErr(t *testing.T) {
348350
}
349351

350352
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")
360358

361359
testcases := []struct {
362360
name string
@@ -367,7 +365,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
367365
name: "etc/redhat-release no symlink",
368366
root: fooRoot,
369367
expectedFilesMap: &filesMap{
370-
root: fooRootAbs,
368+
root: fooRoot,
371369
files: map[string]*fileMetadata{
372370
"etc/redhat-release": {
373371
isExecutable: false,
@@ -382,7 +380,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
382380
name: "etc/redhat-release with absolute symlink",
383381
root: rhcosRoot,
384382
expectedFilesMap: &filesMap{
385-
root: rhcosRootAbs,
383+
root: rhcosRoot,
386384
files: map[string]*fileMetadata{
387385
"etc/redhat-release": {
388386
isExecutable: false,
@@ -397,7 +395,7 @@ func Test_extractFilesFromDirectory(t *testing.T) {
397395
name: "etc/redhat-release with relative symlink",
398396
root: symlinkRelRoot,
399397
expectedFilesMap: &filesMap{
400-
root: symlinkRelRootAbs,
398+
root: symlinkRelRoot,
401399
files: map[string]*fileMetadata{
402400
"etc/redhat-release": {
403401
isExecutable: false,
@@ -427,3 +425,51 @@ func Test_extractFilesFromDirectory(t *testing.T) {
427425
})
428426
}
429427
}
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+
}

pkg/analyzer/nodes/testdata/rootfs-foo/etc/redhat-release

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos-rel-symlink/README.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos-rel-symlink/etc/redhat-release

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos-rel-symlink/release

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos/README.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos/etc/redhat-release

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/analyzer/nodes/testdata/rootfs-rhcos/usr/lib/system-release

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)