Skip to content

Commit abb7f02

Browse files
authored
Merge pull request #495 from q384566678/fix-hostspecific
Add validation when host-specific is set
2 parents 6df2639 + 8fb3e83 commit abb7f02

4 files changed

Lines changed: 42 additions & 23 deletions

File tree

cmd/oci-runtime-tool/validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"runtime"
56

67
rfc2119 "github.com/opencontainers/runtime-tools/error"
78
"github.com/opencontainers/runtime-tools/specerror"
@@ -12,7 +13,7 @@ import (
1213

1314
var bundleValidateFlags = []cli.Flag{
1415
cli.StringFlag{Name: "path", Value: ".", Usage: "path to a bundle"},
15-
cli.StringFlag{Name: "platform", Value: "linux", Usage: "platform of the target bundle (linux, windows, solaris)"},
16+
cli.StringFlag{Name: "platform", Value: runtime.GOOS, Usage: "platform of the target bundle (linux, windows, solaris)"},
1617
}
1718

1819
var bundleValidateCommand = cli.Command{

man/oci-runtime-tool-validate.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Validate an OCI bundle
1919
Path to bundle. The default is current working directory.
2020

2121
**--platform**=PLATFORM
22-
Platform of the target bundle. (linux, windows, solaris) (default: "linux")
22+
Platform of the target bundle. (linux, windows, solaris) The default is host platform.
2323
It will be overwritten by the host platform if the global option '--host-specific' was set.
2424

2525
# SEE ALSO

validate/validate.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,20 @@ type Validator struct {
6666
}
6767

6868
// NewValidator creates a Validator
69-
func NewValidator(spec *rspec.Spec, bundlePath string, hostSpecific bool, platform string) Validator {
69+
func NewValidator(spec *rspec.Spec, bundlePath string, hostSpecific bool, platform string) (Validator, error) {
7070
if hostSpecific && platform != runtime.GOOS {
71-
platform = runtime.GOOS
71+
return Validator{}, fmt.Errorf("When hostSpecific is set, platform must be same as the host platform")
7272
}
7373
return Validator{
7474
spec: spec,
7575
bundlePath: bundlePath,
7676
HostSpecific: hostSpecific,
7777
platform: platform,
78-
}
78+
}, nil
7979
}
8080

8181
// NewValidatorFromPath creates a Validator with specified bundle path
8282
func NewValidatorFromPath(bundlePath string, hostSpecific bool, platform string) (Validator, error) {
83-
if hostSpecific && platform != runtime.GOOS {
84-
platform = runtime.GOOS
85-
}
8683
if bundlePath == "" {
8784
return Validator{}, fmt.Errorf("bundle path shouldn't be empty")
8885
}
@@ -104,7 +101,7 @@ func NewValidatorFromPath(bundlePath string, hostSpecific bool, platform string)
104101
return Validator{}, err
105102
}
106103

107-
return NewValidator(&spec, bundlePath, hostSpecific, platform), nil
104+
return NewValidator(&spec, bundlePath, hostSpecific, platform)
108105
}
109106

110107
// CheckAll checks all parts of runtime bundle

validate/validate_test.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ func TestNewValidator(t *testing.T) {
2323
val Validator
2424
expected Validator
2525
}{
26-
{Validator{testSpec, testBundle, true, testPlatform}, Validator{testSpec, testBundle, true, runtime.GOOS}},
2726
{Validator{testSpec, testBundle, true, runtime.GOOS}, Validator{testSpec, testBundle, true, runtime.GOOS}},
2827
{Validator{testSpec, testBundle, false, testPlatform}, Validator{testSpec, testBundle, false, testPlatform}},
2928
}
3029

3130
for _, c := range cases {
32-
assert.Equal(t, c.expected, NewValidator(c.val.spec, c.val.bundlePath, c.val.HostSpecific, c.val.platform))
31+
v, err := NewValidator(c.val.spec, c.val.bundlePath, c.val.HostSpecific, c.val.platform)
32+
if err != nil {
33+
t.Errorf("unexpected NewValidator error: %+v", err)
34+
}
35+
assert.Equal(t, c.expected, v)
3336
}
3437
}
3538

