Skip to content

Commit 27ad856

Browse files
committed
validation: add a generate smoke-test
To avoid cases where our own validation code would consider our defaults unsafe (which has happened in the past several times), add a smoke-test to ensure that this won't happen. Our defaults should not be intentionally invalid, as that confuses downstreams like umoci which use runtime-tools for the default as well as for validation of the generated configuration. Signed-off-by: Aleksa Sarai <asarai@suse.de>
1 parent 30328d5 commit 27ad856

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

validation/generate_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package validation
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"runtime"
8+
"testing"
9+
10+
rfc2119 "github.com/opencontainers/runtime-tools/error"
11+
"github.com/opencontainers/runtime-tools/generate"
12+
"github.com/opencontainers/runtime-tools/specerror"
13+
"github.com/opencontainers/runtime-tools/validate"
14+
)
15+
16+
// Smoke test to ensure that _at the very least_ our default configuration
17+
// passes the validation tests. If this test fails, something is _very_ wrong
18+
// and needs to be fixed immediately (as it will break downstreams that depend
19+
// on us for a "sane default" and do compliance testing -- such as umoci).
20+
func TestGenerateValid(t *testing.T) {
21+
bundle, err := ioutil.TempDir("", "TestGenerateValid_bundle")
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
defer os.RemoveAll(bundle)
26+
27+
// Create our toy bundle.
28+
rootfsPath := filepath.Join(bundle, "rootfs")
29+
if err := os.Mkdir(rootfsPath, 0755); err != nil {
30+
t.Fatal(err)
31+
}
32+
configPath := filepath.Join(bundle, "config.json")
33+
g := generate.New()
34+
if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
// Validate the bundle.
39+
v, err := validate.NewValidatorFromPath(bundle, true, runtime.GOOS)
40+
if err != nil {
41+
t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
42+
}
43+
if err := v.CheckAll(); err != nil {
44+
levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
45+
if err != nil {
46+
t.Errorf("unexpected non-multierror: %+v", err)
47+
return
48+
}
49+
for _, e := range levelErrors.Warnings {
50+
t.Logf("unexpected warning: %v", e)
51+
}
52+
if err := levelErrors.Error; err != nil {
53+
t.Errorf("unexpected MUST error(s): %+v", err)
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)