Image caches: allow overriding cache dir

Images were being cached in /tmp, with no option to
override. Now $PODMAN_TEST_IMAGE_CACHE_DIR can be
used to point to a user-preferred location. If unset,
try $TMPDIR before settling on /tmp.

Also: refactor the logic for determining the tarball name.
Also: include registry name in tarball name.
Also: clean up unused/unnecessary code
Also: do not echo "Restoring..." if we're not actually restoring.

Signed-off-by: Ed Santiago <santiago@redhat.com>
This commit is contained in:
Ed Santiago 2021-12-01 12:47:30 -07:00
parent 5ac66e2aab
commit 3ac1b9bc0f
3 changed files with 42 additions and 26 deletions

View File

@ -308,15 +308,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 +340,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 +752,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.Root = 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
} }

View File

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