Repository navigation
Expand file tree
/
Copy pathassert.go
More file actions
280 lines (257 loc) · 9.81 KB
/
Copy pathassert.go
File metadata and controls
280 lines (257 loc) · 9.81 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 The Telepathy Authors
//
// This file is part of Telepathy.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"fmt"
"sort"
"strings"
"sigs.k8s.io/yaml"
)
// Assertion is one expected-connectivity claim a caller wants to gate on: a
// flow From->To at (Port, Protocol) that must evaluate to Expect ("allow" or
// "deny"). It is the unit of a Telepathy test file — the thing that turns the
// connectivity matrix from a report into a pass/fail CI check.
//
// Port/Protocol are optional: when zero/empty they inherit the enclosing
// Request's probe (which itself defaults to 8080/tcp), so a test file that
// only cares about one port can omit them everywhere. Name is a free-text
// label echoed back in the result so a failure reads like a test case
// ("frontend must reach backend") rather than a pair of IDs.
type Assertion struct {
Name string `json:"name,omitempty"`
From string `json:"from"`
To string `json:"to"`
Port int `json:"port,omitempty"`
Protocol string `json:"protocol,omitempty"`
Expect string `json:"expect"`
}
// AssertionFile is the on-disk form a caller writes (telepathy.tests.yaml). It
// accepts either a bare YAML list of assertions or an object with an
// `assertions:` key — DecodeAssertions handles both — so the file can grow
// top-level options later without breaking the simple list form.
type AssertionFile struct {
Assertions []Assertion `json:"assertions"`
}
// AssertionResult is the outcome of checking one Assertion against an evaluated
// matrix. Pass is true iff the flow was found and Got == Expect. Got is the
// matrix verdict ("allow"/"deny", or "" when the flow wasn't in the matrix);
// Err is set for a malformed assertion or an unknown From->To pair, which counts
// as a failure distinct from a wrong verdict.
type AssertionResult struct {
Assertion Assertion `json:"assertion"`
Got string `json:"got"`
Pass bool `json:"pass"`
Err string `json:"err,omitempty"`
}
// AssertionReport is the full result of RunAssertions: every per-assertion
// result plus pass/fail counts and the engine's own errors/warnings (surfaced
// once across all probe groups, deduplicated). Ok reports whether every
// assertion passed — the bit a CLI turns into its exit code.
type AssertionReport struct {
Results []AssertionResult `json:"results"`
Passed int `json:"passed"`
Failed int `json:"failed"`
Errors []string `json:"errors,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
// Ok reports whether every assertion passed (and at least one ran). A report
// with zero assertions is not Ok — an empty test file is a mistake, not a pass.
func (r AssertionReport) Ok() bool {
return r.Failed == 0 && r.Passed > 0
}
// DecodeAssertions parses a test file (JSON or YAML, via sigs.k8s.io/yaml) into
// a slice of Assertions. It accepts both the bare-list form
//
// - {from: a, to: b, expect: allow}
//
// and the wrapped form
//
// assertions:
// - {from: a, to: b, expect: allow}
//
// Empty input is an error here (unlike DecodeRequest) — running the test
// command with nothing to assert is a usage mistake worth surfacing.
func DecodeAssertions(data []byte) ([]Assertion, error) {
if strings.TrimSpace(string(data)) == "" {
return nil, fmt.Errorf("empty assertions file: nothing to test")
}
// Try the bare-list form first; sigs.k8s.io/yaml routes YAML through JSON,
// so a leading `-`/`[` lands as a JSON array and a mapping as an object.
var list []Assertion
if err := yaml.Unmarshal(data, &list); err == nil && len(list) > 0 {
return list, nil
}
var file AssertionFile
if err := yaml.Unmarshal(data, &file); err != nil {
return nil, fmt.Errorf("decode assertions: %w", err)
}
if len(file.Assertions) == 0 {
return nil, fmt.Errorf("no assertions found: expected a list or an `assertions:` key")
}
return file.Assertions, nil
}
// RunAssertions evaluates req's topology against the given assertions and
// reports which held. Because the connectivity matrix is computed for a single
// (port, protocol) probe, assertions are grouped by their effective probe —
// each distinct (port, protocol) triggers one Evaluate over the complete
// topology, requesting only the pairs in that group. Providers supporting
// MatrixPairs avoid materializing an entire matrix for a handful of assertions. Engine errors/warnings are collected across groups and
// deduplicated.
//
// req is not mutated; each group runs on a shallow copy with Port/Protocol
// overridden (the slices are shared read-only, which Evaluate never writes).
func RunAssertions(eval func(Request) Response, req Request, assertions []Assertion) AssertionReport {
var report AssertionReport
report.Results = make([]AssertionResult, len(assertions))
// probeKey identifies one Evaluate sweep; idxByProbe gathers the original
// assertion indices that share it so results land back in input order.
type probeKey struct {
port int
proto string
}
idxByProbe := map[probeKey][]int{}
seenErr := map[string]bool{}
seenWarn := map[string]bool{}
for i, a := range assertions {
if res, bad := validateAssertion(a); bad {
report.Results[i] = res
continue
}
k := probeKey{port: effectivePort(a, req), proto: effectiveProto(a, req)}
idxByProbe[k] = append(idxByProbe[k], i)
}
// Stable probe order: by port then protocol, so DEBUG output / any future
// streaming is deterministic regardless of map iteration.
probes := make([]probeKey, 0, len(idxByProbe))
for k := range idxByProbe {
probes = append(probes, k)
}
sort.Slice(probes, func(i, j int) bool {
if probes[i].port != probes[j].port {
return probes[i].port < probes[j].port
}
return probes[i].proto < probes[j].proto
})
for _, k := range probes {
probeReq := req
probeReq.MatrixSink = nil // Assertions need the in-memory matrix.
probeReq.Port = k.port
probeReq.Protocol = k.proto
probeReq.MatrixPairs = make([]MatrixPair, 0, len(idxByProbe[k]))
for _, i := range idxByProbe[k] {
a := assertions[i]
probeReq.MatrixPairs = append(probeReq.MatrixPairs, MatrixPair{From: a.From, To: a.To})
}
resp := eval(probeReq)
for _, e := range resp.Errors {
if !seenErr[e] {
seenErr[e] = true
report.Errors = append(report.Errors, e)
}
}
for _, w := range resp.Warnings {
if !seenWarn[w] {
seenWarn[w] = true
report.Warnings = append(report.Warnings, w)
}
}
for _, i := range idxByProbe[k] {
report.Results[i] = checkAssertion(assertions[i], resp.Matrix)
}
}
return summarizeAssertions(report)
}
// RunAssertionsOnMatrix checks assertions against an already-computed matrix
// (e.g. one streamed to disk by the CLI's -matrix-out) instead of re-evaluating.
// The matrix is a single probe, so per-assertion port/protocol overrides are
// ignored — every assertion is gated against the matrix as given.
func RunAssertionsOnMatrix(matrix map[string]string, assertions []Assertion) AssertionReport {
var report AssertionReport
report.Results = make([]AssertionResult, len(assertions))
for i, a := range assertions {
if res, bad := validateAssertion(a); bad {
report.Results[i] = res
continue
}
report.Results[i] = checkAssertion(a, matrix)
}
return summarizeAssertions(report)
}
// validateAssertion rejects a structurally invalid assertion before it reaches
// the evaluator, returning a failed result and bad=true. From/To are required
// and Expect must normalise to allow/deny.
func validateAssertion(a Assertion) (AssertionResult, bool) {
if strings.TrimSpace(a.From) == "" || strings.TrimSpace(a.To) == "" {
return AssertionResult{Assertion: a, Err: "assertion missing `from` or `to`"}, true
}
switch strings.ToLower(strings.TrimSpace(a.Expect)) {
case "allow", "deny":
return AssertionResult{}, false
default:
return AssertionResult{Assertion: a, Err: fmt.Sprintf("assertion `expect` must be allow|deny, got %q", a.Expect)}, true
}
}
// checkAssertion looks the From->To flow up in an evaluated matrix and compares
// it to Expect (case-insensitively). A flow absent from the matrix is a failure
// with a clear Err — almost always a typo in an endpoint ID — rather than a
// silent mismatch.
func checkAssertion(a Assertion, matrix map[string]string) AssertionResult {
got, ok := matrix[a.From+"->"+a.To]
if !ok {
return AssertionResult{
Assertion: a,
Err: fmt.Sprintf("no flow %s->%s in matrix (check endpoint ids)", a.From, a.To),
}
}
return AssertionResult{
Assertion: a,
Got: got,
Pass: got == strings.ToLower(strings.TrimSpace(a.Expect)),
}
}
// effectivePort resolves the probe port for an assertion: its own Port, else
// the Request's, else the engine default (8080) — mirroring Evaluate's default
// so grouping keys match what Evaluate actually probes.
func effectivePort(a Assertion, req Request) int {
if a.Port != 0 {
return a.Port
}
if req.Port != 0 {
return req.Port
}
return 8080
}
// effectiveProto resolves the probe protocol the same way (default tcp).
func effectiveProto(a Assertion, req Request) string {
if a.Protocol != "" {
return a.Protocol
}
if req.Protocol != "" {
return req.Protocol
}
return "tcp"
}
func summarizeAssertions(report AssertionReport) AssertionReport {
for _, r := range report.Results {
if r.Pass {
report.Passed++
} else {
report.Failed++
}
}
return report
}