Merge pull request #22362 from tnk4on/e2e-macos-tmpdir

Change tmpDir when running e2e localmachine test on macOS
This commit is contained in:
openshift-merge-bot[bot]
2024-05-16 18:11:55 +00:00
committed by GitHub
2 changed files with 52 additions and 5 deletions

View File

@ -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.

View File

@ -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
}