@@ -165,8 +168,11 @@ func TestCheckRoot(t *testing.T) {
165168
{rspec.Spec{Root: &rspec.Root{Readonly: true}}, "windows", specerror.RootReadonlyOnWindowsFalse},
166169
}
167170
for _, c := range cases {
168-
v := NewValidator(&c.val, tmpBundle, false, c.platform)
169-
err := v.CheckRoot()
171+
v, err := NewValidator(&c.val, tmpBundle, false, c.platform)
172+
if err != nil {
173+
t.Errorf("unexpected NewValidator error: %+v", err)
174+
}
175+
err = v.CheckRoot()
170176
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), fmt.Sprintf("Fail to check Root: %v %d", err, c.expected))
171177
}
172178
}
@@ -183,8 +189,11 @@ func TestCheckSemVer(t *testing.T) {
183189
}
184190

185191
for _, c := range cases {
186-
v := NewValidator(&rspec.Spec{Version: c.val}, "", false, "linux")
187-
err := v.CheckSemVer()
192+
v, err := NewValidator(&rspec.Spec{Version: c.val}, "", false, "linux")
193+
if err != nil {
194+
t.Errorf("unexpected NewValidator error: %+v", err)
195+
}
196+
err = v.CheckSemVer()
188197
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), "Fail to check SemVer "+c.val)
189198
}
190199
}
@@ -328,8 +337,11 @@ func TestCheckProcess(t *testing.T) {
328337
},
329338
}
330339
for _, c := range cases {
331-
v := NewValidator(&c.val, ".", false, c.platform)
332-
err := v.CheckProcess()
340+
v, err := NewValidator(&c.val, ".", false, c.platform)
341+
if err != nil {
342+
t.Errorf("unexpected NewValidator error: %+v", err)
343+
}
344+
err = v.CheckProcess()
333345
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), fmt.Sprintf("failed CheckProcess: %v %d", err, c.expected))
334346
}
335347
}
@@ -409,8 +421,11 @@ func TestCheckLinux(t *testing.T) {
409421
},
410422
}
411423
for _, c := range cases {
412-
v := NewValidator(&c.val, ".", false, "linux")
413-
err := v.CheckLinux()
424+
v, err := NewValidator(&c.val, ".", false, "linux")
425+
if err != nil {
426+
t.Errorf("unexpected NewValidator error: %+v", err)
427+
}
428+
err = v.CheckLinux()
414429
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), fmt.Sprintf("failed CheckLinux: %v %d", err, c.expected))
415430
}
416431
}
@@ -447,8 +462,11 @@ func TestCheckPlatform(t *testing.T) {
447462
},
448463
}
449464
for _, c := range cases {
450-
v := NewValidator(&c.val, ".", false, c.platform)
451-
err := v.CheckPlatform()
465+
v, err := NewValidator(&c.val, ".", false, c.platform)
466+
if err != nil {
467+
t.Errorf("unexpected NewValidator error: %+v", err)
468+
}
469+
err = v.CheckPlatform()
452470
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), fmt.Sprintf("failed CheckPlatform: %v %d", err, c.expected))
453471
}
454472
}
@@ -486,8 +504,11 @@ func TestCheckHooks(t *testing.T) {
486504
},
487505
}
488506
for _, c := range cases {
489-
v := NewValidator(&c.val, ".", false, "linux")
490-
err := v.CheckHooks()
507+
v, err := NewValidator(&c.val, ".", false, "linux")
508+
if err != nil {
509+
t.Errorf("unexpected NewValidator error: %+v", err)
510+
}
511+
err = v.CheckHooks()
491512
assert.Equal(t, c.expected, specerror.FindError(err, c.expected), fmt.Sprintf("failed CheckHooks: %v %d", err, c.expected))
492513
}
493514
}

0 commit comments

Comments
 (0)