Fix hook timeout review feedback

Signed-off-by: chlins <chlins.zhang@gmail.com>
This commit is contained in:
chlins
2026-08-03 13:28:31 +08:00
parent 950e4adc73
commit 2649b2554c
3 changed files with 130 additions and 22 deletions
+57
View File
@@ -177,6 +177,15 @@ func TestExecutePodCommand(t *testing.T) {
hookError: errors.New("hook error"),
expectedError: "hook error",
},
{
name: "stream deadline exceeded before local timeout",
command: []string{"some", "command"},
expectedContainerName: "foo",
expectedErrorMode: v1.HookErrorModeFail,
expectedTimeout: defaultTimeout,
hookError: context.DeadlineExceeded,
expectedError: context.DeadlineExceeded.Error(),
},
{
// Timeouts from pod annotations go through time.ParseDuration, which accepts
// negative values. Without clamping, the hook would run with no timeout at all.
@@ -264,6 +273,54 @@ func TestExecutePodCommand(t *testing.T) {
}
}
func TestNormalizeExecHookError(t *testing.T) {
hookErr := errors.New("hook error")
tests := []struct {
name string
streamErr error
contextErr error
expectedError string
preserveStreamErr bool
}{
{
name: "local context deadline exceeded",
streamErr: context.DeadlineExceeded,
contextErr: context.DeadlineExceeded,
expectedError: "timed out after 30s",
},
{
name: "stream deadline exceeded before local timeout",
streamErr: context.DeadlineExceeded,
expectedError: context.DeadlineExceeded.Error(),
preserveStreamErr: true,
},
{
name: "ordinary hook error",
streamErr: hookErr,
expectedError: hookErr.Error(),
preserveStreamErr: true,
},
{
name: "no errors",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := normalizeExecHookError(test.streamErr, test.contextErr, defaultTimeout)
if test.expectedError == "" {
require.NoError(t, err)
return
}
require.EqualError(t, err, test.expectedError)
if test.preserveStreamErr && err != test.streamErr {
t.Fatalf("expected stream error to be returned unchanged")
}
})
}
}
func TestEnsureContainerExists(t *testing.T) {
pod := &corev1api.Pod{
Spec: corev1api.PodSpec{