Merge pull request #12471 from edsantiago/e2e_tmpdir_cleanup

e2e tmpdir cleanup
This commit is contained in:
OpenShift Merge Robot
2021-12-02 14:28:46 +01:00
committed by GitHub
4 changed files with 50 additions and 45 deletions

View File

@ -36,7 +36,6 @@ var (
PODMAN_BINARY string //nolint:golint,stylecheck PODMAN_BINARY string //nolint:golint,stylecheck
INTEGRATION_ROOT string //nolint:golint,stylecheck INTEGRATION_ROOT string //nolint:golint,stylecheck
CGROUP_MANAGER = "systemd" //nolint:golint,stylecheck CGROUP_MANAGER = "systemd" //nolint:golint,stylecheck
ARTIFACT_DIR = "/tmp/.artifacts" //nolint:golint,stylecheck
RESTORE_IMAGES = []string{ALPINE, BB, nginx} //nolint:golint,stylecheck RESTORE_IMAGES = []string{ALPINE, BB, nginx} //nolint:golint,stylecheck
defaultWaitTimeout = 90 defaultWaitTimeout = 90
CGROUPSV2, _ = cgroups.IsCgroup2UnifiedMode() //nolint:golint,stylecheck CGROUPSV2, _ = cgroups.IsCgroup2UnifiedMode() //nolint:golint,stylecheck
@ -46,7 +45,7 @@ var (
type PodmanTestIntegration struct { type PodmanTestIntegration struct {
PodmanTest PodmanTest
ConmonBinary string ConmonBinary string
CrioRoot string Root string
CNIConfigDir string CNIConfigDir string
OCIRuntime string OCIRuntime string
RunRoot string RunRoot string
@ -111,13 +110,6 @@ var _ = SynchronizedBeforeSuite(func() []byte {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
INTEGRATION_ROOT = filepath.Join(cwd, "../../") INTEGRATION_ROOT = filepath.Join(cwd, "../../")
podman := PodmanTestSetup("/tmp") podman := PodmanTestSetup("/tmp")
podman.ArtifactPath = ARTIFACT_DIR
if _, err := os.Stat(ARTIFACT_DIR); os.IsNotExist(err) {
if err = os.Mkdir(ARTIFACT_DIR, 0777); err != nil {
fmt.Printf("%q\n", err)
os.Exit(1)
}
}
// Pull cirros but don't put it into the cache // Pull cirros but don't put it into the cache
pullImages := []string{cirros, fedoraToolbox, volumeTest} pullImages := []string{cirros, fedoraToolbox, volumeTest}
@ -130,7 +122,7 @@ var _ = SynchronizedBeforeSuite(func() []byte {
fmt.Printf("%q\n", err) fmt.Printf("%q\n", err)
os.Exit(1) os.Exit(1)
} }
podman.CrioRoot = ImageCacheDir podman.Root = ImageCacheDir
// If running localized tests, the cache dir is created and populated. if the // If running localized tests, the cache dir is created and populated. if the
// tests are remote, this is a no-op // tests are remote, this is a no-op
populateCache(podman) populateCache(podman)
@ -170,7 +162,6 @@ var _ = SynchronizedBeforeSuite(func() []byte {
func (p *PodmanTestIntegration) Setup() { func (p *PodmanTestIntegration) Setup() {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
INTEGRATION_ROOT = filepath.Join(cwd, "../../") INTEGRATION_ROOT = filepath.Join(cwd, "../../")
p.ArtifactPath = ARTIFACT_DIR
} }
var _ = SynchronizedAfterSuite(func() {}, var _ = SynchronizedAfterSuite(func() {},
@ -181,14 +172,14 @@ var _ = SynchronizedAfterSuite(func() {},
fmt.Printf("%s\t\t%f\n", result.name, result.length) fmt.Printf("%s\t\t%f\n", result.name, result.length)
} }
// previous crio-run // previous runroot
tempdir, err := CreateTempDirInTempDir() tempdir, err := CreateTempDirInTempDir()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
podmanTest := PodmanTestCreate(tempdir) podmanTest := PodmanTestCreate(tempdir)
if err := os.RemoveAll(podmanTest.CrioRoot); err != nil { if err := os.RemoveAll(podmanTest.Root); err != nil {
fmt.Printf("%q\n", err) fmt.Printf("%q\n", err)
} }
@ -265,18 +256,17 @@ func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {
PodmanTest: PodmanTest{ PodmanTest: PodmanTest{
PodmanBinary: podmanBinary, PodmanBinary: podmanBinary,
RemotePodmanBinary: podmanRemoteBinary, RemotePodmanBinary: podmanRemoteBinary,
ArtifactPath: ARTIFACT_DIR,
TempDir: tempDir, TempDir: tempDir,
RemoteTest: remote, RemoteTest: remote,
ImageCacheFS: storageFs, ImageCacheFS: storageFs,
ImageCacheDir: ImageCacheDir, ImageCacheDir: ImageCacheDir,
}, },
ConmonBinary: conmonBinary, ConmonBinary: conmonBinary,
CrioRoot: filepath.Join(tempDir, "crio"), Root: filepath.Join(tempDir, "root"),
TmpDir: tempDir, TmpDir: tempDir,
CNIConfigDir: CNIConfigDir, CNIConfigDir: CNIConfigDir,
OCIRuntime: ociRuntime, OCIRuntime: ociRuntime,
RunRoot: filepath.Join(tempDir, "crio-run"), RunRoot: filepath.Join(tempDir, "runroot"),
StorageOptions: storageOptions, StorageOptions: storageOptions,
SignaturePolicyPath: filepath.Join(INTEGRATION_ROOT, "test/policy.json"), SignaturePolicyPath: filepath.Join(INTEGRATION_ROOT, "test/policy.json"),
CgroupManager: cgroupManager, CgroupManager: cgroupManager,
@ -308,15 +298,29 @@ func (p PodmanTestIntegration) AddImageToRWStore(image string) {
} }
} }
// createArtifact creates a cached image in the artifact dir func imageTarPath(image string) string {
cacheDir := os.Getenv("PODMAN_TEST_IMAGE_CACHE_DIR")
if cacheDir == "" {
cacheDir = os.Getenv("TMPDIR")
if cacheDir == "" {
cacheDir = "/tmp"
}
}
// e.g., registry.com/fubar:latest -> registry.com-fubar-latest.tar
imageCacheName := strings.Replace(strings.Replace(image, ":", "-", -1), "/", "-", -1) + ".tar"
return filepath.Join(cacheDir, imageCacheName)
}
// createArtifact creates a cached image tarball in a local directory
func (p *PodmanTestIntegration) createArtifact(image string) { func (p *PodmanTestIntegration) createArtifact(image string) {
if os.Getenv("NO_TEST_CACHE") != "" { if os.Getenv("NO_TEST_CACHE") != "" {
return return
} }
dest := strings.Split(image, "/") destName := imageTarPath(image)
destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
fmt.Printf("Caching %s at %s...\n", image, destName)
if _, err := os.Stat(destName); os.IsNotExist(err) { if _, err := os.Stat(destName); os.IsNotExist(err) {
fmt.Printf("Caching %s at %s...\n", image, destName)
pull := p.PodmanNoCache([]string{"pull", image}) pull := p.PodmanNoCache([]string{"pull", image})
pull.Wait(440) pull.Wait(440)
Expect(pull).Should(Exit(0)) Expect(pull).Should(Exit(0))
@ -326,7 +330,7 @@ func (p *PodmanTestIntegration) createArtifact(image string) {
Expect(save).Should(Exit(0)) Expect(save).Should(Exit(0))
fmt.Printf("\n") fmt.Printf("\n")
} else { } else {
fmt.Printf(" already exists.\n") fmt.Printf("[image already cached: %s]\n", destName)
} }
} }
@ -738,12 +742,13 @@ func (p *PodmanTestIntegration) RestartRemoteService() {
// RestoreArtifactToCache populates the imagecache from tarballs that were cached earlier // RestoreArtifactToCache populates the imagecache from tarballs that were cached earlier
func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error { func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error {
fmt.Printf("Restoring %s...\n", image) tarball := imageTarPath(image)
dest := strings.Split(image, "/") if _, err := os.Stat(tarball); err == nil {
destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1)) fmt.Printf("Restoring %s...\n", image)
p.CrioRoot = p.ImageCacheDir p.Root = p.ImageCacheDir
restore := p.PodmanNoEvents([]string{"load", "-q", "-i", destName}) restore := p.PodmanNoEvents([]string{"load", "-q", "-i", tarball})
restore.WaitWithDefaultTimeout() restore.WaitWithDefaultTimeout()
}
return nil return nil
} }
@ -795,7 +800,7 @@ func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache boo
} }
podmanOptions := strings.Split(fmt.Sprintf("%s--root %s --runroot %s --runtime %s --conmon %s --cni-config-dir %s --cgroup-manager %s --tmpdir %s --events-backend %s", podmanOptions := strings.Split(fmt.Sprintf("%s--root %s --runroot %s --runtime %s --conmon %s --cni-config-dir %s --cgroup-manager %s --tmpdir %s --events-backend %s",
debug, p.CrioRoot, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager, p.TmpDir, eventsType), " ") debug, p.Root, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager, p.TmpDir, eventsType), " ")
if os.Getenv("HOOK_OPTION") != "" { if os.Getenv("HOOK_OPTION") != "" {
podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION")) podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION"))
} }

View File

@ -148,7 +148,7 @@ func (p *PodmanTestIntegration) StopRemoteService() {
//MakeOptions assembles all the podman main options //MakeOptions assembles all the podman main options
func getRemoteOptions(p *PodmanTestIntegration, args []string) []string { func getRemoteOptions(p *PodmanTestIntegration, args []string) []string {
podmanOptions := strings.Split(fmt.Sprintf("--root %s --runroot %s --runtime %s --conmon %s --cni-config-dir %s --cgroup-manager %s", podmanOptions := strings.Split(fmt.Sprintf("--root %s --runroot %s --runtime %s --conmon %s --cni-config-dir %s --cgroup-manager %s",
p.CrioRoot, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager), " ") p.Root, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager), " ")
if os.Getenv("HOOK_OPTION") != "" { if os.Getenv("HOOK_OPTION") != "" {
podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION")) podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION"))
} }
@ -164,15 +164,16 @@ func (p *PodmanTestIntegration) SeedImages() error {
// RestoreArtifact puts the cached image into our test store // RestoreArtifact puts the cached image into our test store
func (p *PodmanTestIntegration) RestoreArtifact(image string) error { func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
fmt.Printf("Restoring %s...\n", image) tarball := imageTarPath(image)
dest := strings.Split(image, "/") if _, err := os.Stat(tarball); err == nil {
destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1)) fmt.Printf("Restoring %s...\n", image)
args := []string{"load", "-q", "-i", destName} args := []string{"load", "-q", "-i", tarball}
podmanOptions := getRemoteOptions(p, args) podmanOptions := getRemoteOptions(p, args)
command := exec.Command(p.PodmanBinary, podmanOptions...) command := exec.Command(p.PodmanBinary, podmanOptions...)
fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " ")) fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
command.Start() command.Start()
command.Wait() command.Wait()
}
return nil return nil
} }

