|
1 | 1 | package util |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "io/ioutil" |
6 | 7 | "os" |
|
21 | 22 | RuntimeCommand = "runc" |
22 | 23 | ) |
23 | 24 |
|
| 25 | +// LifecycleAction defines the phases will be called. |
| 26 | +type LifecycleAction int |
| 27 | + |
| 28 | +const ( |
| 29 | + // LifecycleActionCreate creates a container |
| 30 | + LifecycleActionCreate = 1 << iota |
| 31 | + // LifecycleActionStart starts a container |
| 32 | + LifecycleActionStart |
| 33 | + // LifecycleActionDelete deletes a container |
| 34 | + LifecycleActionDelete |
| 35 | +) |
| 36 | + |
| 37 | +// LifecycleStatus follows https://github.com/opencontainers/runtime-spec/blob/master/runtime.md#state |
| 38 | +type LifecycleStatus int |
| 39 | + |
| 40 | +const ( |
| 41 | + // LifecycleStatusCreating "creating" |
| 42 | + LifecycleStatusCreating = 1 << iota |
| 43 | + // LifecycleStatusCreated "created" |
| 44 | + LifecycleStatusCreated |
| 45 | + // LifecycleStatusRunning "running" |
| 46 | + LifecycleStatusRunning |
| 47 | + // LifecycleStatusStopped "stopped" |
| 48 | + LifecycleStatusStopped |
| 49 | +) |
| 50 | + |
| 51 | +var lifecycleStatusMap = map[string]LifecycleStatus{ |
| 52 | + "creating": LifecycleStatusCreating, |
| 53 | + "created": LifecycleStatusCreated, |
| 54 | + "running": LifecycleStatusRunning, |
| 55 | + "stopped": LifecycleStatusStopped, |
| 56 | +} |
| 57 | + |
| 58 | +// LifecycleConfig includes |
| 59 | +// 1. Actions to define the default running lifecycles. |
| 60 | +// 2. Four phases for user to add his/her own operations. |
| 61 | +type LifecycleConfig struct { |
| 62 | + Actions LifecycleAction |
| 63 | + PreCreate func(runtime *Runtime) error |
| 64 | + PostCreate func(runtime *Runtime) error |
| 65 | + PreDelete func(runtime *Runtime) error |
| 66 | + PostDelete func(runtime *Runtime) error |
| 67 | +} |
| 68 | + |
24 | 69 | // PreFunc initializes the test environment after preparing the bundle |
25 | 70 | // but before creating the container. |
26 | 71 | type PreFunc func(string) error |
@@ -78,6 +123,28 @@ func GetDefaultGenerator() *generate.Generator { |
78 | 123 | return &g |
79 | 124 | } |
80 | 125 |
|
| 126 | +// WaitingForStatus waits an expected runtime status, return error if |
| 127 | +// 1. fail to query the status |
| 128 | +// 2. timeout |
| 129 | +func WaitingForStatus(r Runtime, status LifecycleStatus, retryTimeout time.Duration, pollInterval time.Duration) error { |
| 130 | + for start := time.Now(); time.Since(start) < retryTimeout; time.Sleep(pollInterval) { |
| 131 | + state, err := r.State() |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + if v, ok := lifecycleStatusMap[state.Status]; ok { |
| 136 | + if status&v != 0 { |
| 137 | + return nil |
| 138 | + } |
| 139 | + } else { |
| 140 | + // In spec, it says 'Additional values MAY be defined by the runtime'. |
| 141 | + continue |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return errors.New("timeout in waiting for the container status") |
| 146 | +} |
| 147 | + |
81 | 148 | // RuntimeInsideValidate runs runtimetest inside a container. |
82 | 149 | func RuntimeInsideValidate(g *generate.Generator, f PreFunc) (err error) { |
83 | 150 | bundleDir, err := PrepareBundle() |
@@ -184,3 +251,74 @@ func RuntimeOutsideValidate(g *generate.Generator, f AfterFunc) error { |
184 | 251 | } |
185 | 252 | return nil |
186 | 253 | } |
| 254 | + |
| 255 | +// RuntimeLifecycleValidate validates runtime lifecycle. |
| 256 | +func RuntimeLifecycleValidate(g *generate.Generator, config LifecycleConfig) error { |
| 257 | + bundleDir, err := PrepareBundle() |
| 258 | + if err != nil { |
| 259 | + return err |
| 260 | + } |
| 261 | + r, err := NewRuntime(RuntimeCommand, bundleDir) |
| 262 | + if err != nil { |
| 263 | + os.RemoveAll(bundleDir) |
| 264 | + return err |
| 265 | + } |
| 266 | + defer r.Clean(true, true) |
| 267 | + err = r.SetConfig(g) |
| 268 | + if err != nil { |
| 269 | + return err |
| 270 | + } |
| 271 | + r.SetID(uuid.NewV4().String()) |
| 272 | + |
| 273 | + if config.PreCreate != nil { |
| 274 | + if err := config.PreCreate(&r); err != nil { |
| 275 | + return err |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + if config.Actions&LifecycleActionCreate != 0 { |
| 280 | + stderr, err := r.Create() |
| 281 | + if err != nil { |
| 282 | + os.Stderr.WriteString("failed to create the container\n") |
| 283 | + os.Stderr.Write(stderr) |
| 284 | + return err |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | + if config.PostCreate != nil { |
| 289 | + if err := config.PostCreate(&r); err != nil { |
| 290 | + return err |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + if config.Actions&LifecycleActionStart != 0 { |
| 295 | + stderr, err := r.Start() |
| 296 | + if err != nil { |
| 297 | + os.Stderr.WriteString("failed to start the container\n") |
| 298 | + os.Stderr.Write(stderr) |
| 299 | + return err |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + if config.PreDelete != nil { |
| 304 | + if err := config.PreDelete(&r); err != nil { |
| 305 | + return err |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + if config.Actions&LifecycleActionDelete != 0 { |
| 310 | + stderr, err := r.Delete() |
| 311 | + if err != nil { |
| 312 | + os.Stderr.WriteString("failed to delete the container\n") |
| 313 | + os.Stderr.Write(stderr) |
| 314 | + return err |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + if config.PostDelete != nil { |
| 319 | + if err := config.PostDelete(&r); err != nil { |
| 320 | + return err |
| 321 | + } |
| 322 | + } |
| 323 | + return nil |
| 324 | +} |
0 commit comments