|
| 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