-
Notifications
You must be signed in to change notification settings - Fork 7
/
validate.go
87 lines (77 loc) · 2.03 KB
/
validate.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/opencontainers/specs"
)
var validateFlags = []cli.Flag{
cli.StringFlag{Name: "rootfs", Usage: "path to the rootfs containing 'cat'"},
cli.StringFlag{Name: "runtime", Usage: "path to the OCI runtime"},
}
var validateCommand = cli.Command{
Name: "validate",
Usage: "validate a OCI spec file",
Flags: validateFlags,
Action: func(context *cli.Context) {
specDir, err := ioutil.TempDir("", "oci_test")
if err != nil {
logrus.Fatal(err)
}
if err = os.MkdirAll(specDir, 0700); err != nil {
logrus.Fatal(err)
}
defer os.RemoveAll(specDir)
rootfs := context.String("rootfs")
if rootfs == "" {
logrus.Fatalf("Rootfs path shouldn't be empty")
}
runtime := context.String("runtime")
if runtime == "" {
logrus.Fatalf("runtime path shouldn't be empty")
}
spec, rspec := getDefaultTemplate()
if err != nil {
logrus.Fatal(err)
}
spec.Process.Args = []string{"cat", "/proc/self/status"}
spec.Root.Path = rootfs
cPath := filepath.Join(specDir, "config.json")
rPath := filepath.Join(specDir, "runtime.json")
data, err := json.MarshalIndent(&spec, "", "\t")
if err != nil {
logrus.Fatal(err)
}
if err := ioutil.WriteFile(cPath, data, 0666); err != nil {
logrus.Fatal(err)
}
rdata, err := json.MarshalIndent(&rspec, "", "\t")
if err != nil {
logrus.Fatal(err)
}
if err := ioutil.WriteFile(rPath, rdata, 0666); err != nil {
logrus.Fatal(err)
}
if err := testRuntime(runtime, specDir, spec, rspec); err != nil {
logrus.Fatal(err)
}
logrus.Infof("Test succeeded.")
},
}
func testRuntime(runtime string, specDir string, spec specs.LinuxSpec, rspec specs.LinuxRuntimeSpec) error {
logrus.Infof("Launcing runtime")
cmd := exec.Command(runtime, "start")
cmd.Dir = specDir
cmd.Stdin = os.Stdin
out, err := cmd.CombinedOutput()
logrus.Infof("Command done")
logrus.Infof(string(out))
if err != nil {
return err
}
return nil
}