Skip to content

Commit df01456

Browse files
author
Zhou Hao
authored
Merge pull request #500 from Mashimiao/more-rfc-error
translate more RFC errors based on specerror
2 parents ccb1390 + 3a97b98 commit df01456

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed

Diff for: cmd/runtimetest/main.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,19 @@ func validateDefaultSymlinks(spec *rspec.Spec) error {
421421
return err
422422
}
423423
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
424-
return fmt.Errorf("%v is not a symbolic link as expected", symlink)
424+
return specerror.NewError(specerror.DefaultRuntimeLinuxSymlinks,
425+
fmt.Errorf("%v is not a symbolic link as expected", symlink),
426+
rspec.Version)
425427
}
426428
realDest, err := os.Readlink(symlink)
427429
if err != nil {
428430
return err
429431
}
430432
if realDest != dest {
431-
return fmt.Errorf("link destation of %v expected is %v, actual is %v", symlink, dest, realDest)
433+
return specerror.NewError(specerror.DefaultRuntimeLinuxSymlinks,
434+
fmt.Errorf("link destation of %v expected is %v, actual is %v",
435+
symlink, dest, realDest),
436+
rspec.Version)
432437
}
433438
}
434439

@@ -447,12 +452,16 @@ func validateDefaultDevices(spec *rspec.Spec) error {
447452
fi, err := os.Stat(device)
448453
if err != nil {
449454
if os.IsNotExist(err) {
450-
return fmt.Errorf("device node %v not found", device)
455+
return specerror.NewError(specerror.DefaultDevices,
456+
fmt.Errorf("device node %v not found", device),
457+
rspec.Version)
451458
}
452459
return err
453460
}
454461
if fi.Mode()&os.ModeDevice != os.ModeDevice {
455-
return fmt.Errorf("file %v is not a device as expected", device)
462+
return specerror.NewError(specerror.DefaultDevices,
463+
fmt.Errorf("file %v is not a device as expected", device),
464+
rspec.Version)
456465
}
457466
}
458467

@@ -512,7 +521,7 @@ func validateOOMScoreAdj(spec *rspec.Spec) error {
512521
return err
513522
}
514523
if actual != expected {
515-
return fmt.Errorf("oomScoreAdj expected: %v, actual: %v", expected, actual)
524+
return specerror.NewError(specerror.LinuxProcOomScoreAdjSet, fmt.Errorf("oomScoreAdj expected: %v, actual: %v", expected, actual), rspec.Version)
516525
}
517526
}
518527
}
@@ -667,14 +676,21 @@ func validateMounts(spec *rspec.Spec) error {
667676
if found {
668677
mountErrs = multierror.Append(
669678
mountErrs,
670-
fmt.Errorf(
671-
"mounts[%d] %v mounted before mounts[%d] %v",
672-
i,
673-
configMount,
674-
highestMatchedConfig,
675-
spec.Mounts[highestMatchedConfig]))
679+
specerror.NewError(specerror.MountsInOrder,
680+
fmt.Errorf(
681+
"mounts[%d] %v mounted before mounts[%d] %v",
682+
i,
683+
configMount,
684+
highestMatchedConfig,
685+
spec.Mounts[highestMatchedConfig]),
686+
rspec.Version))
676687
} else {
677-
mountErrs = multierror.Append(mountErrs, fmt.Errorf("mounts[%d] %v does not exist", i, configMount))
688+
mountErrs = multierror.Append(
689+
mountErrs,
690+
specerror.NewError(specerror.MountsInOrder, fmt.Errorf(
691+
"mounts[%d] %v does not exist",
692+
i,
693+
configMount), rspec.Version))
678694
}
679695
}
680696
}

Diff for: validate/validate.go

