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

     1  // +build remoteclient
     2  
     3  package integration
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"github.com/containers/libpod/pkg/rootless"
     9  	"io/ioutil"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"strconv"
    14  	"strings"
    15  	"syscall"
    16  	"time"
    17  
    18  	"github.com/onsi/ginkgo"
    19  )
    20  
    21  func SkipIfRemote() {
    22  	ginkgo.Skip("This function is not enabled for remote podman")
    23  }
    24  
    25  func SkipIfRootless() {
    26  	if os.Geteuid() != 0 {
    27  		ginkgo.Skip("This function is not enabled for rootless podman")
    28  	}
    29  }
    30  
    31  // Podman is the exec call to podman on the filesystem
    32  func (p *PodmanTestIntegration) Podman(args []string) *PodmanSessionIntegration {
    33  	podmanSession := p.PodmanBase(args, false, false)
    34  	return &PodmanSessionIntegration{podmanSession}
    35  }
    36  
    37  // PodmanExtraFiles is the exec call to podman on the filesystem and passes down extra files
    38  func (p *PodmanTestIntegration) PodmanExtraFiles(args []string, extraFiles []*os.File) *PodmanSessionIntegration {
    39  	podmanSession := p.PodmanAsUserBase(args, 0, 0, "", nil, false, false, nil, extraFiles)
    40  	return &PodmanSessionIntegration{podmanSession}
    41  }
    42  
    43  // PodmanNoCache calls podman with out adding the imagecache
    44  func (p *PodmanTestIntegration) PodmanNoCache(args []string) *PodmanSessionIntegration {
    45  	podmanSession := p.PodmanBase(args, false, true)
    46  	return &PodmanSessionIntegration{podmanSession}
    47  }
    48  
    49  // PodmanNoEvents calls the Podman command without an imagecache and without an
    50  // events backend. It is used mostly for caching and uncaching images.
    51  func (p *PodmanTestIntegration) PodmanNoEvents(args []string) *PodmanSessionIntegration {
    52  	podmanSession := p.PodmanBase(args, true, true)
    53  	return &PodmanSessionIntegration{podmanSession}
    54  }
    55  
    56  func (p *PodmanTestIntegration) setDefaultRegistriesConfigEnv() {
    57  	defaultFile := filepath.Join(INTEGRATION_ROOT, "test/registries.conf")
    58  	os.Setenv("REGISTRIES_CONFIG_PATH", defaultFile)
    59  }
    60  
    61  func (p *PodmanTestIntegration) setRegistriesConfigEnv(b []byte) {
    62  	outfile := filepath.Join(p.TempDir, "registries.conf")
    63  	os.Setenv("REGISTRIES_CONFIG_PATH", outfile)
    64  	ioutil.WriteFile(outfile, b, 0644)
    65  }
    66  
    67  func resetRegistriesConfigEnv() {
    68  	os.Setenv("REGISTRIES_CONFIG_PATH", "")
    69  }
    70  func PodmanTestCreate(tempDir string) *PodmanTestIntegration {
    71  	pti := PodmanTestCreateUtil(tempDir, true)
    72  	pti.StartVarlink()
    73  	return pti
    74  }
    75  
    76  func (p *PodmanTestIntegration) ResetVarlinkAddress() {
    77  	os.Unsetenv("PODMAN_VARLINK_ADDRESS")
    78  }
    79  
    80  func (p *PodmanTestIntegration) SetVarlinkAddress(addr string) {
    81  	os.Setenv("PODMAN_VARLINK_ADDRESS", addr)
    82  }
    83  
    84  func (p *PodmanTestIntegration) StartVarlink() {
    85  	if os.Geteuid() == 0 {
    86  		os.MkdirAll("/run/podman", 0755)
    87  	}
    88  	varlinkEndpoint := p.VarlinkEndpoint
    89  	p.SetVarlinkAddress(p.VarlinkEndpoint)
    90  
    91  	args := []string{"varlink", "--timeout", "0", varlinkEndpoint}
    92  	podmanOptions := getVarlinkOptions(p, args)
    93  	command := exec.Command(p.PodmanBinary, podmanOptions...)
    94  	fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
    95  	command.Start()
    96  	command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
    97  	p.VarlinkCommand = command
    98  	p.VarlinkSession = command.Process
    99  	p.DelayForVarlink()
   100  }
   101  
   102  func (p *PodmanTestIntegration) StopVarlink() {
   103  	var out bytes.Buffer
   104  	var pids []int
   105  	varlinkSession := p.VarlinkSession
   106  
   107  	if !rootless.IsRootless() {
   108  		if err := varlinkSession.Kill(); err != nil {
   109  			fmt.Fprintf(os.Stderr, "error on varlink stop-kill %q", err)
   110  		}
   111  		if _, err := varlinkSession.Wait(); err != nil {
   112  			fmt.Fprintf(os.Stderr, "error on varlink stop-wait %q", err)
   113  		}
   114  
   115  	} else {
   116  		p.ResetVarlinkAddress()
   117  		parentPid := fmt.Sprintf("%d", p.VarlinkSession.Pid)
   118  		pgrep := exec.Command("pgrep", "-P", parentPid)
   119  		fmt.Printf("running: pgrep %s\n", parentPid)
   120  		pgrep.Stdout = &out
   121  		err := pgrep.Run()
   122  		if err != nil {
   123  			fmt.Fprint(os.Stderr, "unable to find varlink pid")
   124  		}
   125  
   126  		for _, s := range strings.Split(out.String(), "\n") {
   127  			if len(s) == 0 {
   128  				continue
   129  			}
   130  			p, err := strconv.Atoi(s)
   131  			if err != nil {
   132  				fmt.Fprintf(os.Stderr, "unable to convert %s to int", s)
   133  			}
   134  			if p != 0 {
   135  				pids = append(pids, p)
   136  			}
   137  		}
   138  
   139  		pids = append(pids, p.VarlinkSession.Pid)
   140  		for _, pid := range pids {
   141  			syscall.Kill(pid, syscall.SIGKILL)
   142  		}
   143  	}
   144  	socket := strings.Split(p.VarlinkEndpoint, ":")[1]
   145  	if err := os.Remove(socket); err != nil {
   146  		fmt.Println(err)
   147  	}
   148  }
   149  
   150  //MakeOptions assembles all the podman main options
   151  func (p *PodmanTestIntegration) makeOptions(args []string, noEvents, noCache bool) []string {
   152  	return args
   153  }
   154  
   155  //MakeOptions assembles all the podman main options
   156  func getVarlinkOptions(p *PodmanTestIntegration, args []string) []string {
   157  	podmanOptions := strings.Split(fmt.Sprintf("--root %s --runroot %s --runtime %s --conmon %s --cni-config-dir %s --cgroup-manager %s",
   158  		p.CrioRoot, p.RunRoot, p.OCIRuntime, p.ConmonBinary, p.CNIConfigDir, p.CgroupManager), " ")
   159  	if os.Getenv("HOOK_OPTION") != "" {
   160  		podmanOptions = append(podmanOptions, os.Getenv("HOOK_OPTION"))
   161  	}
   162  	podmanOptions = append(podmanOptions, strings.Split(p.StorageOptions, " ")...)
   163  	podmanOptions = append(podmanOptions, args...)
   164  	return podmanOptions
   165  }
   166  
   167  func (p *PodmanTestIntegration) RestoreArtifactToCache(image string) error {
   168  	fmt.Printf("Restoring %s...\n", image)
   169  	dest := strings.Split(image, "/")
   170  	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
   171  	p.CrioRoot = p.ImageCacheDir
   172  	restore := p.PodmanNoEvents([]string{"load", "-q", "-i", destName})
   173  	restore.WaitWithDefaultTimeout()
   174  	return nil
   175  }
   176  
   177  // SeedImages restores all the artifacts into the main store for remote tests
   178  func (p *PodmanTestIntegration) SeedImages() error {
   179  	return p.RestoreAllArtifacts()
   180  }
   181  
   182  // RestoreArtifact puts the cached image into our test store
   183  func (p *PodmanTestIntegration) RestoreArtifact(image string) error {
   184  	fmt.Printf("Restoring %s...\n", image)
   185  	dest := strings.Split(image, "/")
   186  	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
   187  	args := []string{"load", "-q", "-i", destName}
   188  	podmanOptions := getVarlinkOptions(p, args)
   189  	command := exec.Command(p.PodmanBinary, podmanOptions...)
   190  	fmt.Printf("Running: %s %s\n", p.PodmanBinary, strings.Join(podmanOptions, " "))
   191  	command.Start()
   192  	command.Wait()
   193  	return nil
   194  }
   195  
   196  func (p *PodmanTestIntegration) DelayForVarlink() {
   197  	for i := 0; i < 5; i++ {
   198  		session := p.Podman([]string{"info"})
   199  		session.WaitWithDefaultTimeout()
   200  		if session.ExitCode() == 0 || i == 4 {
   201  			break
   202  		}
   203  		time.Sleep(1 * time.Second)
   204  	}
   205  }
   206  
   207  func populateCache(podman *PodmanTestIntegration) {}
   208  func removeCache()                                {}