Skip to content

Commit 6316a4e

Browse files
committed
use released version as reference; improve Parse error
Signed-off-by: liangchenye <[email protected]>
1 parent 1259e3e commit 6316a4e

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

cmd/runtimetest/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.NewError(ociErr.DefaultFilesystems, fmt.Sprintf("%v must exist and expected type is %v", fs, fstype))
335+
return ociErr.NewError(ociErr.DefaultFilesystems, fmt.Sprintf("%v SHOULD exist and expected type is %v", fs, fstype))
336336
}
337337
}
338338

@@ -696,7 +696,11 @@ func validate(context *cli.Context) error {
696696
t.Header(0)
697697

698698
complianceLevelString := context.String("compliance-level")
699-
complianceLevel := ociErr.ParseLevel(complianceLevelString)
699+
complianceLevel, err := ociErr.ParseLevel(complianceLevelString)
700+
if err != nil {
701+
complianceLevel = ociErr.ComplianceMust
702+
logrus.Warningf("%s, using 'MUST' by default.", err.Error())
703+
}
700704
var validationErrors error
701705
for _, v := range defaultValidations {
702706
err := v.test(spec)

completions/bash/oci-runtime-tool

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ __oci-runtime-tool_complete_log_level() {
121121

122122
__oci-runtime-tool_complete_compliance_level() {
123123
COMPREPLY=( $( compgen -W "
124-
must
124+
may
125125
should
126+
must
126127
" -- "$cur" ) )
127128
}
128129

validate/error.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,51 @@ import (
44
"errors"
55
"fmt"
66
"strings"
7+
8+
rspec "github.com/opencontainers/runtime-spec/specs-go"
79
)
810

911
// ComplianceLevel represents the OCI compliance levels
1012
type ComplianceLevel int
1113

1214
const (
1315
// MAY-level
16+
17+
// ComplianceMay represents 'MAY' in RFC2119
1418
ComplianceMay ComplianceLevel = iota
19+
// ComplianceOptional represents 'OPTIONAL' in RFC2119
1520
ComplianceOptional
21+
1622
// SHOULD-level
23+
24+
// ComplianceShould represents 'SHOULD' in RFC2119
1725
ComplianceShould
26+
// ComplianceShouldNot represents 'SHOULD NOT' in RFC2119
1827
ComplianceShouldNot
28+
// ComplianceRecommended represents 'RECOMMENDED' in RFC2119
1929
ComplianceRecommended
30+
// ComplianceNotRecommended represents 'NOT RECOMMENDED' in RFC2119
2031
ComplianceNotRecommended
32+
2133
// MUST-level
34+
35+
// ComplianceMust represents 'MUST' in RFC2119
2236
ComplianceMust
37+
// ComplianceMustNot represents 'MUST NOT' in RFC2119
2338
ComplianceMustNot
39+
// ComplianceShall represents 'SHALL' in RFC2119
2440
ComplianceShall
41+
// ComplianceShallNot represents 'SHALL NOT' in RFC2119
2542
ComplianceShallNot
43+
// ComplianceRequired represents 'REQUIRED' in RFC2119
2644
ComplianceRequired
2745
)
2846

2947
// ErrorCode represents the compliance content
3048
type ErrorCode int
3149

3250
const (
51+
// DefaultFilesystems represents the error code of default filesystems test
3352
DefaultFilesystems ErrorCode = iota
3453
)
3554

@@ -40,28 +59,27 @@ type Error struct {
4059
Err error
4160
}
4261

43-
//FIXME: change to tagged spec releases
44-
const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/"
62+
const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob"
4563

4664
var ociErrors = map[ErrorCode]Error{
4765
DefaultFilesystems: Error{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"},
4866
}
4967

5068
// ParseLevel takes a string level and returns the OCI compliance level constant
51-
func ParseLevel(level string) ComplianceLevel {
69+
func ParseLevel(level string) (ComplianceLevel, error) {
5270
switch strings.ToUpper(level) {
5371
case "MAY":
5472
fallthrough
5573
case "OPTIONAL":
56-
return ComplianceMay
74+
return ComplianceMay, nil
5775
case "SHOULD":
5876
fallthrough
5977
case "SHOULDNOT":
6078
fallthrough
6179
case "RECOMMENDED":
6280
fallthrough
6381
case "NOTRECOMMENDED":
64-
return ComplianceShould
82+
return ComplianceShould, nil
6583
case "MUST":
6684
fallthrough
6785
case "MUSTNOT":
@@ -71,10 +89,11 @@ func ParseLevel(level string) ComplianceLevel {
7189
case "SHALLNOT":
7290
fallthrough
7391
case "REQUIRED":
74-
return ComplianceMust
75-
default:
76-
return ComplianceMust
92+
return ComplianceMust, nil
7793
}
94+
95+
var l ComplianceLevel
96+
return l, fmt.Errorf("%q is not a valid compliance level", level)
7897
}
7998

8099
// NewError creates an Error by ErrorCode and message
@@ -87,5 +106,5 @@ func NewError(code ErrorCode, msg string) error {
87106

88107
// Error returns the error message with OCI reference
89108
func (oci *Error) Error() string {
90-
return fmt.Sprintf("%s\nRefer to: %s%s", oci.Err.Error(), referencePrefix, oci.Reference)
109+
return fmt.Sprintf("%s\nRefer to: %s/v%s/%s", oci.Err.Error(), referencePrefix, rspec.Version, oci.Reference)
91110
}

0 commit comments

Comments
 (0)