+51-10
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (v *Validator) CheckAll() error {
130130
func JSONSchemaURL(version string) (url string, err error) {
131131
ver, err := semver.Parse(version)
132132
if err != nil {
133-
return "", err
133+
return "", specerror.NewError(specerror.SpecVersionInSemVer, err, rspec.Version)
134134
}
135135
configRenamedToConfigSchemaVersion, err := semver.Parse("1.0.0-rc2") // config.json became config-schema.json in 1.0.0-rc2
136136
if ver.Compare(configRenamedToConfigSchemaVersion) == -1 {
@@ -276,7 +276,12 @@ func (v *Validator) CheckHooks() (errs error) {
276276
func (v *Validator) checkEventHooks(hookType string, hooks []rspec.Hook, hostSpecific bool) (errs error) {
277277
for i, hook := range hooks {
278278
if !osFilepath.IsAbs(v.platform, hook.Path) {
279-
errs = multierror.Append(errs, fmt.Errorf("hooks.%s[%d].path %v: is not absolute path", hookType, i, hook.Path))
279+
errs = multierror.Append(errs,
280+
specerror.NewError(
281+
specerror.PosixHooksPathAbs,
282+
fmt.Errorf("hooks.%s[%d].path %v: is not absolute path",
283+
hookType, i, hook.Path),
284+
rspec.Version))
280285
}
281286

282287
if hostSpecific {
@@ -309,7 +314,11 @@ func (v *Validator) CheckProcess() (errs error) {
309314

310315
process := v.spec.Process
311316
if !osFilepath.IsAbs(v.platform, process.Cwd) {
312-
errs = multierror.Append(errs, fmt.Errorf("cwd %q is not an absolute path", process.Cwd))
317+
errs = multierror.Append(errs,
318+
specerror.NewError(
319+
specerror.ProcCwdAbs,
320+
fmt.Errorf("cwd %q is not an absolute path", process.Cwd),
321+
rspec.Version))
313322
}
314323

315324
for _, env := range process.Env {
@@ -319,7 +328,11 @@ func (v *Validator) CheckProcess() (errs error) {
319328
}
320329

321330
if len(process.Args) == 0 {
322-
errs = multierror.Append(errs, fmt.Errorf("args must not be empty"))
331+
errs = multierror.Append(errs,
332+
specerror.NewError(
333+
specerror.ProcArgsOneEntryRequired,
334+
fmt.Errorf("args must not be empty"),
335+
rspec.Version))
323336
} else {
324337
if filepath.IsAbs(process.Args[0]) {
325338
var rootfsPath string
@@ -432,7 +445,12 @@ func (v *Validator) CheckRlimits() (errs error) {
432445
for index, rlimit := range process.Rlimits {
433446
for i := index + 1; i < len(process.Rlimits); i++ {
434447
if process.Rlimits[index].Type == process.Rlimits[i].Type {
435-
errs = multierror.Append(errs, fmt.Errorf("rlimit can not contain the same type %q", process.Rlimits[index].Type))
448+
errs = multierror.Append(errs,
449+
specerror.NewError(
450+
specerror.PosixProcRlimitsErrorOnDup,
451+
fmt.Errorf("rlimit can not contain the same type %q",
452+
process.Rlimits[index].Type),
453+
rspec.Version))
436454
}
437455
}
438456
errs = multierror.Append(errs, v.rlimitValid(rlimit))
@@ -497,7 +515,13 @@ func (v *Validator) CheckMounts() (errs error) {
497515
errs = multierror.Append(errs, fmt.Errorf("unsupported mount type %q", mountA.Type))
498516
}
499517
if !osFilepath.IsAbs(v.platform, mountA.Destination) {
500-
errs = multierror.Append(errs, fmt.Errorf("mounts[%d].destination %q is not absolute", i, mountA.Destination))
518+
errs = multierror.Append(errs,
519+
specerror.NewError(
520+
specerror.MountsDestAbs,
521+
fmt.Errorf("mounts[%d].destination %q is not absolute",
522+
i,
523+
mountA.Destination),
524+
rspec.Version))
501525
}
502526
for j, mountB := range v.spec.Mounts {
503527
if i == j {
@@ -511,7 +535,12 @@ func (v *Validator) CheckMounts() (errs error) {
511535
}
512536
if nested {
513537
if v.platform == "windows" && i < j {
514-
errs = multierror.Append(errs, fmt.Errorf("on Windows, %v nested within %v is forbidden", mountB.Destination, mountA.Destination))
538+
errs = multierror.Append(errs,
539+
specerror.NewError(
540+
specerror.MountsDestOnWindowsNotNested,
541+
fmt.Errorf("on Windows, %v nested within %v is forbidden",
542+
mountB.Destination, mountA.Destination),
543+
rspec.Version))
515544
}
516545
if i > j {
517546
logrus.Warnf("%v will be covered by %v", mountB.Destination, mountA.Destination)
@@ -534,7 +563,11 @@ func (v *Validator) CheckPlatform() (errs error) {
534563

535564
if v.platform == "windows" {
536565
if v.spec.Windows == nil {
537-
errs = multierror.Append(errs, errors.New("'windows' MUST be set when platform is `windows`"))
566+
errs = multierror.Append(errs,
567+
specerror.NewError(
568+
specerror.PlatformSpecConfOnWindowsSet,
569+
fmt.Errorf("'windows' MUST be set when platform is `windows`"),
570+
rspec.Version))
538571
}
539572
}
540573

@@ -705,13 +738,21 @@ func (v *Validator) CheckLinux() (errs error) {
705738

706739
for _, maskedPath := range v.spec.Linux.MaskedPaths {
707740
if !strings.HasPrefix(maskedPath, "/") {
708-
errs = multierror.Append(errs, fmt.Errorf("maskedPath %v is not an absolute path", maskedPath))
741+
errs = multierror.Append(errs,
742+
specerror.NewError(
743+
specerror.MaskedPathsAbs,
744+
fmt.Errorf("maskedPath %v is not an absolute path", maskedPath),
745+
rspec.Version))
709746
}
710747
}
711748

712749
for _, readonlyPath := range v.spec.Linux.ReadonlyPaths {
713750
if !strings.HasPrefix(readonlyPath, "/") {
714-
errs = multierror.Append(errs, fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath))
751+
errs = multierror.Append(errs,
752+
specerror.NewError(
753+
specerror.ReadonlyPathsAbs,
754+
fmt.Errorf("readonlyPath %v is not an absolute path", readonlyPath),
755+
rspec.Version))
715756
}
716757
}
717758

Diff for: validate/validate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestJSONSchema(t *testing.T) {
4040
}{
4141
{
4242
config: &rspec.Spec{},
43-
error: "Version string empty",
43+
error: "1 error occurred:\n\n* Version string empty\nRefer to: https://github.com/opencontainers/runtime-spec/blob/v1.0.0/config.md#specification-version",
4444
},
4545
{
4646
config: &rspec.Spec{

0 commit comments

Comments
 (0)