Skip to content

Commit cd790c4

Browse files
committed
Move generate valid test
This change moves the TestGenerateValid test to the validate package from the generate package. This decreases the transitive dependencies for clients of the generate package -- the more common use case. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent f7e3563 commit cd790c4

File tree

2 files changed

+71
-75
lines changed

2 files changed

+71
-75
lines changed

generate/generate_test.go

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,12 @@
11
package generate_test
22

33
import (
4-
"os"
5-
"path/filepath"
6-
"runtime"
74
"testing"
85

9-
rfc2119 "github.com/opencontainers/runtime-tools/error"
106
"github.com/opencontainers/runtime-tools/generate"
11-
"github.com/opencontainers/runtime-tools/specerror"
12-
"github.com/opencontainers/runtime-tools/validate"
137
"github.com/stretchr/testify/assert"
148
)
159

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-
plat := "linux"
22-
if runtime.GOOS == "windows" {
23-
plat = "windows"
24-
}
25-
26-
isolations := []string{"process", "hyperv"}
27-
for _, isolation := range isolations {
28-
if plat == "linux" && isolation == "hyperv" {
29-
// Combination doesn't make sense.
30-
continue
31-
}
32-
33-
bundle, err := os.MkdirTemp("", "TestGenerateValid_bundle")
34-
if err != nil {
35-
t.Fatal(err)
36-
}
37-
defer os.RemoveAll(bundle)
38-
39-
// Create our toy bundle.
40-
rootfsPath := filepath.Join(bundle, "rootfs")
41-
if err := os.Mkdir(rootfsPath, 0o755); err != nil {
42-
t.Fatal(err)
43-
}
44-
configPath := filepath.Join(bundle, "config.json")
45-
g, err := generate.New(plat)
46-
if err != nil {
47-
t.Fatal(err)
48-
}
49-
if runtime.GOOS == "windows" {
50-
g.AddWindowsLayerFolders("C:\\fakelayer")
51-
g.AddWindowsLayerFolders("C:\\fakescratch")
52-
if isolation == "process" {
53-
// Add the Rootfs section (note: fake volume guid)
54-
g.SetRootPath("\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\")
55-
} else {
56-
// Add the Hyper-V section
57-
g.SetWindowsHypervUntilityVMPath("")
58-
}
59-
}
60-
if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
61-
t.Fatal(err)
62-
}
63-
64-
// Validate the bundle.
65-
v, err := validate.NewValidatorFromPath(bundle, true, runtime.GOOS)
66-
if err != nil {
67-
t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
68-
}
69-
if err := v.CheckAll(); err != nil {
70-
levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
71-
if err != nil {
72-
t.Errorf("unexpected non-multierror: %+v", err)
73-
return
74-
}
75-
for _, e := range levelErrors.Warnings {
76-
t.Logf("unexpected warning: %v", e)
77-
}
78-
if err := levelErrors.Error; err != nil {
79-
t.Errorf("unexpected MUST error(s): %+v", err)
80-
}
81-
}
82-
}
83-
}
84-
8510
func TestRemoveMount(t *testing.T) {
8611
g, err := generate.New("linux")
8712
if err != nil {

validate/validate_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,80 @@ import (
1111
rspec "github.com/opencontainers/runtime-spec/specs-go"
1212
"github.com/stretchr/testify/assert"
1313

14+
rfc2119 "github.com/opencontainers/runtime-tools/error"
15+
"github.com/opencontainers/runtime-tools/generate"
1416
"github.com/opencontainers/runtime-tools/specerror"
1517
)
1618

19+
// Smoke test to ensure that _at the very least_ our default configuration
20+
// passes the validation tests. If this test fails, something is _very_ wrong
21+
// and needs to be fixed immediately (as it will break downstreams that depend
22+
// on us for a "sane default" and do compliance testing -- such as umoci).
23+
func TestGenerateValid(t *testing.T) {
24+
plat := "linux"
25+
if runtime.GOOS == "windows" {
26+
plat = "windows"
27+
}
28+
29+
isolations := []string{"process", "hyperv"}
30+
for _, isolation := range isolations {
31+
if plat == "linux" && isolation == "hyperv" {
32+
// Combination doesn't make sense.
33+
continue
34+
}
35+
36+
bundle, err := os.MkdirTemp("", "TestGenerateValid_bundle")
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
defer os.RemoveAll(bundle)
41+
42+
// Create our toy bundle.
43+
rootfsPath := filepath.Join(bundle, "rootfs")
44+
if err := os.Mkdir(rootfsPath, 0o755); err != nil {
45+
t.Fatal(err)
46+
}
47+
configPath := filepath.Join(bundle, "config.json")
48+
g, err := generate.New(plat)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
if runtime.GOOS == "windows" {
53+
g.AddWindowsLayerFolders("C:\\fakelayer")
54+
g.AddWindowsLayerFolders("C:\\fakescratch")
55+
if isolation == "process" {
56+
// Add the Rootfs section (note: fake volume guid)
57+
g.SetRootPath("\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\")
58+
} else {
59+
// Add the Hyper-V section
60+
g.SetWindowsHypervUntilityVMPath("")
61+
}
62+
}
63+
if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
64+
t.Fatal(err)
65+
}
66+
67+
// Validate the bundle.
68+
v, err := NewValidatorFromPath(bundle, true, runtime.GOOS)
69+
if err != nil {
70+
t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
71+
}
72+
if err := v.CheckAll(); err != nil {
73+
levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
74+
if err != nil {
75+
t.Errorf("unexpected non-multierror: %+v", err)
76+
return
77+
}
78+
for _, e := range levelErrors.Warnings {
79+
t.Logf("unexpected warning: %v", e)
80+
}
81+
if err := levelErrors.Error; err != nil {
82+
t.Errorf("unexpected MUST error(s): %+v", err)
83+
}
84+
}
85+
}
86+
}
87+
1788
func TestNewValidator(t *testing.T) {
1889
testSpec := &rspec.Spec{}
1990
testBundle := ""

0 commit comments

Comments
 (0)