summaryrefslogtreecommitdiff
path: root/test/utils/matchers.go
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2019-10-04 10:09:24 -0700
committerJhon Honce <jhonce@redhat.com>2019-10-16 08:10:07 -0700
commit60d0be17fc55404929ad24d861f318968997458f (patch)
treee6aeecf49dd00a8e1f4a32cb3c0638b041fb0572 /test/utils/matchers.go
parent7825c5827e039aed6656f5df65de2262591681e6 (diff)
downloadpodman-60d0be17fc55404929ad24d861f318968997458f.tar.gz
podman-60d0be17fc55404929ad24d861f318968997458f.tar.bz2
podman-60d0be17fc55404929ad24d861f318968997458f.zip
Refactor tests when checking for error exit codes
Rather than checking for non-zero, we need to check for >0 to distinguish between timeouts and error exit codes. Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'test/utils/matchers.go')
-rw-r--r--test/utils/matchers.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/utils/matchers.go b/test/utils/matchers.go
new file mode 100644
index 000000000..07c1232e7
--- /dev/null
+++ b/test/utils/matchers.go
@@ -0,0 +1,61 @@
+package utils
+
+import (
+ "fmt"
+
+ "github.com/onsi/gomega/format"
+ "github.com/onsi/gomega/gexec"
+)
+
+// ExitWithError matches when assertion is > argument. Default 0
+// Modeled after the gomega Exit() matcher
+func ExitWithError(optionalExitCode ...int) *exitMatcher {
+ exitCode := 0
+ if len(optionalExitCode) > 0 {
+ exitCode = optionalExitCode[0]
+ }
+ return &exitMatcher{exitCode: exitCode}
+}
+
+type exitMatcher struct {
+ exitCode int
+ actualExitCode int
+}
+
+func (m *exitMatcher) Match(actual interface{}) (success bool, err error) {
+ exiter, ok := actual.(gexec.Exiter)
+ if !ok {
+ return false, fmt.Errorf("ExitWithError must be passed a gexec.Exiter (Missing method ExitCode() int) Got:\n#{format.Object(actual, 1)}")
+ }
+
+ m.actualExitCode = exiter.ExitCode()
+ if m.actualExitCode == -1 {
+ return false, nil
+ }
+ return m.actualExitCode > m.exitCode, nil
+}
+
+func (m *exitMatcher) FailureMessage(actual interface{}) (message string) {
+ if m.actualExitCode == -1 {
+ return "Expected process to exit. It did not."
+ }
+ return format.Message(m.actualExitCode, "to be greater than exit code:", m.exitCode)
+}
+
+func (m *exitMatcher) NegatedFailureMessage(actual interface{}) (message string) {
+ if m.actualExitCode == -1 {
+ return "you really shouldn't be able to see this!"
+ } else {
+ if m.exitCode == -1 {
+ return "Expected process not to exit. It did."
+ }
+ return format.Message(m.actualExitCode, "is less than or equal to exit code:", m.exitCode)
+ }
+}
+func (m *exitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
+ session, ok := actual.(*gexec.Session)
+ if ok {
+ return session.ExitCode() == -1
+ }
+ return true
+}