/
hooks_stdin.go
164 lines (148 loc) · 4.77 KB
/
hooks_stdin.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"time"
multierror "github.com/hashicorp/go-multierror"
tap "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"
)
func stdinStateCheck(outputDir, hookName string, expectedState rspecs.State) (errs *multierror.Error) {
var state rspecs.State
data, err := ioutil.ReadFile(filepath.Join(outputDir, hookName))
if err != nil {
errs = multierror.Append(errs, err)
return
}
err = json.Unmarshal(data, &state)
if err != nil {
errs = multierror.Append(errs, err)
return
}
if state.ID != expectedState.ID {
err = fmt.Errorf("wrong container ID %q in the stdin of %s hook, expected %q", state.ID, hookName, expectedState.ID)
errs = multierror.Append(errs, err)
}
if state.Bundle != expectedState.Bundle {
err = fmt.Errorf("wrong bundle directory %q in the stdin of %s hook, expected %q", state.Bundle, hookName, expectedState.Bundle)
errs = multierror.Append(errs, err)
}
if hookName != "poststop" && state.Pid != expectedState.Pid {
err = fmt.Errorf("wrong container process ID %q in the stdin of %s hook, expected %q", state.Version, hookName, expectedState.Version)
errs = multierror.Append(errs, err)
}
if !reflect.DeepEqual(state.Annotations, expectedState.Annotations) {
err = fmt.Errorf("wrong annotations \"%v\" in the stdin of %s hook, expected \"%v\"", state.Annotations, hookName, expectedState.Annotations)
errs = multierror.Append(errs, err)
}
switch hookName {
case "prestart":
if state.Status != "created" {
err = fmt.Errorf("wrong status %q in the stdin of %s hook, expected %q", state.Status, hookName, "created")
errs = multierror.Append(errs, err)
}
case "poststart":
if state.Status != "running" {
err = fmt.Errorf("wrong status %q in the stdin of %s hook, expected %q", state.Status, hookName, "running")
errs = multierror.Append(errs, err)
}
case "poststop":
if state.Status == "" {
err = fmt.Errorf("status in the stdin of %s hook should not be empty", hookName)
errs = multierror.Append(errs, err)
}
default:
err = fmt.Errorf("internal error, unexpected hook name %q", hookName)
errs = multierror.Append(errs, err)
}
return
}
func main() {
t := tap.New()
t.Header(0)
bundleDir, err := util.PrepareBundle()
if err != nil {
util.Fatal(err)
}
containerID := uuid.NewV4().String()
defer os.RemoveAll(bundleDir)
var containerPid int
annotationKey := "org.opencontainers.runtime-tools"
annotationValue := "hook stdin test"
g, err := util.GetDefaultGenerator()
if err != nil {
util.Fatal(err)
}
outputDir := filepath.Join(bundleDir, g.Spec().Root.Path)
timeout := 1
g.AddAnnotation(annotationKey, annotationValue)
g.AddPreStartHook(rspecs.Hook{
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"),
Args: []string{
"sh", "-c", fmt.Sprintf("cat > %s", filepath.Join(outputDir, "prestart")),
},
Timeout: &timeout,
})
g.AddPostStartHook(rspecs.Hook{
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"),
Args: []string{
"sh", "-c", fmt.Sprintf("cat > %s", filepath.Join(outputDir, "poststart")),
},
Timeout: &timeout,
})
g.AddPostStopHook(rspecs.Hook{
Path: filepath.Join(bundleDir, g.Spec().Root.Path, "/bin/sh"),
Args: []string{
"sh", "-c", fmt.Sprintf("cat > %s", filepath.Join(outputDir, "poststop")),
},
Timeout: &timeout,
})
g.SetProcessArgs([]string{"true"})
config := util.LifecycleConfig{
BundleDir: bundleDir,
Config: g,
Actions: util.LifecycleActionCreate | util.LifecycleActionStart | util.LifecycleActionDelete,
PreCreate: func(r *util.Runtime) error {
r.SetID(containerID)
return nil
},
PreDelete: func(r *util.Runtime) error {
state, err := r.State()
if err != nil {
return err
}
containerPid = state.Pid
util.WaitingForStatus(*r, util.LifecycleStatusStopped, time.Second*10, time.Second)
return nil
},
}
err = util.RuntimeLifecycleValidate(config)
if err != nil {
t.Fail(err.Error())
}
expectedState := rspecs.State{
Pid: containerPid,
ID: containerID,
Bundle: bundleDir,
Annotations: map[string]string{annotationKey: annotationValue},
}
for _, file := range []string{"prestart", "poststart", "poststop"} {
errs := stdinStateCheck(outputDir, file, expectedState)
var newError error
if errs == nil {
newError = errors.New("")
} else {
newError = errors.New(errs.Error())
}
util.SpecErrorOK(t, errs.ErrorOrNil() == nil, specerror.NewError(specerror.PosixHooksStateToStdin, fmt.Errorf("the state of the container MUST be passed to %q hook over stdin", file), rspecs.Version), newError)
}
t.AutoPlan()
}