Skip to content

Commit

Permalink
oci error: add error level and reference
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 ca03d44 commit 4029999
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmd/runtimetest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/mndrix/tap-go"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
"github.com/syndtr/gocapability/capability"
"github.com/urfave/cli"

"github.com/opencontainers/runtime-tools/cmd/runtimetest/mount"
ociErr "github.com/opencontainers/runtime-tools/validate"
)

// PrGetNoNewPrivs isn't exposed in Golang so we define it ourselves copying the value from
Expand Down Expand Up @@ -660,11 +662,16 @@ func validate(context *cli.Context) error {
t := tap.New()
t.Header(0)

complianceLevelString := context.String("compliance-level")
complianceLevel := ociErr.ParseLevel(complianceLevelString)
var validationErrors error
for _, v := range defaultValidations {
err := v.test(spec)
t.Ok(err == nil, v.description)
if err != nil {
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
continue
}
validationErrors = multierror.Append(validationErrors, err)
}
}
Expand All @@ -674,6 +681,9 @@ func validate(context *cli.Context) error {
err := v.test(spec)
t.Ok(err == nil, v.description)
if err != nil {
if e, ok := err.(*ociErr.OCIError); ok && e.Level < complianceLevel {
continue
}
validationErrors = multierror.Append(validationErrors, err)
}
}
Expand All @@ -700,6 +710,11 @@ func main() {
Value: ".",
Usage: "Path to the configuration",
},
cli.StringFlag{
Name: "compliance-level",
Value: "must",
Usage: "Compliance level (must or should)",
},
}

app.Action = validate
Expand Down
15 changes: 15 additions & 0 deletions completions/bash/oci-runtime-tool
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ __oci-runtime-tool_complete_log_level() {
" -- "$cur" ) )
}

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

__oci-runtime-tool_complete_propagations() {
COMPREPLY=( $( compgen -W "
private
Expand Down Expand Up @@ -218,6 +225,10 @@ _oci-runtime-tool_oci-runtime-tool() {
--log-level
"

local options_with_args="
--compliance-level
"

local boolean_options="
--help -h
--host-specific
Expand All @@ -231,6 +242,10 @@ _oci-runtime-tool_oci-runtime-tool() {
__oci-runtime-tool_complete_log_level
return
;;
--compliance-level)
__oci-runtime-tool_complete_compliance_level
return
;;
esac

case "$cur" in
Expand Down
3 changes: 3 additions & 0 deletions man/oci-runtime-tool.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ oci-runtime-tool is a collection of tools for working with the [OCI runtime spec
**--log-level**=LEVEL
Log level (panic, fatal, error, warn, info, or debug) (default: "error").

**--compliance-level**=LEVEL
Compliance level (must or should) (default: "must").

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

Expand Down
69 changes: 69 additions & 0 deletions validate/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package validate

import (
"errors"
"fmt"
"strings"
)

// ComplianceLevel represents the OCI compliance levels
type ComplianceLevel int

const (
ComplianceOptional ComplianceLevel = iota
ComplianceMay
ComplianceRecommended
ComplianceShould
ComplianceShouldNot
ComplianceShall
ComplianceShallNot
ComplianceRequired
ComplianceMustNot
ComplianceMust
)

// OCIErrorCode represents the compliance content
type OCIErrorCode int

const (
DefaultFilesystems OCIErrorCode = iota
)

// OCIError represents an error with compliance level and OCI reference
type OCIError struct {
Level ComplianceLevel
Reference string
Err error
}

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

var ociErrors = map[OCIErrorCode]OCIError{
DefaultFilesystems: OCIError{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 {
switch strings.ToUpper(level) {
case "SHOULD":
return ComplianceShould
case "MUST":
return ComplianceMust
default:
return ComplianceMust
}
}

// NewOCIError creates an OCIError by OCIErrorCode and message
func NewOCIError(code OCIErrorCode, msg string) error {
err := ociErrors[code]
err.Err = errors.New(msg)

return &err
}

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

0 comments on commit 4029999

Please sign in to comment.