Skip to content

Commit 28cb369

Browse files
committed
validation: Use prove(1) as a TAP harness
Capture stdout and stderr from create invocations, because we don't want to pollute the TAP output with things like runc's: Incorrect Usage. ... (which is for some reason printed to stdout) or: runc: "create" requires exactly 1 argument(s) which is printed to stderr. Instead, show the captured stderr as a diagnostic, and hide the stdout completely. Unless stderr is empty, in which case show stdout in case it contains something useful. Most of these tests are broken because we aren't collecting the container exit code or post-start stdout. But the tests haven't been doing that since the create/start split in 15577bd (add runtime struct; add create test, 2017-08-24, #447) anyway [1]. This commit just makes that more obvious. The patsubst and wildcard Makefile syntax is documented in [2]. The $(VALIDATION_TESTS) rule uses the static pattern rule syntax [3]. [1]: #447 (comment) [2]: https://www.gnu.org/software/make/manual/html_node/Wildcard-Function.html [3]: https://www.gnu.org/software/make/manual/html_node/Static-Usage.html#Static-Usage Signed-off-by: W. Trevor King <[email protected]>
1 parent 1c2dca0 commit 28cb369

23 files changed

+551
-330
lines changed

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ BUILDTAGS=
55
RUNTIME ?= runc
66
COMMIT=$(shell git rev-parse HEAD 2> /dev/null || true)
77
VERSION := ${shell cat ./VERSION}
8+
VALIDATION_TESTS ?= $(patsubst %.go,%.t,$(wildcard validation/*.go))
89

910
all: tool runtimetest
1011

@@ -35,10 +36,14 @@ uninstall:
3536
rm -f $(PREFIX)/share/bash-completion/completions/oci-runtime-tool
3637

3738
clean:
38-
rm -f oci-runtime-tool runtimetest *.1
39+
rm -f oci-runtime-tool runtimetest *.1 $(VALIDATION_TESTS)
3940

40-
localvalidation: runtimetest
41-
RUNTIME=$(RUNTIME) go test -tags "$(BUILDTAGS)" ${TESTFLAGS} -v github.com/opencontainers/runtime-tools/validation
41+
localvalidation: runtimetest $(VALIDATION_TESTS)
42+
RUNTIME=$(RUNTIME) prove $(VALIDATION_TESTS)
43+
44+
.PHONY: $(VALIDATION_TESTS)
45+
$(VALIDATION_TESTS): %.t: %.go
46+
go build -tags "$(BUILDTAGS)" ${TESTFLAGS} -o $@ $<
4247

4348
.PHONY: test .gofmt .govet .golint
4449

validation/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*.t

validation/create.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/mndrix/tap-go"
7+
rspecs "github.com/opencontainers/runtime-spec/specs-go"
8+
"github.com/opencontainers/runtime-tools/generate"
9+
"github.com/opencontainers/runtime-tools/specerror"
10+
"github.com/opencontainers/runtime-tools/validation/util"
11+
"github.com/satori/go.uuid"
12+
)
13+
14+
func main() {
15+
t := tap.New()
16+
17+
g := generate.New()
18+
g.SetRootPath(".")
19+
g.SetProcessArgs([]string{"ls"})
20+
21+
bundleDir, err := util.PrepareBundle()
22+
if err != nil {
23+
util.Fatal(err)
24+
}
25+
26+
r, err := util.NewRuntime(util.RuntimeCommand, bundleDir)
27+
if err != nil {
28+
util.Fatal(err)
29+
}
30+
defer r.Clean(true)
31+
32+
err = r.SetConfig(&g)
33+
if err != nil {
34+
util.Fatal(err)
35+
}
36+
37+
containerID := uuid.NewV4().String()
38+
cases := []struct {
39+
id string
40+
errExpected bool
41+
err error
42+
}{
43+
{"", false, specerror.NewError(specerror.CreateWithBundlePathAndID, fmt.Errorf("create MUST generate an error if the ID is not provided"), rspecs.Version)},
44+
{containerID, true, specerror.NewError(specerror.CreateNewContainer, fmt.Errorf("create MUST create a new container"), rspecs.Version)},
45+
{containerID, false, specerror.NewError(specerror.CreateWithUniqueID, fmt.Errorf("create MUST generate an error if the ID provided is not unique"), rspecs.Version)},
46+
}
47+
48+
for _, c := range cases {
49+
r.SetID(c.id)
50+
stderr, err := r.Create()
51+
t.Ok((err == nil) == c.errExpected, c.err.(*specerror.Error).Err.Err.Error())
52+
t.Diagnostic(c.err.(*specerror.Error).Err.Reference)
53+
t.Diagnostic(err.Error())
54+
if len(stderr) > 0 {
55+
t.Diagnostic(string(stderr))
56+
}
57+
58+
if err == nil {
59+
state, _ := r.State()
60+
t.Ok(state.ID == c.id, "")
61+
t.Diagnosticf("container PID: %d, state ID: %d", c.id, state.ID)
62+
}
63+
}
64+
65+
t.AutoPlan()
66+
}

validation/default.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/opencontainers/runtime-tools/validation/util"
5+
)
6+
7+
func main() {
8+
g := util.GetDefaultGenerator()
9+
err := util.RuntimeInsideValidate(g, nil)
10+
if err != nil {
11+
util.Fatal(err)
12+
}
13+
}

validation/hostname.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/opencontainers/runtime-tools/validation/util"
5+
)
6+
7+
func main() {
8+
g := util.GetDefaultGenerator()
9+
g.SetHostname("hostname-specific")
10+
err := util.RuntimeInsideValidate(g, nil)
11+
if err != nil {
12+
util.Fatal(err)
13+
}
14+
}

validation/linux_devices.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
rspecs "github.com/opencontainers/runtime-spec/specs-go"
7+
"github.com/opencontainers/runtime-tools/validation/util"
8+
)
9+
10+
func main() {
11+
g := util.GetDefaultGenerator()
12+
13+
// add char device
14+
cdev := rspecs.LinuxDevice{}
15+
cdev.Path = "/dev/test1"
16+
cdev.Type = "c"
17+
cdev.Major = 10
18+
cdev.Minor = 666
19+
cmode := os.FileMode(int32(432))
20+
cdev.FileMode = &cmode
21+
cuid := uint32(0)
22+
cdev.UID = &cuid
23+
cgid := uint32(0)
24+
cdev.GID = &cgid
25+
g.AddDevice(cdev)
26+
27+
// add block device
28+
bdev := rspecs.LinuxDevice{}
29+
bdev.Path = "/dev/test2"
30+
bdev.Type = "b"
31+
bdev.Major = 8
32+
bdev.Minor = 666
33+
bmode := os.FileMode(int32(432))
34+
bdev.FileMode = &bmode
35+
uid := uint32(0)
36+
bdev.UID = &uid
37+
gid := uint32(0)
38+
bdev.GID = &gid
39+
g.AddDevice(bdev)
40+
41+
// add fifo device
42+
pdev := rspecs.LinuxDevice{}
43+
pdev.Path = "/dev/test3"
44+
pdev.Type = "p"
45+
pdev.Major = 8
46+
pdev.Minor = 666
47+
pmode := os.FileMode(int32(432))
48+
pdev.FileMode = &pmode
49+
g.AddDevice(pdev)
50+
51+
err := util.RuntimeInsideValidate(g, nil)
52+
if err != nil {
53+
util.Fatal(err)
54+
}
55+
}

validation/linux_gid_mappings.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/opencontainers/runtime-tools/validation/util"
5+
)
6+
7+
func main() {
8+
g := util.GetDefaultGenerator()
9+
g.AddLinuxGIDMapping(uint32(1000), uint32(0), uint32(3200))
10+
err := util.RuntimeInsideValidate(g, nil)
11+
if err != nil {
12+
util.Fatal(err)
13+
}
14+
}

validation/linux_masked_paths.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/opencontainers/runtime-tools/validation/util"
8+
)
9+
10+
func main() {
11+
g := util.GetDefaultGenerator()
12+
g.AddLinuxMaskedPaths("/masktest")
13+
err := util.RuntimeInsideValidate(g, func(path string) error {
14+
pathName := filepath.Join(path, "masktest")
15+
return os.MkdirAll(pathName, 0700)
16+
})
17+
if err != nil {
18+
util.Fatal(err)
19+
}
20+
}

validation/linux_readonly_paths.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/opencontainers/runtime-tools/validation/util"
8+
)
9+
10+
func main() {
11+
g := util.GetDefaultGenerator()
12+
g.AddLinuxReadonlyPaths("readonlytest")
13+
err := util.RuntimeInsideValidate(g, func(path string) error {
14+
pathName := filepath.Join(path, "readonlytest")
15+
return os.MkdirAll(pathName, 0700)
16+
})
17+
if err != nil {
18+
util.Fatal(err)
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"github.com/opencontainers/runtime-tools/validation/util"
5+
)
6+
7+
func main() {
8+
g := util.GetDefaultGenerator()
9+
g.SetupPrivileged(true)
10+
g.SetLinuxRootPropagation("shared")
11+
err := util.RuntimeInsideValidate(g, nil)
12+
if err != nil {
13+
util.Fatal(err)
14+
}
15+
}

0 commit comments

Comments
 (0)