File: killsig.go

package info (click to toggle)
golang-github-opencontainers-runtime-tools 0.9.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,108 kB
  • sloc: sh: 557; makefile: 104
file content (65 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/mndrix/tap-go"
	rspecs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/opencontainers/runtime-tools/specerror"
	"github.com/opencontainers/runtime-tools/validation/util"
	uuid "github.com/satori/go.uuid"
)

var signals = []string{
	"TERM",
	"USR1",
	"USR2",
}

func main() {
	t := tap.New()
	t.Header(0)
	bundleDir, err := util.PrepareBundle()
	if err != nil {
		util.Fatal(err)
	}
	defer os.RemoveAll(bundleDir)

	containerID := uuid.NewV4().String()
	sigConfig, err := util.GetDefaultGenerator()
	if err != nil {
		os.RemoveAll(bundleDir)
		util.Fatal(err)
	}
	rootDir := filepath.Join(bundleDir, sigConfig.Spec().Root.Path)
	for _, signal := range signals {
		sigConfig.SetProcessArgs([]string{"sh", "-c", fmt.Sprintf("trap 'touch /%s' %s; sleep 10 & wait $!", signal, signal)})
		config := util.LifecycleConfig{
			Config:    sigConfig,
			BundleDir: bundleDir,
			Actions:   util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
			PreCreate: func(r *util.Runtime) error {
				r.SetID(containerID)
				return nil
			},
			PreDelete: func(r *util.Runtime) error {
				util.WaitingForStatus(*r, util.LifecycleStatusRunning, time.Second*5, time.Second*1)
				err = r.Kill(signal)
				// wait before the container been deleted
				util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*5, time.Second*1)
				return err
			},
		}
		err = util.RuntimeLifecycleValidate(config)
		if err != nil {
			util.SpecErrorOK(t, false, specerror.NewError(specerror.KillSignalImplement, fmt.Errorf("`kill` operation MUST send the specified signal to the container process"), rspecs.Version), err)
		} else {
			_, err = os.Stat(filepath.Join(rootDir, signal))
			util.SpecErrorOK(t, err == nil, specerror.NewError(specerror.KillSignalImplement, fmt.Errorf("`kill` operation MUST send the specified signal to the container process"), rspecs.Version), err)
		}
	}
	t.AutoPlan()
}