|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/mndrix/tap-go" |
| 12 | + rspecs "github.com/opencontainers/runtime-spec/specs-go" |
| 13 | + "github.com/opencontainers/runtime-tools/specerror" |
| 14 | + "github.com/opencontainers/runtime-tools/validation/util" |
| 15 | + uuid "github.com/satori/go.uuid" |
| 16 | +) |
| 17 | + |
| 18 | +func saveConfig(path string, v interface{}) error { |
| 19 | + data, err := json.Marshal(v) |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + |
| 24 | + return ioutil.WriteFile(path, data, 0644) |
| 25 | +} |
| 26 | + |
| 27 | +func main() { |
| 28 | + t := tap.New() |
| 29 | + t.Header(0) |
| 30 | + bundleDir, err := util.PrepareBundle() |
| 31 | + if err != nil { |
| 32 | + util.Fatal(err) |
| 33 | + } |
| 34 | + defer os.RemoveAll(bundleDir) |
| 35 | + configFile := filepath.Join(bundleDir, "config.json") |
| 36 | + |
| 37 | + type extendedSpec struct { |
| 38 | + rspecs.Spec |
| 39 | + Unknown string `json:"unknown,omitempty"` |
| 40 | + } |
| 41 | + |
| 42 | + containerID := uuid.NewV4().String() |
| 43 | + basicConfig := util.GetDefaultGenerator() |
| 44 | + basicConfig.SetProcessArgs([]string{"true"}) |
| 45 | + annotationConfig := basicConfig |
| 46 | + annotationConfig.AddAnnotation(fmt.Sprintf("org.%s", containerID), "") |
| 47 | + invalidConfig := basicConfig |
| 48 | + invalidConfig.SetVersion("invalid") |
| 49 | + |
| 50 | + cases := []struct { |
| 51 | + eSpec extendedSpec |
| 52 | + action util.LifecycleAction |
| 53 | + errExpected bool |
| 54 | + err error |
| 55 | + }{ |
| 56 | + {extendedSpec{Spec: *annotationConfig.Spec()}, util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, true, specerror.NewError(specerror.AnnotationsKeyIgnoreUnknown, fmt.Errorf("implementations that are reading/processing this configuration file MUST NOT generate an error if they encounter an unknown annotation key"), rspecs.Version)}, |
| 57 | + {extendedSpec{Spec: *basicConfig.Spec(), Unknown: "unknown"}, util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, true, specerror.NewError(specerror.ExtensibilityIgnoreUnknownProp, fmt.Errorf("runtimes that are reading or processing this configuration file MUST NOT generate an error if they encounter an unknown property"), rspecs.Version)}, |
| 58 | + {extendedSpec{Spec: *invalidConfig.Spec()}, util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, false, specerror.NewError(specerror.ValidValues, fmt.Errorf("runtimes that are reading or processing this configuration file MUST generate an error when invalid or unsupported values are encountered"), rspecs.Version)}, |
| 59 | + } |
| 60 | + |
| 61 | + for _, c := range cases { |
| 62 | + config := util.LifecycleConfig{ |
| 63 | + BundleDir: bundleDir, |
| 64 | + Actions: c.action, |
| 65 | + PreCreate: func(r *util.Runtime) error { |
| 66 | + r.SetID(containerID) |
| 67 | + return saveConfig(configFile, c.eSpec) |
| 68 | + }, |
| 69 | + PreDelete: func(r *util.Runtime) error { |
| 70 | + util.WaitingForStatus(*r, util.LifecycleStatusCreated|util.LifecycleStatusStopped, time.Second*10, time.Second*1) |
| 71 | + return nil |
| 72 | + }, |
| 73 | + } |
| 74 | + err := util.RuntimeLifecycleValidate(config) |
| 75 | + util.SpecErrorOK(t, (err == nil) == c.errExpected, c.err, err) |
| 76 | + } |
| 77 | + |
| 78 | + t.AutoPlan() |
| 79 | +} |
0 commit comments