Skip to content

Commit

Permalink
Merge pull request #22362 from tnk4on/e2e-macos-tmpdir
Browse files Browse the repository at this point in the history
Change tmpDir when running e2e localmachine test on macOS
  • Loading branch information
openshift-merge-bot[bot] authored May 16, 2024
2 parents f7a3046 + 590fec7 commit 1afeb13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pkg/machine/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
49 changes: 47 additions & 2 deletions pkg/machine/e2e/machine_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
}
}

Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
}

1 comment on commit 1afeb13

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.