forked from cisco-app-networking/networkservicemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.go
218 lines (173 loc) · 5.03 KB
/
verifier.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package utils
import (
"testing"
. "github.com/onsi/gomega"
"github.com/adodon2go/networkservicemesh/controlplane/api/crossconnect"
"github.com/adodon2go/networkservicemesh/controlplane/pkg/model"
)
// Verifier is a common verifier interface to be used in tests
type Verifier interface {
Verify(t *testing.T)
}
// ModelVerifier is a Verifier to check model.Model state
type ModelVerifier struct {
model model.Model
verifiers []Verifier
}
// NewModelVerifier is a constructor for ModelVerifier
func NewModelVerifier(model model.Model) *ModelVerifier {
return &ModelVerifier{
model: model,
verifiers: []Verifier{},
}
}
// EndpointNotExists is a builder method to add check if model.Endpoint with
// Endpoint.NetworkServiceEndpoint.Name == name
// doesn't exist in v.model
func (v *ModelVerifier) EndpointNotExists(name string) *ModelVerifier {
v.verifiers = append(v.verifiers, &endpointVerifier{
exists: false,
name: name,
model: v.model,
})
return v
}
// EndpointExists is a builder method to add check if model.Endpoint with
// Endpoint.NetworkServiceEndpoint.Name == name
// Endpoint.NetworkServiceManager.Name == nsm
// exists in v.model
func (v *ModelVerifier) EndpointExists(name, nsm string) *ModelVerifier {
v.verifiers = append(v.verifiers, &endpointVerifier{
exists: true,
name: name,
nsm: nsm,
model: v.model,
})
return v
}
// ClientConnectionNotExists is a builder method to add check if model.ClientConnection with
// ConnectionID == connectionID
// doesn't exist in v.model
func (v *ModelVerifier) ClientConnectionNotExists(connectionID string) *ModelVerifier {
v.verifiers = append(v.verifiers, &clientConnectionVerifier{
exists: false,
connectionID: connectionID,
model: v.model,
})
return v
}
// ClientConnectionExists is a builder method to add check if model.ClientConnection with
// ConnectionID == connectionID
// Xcon.Source.Id = srcID
// Xcon.Destination.Id = dst.ID
// RemoteNsm.Name = remoteNSM
// Endpoint.NetworkServiceEndpoint.Name = nse
// Forwarder.RegisteredName = forwarder
// exists in v.model
func (v *ModelVerifier) ClientConnectionExists(connectionID, srcID, dstID, remoteNSM, nse, forwarder string) *ModelVerifier {
v.verifiers = append(v.verifiers, &clientConnectionVerifier{
exists: true,
connectionID: connectionID,
srcID: srcID,
dstID: dstID,
remoteNSM: remoteNSM,
nse: nse,
forwarder: forwarder,
model: v.model,
})
return v
}
// ForwarderNotExists is a builder method to add check if model.Forwarder with
// RegisteredName = name
// doesn't exist in v.model
func (v *ModelVerifier) ForwarderNotExists(name string) *ModelVerifier {
v.verifiers = append(v.verifiers, &forwarderVerifier{
exists: false,
name: name,
model: v.model,
})
return v
}
// ForwarderExists is a builder method to add check if model.Forwarder with
// RegisteredName = name
// exists in v.model
func (v *ModelVerifier) ForwarderExists(name string) *ModelVerifier {
v.verifiers = append(v.verifiers, &forwarderVerifier{
exists: true,
name: name,
model: v.model,
})
return v
}
// Verify invokes all stored checks
func (v *ModelVerifier) Verify(t *testing.T) {
for _, verifier := range v.verifiers {
verifier.Verify(t)
}
}
type endpointVerifier struct {
exists bool
name string
nsm string
model model.Model
}
func (v *endpointVerifier) Verify(t *testing.T) {
g := NewWithT(t)
nse := v.model.GetEndpoint(v.name)
if !v.exists {
g.Expect(nse).To(BeNil())
return
}
g.Expect(nse).NotTo(BeNil())
g.Expect(nse.Endpoint.GetNetworkServiceManager().GetName()).To(Equal(v.nsm))
}
type clientConnectionVerifier struct {
exists bool
connectionID string
srcID string
dstID string
remoteNSM string
nse string
forwarder string
model model.Model
}
func (v *clientConnectionVerifier) Verify(t *testing.T) {
g := NewWithT(t)
connection := v.model.GetClientConnection(v.connectionID)
if !v.exists {
g.Expect(connection).To(BeNil())
return
}
g.Expect(connection).NotTo(BeNil())
v.verifyXcon(connection.Xcon, t)
g.Expect(connection.RemoteNsm.GetName()).To(Equal(v.remoteNSM))
g.Expect(connection.Endpoint.GetNetworkServiceEndpoint().GetName()).To(Equal(v.nse))
g.Expect(connection.ForwarderRegisteredName).To(Equal(v.forwarder))
}
func (v *clientConnectionVerifier) verifyXcon(xcon *crossconnect.CrossConnect, t *testing.T) {
g := NewWithT(t)
if source := xcon.GetSource(); source != nil {
g.Expect(source.GetId()).To(Equal(v.srcID))
} else {
t.Fatalf("Expected xcon.Source not to be nil")
}
if destination := xcon.GetDestination(); destination != nil {
g.Expect(destination.GetId()).To(Equal(v.dstID))
} else {
t.Fatalf("Expected xcon.Destination not to be nil")
}
}
type forwarderVerifier struct {
exists bool
name string
model model.Model
}
func (v *forwarderVerifier) Verify(t *testing.T) {
g := NewWithT(t)
forwarder := v.model.GetForwarder(v.name)
if !v.exists {
g.Expect(forwarder).To(BeNil())
return
}
g.Expect(forwarder).NotTo(BeNil())
}