From 70d8c602e6f62367b39fa9471face11ebff25ec5 Mon Sep 17 00:00:00 2001 From: liangchenye Date: Wed, 12 Apr 2017 15:37:42 +0800 Subject: [PATCH] use released version as reference; improve Parse error Signed-off-by: liangchenye --- cmd/runtimetest/main.go | 8 ++++++-- completions/bash/oci-runtime-tool | 3 ++- validate/error.go | 20 +++++++++++--------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/cmd/runtimetest/main.go b/cmd/runtimetest/main.go index 90a56c6f2..4a32b9c91 100644 --- a/cmd/runtimetest/main.go +++ b/cmd/runtimetest/main.go @@ -336,7 +336,7 @@ func validateDefaultFS(spec *rspec.Spec) error { for fs, fstype := range defaultFS { if !(mountsMap[fs] == fstype) { - return ociErr.NewError(ociErr.DefaultFilesystems, fmt.Sprintf("%v must exist and expected type is %v", fs, fstype)) + return ociErr.NewError(ociErr.DefaultFilesystems, fmt.Sprintf("%v SHOULD exist and expected type is %v", fs, fstype)) } } @@ -712,7 +712,11 @@ func validate(context *cli.Context) error { t.Header(0) complianceLevelString := context.String("compliance-level") - complianceLevel := ociErr.ParseLevel(complianceLevelString) + complianceLevel, err := ociErr.ParseLevel(complianceLevelString) + if err != nil { + complianceLevel = ociErr.ComplianceMust + logrus.Warningf("%s, using 'MUST' by default.", err.Error()) + } var validationErrors error for _, v := range defaultValidations { err := v.test(spec) diff --git a/completions/bash/oci-runtime-tool b/completions/bash/oci-runtime-tool index d562f25c0..c8af7596a 100644 --- a/completions/bash/oci-runtime-tool +++ b/completions/bash/oci-runtime-tool @@ -121,8 +121,9 @@ __oci-runtime-tool_complete_log_level() { __oci-runtime-tool_complete_compliance_level() { COMPREPLY=( $( compgen -W " - must + may should + must " -- "$cur" ) ) } diff --git a/validate/error.go b/validate/error.go index ce7d286f3..fb4a4c2c2 100644 --- a/validate/error.go +++ b/validate/error.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" "strings" + + rspec "github.com/opencontainers/runtime-spec/specs-go" ) // ComplianceLevel represents the OCI compliance levels @@ -40,20 +42,19 @@ type Error struct { Err error } -//FIXME: change to tagged spec releases -const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob/master/" +const referencePrefix = "https://github.com/opencontainers/runtime-spec/blob" var ociErrors = map[ErrorCode]Error{ DefaultFilesystems: Error{Level: ComplianceShould, Reference: "config-linux.md#default-filesystems"}, } // ParseLevel takes a string level and returns the OCI compliance level constant -func ParseLevel(level string) ComplianceLevel { +func ParseLevel(level string) (ComplianceLevel, error) { switch strings.ToUpper(level) { case "MAY": fallthrough case "OPTIONAL": - return ComplianceMay + return ComplianceMay, nil case "SHOULD": fallthrough case "SHOULDNOT": @@ -61,7 +62,7 @@ func ParseLevel(level string) ComplianceLevel { case "RECOMMENDED": fallthrough case "NOTRECOMMENDED": - return ComplianceShould + return ComplianceShould, nil case "MUST": fallthrough case "MUSTNOT": @@ -71,10 +72,11 @@ func ParseLevel(level string) ComplianceLevel { case "SHALLNOT": fallthrough case "REQUIRED": - return ComplianceMust - default: - return ComplianceMust + return ComplianceMust, nil } + + var l ComplianceLevel + return l, fmt.Errorf("%q is not a valid compliance level", level) } // NewError creates an Error by ErrorCode and message @@ -87,5 +89,5 @@ func NewError(code ErrorCode, msg string) error { // Error returns the error message with OCI reference func (oci *Error) Error() string { - return fmt.Sprintf("%s\nRefer to: %s%s", oci.Err.Error(), referencePrefix, oci.Reference) + return fmt.Sprintf("%s\nRefer to: %s/v%s/%s", oci.Err.Error(), referencePrefix, rspec.Version, oci.Reference) }