Skip to content

Commit

Permalink
Tests to error on timeout
Browse files Browse the repository at this point in the history
Signed-off-by: apostasie <[email protected]>
  • Loading branch information
apostasie committed Feb 14, 2025
1 parent 166b078 commit cfc556d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 10 additions & 5 deletions cmd/nerdctl/system/system_events_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func TestEventFilters(t *testing.T) {
Command: testEventFilterExecutor,
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(data.Get("output")),
ExitCode: test.ExitCodeTimeout,
Output: test.Contains(data.Get("output")),
}
},
Data: test.WithData("filter", "event=START").
Expand All @@ -53,7 +54,8 @@ func TestEventFilters(t *testing.T) {
Command: testEventFilterExecutor,
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(data.Get("output")),
ExitCode: test.ExitCodeTimeout,
Output: test.Contains(data.Get("output")),
}
},
Data: test.WithData("filter", "event=start").
Expand All @@ -65,7 +67,8 @@ func TestEventFilters(t *testing.T) {
Command: testEventFilterExecutor,
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(data.Get("output")),
ExitCode: test.ExitCodeTimeout,
Output: test.Contains(data.Get("output")),
}
},
Data: test.WithData("filter", "event=unknown").
Expand All @@ -76,7 +79,8 @@ func TestEventFilters(t *testing.T) {
Command: testEventFilterExecutor,
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(data.Get("output")),
ExitCode: test.ExitCodeTimeout,
Output: test.Contains(data.Get("output")),
}
},
Data: test.WithData("filter", "status=start").
Expand All @@ -88,7 +92,8 @@ func TestEventFilters(t *testing.T) {
Command: testEventFilterExecutor,
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(data.Get("output")),
ExitCode: test.ExitCodeTimeout,
Output: test.Contains(data.Get("output")),
}
},
Data: test.WithData("filter", "status=unknown").
Expand Down
10 changes: 7 additions & 3 deletions pkg/testutil/test/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

const ExitCodeGenericFail = -1
const ExitCodeNoCheck = -2
const ExitCodeTimeout = -3

// GenericCommand is a concrete Command implementation
type GenericCommand struct {
Expand Down Expand Up @@ -150,11 +151,14 @@ func (gc *GenericCommand) Run(expect *Expected) {
debug := result.String() + "Env:\n" + strings.Join(env, "\n")
// ExitCode goes first
if expect.ExitCode == ExitCodeNoCheck { //nolint:revive
// -2 means we do not care at all about exit code
// ExitCodeNoCheck means we do not care at all about exit code. It can be a failure, a success, or a timeout.
} else if expect.ExitCode == ExitCodeGenericFail {
// -1 means any error
// ExitCodeGenericFail means we expect an error (excluding timeout).
assert.Assert(gc.t, result.ExitCode != 0,
"Expected exit code to be different than 0\n"+debug)
"Command succeeded while we were expecting an error\n"+debug)
} else if result.Timeout {
assert.Assert(gc.t, expect.ExitCode == ExitCodeTimeout,
"Command unexpectedly timed-out\n"+debug)
} else {
assert.Assert(gc.t, expect.ExitCode == result.ExitCode,
fmt.Sprintf("Expected exit code: %d\n", expect.ExitCode)+debug)
Expand Down

0 comments on commit cfc556d

Please sign in to comment.