Skip to content

Commit

Permalink
Fix fs_util tests failing on systems with /tmp mountpoint (#2583)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andreasf authored Jun 19, 2023
1 parent bb712b6 commit 01763bc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ var BuildContextDir = fmt.Sprintf("%s/buildcontext/", KanikoDir)
// as tarballs in case they are needed later on
var KanikoIntermediateStagesDir = fmt.Sprintf("%s/stages/", KanikoDir)

var IgnoreListPath string
var MountInfoPath string

func init() {
RootDir = constants.RootDir
IgnoreListPath = constants.IgnoreListPath
MountInfoPath = constants.MountInfoPath
}
2 changes: 1 addition & 1 deletion pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// RootDir is the path to the root directory
RootDir = "/"

IgnoreListPath = "/proc/self/mountinfo"
MountInfoPath = "/proc/self/mountinfo"

DefaultKanikoPath = "/kaniko"

Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/copy_multistage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func setupMultistageTests(t *testing.T) (string, func()) {
if err := ioutil.WriteFile(mFile, []byte(mountInfo), 0644); err != nil {
t.Fatal(err)
}
config.IgnoreListPath = mFile
config.MountInfoPath = mFile
return testDir, func() {
config.RootDir = constants.RootDir
config.IgnoreListPath = constants.IgnoreListPath
config.MountInfoPath = constants.MountInfoPath
}
}
2 changes: 1 addition & 1 deletion pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ func InitIgnoreList(detectFilesystem bool) error {
ignorelist = append([]IgnoreListEntry{}, defaultIgnoreList...)

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")
}
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()

ctrl := gomock.NewController(t)

root := t.TempDir()
Expand Down Expand Up @@ -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) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()

ctrl := gomock.NewController(t)

root := t.TempDir()
Expand Down Expand Up @@ -1209,6 +1225,9 @@ func Test_GetFSFromLayers_with_whiteouts_include_whiteout_disabled(t *testing.T)
}

func Test_GetFSFromLayers_ignorelist(t *testing.T) {
var resetMountInfoFile = provideEmptyMountinfoFile()
defer resetMountInfoFile()

ctrl := gomock.NewController(t)

root := t.TempDir()
Expand Down Expand Up @@ -1452,9 +1471,9 @@ func TestInitIgnoreList(t *testing.T) {
if _, err := mFile.WriteString(mountInfo); err != nil {
t.Fatal(err)
}
config.IgnoreListPath = mFile.Name()
config.MountInfoPath = mFile.Name()
defer func() {
config.IgnoreListPath = constants.IgnoreListPath
config.MountInfoPath = constants.MountInfoPath
}()

expected := []IgnoreListEntry{
Expand Down

0 comments on commit 01763bc

Please sign in to comment.