Skip to content

Commit

Permalink
Merge pull request #5043 from rhatdan/tmpdir
Browse files Browse the repository at this point in the history
GetTmpDir is not using ImageCopyTmpdir correctly
  • Loading branch information
rhatdan authored Sep 14, 2023
2 parents 42eaf2f + e90b350 commit 0ad0a8c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
6 changes: 2 additions & 4 deletions internal/tmpdir/tmpdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ func GetTempDir() string {
}
logrus.Warnf("ignoring TMPDIR from environment, evaluating it: %v", err)
}
containerConfig, err := config.Default()
if err != nil {
tmpdir, err := containerConfig.ImageCopyTmpDir()
if err != nil {
if containerConfig, err := config.Default(); err == nil {
if tmpdir, err := containerConfig.ImageCopyTmpDir(); err == nil {
return tmpdir
}
}
Expand Down
49 changes: 49 additions & 0 deletions internal/tmpdir/tmpdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tmpdir

import (
"os"
"testing"

"github.com/containers/common/pkg/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetTempDir(t *testing.T) {
// test default
err := os.Unsetenv("TMPDIR")
require.NoError(t, err)
err = os.Setenv("CONTAINERS_CONF", "/dev/null")
require.NoError(t, err)
tmpdir := GetTempDir()
assert.Equal(t, "/var/tmp", tmpdir)

// test TMPDIR Environment
err = os.Setenv("TMPDIR", "/tmp/bogus")
require.NoError(t, err)
tmpdir = GetTempDir()
assert.Equal(t, tmpdir, "/tmp/bogus")
err = os.Unsetenv("TMPDIR")
require.NoError(t, err)

f, err := os.CreateTemp("", "containers.conf-")
require.NoError(t, err)
// close and remove the temporary file at the end of the program
defer f.Close()
defer os.Remove(f.Name())
data := []byte("[engine]\nimage_copy_tmp_dir=\"/mnt\"\n")
_, err = f.Write(data)
require.NoError(t, err)

err = os.Setenv("CONTAINERS_CONF", f.Name())
require.NoError(t, err)
// force config reset of default containers.conf
options := config.Options{
SetDefault: true,
}
_, err = config.New(&options)
require.NoError(t, err)
tmpdir = GetTempDir()
assert.Equal(t, "/mnt", tmpdir)

}

0 comments on commit 0ad0a8c

Please sign in to comment.