View File

@ -7,7 +7,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/containers/podman/v3/pkg/rootless" "github.com/containers/podman/v3/pkg/rootless"
) )
@ -59,11 +58,12 @@ func PodmanTestCreate(tempDir string) *PodmanTestIntegration {
// RestoreArtifact puts the cached image into our test store // RestoreArtifact puts the cached image into our test store
func (p *PodmanTestIntegration) RestoreArtifact(image string) error { func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
fmt.Printf("Restoring %s...\n", image) tarball := imageTarPath(image)
dest := strings.Split(image, "/") if _, err := os.Stat(tarball); err == nil {
destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1)) fmt.Printf("Restoring %s...\n", image)
restore := p.PodmanNoEvents([]string{"load", "-q", "-i", destName}) restore := p.PodmanNoEvents([]string{"load", "-q", "-i", tarball})
restore.Wait(90) restore.Wait(90)
}
return nil return nil
} }

View File

@ -36,7 +36,6 @@ type PodmanTestCommon interface {
type PodmanTest struct { type PodmanTest struct {
PodmanMakeOptions func(args []string, noEvents, noCache bool) []string PodmanMakeOptions func(args []string, noEvents, noCache bool) []string
PodmanBinary string PodmanBinary string
ArtifactPath string
TempDir string TempDir string
RemoteTest bool RemoteTest bool
RemotePodmanBinary string RemotePodmanBinary string