From 6cbd17c0f4e5d6ddc47c8400a2a53a4dfc000ae0 Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Tue, 29 Aug 2023 13:48:51 -0600 Subject: [PATCH] CI: e2e: new ginkgo matcher, ExitCleanly() Combined test for (exitcode == 0) && (nothing on stderr). Returns more useful diagnostic messages than the default: old: Expected N to equal 0 new: Command failed with exit status N new: Unexpected warnings seen on stderr: "...." Signed-off-by: Ed Santiago --- test/utils/matchers.go | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/utils/matchers.go b/test/utils/matchers.go index c56bd55c38..da40dba20d 100644 --- a/test/utils/matchers.go +++ b/test/utils/matchers.go @@ -168,6 +168,57 @@ func (matcher *ExitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool { return true } +// ExitCleanly asserts that a PodmanSession exits 0 and with no stderr +func ExitCleanly() types.GomegaMatcher { + return &exitCleanlyMatcher{} +} + +type exitCleanlyMatcher struct { + msg string +} + +type podmanSession interface { + ExitCode() int + ErrorToString() string +} + +func (matcher *exitCleanlyMatcher) Match(actual interface{}) (success bool, err error) { + session, ok := actual.(podmanSession) + if !ok { + return false, fmt.Errorf("ExitCleanly must be passed a PodmanSession; Got:\n %+v\n%q", actual, format.Object(actual, 1)) + } + + exitcode := session.ExitCode() + stderr := session.ErrorToString() + if exitcode != 0 { + matcher.msg = fmt.Sprintf("Command failed with exit status %d", exitcode) + if stderr != "" { + matcher.msg += ". See above for error message." + } + return false, nil + } + + // FIXME: #19809, "failed to connect to syslog" warnings on f38 + // FIXME: so, until that is fixed, don't check stderr if containerized + if !Containerized() { + if stderr != "" { + matcher.msg = fmt.Sprintf("Unexpected warnings seen on stderr: %q", stderr) + return false, nil + } + } + + return true, nil +} + +func (matcher *exitCleanlyMatcher) FailureMessage(_ interface{}) (message string) { + return matcher.msg +} + +func (matcher *exitCleanlyMatcher) NegatedFailureMessage(_ interface{}) (message string) { + // FIXME - I see no situation in which we could ever want this? + return matcher.msg + " (NOT!)" +} + type ValidJSONMatcher struct { types.GomegaMatcher }