2016-05-27 17:35:42 +00:00
|
|
|
package testutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// GetTestOutDir returns the output directory for testing and benchmark artifacts
|
2016-05-27 17:35:42 +00:00
|
|
|
func GetTestOutDir() string {
|
|
|
|
out, _ := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
|
|
|
|
repoRoot := string(out)
|
|
|
|
prefix := filepath.Join(strings.TrimSpace(repoRoot), "output")
|
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-06-03 22:00:49 +00:00
|
|
|
// ArchivesDir holds the location of the available rootfs
|
|
|
|
ArchivesDir = filepath.Join("test-artifacts", "archives")
|
|
|
|
// BundlesRoot holds the location where OCI Bundles are stored
|
|
|
|
BundlesRoot = filepath.Join("test-artifacts", "oci-bundles")
|
|
|
|
// OutputDirFormat holds the standard format used when creating a
|
|
|
|
// new test output directory
|
2016-05-27 17:35:42 +00:00
|
|
|
OutputDirFormat = filepath.Join("test-artifacts", "runs", "%s")
|
2016-06-03 22:00:49 +00:00
|
|
|
// RefOciSpecsPath holds the path to the generic OCI config
|
2016-05-27 17:35:42 +00:00
|
|
|
RefOciSpecsPath = filepath.Join(BundlesRoot, "config.json")
|
2016-06-03 22:00:49 +00:00
|
|
|
// StateDir holds the path to the directory used by the containerd
|
|
|
|
// started by tests
|
|
|
|
StateDir = "/run/containerd-bench-test"
|
2016-05-27 17:35:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// untarRootfs untars the given `source` tarPath into `destination/rootfs`
|
|
|
|
func untarRootfs(source string, destination string) error {
|
|
|
|
rootfs := filepath.Join(destination, "rootfs")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(rootfs, 0755); err != nil {
|
|
|
|
fmt.Println("untarRootfs os.MkdirAll failed with err %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
tar := exec.Command("tar", "-C", rootfs, "-xf", source)
|
|
|
|
return tar.Run()
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// GenerateReferenceSpecs generates a default OCI specs via `runc spec`
|
2016-05-27 17:35:42 +00:00
|
|
|
func GenerateReferenceSpecs(destination string) error {
|
|
|
|
if _, err := os.Stat(filepath.Join(destination, "config.json")); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
specs := exec.Command("runc", "spec")
|
|
|
|
specs.Dir = destination
|
|
|
|
return specs.Run()
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// CreateBundle generates a valid OCI bundle from the given rootfs
|
2016-05-27 17:35:42 +00:00
|
|
|
func CreateBundle(source, name string) error {
|
|
|
|
bundlePath := filepath.Join(BundlesRoot, name)
|
|
|
|
|
|
|
|
if err := untarRootfs(filepath.Join(ArchivesDir, source+".tar"), bundlePath); err != nil {
|
|
|
|
return fmt.Errorf("Failed to untar %s.tar: %v", source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// CreateBusyboxBundle generates a bundle based on the busybox rootfs
|
2016-05-27 17:35:42 +00:00
|
|
|
func CreateBusyboxBundle(name string) error {
|
|
|
|
return CreateBundle("busybox", name)
|
|
|
|
}
|