diff --git a/pkg/machine/e2e/README.md b/pkg/machine/e2e/README.md index 4b737b686b..48e12612f0 100644 --- a/pkg/machine/e2e/README.md +++ b/pkg/machine/e2e/README.md @@ -9,7 +9,7 @@ Note: you must not have any machines defined before running tests ## Microsoft Windows -### HyperV +### Hyper-V 1. Open a powershell as admin 1. $env:CONTAINERS_MACHINE_PROVIDER="hyperv" @@ -28,9 +28,11 @@ Note: To run specific test files, add the test files to the end of the winmake c `./winmake localmachine "basic_test.go start_test.go"` -## MacOS +## macOS ### Apple Hypervisor 1. `make podman-remote` -1. `make localmachine` (Add `FOCUS_FILE=basic_test.go` to only run basic test) +1. `make localmachine` (Add `FOCUS_FILE=basic_test.go` to only run basic test. Or add `FOCUS="simple init with start"` to only run one test case) + +Note: On macOS, an error will occur if the path length of `$TMPDIR` is longer than 22 characters. Please set the appropriate path to `$TMPDIR`. Also, if `$TMPDIR` is empty, `/private/tmp` will be set. diff --git a/pkg/machine/e2e/machine_test.go b/pkg/machine/e2e/machine_test.go index d8962660c0..d83d0d50e2 100644 --- a/pkg/machine/e2e/machine_test.go +++ b/pkg/machine/e2e/machine_test.go @@ -1,13 +1,16 @@ package e2e_test import ( + "errors" "fmt" "io" "os" "path/filepath" "runtime" + "strings" "testing" + "github.com/containers/common/pkg/config" "github.com/containers/podman/v5/pkg/machine/compression" "github.com/containers/podman/v5/pkg/machine/define" "github.com/containers/podman/v5/pkg/machine/provider" @@ -33,7 +36,11 @@ var ( func init() { if value, ok := os.LookupEnv("TMPDIR"); ok { - tmpDir = value + var err error + tmpDir, err = setTmpDir(value) + if err != nil { + fmt.Printf("failed to set TMPDIR: %q\n", err) + } } } @@ -69,7 +76,14 @@ var _ = SynchronizedAfterSuite(func() {}, func() {}) func setup() (string, *machineTestBuilder) { // Set TMPDIR if this needs a new directory - homeDir, err := os.MkdirTemp("", "podman_test") + if value, ok := os.LookupEnv("TMPDIR"); ok { + var err error + tmpDir, err = setTmpDir(value) + if err != nil { + Fail(fmt.Sprintf("failed to set TMPDIR: %q", err)) + } + } + homeDir, err := os.MkdirTemp(tmpDir, "podman_test") if err != nil { Fail(fmt.Sprintf("failed to create home directory: %q", err)) } @@ -169,3 +183,34 @@ func copySparse(dst io.WriteSeeker, src io.Reader) (retErr error) { _, err := io.Copy(spWriter, src) return err } + +func setTmpDir(value string) (string, error) { + switch { + case runtime.GOOS != "darwin": + tmpDir = value + case len(value) >= 22: + return "", errors.New(value + " path length should be less than 22 characters") + case value == "": + return "", errors.New("TMPDIR cannot be empty. Set to directory mounted on podman machine (e.g. /private/tmp)") + default: + cfg, err := config.Default() + if err != nil { + return "", err + } + volumes := cfg.Machine.Volumes.Get() + containsPath := false + for _, volume := range volumes { + parts := strings.Split(volume, ":") + hostPath := parts[0] + if strings.Contains(value, hostPath) { + containsPath = true + break + } + } + if !containsPath { + return "", fmt.Errorf("%s cannot be used. Change to directory mounted on podman machine (e.g. /private/tmp)", value) + } + tmpDir = value + } + return tmpDir, nil +}