|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/mndrix/tap-go" |
| 9 | + rspecs "github.com/opencontainers/runtime-spec/specs-go" |
| 10 | + "github.com/opencontainers/runtime-tools/generate" |
| 11 | + "github.com/opencontainers/runtime-tools/specerror" |
| 12 | + "github.com/opencontainers/runtime-tools/validation/util" |
| 13 | + uuid "github.com/satori/go.uuid" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + t := tap.New() |
| 18 | + t.Header(0) |
| 19 | + bundleDir, err := util.PrepareBundle() |
| 20 | + if err != nil { |
| 21 | + util.Fatal(err) |
| 22 | + } |
| 23 | + defer os.RemoveAll(bundleDir) |
| 24 | + |
| 25 | + stoppedConfig := util.GetDefaultGenerator() |
| 26 | + stoppedConfig.SetProcessArgs([]string{"true"}) |
| 27 | + runningConfig := util.GetDefaultGenerator() |
| 28 | + runningConfig.SetProcessArgs([]string{"sleep", "30"}) |
| 29 | + containerID := uuid.NewV4().String() |
| 30 | + testRuntime, _ := util.NewRuntime(util.RuntimeCommand, bundleDir) |
| 31 | + cases := []struct { |
| 32 | + config *generate.Generator |
| 33 | + id string |
| 34 | + action util.LifecycleAction |
| 35 | + errExpected bool |
| 36 | + // set true to check whether 'delete' takes effect and just check if 'delete' DOES NOT take effect |
| 37 | + effectCheck bool |
| 38 | + err error |
| 39 | + }{ |
| 40 | + // Note: the nil config test case should run first since we are re-using the bundle |
| 41 | + // delete without id |
| 42 | + {nil, "", util.LifecycleActionNone, false, false, specerror.NewError(specerror.DeleteWithoutIDGenError, fmt.Errorf("`delete` operation MUST generate an error if it is not provided the container ID"), rspecs.Version)}, |
| 43 | + // delete a created container |
| 44 | + {stoppedConfig, containerID, util.LifecycleActionCreate, false, true, specerror.NewError(specerror.DeleteNonStopGenError, fmt.Errorf("attempting to `delete` a container that is not `stopped` MUST generate an error"), rspecs.Version)}, |
| 45 | + // delete a running container |
| 46 | + {runningConfig, containerID, util.LifecycleActionCreate | util.LifecycleActionStart, false, true, specerror.NewError(specerror.DeleteNonStopGenError, fmt.Errorf("attempting to `delete` a container that is not `stopped` MUST generate an error"), rspecs.Version)}, |
| 47 | + } |
| 48 | + |
| 49 | + for _, c := range cases { |
| 50 | + config := util.LifecycleConfig{ |
| 51 | + Config: c.config, |
| 52 | + BundleDir: bundleDir, |
| 53 | + Actions: c.action, |
| 54 | + PreCreate: func(r *util.Runtime) error { |
| 55 | + r.SetID(c.id) |
| 56 | + return nil |
| 57 | + }, |
| 58 | + } |
| 59 | + util.RuntimeLifecycleValidate(config) |
| 60 | + // waiting the 'stoppedConfig' testcase to stop |
| 61 | + // the 'runningConfig' testcase sleeps 30 seconds, so 10 seconds are enough for this case |
| 62 | + testRuntime.SetID(c.id) |
| 63 | + util.WaitingForStatus(testRuntime, util.LifecycleStatusCreated|util.LifecycleStatusStopped, time.Second*10, time.Second*1) |
| 64 | + deletedErr := testRuntime.Delete() |
| 65 | + util.SpecErrorOK(t, (deletedErr == nil) == c.errExpected, c.err, deletedErr) |
| 66 | + |
| 67 | + if c.effectCheck { |
| 68 | + // waiting for the error of State, just in case the delete operation takes time |
| 69 | + util.WaitingForStatus(testRuntime, util.LifecycleActionNone, time.Second*10, time.Second*1) |
| 70 | + _, err := testRuntime.State() |
| 71 | + // err == nil means the 'delete' operation does NOT take effect |
| 72 | + util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.DeleteNonStopHaveNoEffect, fmt.Errorf("attempting to `delete` a container that is not `stopped` MUST have no effect on the container"), rspecs.Version), err) |
| 73 | + } |
| 74 | + |
| 75 | + // created and but deleted |
| 76 | + if (c.action&util.LifecycleActionCreate != 0) && (deletedErr != nil) { |
| 77 | + testRuntime.Kill("KILL") |
| 78 | + // waiting for the container to be killed, just in case the kill operation takes time |
| 79 | + util.WaitingForStatus(testRuntime, util.LifecycleStatusStopped, time.Second*10, time.Second*1) |
| 80 | + err = testRuntime.Delete() |
| 81 | + if err != nil { |
| 82 | + util.Fatal(err) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + t.AutoPlan() |
| 88 | +} |
0 commit comments