forked from maistra/istio-operator
/
assertions.go
141 lines (119 loc) · 4.71 KB
/
assertions.go
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
package test
import (
"testing"
"k8s.io/apimachinery/pkg/runtime"
clienttesting "k8s.io/client-go/testing"
)
// Assert creates a new ActionAssertionFactory for the given verb.
// Use "*" to match any verb. Unless the filter is narrowed, e.g. using On(),
// the ActionAssertion created by this factory will match all resource types in
// all namespaces (i.e. the other filter fields are initialized to "*").
func Assert(verb string) *ActionAssertionFactory {
return &ActionAssertionFactory{
AbstractActionFilter: AbstractActionFilter{
Verb: verb,
Namespace: "*",
Name: "*",
Resource: "*",
Subresource: "*",
},
}
}
// ActionAssertionFactory serves as a factory for building ActionAssertion types
// that filter actions based on verb, resource, subresource, namespace, and name.
type ActionAssertionFactory struct {
AbstractActionFilter
}
// On initializes the resource and subresource name to which the created
// assertion should apply. resource parameter should be specified using a slash
// between resource an subresource, e.g. deployments/status. Use "*" to match
// all resources.
func (f *ActionAssertionFactory) On(resource string) *ActionAssertionFactory {
f.AbstractActionFilter.On(resource)
return f
}
// Version initializes the version whithin which the created assertion should apply.
// Use "*" to match all versions.
func (f *ActionAssertionFactory) Version(version string) *ActionAssertionFactory {
f.AbstractActionFilter.Version(version)
return f
}
// In initializes the namespace whithin which the created assertion should apply.
// Use "*" to match all namespaces.
func (f *ActionAssertionFactory) In(namespace string) *ActionAssertionFactory {
f.AbstractActionFilter.In(namespace)
return f
}
// Named initializes the name of the resource to which the created assertion
// should apply. Use "*" to match all names.
func (f *ActionAssertionFactory) Named(name string) *ActionAssertionFactory {
f.AbstractActionFilter.Named(name)
return f
}
// SeenCountIs returns an ActionAssertion object that asserts the specified event
// has been seen the expected number of times.
func (f *ActionAssertionFactory) SeenCountIs(expected int) ActionAssertion {
return &ActionSeenCountAssertion{AbstractActionFilter: f.AbstractActionFilter, Expected: expected}
}
// IsSeen returns an ActionAssertion object that asserts the specified event
// has been seen.
func (f *ActionAssertionFactory) IsSeen() ActionAssertion {
return &ActionIsSeenAssertion{AbstractActionFilter: f.AbstractActionFilter}
}
// IsNotSeen returns an ActionAssertion object that asserts the specified
// event has not been seen.
func (f *ActionAssertionFactory) IsNotSeen() ActionAssertion {
return &ActionNotSeenAssertion{ActionIsSeenAssertion: ActionIsSeenAssertion{AbstractActionFilter: f.AbstractActionFilter}}
}
// ActionSeenCountAssertion asserts that a required number of actions matching its ActionAssertionFilter have been seen.
type ActionSeenCountAssertion struct {
// AbstractActionFilter filters the actions to be included in the count.
AbstractActionFilter
// Expected number of actions passing the filter
Expected int
// seen is the number of actions that passed the filter
seen int
}
var _ ActionAssertion = (*ActionSeenCountAssertion)(nil)
// React increments the number of seen actions.
func (a *ActionSeenCountAssertion) React(_ clienttesting.Action) (bool, runtime.Object, error) {
a.seen++
return false, nil, nil
}
// Assert that the number of actions seen matches the Expected number.
func (a *ActionSeenCountAssertion) Assert(t *testing.T) {
t.Helper()
if a.seen != a.Expected {
t.Errorf("unexpected number of '%s' actions: expected %d, saw %d.", a.AbstractActionFilter.String(), a.Expected, a.seen)
}
}
// ActionIsSeenAssertion asserts that an action matching its ActionAssertionFilter has been seen.
type ActionIsSeenAssertion struct {
AbstractActionFilter
seen bool
}
var _ ActionAssertion = (*ActionIsSeenAssertion)(nil)
// React marks the action as being seen.
func (a *ActionIsSeenAssertion) React(_ clienttesting.Action) (bool, runtime.Object, error) {
a.seen = true
return false, nil, nil
}
// Assert that the action has been seen.
func (a *ActionIsSeenAssertion) Assert(t *testing.T) {
if !a.seen {
t.Helper()
t.Errorf("expected '%s' action not seen.", a.AbstractActionFilter.String())
}
}
// ActionNotSeenAssertion asserts that an action matching its ActionAssertionFilter has not been seen.
type ActionNotSeenAssertion struct {
ActionIsSeenAssertion
}
var _ ActionAssertion = (*ActionNotSeenAssertion)(nil)
// Assert that the action has not been seen.
func (a *ActionNotSeenAssertion) Assert(t *testing.T) {
if a.seen {
t.Helper()
t.Errorf("unexpected '%s' action was seen.", a.AbstractActionFilter.String())
}
}