Skip to content

Commit 7990d0a

Browse files
committed
Add cwd argument for hooks exec Run func
Signed-off-by: Fang-Pen Lin <[email protected]>
1 parent 4b1b6ad commit 7990d0a

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

pkg/hooks/exec/exec.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ const DefaultPostKillTimeout = time.Duration(10) * time.Second
1818

1919
// Run executes the hook and waits for it to complete or for the
2020
// context or hook-specified timeout to expire.
21-
func Run(ctx context.Context, hook *rspec.Hook, state []byte, stdout io.Writer, stderr io.Writer, postKillTimeout time.Duration) (hookErr, err error) {
21+
func Run(ctx context.Context, hook *rspec.Hook, cwd string, state []byte, stdout io.Writer, stderr io.Writer, postKillTimeout time.Duration) (hookErr, err error) {
2222
cmd := osexec.Cmd{
2323
Path: hook.Path,
2424
Args: hook.Args,
2525
Env: hook.Env,
26+
Dir: cwd,
2627
Stdin: bytes.NewReader(state),
2728
Stdout: stdout,
2829
Stderr: stderr,

pkg/hooks/exec/exec_test.go

+49-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ func TestRun(t *testing.T) {
2929
Args: []string{"sh", "-c", "cat"},
3030
}
3131
var stderr, stdout bytes.Buffer
32-
hookErr, err := Run(ctx, hook, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
32+
cwd, err := os.Getwd()
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
3337
if err != nil {
3438
t.Fatal(err)
3539
}
@@ -46,7 +50,11 @@ func TestRunIgnoreOutput(t *testing.T) {
4650
Path: path,
4751
Args: []string{"sh", "-c", "cat"},
4852
}
49-
hookErr, err := Run(ctx, hook, []byte("{}"), nil, nil, DefaultPostKillTimeout)
53+
cwd, err := os.Getwd()
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), nil, nil, DefaultPostKillTimeout)
5058
if err != nil {
5159
t.Fatal(err)
5260
}
@@ -60,7 +68,11 @@ func TestRunFailedStart(t *testing.T) {
6068
hook := &rspec.Hook{
6169
Path: "/does/not/exist",
6270
}
63-
hookErr, err := Run(ctx, hook, []byte("{}"), nil, nil, DefaultPostKillTimeout)
71+
cwd, err := os.Getwd()
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), nil, nil, DefaultPostKillTimeout)
6476
if err == nil {
6577
t.Fatal("unexpected success")
6678
}
@@ -91,6 +103,10 @@ func parseEnvironment(input string) (env map[string]string, err error) {
91103

92104
func TestRunEnvironment(t *testing.T) {
93105
ctx := context.Background()
106+
cwd, err := os.Getwd()
107+
if err != nil {
108+
t.Fatal(err)
109+
}
94110
hook := &rspec.Hook{
95111
Path: path,
96112
Args: []string{"sh", "-c", "env"},
@@ -125,7 +141,7 @@ func TestRunEnvironment(t *testing.T) {
125141
t.Run(test.name, func(t *testing.T) {
126142
var stderr, stdout bytes.Buffer
127143
hook.Env = test.env
128-
hookErr, err := Run(ctx, hook, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
144+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
129145
if err != nil {
130146
t.Fatal(err)
131147
}
@@ -143,11 +159,34 @@ func TestRunEnvironment(t *testing.T) {
143159
}
144160
}
145161

162+
func TestRunCwd(t *testing.T) {
163+
ctx := context.Background()
164+
hook := &rspec.Hook{
165+
Path: path,
166+
Args: []string{"sh", "-c", "pwd"},
167+
}
168+
cwd := "/path/to/container/userdata"
169+
var stderr, stdout bytes.Buffer
170+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
if hookErr != nil {
175+
t.Fatal(hookErr)
176+
}
177+
assert.Equal(t, "", stderr.String())
178+
assert.Equal(t, stdout.String(), cwd)
179+
}
180+
146181
func TestRunCancel(t *testing.T) {
147182
hook := &rspec.Hook{
148183
Path: path,
149184
Args: []string{"sh", "-c", "echo waiting; sleep 2; echo done"},
150185
}
186+
cwd, err := os.Getwd()
187+
if err != nil {
188+
t.Fatal(err)
189+
}
151190
one := 1
152191
for _, tt := range []struct {
153192
name string
@@ -186,7 +225,7 @@ func TestRunCancel(t *testing.T) {
186225
defer cancel()
187226
}
188227
hook.Timeout = test.hookTimeout
189-
hookErr, err := Run(ctx, hook, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
228+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), &stdout, &stderr, DefaultPostKillTimeout)
190229
assert.Equal(t, test.expectedRunError, err)
191230
if test.expectedHookError == "" {
192231
if hookErr != nil {
@@ -204,11 +243,15 @@ func TestRunCancel(t *testing.T) {
204243
func TestRunKillTimeout(t *testing.T) {
205244
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(500)*time.Millisecond)
206245
defer cancel()
246+
cwd, err := os.Getwd()
247+
if err != nil {
248+
t.Fatal(err)
249+
}
207250
hook := &rspec.Hook{
208251
Path: path,
209252
Args: []string{"sh", "-c", "sleep 1"},
210253
}
211-
hookErr, err := Run(ctx, hook, []byte("{}"), nil, nil, time.Duration(0))
254+
hookErr, err := Run(ctx, hook, cwd, []byte("{}"), nil, nil, time.Duration(0))
212255
assert.Equal(t, context.DeadlineExceeded, err)
213256
assert.Regexp(t, "^(failed to reap process within 0s of the kill signal|executing \\[sh -c sleep 1]: signal: killed)$", hookErr)
214257
}

pkg/hooks/exec/runtimeconfigfilter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ var spewConfig = spew.ConfigState{
2525
// passing container state on their standard input,
2626
// RuntimeConfigFilter passes the proposed runtime configuration (and
2727
// reads back a possibly-altered form from their standard output).
28-
func RuntimeConfigFilter(ctx context.Context, hooks []spec.Hook, config *spec.Spec, postKillTimeout time.Duration) (hookErr, err error) {
28+
func RuntimeConfigFilter(ctx context.Context, hooks []spec.Hook, cwd string, config *spec.Spec, postKillTimeout time.Duration) (hookErr, err error) {
2929
data, err := json.Marshal(config)
3030
if err != nil {
3131
return nil, err
3232
}
3333
for i, hook := range hooks {
3434
hook := hook
3535
var stdout bytes.Buffer
36-
hookErr, err = Run(ctx, &hook, data, &stdout, nil, postKillTimeout)
36+
hookErr, err = Run(ctx, &hook, cwd, data, &stdout, nil, postKillTimeout)
3737
if err != nil {
3838
return hookErr, err
3939
}

pkg/hooks/exec/runtimeconfigfilter_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func TestRuntimeConfigFilter(t *testing.T) {
1717
fileMode := os.FileMode(0o600)
1818
rootUint32 := uint32(0)
1919
binUser := int(1)
20+
cwd, err := os.Getwd()
21+
if err != nil {
22+
t.Fatal(err)
23+
}
2024
for _, tt := range []struct {
2125
name string
2226
contextTimeout time.Duration
@@ -243,7 +247,7 @@ func TestRuntimeConfigFilter(t *testing.T) {
243247
ctx, cancel = context.WithTimeout(ctx, test.contextTimeout)
244248
defer cancel()
245249
}
246-
hookErr, err := RuntimeConfigFilter(ctx, test.hooks, test.input, DefaultPostKillTimeout)
250+
hookErr, err := RuntimeConfigFilter(ctx, test.hooks, cwd, test.input, DefaultPostKillTimeout)
247251
if test.expectedRunErrorString != "" {
248252
// We have to compare the error strings in that case because
249253
// errors.Is works differently.

0 commit comments

Comments
 (0)