Skip to content

Commit 1259e3e

Browse files
committed
sort compliance level according to RFC2119
Signed-off-by: liangchenye <[email protected]>
1 parent 6b1d25d commit 1259e3e

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

cmd/runtimetest/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func validateDefaultFS(spec *rspec.Spec) error {
322322

323323
mountInfos, err := mount.GetMounts()
324324
if err != nil {
325-
return ociErr.NewOCIError(ociErr.DefaultFilesystems, err.Error())
325+
return ociErr.NewError(ociErr.DefaultFilesystems, err.Error())
326326
}
327327

328328
mountsMap := make(map[string]string)
@@ -332,7 +332,7 @@ func validateDefaultFS(spec *rspec.Spec) error {
332332

333333
for fs, fstype := range defaultFS {
334334
if !(mountsMap[fs] == fstype) {
335-
return ociErr.NewOCIError(ociErr.DefaultFilesystems, fmt.Sprintf("%v must exist and expected type is %v", fs, fstype))
335+
return ociErr.NewError(ociErr.DefaultFilesystems, fmt.Sprintf("%v must exist and expected type is %v", fs, fstype))
336336
}
337337
}
338338

@@ -702,7 +702,7 @@ func validate(context *cli.Context) error {
702702
err := v.test(spec)
703703
t.Ok(err == nil, v.description)
704704
if err != nil {
705-
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
705+
if e, ok := err.(*ociErr.Error); ok && e.Level < complianceLevel {
706706
continue
707707
}
708708
validationErrors = multierror.Append(validationErrors, err)
@@ -714,7 +714,7 @@ func validate(context *cli.Context) error {
714714
err := v.test(spec)
715715
t.Ok(err == nil, v.description)
716716
if err != nil {
717-
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
717+
if e, ok := err.(*ociErr.Error); ok && e.Level < complianceLevel {
718718
continue
719719
}
720720
validationErrors = multierror.Append(validationErrors, err)
@@ -746,7 +746,7 @@ func main() {
746746
cli.StringFlag{
747747
Name: "compliance-level",
748748
Value: "must",
749-
Usage: "Compliance level (must or should)",
749+
Usage: "Compliance level (may, should or must)",
750750
},
751751
}
752752

man/oci-runtime-tool.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ oci-runtime-tool is a collection of tools for working with the [OCI runtime spec
3333
Log level (panic, fatal, error, warn, info, or debug) (default: "error").
3434

3535
**--compliance-level**=LEVEL
36-
Compliance level (must or should) (default: "must").
36+
Compliance level (may, should or must) (default: "must").
3737

3838
**-v**, **--version**
3939
Print version information.

validate/error.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@ import (
1010
type ComplianceLevel int
1111

1212
const (
13-
ComplianceOptional ComplianceLevel = iota
14-
ComplianceMay
15-
ComplianceRecommended
13+
// MAY-level
14+
ComplianceMay ComplianceLevel = iota
15+
ComplianceOptional
16+
// SHOULD-level
1617
ComplianceShould
1718
ComplianceShouldNot
19+
ComplianceRecommended
20+
ComplianceNotRecommended
21+
// MUST-level
22+
ComplianceMust
23+
ComplianceMustNot
1824
ComplianceShall
1925
ComplianceShallNot
2026
ComplianceRequired
21-
ComplianceMustNot
22-
ComplianceMust
2327
)
2428

25-
// OCIErrorCode represents the compliance content
26-
type OCIErrorCode int
29+
// ErrorCode represents the compliance content
30+
type ErrorCode int
2731

2832
const (
29-
DefaultFilesystems OCIErrorCode = iota
33+
DefaultFilesystems ErrorCode = iota
3034
)
3135

32-
// OCIError represents an error with compliance level and OCI reference
33-
type OCIError struct {
36+
// Error represents an error with compliance level and OCI reference
37+
type Error struct {
3438
Level ComplianceLevel
3539
Reference string
3640
Err error
@@ -39,31 +43,49 @@ type OCIError struct {
3943
//FIXME: change to tagged spec releases
4044
const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/"
4145

42-
var ociErrors = map[OCIErrorCode]OCIError{
43-
DefaultFilesystems: OCIError{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"},
46+
var ociErrors = map[ErrorCode]Error{
47+
DefaultFilesystems: Error{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"},
4448
}
4549

4650
// ParseLevel takes a string level and returns the OCI compliance level constant
4751
func ParseLevel(level string) ComplianceLevel {
4852
switch strings.ToUpper(level) {
53+
case "MAY":
54+
fallthrough
55+
case "OPTIONAL":
56+
return ComplianceMay
4957
case "SHOULD":
58+
fallthrough
59+
case "SHOULDNOT":
60+
fallthrough
61+
case "RECOMMENDED":
62+
fallthrough
63+
case "NOTRECOMMENDED":
5064
return ComplianceShould
5165
case "MUST":
66+
fallthrough
67+
case "MUSTNOT":
68+
fallthrough
69+
case "SHALL":
70+
fallthrough
71+
case "SHALLNOT":
72+
fallthrough
73+
case "REQUIRED":
5274
return ComplianceMust
5375
default:
5476
return ComplianceMust
5577
}
5678
}
5779

58-
// NewOCIError creates an OCIError by OCIErrorCode and message
59-
func NewOCIError(code OCIErrorCode, msg string) error {
80+
// NewError creates an Error by ErrorCode and message
81+
func NewError(code ErrorCode, msg string) error {
6082
err := ociErrors[code]
6183
err.Err = errors.New(msg)
6284

6385
return &err
6486
}
6587

6688
// Error returns the error message with OCI reference
67-
func (oci *OCIError) Error() string {
89+
func (oci *Error) Error() string {
6890
return fmt.Sprintf("%s\nRefer to: %s%s", oci.Err.Error(), referencePrefix, oci.Reference)
6991
}

0 commit comments

Comments
 (0)