From dd81f7ac616990728322044217c72690207fd799 Mon Sep 17 00:00:00 2001 From: Fang-Pen Lin Date: Fri, 16 Jun 2023 13:55:00 -0700 Subject: [PATCH] Pass in correct cwd value for hooks exe Signed-off-by: Fang-Pen Lin --- libpod/container_internal.go | 22 ++++++++++++++++++++-- libpod/container_internal_test.go | 11 +++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libpod/container_internal.go b/libpod/container_internal.go index dd1936ef449b..3188f1b40bac 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -2094,7 +2094,17 @@ func (c *Container) postDeleteHooks(ctx context.Context) error { hook := hook logrus.Debugf("container %s: invoke poststop hook %d, path %s", c.ID(), i, hook.Path) var stderr, stdout bytes.Buffer - hookErr, err := exec.Run(ctx, &hook, state, &stdout, &stderr, exec.DefaultPostKillTimeout) //nolint:staticcheck + hookErr, err := exec.RunWithOptions( + ctx, + exec.RunOptions{ + Hook: &hook, + Dir: c.bundlePath(), + State: state, + Stdout: &stdout, + Stderr: &stderr, + PostKillTimeout: exec.DefaultPostKillTimeout, + }, + ) if err != nil { logrus.Warnf("Container %s: poststop hook %d: %v", c.ID(), i, err) if hookErr != err { @@ -2223,7 +2233,15 @@ func (c *Container) setupOCIHooks(ctx context.Context, config *spec.Spec) (map[s } } - hookErr, err := exec.RuntimeConfigFilter(ctx, allHooks["precreate"], config, exec.DefaultPostKillTimeout) //nolint:staticcheck + hookErr, err := exec.RuntimeConfigFilterWithOptions( + ctx, + exec.RuntimeConfigFilterOptions{ + Hooks: allHooks["precreate"], + Dir: c.bundlePath(), + Config: config, + PostKillTimeout: exec.DefaultPostKillTimeout, + }, + ) if err != nil { logrus.Warnf("Container %s: precreate hook: %v", c.ID(), err) if hookErr != nil && hookErr != err { diff --git a/libpod/container_internal_test.go b/libpod/container_internal_test.go index b1e2bc1483e8..1c096d1d1690 100644 --- a/libpod/container_internal_test.go +++ b/libpod/container_internal_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "github.com/containers/storage/pkg/idtools" @@ -142,6 +143,7 @@ func TestPostDeleteHooks(t *testing.T) { statePath := filepath.Join(dir, "state") copyPath := filepath.Join(dir, "copy") + cwdPath := filepath.Join(dir, "cwd") c := Container{ runtime: &Runtime{}, config: &ContainerConfig{ @@ -169,6 +171,10 @@ func TestPostDeleteHooks(t *testing.T) { Path: hookPath, Args: []string{"sh", "-c", fmt.Sprintf("cp %s %s", statePath, copyPath)}, }, + rspec.Hook{ + Path: hookPath, + Args: []string{"sh", "-c", fmt.Sprintf("pwd >%s", cwdPath)}, + }, }, }, }, @@ -188,6 +194,11 @@ func TestPostDeleteHooks(t *testing.T) { assert.Regexp(t, stateRegexp, string(content)) }) } + content, err := os.ReadFile(cwdPath) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, strings.TrimSuffix(string(content), "\n"), dir) } func init() {