Skip to content

Commit

Permalink
Try a temporary directory if the user cache fails
Browse files Browse the repository at this point in the history
Since commit 58c17f6 ("addr.Suggest should lock a file instead of
memory"), pkg/internal/testing/addr/manager.go’s init() function tries
to create a directory using either os.UserCacheDir() or os.TempDir(),
whichever succeeds first. In many build environments, $HOME is
non-empty but points to an unusable directory; in such cases,
os.UserCacheDir() returns an unusable directory, which causes init()
to panic.

This changes init() to first try os.UserCacheDir(), including creating
the desired directory; if that fails, the whole operation is tried
again with os.TempDir().

Fixes: #1799
Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Feb 10, 2022
1 parent 273e608 commit 8104aa8
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/internal/testing/addr/manager.go
Expand Up @@ -42,16 +42,30 @@ var (
)

func init() {
baseDir, err := os.UserCacheDir()
var err error
cacheDir, err = tryUserCacheDir()
if err != nil {
baseDir = os.TempDir()
// Either we didn't get a cache directory, or we can't use it
cacheDir, err = tryCacheInDir(os.TempDir())
}
cacheDir = filepath.Join(baseDir, "kubebuilder-envtest")
if err := os.MkdirAll(cacheDir, 0750); err != nil {
if err != nil {
panic(err)
}
}

func tryUserCacheDir() (string, error) {
baseDir, err := os.UserCacheDir()
if err != nil {
return "", err
}
return tryCacheInDir(baseDir)
}

func tryCacheInDir(dir string) (string, error) {
dir = filepath.Join(dir, "kubebuilder-envtest")
return dir, os.MkdirAll(dir, 0o750)
}

type portCache struct{}

func (c *portCache) add(port int) (bool, error) {
Expand Down

0 comments on commit 8104aa8

Please sign in to comment.