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 Jul 6, 2017
1 parent 1259e3e commit ccf57b5
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -332,7 +332,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 @@ -696,7 +696,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
31 changes: 22 additions & 9 deletions validate/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@ import (
"errors"
"fmt"
"strings"

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

// ComplianceLevel represents the OCI compliance levels
type ComplianceLevel int

const (
// MAY-level
// ComplianceMay represents 'MAY' in RFC2119
ComplianceMay ComplianceLevel = iota
// ComplianceOptional represents 'OPTIONAL' in RFC2119
ComplianceOptional
// SHOULD-level
// ComplianceShould represents 'SHOULD' in RFC2119
ComplianceShould
// ComplianceShouldNot represents 'SHOULD NOT' in RFC2119
ComplianceShouldNot
// ComplianceRecommended represents 'RECOMMENDED' in RFC2119
ComplianceRecommended
// ComplianceNotRecommended represents 'NOT RECOMMENDED' in RFC2119
ComplianceNotRecommended
// MUST-level
// ComplianceMust represents 'MUST' in RFC2119
ComplianceMust
// ComplianceMustNot represents 'MUST NOT' in RFC2119
ComplianceMustNot
// ComplianceShall represents 'SHALL' in RFC2119
ComplianceShall
// ComplianceShallNot represents 'SHALL NOT' in RFC2119
ComplianceShallNot
// ComplianceRequired represents 'REQUIRED' in RFC2119
ComplianceRequired
)

Expand All @@ -40,28 +53,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 +83,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 +100,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 ccf57b5

Please sign in to comment.