github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/test/e2e/libpod_suite_test.go (about)

     1  // +build !remoteclient
     2  
     3  package integration
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/onsi/ginkgo"
    13  )
    14  
    15  func SkipIfRemote() {
    16  }
    17  
    18  func SkipIfRootless() {
    19  	if os.Geteuid() != 0 {
    20  		ginkgo.Skip("This function is not enabled for rootless podman")
    21  	}
    22  }
    23  
    24  // Podman is the exec call to podman on the filesystem
    25  func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
    26  	podmanSession := p.PodmanBase(args, false, false)
    27  	return &PodmanSessionIntegration{podmanSession}
    28  }
    29  
    30  // PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
    31  func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
    32  	podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, extraFiles)
    33  	return &PodmanSessionIntegration{podmanSession}
    34  }
    35  
    36  // PodmanNoCache calls the podman command with no configured imagecache
    37  func (p *PodmanTestIntegration) PodmanNoCache(args []string) *PodmanSessionIntegration {
    38  	podmanSession := p.PodmanBase(args, false, true)
    39  	return &PodmanSessionIntegration{podmanSession}
    40  }
    41  
    42  // PodmanNoEvents calls the Podman command without an imagecache and without an
    43  // events backend. It is used mostly for caching and uncaching images.
    44  func (p *PodmanTestIntegration) PodmanNoEvents(args []string) *PodmanSessionIntegration {
    45  	podmanSession := p.PodmanBase(args, true, true)
    46  	return &PodmanSessionIntegration{podmanSession}
    47  }
    48  
    49  // PodmanAsUser is the exec call to podman on the filesystem with the specified uid/gid and environment
    50  func (p *PodmanTestIntegration) PodmanAsUser(args []string, uid, gid uint32, cwd string, env []string) *PodmanSessionIntegration {
    51  	podmanSession := p.PodmanAsUserBase(args, uid, gid, cwd, env, false, false, nil)
    52  	return &PodmanSessionIntegration{podmanSession}
    53  }
    54  
    55  func (p *PodmanTestIntegration) setDefaultRegistriesConfigEnv() {
    56  	defaultFile := filepath.Join(INTEGRATION_ROOT, "test/registries.conf")
    57  	os.Setenv("REGISTRIES_CONFIG_PATH", defaultFile)
    58  }
    59  
    60  func (p *PodmanTestIntegration) setRegistriesConfigEnv(b []byte) {
    61  	outfile := filepath.Join(p.TempDir, "registries.conf")
    62  	os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
    63  	ioutil.WriteFile(outfile, b, 0644)
    64  }
    65  
    66  func resetRegistriesConfigEnv() {
    67  	os.Setenv("REGISTRIES_CONFIG_PATH", "")
    68  }
    69  
    70  func PodmanTestCreate(tempDir string) *PodmanTestIntegration {
    71  	return PodmanTestCreateUtil(tempDir, false)
    72  }
    73  
    74  // MakeOptions assembles all the podman main options
    75  func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache bool) []string {
    76  	var debug string
    77  	if _, ok := os.LookupEnv("DEBUG"); ok {
    78  		debug = "--log-level=debug --syslog=true "
    79  	}
    80  
    81  	eventsType := "file"
    82  	if noEvents {
    83  		eventsType = "none"
    84  	}
    85  
    86  	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",
    87  		debug, p.CrioRoot, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager, p.TmpDir, eventsType), " ")
    88  	if os.Getenv("HOOK_OPTION") != "" {
    89  		podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION"))
    90  	}
    91  
    92  	podmanOptions = append(podmanOptions, strings.Split(p.StorageOptions, " ")...)
    93  	if !noCache {
    94  		cacheOptions := []string{"--storage-opt",
    95  			fmt.Sprintf("%s.imagestore=%s", p.PodmanTest.ImageCacheFS, p.PodmanTest.ImageCacheDir)}
    96  		podmanOptions = append(cacheOptions, podmanOptions...)
    97  	}
    98  	podmanOptions = append(podmanOptions, args...)
    99  	return podmanOptions
   100  }
   101  
   102  // RestoreArtifact puts the cached image into our test store
   103  func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
   104  	fmt.Printf("Restoring %s...\n", image)
   105  	dest := strings.Split(image, "/")
   106  	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
   107  	restore := p.PodmanNoEvents([]string{"load", "-q", "-i", destName})
   108  	restore.Wait(90)
   109  	return nil
   110  }
   111  
   112  // RestoreArtifactToCache populates the imagecache from tarballs that were cached earlier
   113  func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error {
   114  	fmt.Printf("Restoring %s...\n", image)
   115  	dest := strings.Split(image, "/")
   116  	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
   117  
   118  	p.CrioRoot = p.ImageCacheDir
   119  	restore := p.PodmanNoEvents([]string{"load", "-q", "-i", destName})
   120  	restore.WaitWithDefaultTimeout()
   121  	return nil
   122  }
   123  
   124  func (p *PodmanTestIntegration) StopVarlink()     {}
   125  func (p *PodmanTestIntegration) DelayForVarlink() {}
   126  
   127  func populateCache(podman *PodmanTestIntegration) {
   128  	for _, image := range CACHE_IMAGES {
   129  		podman.RestoreArtifactToCache(image)
   130  	}
   131  	// logformatter uses this to recognize the first test
   132  	fmt.Printf("-----------------------------\n")
   133  }
   134  
   135  func removeCache() {
   136  	// Remove cache dirs
   137  	if err := os.RemoveAll(ImageCacheDir); err != nil {
   138  		fmt.Printf("%q\n", err)
   139  	}
   140  }
   141  
   142  // SeedImages is a no-op for localized testing
   143  func (p *PodmanTestIntegration) SeedImages() error {
   144  	return nil
   145  }
   146  
   147  // We don't support running Varlink when local
   148  func (p *PodmanTestIntegration) StartVarlink() {
   149  }