Fix fs_util tests failing on systems with /tmp mountpoint (#2583)

* Rename IgnoreListPath to MountInfoPath in config & constants

The string points to /proc/self/mountinfo

* fs_util_test.go: fix tests failing when /tmp mountpoint present

The tests

* Test_GetFSFromLayers_ignorelist
* Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled
* Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled

were failing on systems with a /tmp mountpoint:

fs_util.InitIgnoreList() adds all mountpoints to the ignore list,
but the tests were expecting file operations in a /tmp subdirectory.

This change provides an empty mountinfo list for the affected tests.

Fixes #1779
This commit is contained in:
Andreas Fleig 2023-06-19 21:01:20 +02:00 committed by GitHub
parent bb712b6b39
commit 01763bce5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 8 deletions

View File

@ -44,9 +44,9 @@ var BuildContextDir = fmt.Sprintf("%s/buildcontext/", KanikoDir)
// as tarballs in case they are needed later on // as tarballs in case they are needed later on
var KanikoIntermediateStagesDir = fmt.Sprintf("%s/stages/", KanikoDir) var KanikoIntermediateStagesDir = fmt.Sprintf("%s/stages/", KanikoDir)
var IgnoreListPath string var MountInfoPath string
func init() { func init() {
RootDir = constants.RootDir RootDir = constants.RootDir
IgnoreListPath = constants.IgnoreListPath MountInfoPath = constants.MountInfoPath
} }

View File

@ -20,7 +20,7 @@ const (
// RootDir is the path to the root directory // RootDir is the path to the root directory
RootDir = "/" RootDir = "/"
IgnoreListPath = "/proc/self/mountinfo" MountInfoPath = "/proc/self/mountinfo"
DefaultKanikoPath = "/kaniko" DefaultKanikoPath = "/kaniko"

View File

@ -176,9 +176,9 @@ func setupMultistageTests(t *testing.T) (string, func()) {
if err := ioutil.WriteFile(mFile, []byte(mountInfo), 0644); err != nil { if err := ioutil.WriteFile(mFile, []byte(mountInfo), 0644); err != nil {
t.Fatal(err) t.Fatal(err)
} }
config.IgnoreListPath = mFile config.MountInfoPath = mFile
return testDir, func() { return testDir, func() {
config.RootDir = constants.RootDir config.RootDir = constants.RootDir
config.IgnoreListPath = constants.IgnoreListPath config.MountInfoPath = constants.MountInfoPath
} }
} }

View File

@ -1022,7 +1022,7 @@ func InitIgnoreList(detectFilesystem bool) error {
ignorelist = append([]IgnoreListEntry{}, defaultIgnoreList...) ignorelist = append([]IgnoreListEntry{}, defaultIgnoreList...)
if detectFilesystem { if detectFilesystem {
if err := DetectFilesystemIgnoreList(config.IgnoreListPath); err != nil { if err := DetectFilesystemIgnoreList(config.MountInfoPath); err != nil {
return errors.Wrap(err, "checking filesystem mount paths for ignore list") return errors.Wrap(err, "checking filesystem mount paths for ignore list")
} }
} }

View File

@ -1013,6 +1013,9 @@ func fakeExtract(dest string, hdr *tar.Header, tr io.Reader) error {
} }
func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T) { func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
root := t.TempDir() root := t.TempDir()
@ -1108,7 +1111,20 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_enabled(t *testing.T)
} }
} }
func provideEmptyMountinfoFile() func() {
// Provide empty mountinfo file to prevent /tmp from ending up in ignore list on
// distributions with /tmp mountpoint. Otherwise, tests expecting operations in /tmp
// can fail.
config.MountInfoPath = "/dev/null"
return func() {
config.MountInfoPath = constants.MountInfoPath
}
}
func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T) { func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
root := t.TempDir() root := t.TempDir()
@ -1209,6 +1225,9 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T)
} }
func Test_GetFSFromLayers_ignorelist(t *testing.T) { func Test_GetFSFromLayers_ignorelist(t *testing.T) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
root := t.TempDir() root := t.TempDir()
@ -1452,9 +1471,9 @@ func TestInitIgnoreList(t *testing.T) {
if _, err := mFile.WriteString(mountInfo); err != nil { if _, err := mFile.WriteString(mountInfo); err != nil {
t.Fatal(err) t.Fatal(err)
} }
config.IgnoreListPath = mFile.Name() config.MountInfoPath = mFile.Name()
defer func() { defer func() {
config.IgnoreListPath = constants.IgnoreListPath config.MountInfoPath = constants.MountInfoPath
}() }()
expected := []IgnoreListEntry{ expected := []IgnoreListEntry{