Skip to content

Commit

Permalink
Pass in correct cwd value for hooks exe
Browse files Browse the repository at this point in the history
Signed-off-by: Fang-Pen Lin <[email protected]>
  • Loading branch information
fangpenlin authored and ashley-cui committed Jul 13, 2023
1 parent f654f7c commit 47e6ce1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
35 changes: 21 additions & 14 deletions libpod/container_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2094,14 +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
opts := exec.RunOptions{
Hook: &hook,
State: state,
Stdout: &stdout,
Stderr: &stderr,
PostKillTimeout: exec.DefaultPostKillTimeout,
}
hookErr, err := exec.RunWithOptions(ctx, opts) //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 {
Expand Down Expand Up @@ -2229,12 +2232,16 @@ func (c *Container) setupOCIHooks(ctx context.Context, config *spec.Spec) (map[s
return nil, err
}
}
opts := exec.RuntimeConfigFilterOptions{
Hooks: allHooks["precreate"],
Config: config,
PostKillTimeout: exec.DefaultPostKillTimeout,
}
hookErr, err := exec.RuntimeConfigFilterWithOptions(ctx, opts) //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 {
Expand Down
11 changes: 11 additions & 0 deletions libpod/container_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/containers/storage/pkg/idtools"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)},
},
},
},
},
Expand All @@ -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() {
Expand Down

0 comments on commit 47e6ce1

Please sign in to comment.