summaryrefslogtreecommitdiff
path: root/test/utils/matchers.go
blob: 0c0948e4b57b6a99424579190900febcf45406ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package utils

import (
	"encoding/json"
	"fmt"
	"net/url"

	"github.com/containers/common/pkg/config"
	. "github.com/onsi/gomega" //nolint:golint,stylecheck
	"github.com/onsi/gomega/format"
	"github.com/onsi/gomega/gexec"
	"github.com/onsi/gomega/matchers"
	"github.com/onsi/gomega/types"
)

// HaveActiveService verifies the given service is the active service.
func HaveActiveService(name interface{}) OmegaMatcher {
	return WithTransform(
		func(cfg *config.Config) string {
			return cfg.Engine.ActiveService
		},
		Equal(name))
}

type ServiceMatcher struct {
	types.GomegaMatcher
	Name                  interface{}
	URI                   interface{}
	Identity              interface{}
	failureMessage        string
	negatedFailureMessage string
}

func VerifyService(name, uri, identity interface{}) OmegaMatcher {
	return &ServiceMatcher{
		Name:     name,
		URI:      uri,
		Identity: identity,
	}
}

func (matcher *ServiceMatcher) Match(actual interface{}) (success bool, err error) {
	cfg, ok := actual.(*config.Config)
	if !ok {
		return false, fmt.Errorf("ServiceMatcher matcher expects a config.Config")
	}

	if _, err = url.Parse(matcher.URI.(string)); err != nil {
		return false, err
	}

	success, err = HaveKey(matcher.Name).Match(cfg.Engine.ServiceDestinations)
	if !success || err != nil {
		matcher.failureMessage = HaveKey(matcher.Name).FailureMessage(cfg.Engine.ServiceDestinations)
		matcher.negatedFailureMessage = HaveKey(matcher.Name).NegatedFailureMessage(cfg.Engine.ServiceDestinations)
		return
	}

	sd := cfg.Engine.ServiceDestinations[matcher.Name.(string)]
	success, err = Equal(matcher.URI).Match(sd.URI)
	if !success || err != nil {
		matcher.failureMessage = Equal(matcher.URI).FailureMessage(sd.URI)
		matcher.negatedFailureMessage = Equal(matcher.URI).NegatedFailureMessage(sd.URI)
		return
	}

	success, err = Equal(matcher.Identity).Match(sd.Identity)
	if !success || err != nil {
		matcher.failureMessage = Equal(matcher.Identity).FailureMessage(sd.Identity)
		matcher.negatedFailureMessage = Equal(matcher.Identity).NegatedFailureMessage(sd.Identity)
		return
	}

	return true, nil
}

func (matcher *ServiceMatcher) FailureMessage(_ interface{}) string {
	return matcher.failureMessage
}

func (matcher *ServiceMatcher) NegatedFailureMessage(_ interface{}) string {
	return matcher.negatedFailureMessage
}

type URLMatcher struct {
	matchers.EqualMatcher
}

// VerifyURL matches when actual is a valid URL and matches expected.
func VerifyURL(uri interface{}) OmegaMatcher {
	return &URLMatcher{matchers.EqualMatcher{Expected: uri}}
}

func (matcher *URLMatcher) Match(actual interface{}) (bool, error) {
	e, ok := matcher.Expected.(string)
	if !ok {
		return false, fmt.Errorf("VerifyURL requires string inputs %T is not supported", matcher.Expected)
	}
	eURI, err := url.Parse(e)
	if err != nil {
		return false, err
	}

	a, ok := actual.(string)
	if !ok {
		return false, fmt.Errorf("VerifyURL requires string inputs %T is not supported", actual)
	}
	aURI, err := url.Parse(a)
	if err != nil {
		return false, err
	}

	return (&matchers.EqualMatcher{Expected: eURI}).Match(aURI)
}

type ExitMatcher struct {
	types.GomegaMatcher
	Expected int
	Actual   int
}

// ExitWithError matches when assertion is > argument.  Default 0
// Modeled after the gomega Exit() matcher and also operates on sessions.
func ExitWithError(optionalExitCode ...int) *ExitMatcher {
	exitCode := 0
	if len(optionalExitCode) > 0 {
		exitCode = optionalExitCode[0]
	}
	return &ExitMatcher{Expected: exitCode}
}

// Match follows gexec.Matcher interface.
func (matcher *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)}")
	}

	matcher.Actual = exiter.ExitCode()
	if matcher.Actual == -1 {
		return false, nil
	}
	return matcher.Actual > matcher.Expected, nil
}

func (matcher *ExitMatcher) FailureMessage(_ interface{}) (message string) {
	if matcher.Actual == -1 {
		return "Expected process to exit.  It did not."
	}
	return format.Message(matcher.Actual, "to be greater than exit code: ", matcher.Expected)
}

func (matcher *ExitMatcher) NegatedFailureMessage(_ interface{}) (message string) {
	switch {
	case matcher.Actual == -1:
		return "you really shouldn't be able to see this!"
	case matcher.Expected == -1:
		return "Expected process not to exit.  It did."
	}
	return format.Message(matcher.Actual, "is less than or equal to exit code: ", matcher.Expected)
}

func (matcher *ExitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
	session, ok := actual.(*gexec.Session)
	if ok {
		return session.ExitCode() == -1
	}
	return true
}

type ValidJSONMatcher struct {
	types.GomegaMatcher
}

func BeValidJSON() *ValidJSONMatcher {
	return &ValidJSONMatcher{}
}

func (matcher *ValidJSONMatcher) Match(actual interface{}) (success bool, err error) {
	s, ok := actual.(string)
	if !ok {
		return false, fmt.Errorf("ValidJSONMatcher expects a string, not %q", actual)
	}

	var i interface{}
	if err := json.Unmarshal([]byte(s), &i); err != nil {
		return false, err
	}
	return true, nil
}

func (matcher *ValidJSONMatcher) FailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "to be valid JSON")
}

func (matcher *ValidJSONMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return format.Message(actual, "to _not_ be valid JSON")
}