Files
podman/pkg/machine/e2e/machine_test.go
Brent Baude fd1d951262 Move ignition functions into Containerfiles
We used to use ignition to perform any customization required for podman
machine because our input was a generic FCOS image.  Now that we are
building our own images, some of this customization can be migrated to
the Containerfile itself and be less of a burden in our code at boot up.

At the time of this PR, the Containerfile can be found at
https://github.com/baude/podman-machine-images/tree/main.  It is only
present for a so-called daily image.  There is little liklihood that
this would the final location for the Containerfile so consider it a
working version only.

Split WSL and rest apart in the e2e tests so we no longer ppull the
generic FCOS image for testing.

Note: the change to the pull image name is so PRs are not immediately
broken that are already in the queue.

[NO NEW TESTS REQUIRED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
2024-02-28 16:05:50 -06:00

187 lines
4.9 KiB
Go

package e2e_test
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/containers/podman/v5/pkg/machine/compression"
"github.com/containers/podman/v5/pkg/machine/define"
"github.com/containers/podman/v5/pkg/machine/provider"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
"github.com/containers/podman/v5/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
const (
defaultDiskSize uint = 11
)
var (
tmpDir = os.TempDir()
fqImageName string
suiteImageName string
)
func init() {
if value, ok := os.LookupEnv("TMPDIR"); ok {
tmpDir = value
}
}
// TestLibpod ginkgo master function
func TestMachine(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Podman Machine tests")
}
var testProvider vmconfigs.VMProvider
var _ = BeforeSuite(func() {
var (
err error
pullError error
)
testProvider, err = provider.Get()
if err != nil {
Fail("unable to create testProvider")
}
if testProvider.VMType() == define.WSLVirt {
pullError = pullWSLDisk()
} else {
pullError = pullOCITestDisk(tmpDir, testProvider.VMType())
}
if pullError != nil {
Fail(fmt.Sprintf("failed to pull wsl disk: %q", pullError))
}
})
var _ = SynchronizedAfterSuite(func() {}, func() {})
func setup() (string, *machineTestBuilder) {
// Set TMPDIR if this needs a new directory
homeDir, err := os.MkdirTemp("", "podman_test")
if err != nil {
Fail(fmt.Sprintf("failed to create home directory: %q", err))
}
if err := os.MkdirAll(filepath.Join(homeDir, ".ssh"), 0700); err != nil {
Fail(fmt.Sprintf("failed to create ssh dir: %q", err))
}
sshConfig, err := os.Create(filepath.Join(homeDir, ".ssh", "config"))
if err != nil {
Fail(fmt.Sprintf("failed to create ssh config: %q", err))
}
if _, err := sshConfig.WriteString("IdentitiesOnly=yes"); err != nil {
Fail(fmt.Sprintf("failed to write ssh config: %q", err))
}
if err := sshConfig.Close(); err != nil {
Fail(fmt.Sprintf("unable to close ssh config file descriptor: %q", err))
}
if err := os.Setenv("HOME", homeDir); err != nil {
Fail("failed to set home dir")
}
if runtime.GOOS == "windows" {
if err := os.Setenv("USERPROFILE", homeDir); err != nil {
Fail("unable to set home dir on windows")
}
}
if err := os.Setenv("XDG_RUNTIME_DIR", homeDir); err != nil {
Fail("failed to set xdg_runtime dir")
}
if err := os.Unsetenv("SSH_AUTH_SOCK"); err != nil {
Fail("unable to unset SSH_AUTH_SOCK")
}
mb, err := newMB()
if err != nil {
Fail(fmt.Sprintf("failed to create machine test: %q", err))
}
src, err := os.Open(fqImageName)
if err != nil {
Fail(fmt.Sprintf("failed to open file %s: %q", fqImageName, err))
}
defer func() {
if err := src.Close(); err != nil {
Fail(fmt.Sprintf("failed to close src reader %q: %q", src.Name(), err))
}
}()
mb.imagePath = filepath.Join(homeDir, suiteImageName)
dest, err := os.Create(mb.imagePath)
if err != nil {
Fail(fmt.Sprintf("failed to create file %s: %q", mb.imagePath, err))
}
defer func() {
closeErr := dest.Close()
if err != nil || !errors.Is(closeErr, fs.ErrClosed) {
fmt.Printf("failed to close destination file %q: %q\n", dest.Name(), err)
}
}()
fmt.Printf("--> copying %q to %q/n", src.Name(), dest.Name())
if runtime.GOOS != "darwin" {
if _, err := io.Copy(dest, src); err != nil {
Fail(fmt.Sprintf("failed to copy %ss to %s: %q", fqImageName, mb.imagePath, err))
}
} else {
if err := copySparse(dest, src); err != nil {
Fail(fmt.Sprintf("failed to copy %q to %q: %q", src.Name(), dest.Name(), err))
}
}
return homeDir, mb
}
func teardown(origHomeDir string, testDir string, mb *machineTestBuilder) {
r := new(rmMachine)
for _, name := range mb.names {
if _, err := mb.setName(name).setCmd(r.withForce()).run(); err != nil {
GinkgoWriter.Printf("error occurred rm'ing machine: %q\n", err)
}
}
if err := utils.GuardedRemoveAll(testDir); err != nil {
Fail(fmt.Sprintf("failed to remove test dir: %q", err))
}
// this needs to be last in teardown
if err := os.Setenv("HOME", origHomeDir); err != nil {
Fail("failed to set home dir")
}
if runtime.GOOS == "windows" {
if err := os.Setenv("USERPROFILE", origHomeDir); err != nil {
Fail("failed to set windows home dir back to original")
}
}
}
// copySparseFile is a helper method for tests only. copies a file sparsely
// between two string inputs
func copySparseFile(src, dst string) error { //nolint:unused
dstWriter, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return err
}
dstWriter.Close()
fSrc, err := os.Open(src)
if err != nil {
return err
}
defer fSrc.Close()
return copySparse(dstWriter, fSrc)
}
// copySparse is a helper method for tests only; caller is responsible for closures
func copySparse(dst compression.WriteSeekCloser, src io.Reader) error {
spWriter := compression.NewSparseWriter(dst)
defer spWriter.Close()
_, err := io.Copy(spWriter, src)
return err
}