Skip to content

Commit

Permalink
use released version as reference; improve Parse error
Browse files Browse the repository at this point in the history
Signed-off-by: liangchenye <liangchenye@huawei.com>
  • Loading branch information
liangchenye committed Apr 12, 2017
1 parent f56b4cd commit 70d8c60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cmd/runtimetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion completions/bash/oci-runtime-tool
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ __oci-runtime-tool_complete_log_level() {

__oci-runtime-tool_complete_compliance_level() {
COMPREPLY=( $( compgen -W "
must
may
should
must
" -- "$cur" ) )
}

Expand Down
20 changes: 11 additions & 9 deletions validate/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"strings"

rspec "github.com/opencontainers/runtime-spec/specs-go"
)

// ComplianceLevel represents the OCI compliance levels
Expand Down Expand Up @@ -40,28 +42,27 @@ 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":
fallthrough
case "RECOMMENDED":
fallthrough
case "NOTRECOMMENDED":
return ComplianceShould
return ComplianceShould, nil
case "MUST":
fallthrough
case "MUSTNOT":
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 70d8c60

Please sign in to comment.