github.com/jzwlqx/containerd@v0.2.5/testutils/testutils.go (about)

     1  package testutils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // GetTestOutDir returns the output directory for testing and benchmark artifacts
    12  func GetTestOutDir() string {
    13  	out, _ := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
    14  	repoRoot := string(out)
    15  	prefix := filepath.Join(strings.TrimSpace(repoRoot), "output")
    16  	return prefix
    17  }
    18  
    19  var (
    20  	// ArchivesDir holds the location of the available rootfs
    21  	ArchivesDir = filepath.Join("test-artifacts", "archives")
    22  	// BundlesRoot holds the location where OCI Bundles are stored
    23  	BundlesRoot = filepath.Join("test-artifacts", "oci-bundles")
    24  	// OutputDirFormat holds the standard format used when creating a
    25  	// new test output directory
    26  	OutputDirFormat = filepath.Join("test-artifacts", "runs", "%s")
    27  	// RefOciSpecsPath holds the path to the generic OCI config
    28  	RefOciSpecsPath = filepath.Join(BundlesRoot, "config.json")
    29  	// StateDir holds the path to the directory used by the containerd
    30  	// started by tests
    31  	StateDir = "/run/containerd-bench-test"
    32  )
    33  
    34  // untarRootfs untars the given `source` tarPath into `destination/rootfs`
    35  func untarRootfs(source string, destination string) error {
    36  	rootfs := filepath.Join(destination, "rootfs")
    37  
    38  	if err := os.MkdirAll(rootfs, 0755); err != nil {
    39  		fmt.Println("untarRootfs os.MkdirAll failed with err %v", err)
    40  		return nil
    41  	}
    42  	tar := exec.Command("tar", "-C", rootfs, "-xf", source)
    43  	return tar.Run()
    44  }
    45  
    46  // GenerateReferenceSpecs generates a default OCI specs via `runc spec`
    47  func GenerateReferenceSpecs(destination string) error {
    48  	if _, err := os.Stat(filepath.Join(destination, "config.json")); err == nil {
    49  		return nil
    50  	}
    51  	specs := exec.Command("runc", "spec")
    52  	specs.Dir = destination
    53  	return specs.Run()
    54  }
    55  
    56  // CreateBundle generates a valid OCI bundle from the given rootfs
    57  func CreateBundle(source, name string) error {
    58  	bundlePath := filepath.Join(BundlesRoot, name)
    59  
    60  	if err := untarRootfs(filepath.Join(ArchivesDir, source+".tar"), bundlePath); err != nil {
    61  		return fmt.Errorf("Failed to untar %s.tar: %v", source, err)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  // CreateBusyboxBundle generates a bundle based on the busybox rootfs
    68  func CreateBusyboxBundle(name string) error {
    69  	return CreateBundle("busybox", name)
    70  }