|
| 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 | + |
| 31 | + cases := []struct { |
| 32 | + config *generate.Generator |
| 33 | + id string |
| 34 | + action util.LifecycleAction |
| 35 | + errExpected bool |
| 36 | + err error |
| 37 | + }{ |
| 38 | + // Note: the nil config test case should run first since we are re-using the bundle |
| 39 | + // kill without id |
| 40 | + {nil, "", util.LifecycleActionNone, false, specerror.NewError(specerror.KillWithoutIDGenError, fmt.Errorf("`kill` operation MUST generate an error if it is not provided the container ID"), rspecs.Version)}, |
| 41 | + // kill a non exist container |
| 42 | + {nil, containerID, util.LifecycleActionNone, false, specerror.NewError(specerror.KillNonCreateRunGenError, fmt.Errorf("attempting to send a signal to a container that is neither `created` nor `running` MUST generate an error"), rspecs.Version)}, |
| 43 | + // kill a created |
| 44 | + {stoppedConfig, containerID, util.LifecycleActionCreate | util.LifecycleActionDelete, true, specerror.NewError(specerror.KillSignalImplement, fmt.Errorf("`kill` operation MUST send the specified signal to the container process"), rspecs.Version)}, |
| 45 | + // kill a stopped |
| 46 | + {stoppedConfig, containerID, util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, false, specerror.NewError(specerror.KillSignalImplement, fmt.Errorf("`kill` operation MUST send the specified signal to the container process"), rspecs.Version)}, |
| 47 | + // kill a running |
| 48 | + {runningConfig, containerID, util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete, true, specerror.NewError(specerror.KillSignalImplement, fmt.Errorf("`kill` operation MUST send the specified signal to the container process"), rspecs.Version)}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, c := range cases { |
| 52 | + config := util.LifecycleConfig{ |
| 53 | + Config: c.config, |
| 54 | + BundleDir: bundleDir, |
| 55 | + Actions: c.action, |
| 56 | + PreCreate: func(r *util.Runtime) error { |
| 57 | + r.SetID(c.id) |
| 58 | + return nil |
| 59 | + }, |
| 60 | + PreDelete: func(r *util.Runtime) error { |
| 61 | + // waiting the 'stoppedConfig' testcase to stop |
| 62 | + // the 'runningConfig' testcase sleeps 30 seconds, so 10 seconds are enough for this case |
| 63 | + util.WaitingForStatus(*r, util.LifecycleStatusCreated|util.LifecycleStatusStopped, time.Second*10, time.Second*1) |
| 64 | + // KILL MUST be supported and KILL cannot be trapped |
| 65 | + err := r.Kill("KILL") |
| 66 | + util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second*1) |
| 67 | + return err |
| 68 | + }, |
| 69 | + } |
| 70 | + err := util.RuntimeLifecycleValidate(config) |
| 71 | + util.SpecErrorOK(t, (err == nil) == c.errExpected, c.err, err) |
| 72 | + } |
| 73 | + |
| 74 | + t.AutoPlan() |
| 75 | +} |
0 commit comments