forked from adamlaska/containerd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestutils.go
More file actions
70 lines (60 loc) · 2.19 KB
/
testutils.go
File metadata and controls
70 lines (60 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package testutils
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// GetTestOutDir returns the output directory for testing and benchmark artifacts
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 (
// 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
OutputDirFormat = filepath.Join("test-artifacts", "runs", "%s")
// RefOciSpecsPath holds the path to the generic OCI config
RefOciSpecsPath = filepath.Join(BundlesRoot, "config.json")
// StateDir holds the path to the directory used by the containerd
// started by tests
StateDir = "/run/containerd-bench-test"
)
// 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.Printf("untarRootfs os.MkdirAll failed with err %v", err)
return nil
}
tar := exec.Command("tar", "-C", rootfs, "-xf", source)
return tar.Run()
}
// GenerateReferenceSpecs generates a default OCI specs via `runc spec`
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()
}
// CreateBundle generates a valid OCI bundle from the given rootfs
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
}
// CreateBusyboxBundle generates a bundle based on the busybox rootfs
func CreateBusyboxBundle(name string) error {
return CreateBundle("busybox", name)
}