Skip to content

Commit 424f14c

Browse files
squeedaanm
authored andcommitted
policy: add aggregation test
This ensures that the userspace and bpf implementation of identity aggregation match exactly. The userspace test outputs a C array of inputs and outputs. The C test includes this and verifies that the results match. Signed-off-by: Casey Callendrello <cdc@isovalent.com>
1 parent 40c9adc commit 424f14c

5 files changed

Lines changed: 109 additions & 6 deletions

File tree

bpf/tests/aggregate_nid_in.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
2+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
3+
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
4+
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
5+
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
6+
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
7+
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
8+
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
9+
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
10+
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
11+
100, 101, 102, 1023, 1024, 1025, 65534, 65535, 65536, 16777215,
12+
16777216, 16777217, 33554431, 33554432, 33554433,

bpf/tests/aggregate_nid_out.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2+
0, 11, 12, 13, 14, 0, 0, 0, 0, 0,
3+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11+
11, 11, 11, 11, 11, 11, 11, 11, 12, 12,
12+
13, 13, 13, 14, 14,

bpf/tests/network_policy.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <lib/policy.h>
1313

1414
#include "lib/policy.h"
15+
#include "network_policy_tuples.h"
1516

1617
#define REMOTE_IDENTITY 112233
1718

@@ -268,5 +269,11 @@ int network_policy_egress_allow_check(struct __ctx_buff *ctx)
268269
assert(aggregate_for_identity(33554432) == n);
269270
});
270271

272+
/* tests that aggregates match the userspace implementation */
273+
TEST("aggregate-for-id-userspace", {
274+
for (unsigned long i = 0; i < ARRAY_SIZE(aggregate_nid_in); i++)
275+
assert(aggregate_for_identity(aggregate_nid_in[i]) == aggregate_nid_out[i]);
276+
});
277+
271278
test_finish();
272279
}

bpf/tests/network_policy_tuples.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2+
/* Copyright Authors of Cilium */
3+
4+
#pragma once
5+
6+
/* Arrays are generated in pkg/policy/aggregate_test.go */
7+
8+
static __u32 aggregate_nid_in[] = {
9+
#include "aggregate_nid_in.txt"
10+
};
11+
12+
static __u32 aggregate_nid_out[] = {
13+
#include "aggregate_nid_out.txt"
14+
};

pkg/policy/aggregate_test.go

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package policy
55

66
import (
7+
"fmt"
8+
"os"
79
"testing"
810

911
"github.com/stretchr/testify/require"
@@ -19,19 +21,75 @@ func TestAllAggregates(t *testing.T) {
1921
require.True(t, isAggregate(nid))
2022
}
2123

22-
// check all identities
23-
// Should only take a second or two.
24-
// Validates that all aggregate identities are known.
25-
for i := range (identity.IdentityScopeRemoteNode | identity.MaxAllocatorLocalIdentity) / 100 {
26-
nid := i * 100
24+
c := identity.ReservedIdentityAggregateCluster
25+
m := identity.ReservedIdentityAggregateClusterMesh
26+
n := identity.ReservedIdentityAggregateRemoteNode
27+
w := identity.ReservedIdentityAggregateWorld
28+
29+
// Gather results, we will optionally write them
30+
// to C test literals to ensure we produce reasonable results
31+
var expectedIn, expectedOut []identity.NumericIdentity
32+
// set to True to update C test literals
33+
writeOutput := false
34+
35+
check := func(nid identity.NumericIdentity) {
2736
// duplicate of AllAggregates for efficiency.
2837
switch nid {
29-
case 0, 6, 2, 11, 12:
38+
case 0, c, m, n, w:
3039
require.True(t, isAggregate(nid))
3140
default:
3241
require.False(t, isAggregate(nid))
3342
}
43+
44+
if writeOutput {
45+
expectedIn = append(expectedIn, nid)
46+
expectedOut = append(expectedOut, aggregateFor(nid))
47+
}
48+
}
49+
50+
// check all interesting identities
51+
// Should only take a second or two.
52+
// Validates that all aggregate identities are known.
53+
for i := range 100 {
54+
check(identity.NumericIdentity(i))
55+
}
56+
for _, nid := range []identity.NumericIdentity{
57+
101,
58+
1024,
59+
0xFFFF,
60+
identity.IdentityScopeLocal,
61+
identity.IdentityScopeRemoteNode,
62+
} {
63+
check(nid - 1)
64+
check(nid)
65+
check(nid + 1)
66+
}
67+
68+
if writeOutput {
69+
err := writeCArray("../../bpf/tests/aggregate_nid_in.txt", expectedIn)
70+
fmt.Printf("wrote %d entries", len(expectedIn))
71+
require.NoError(t, err)
72+
err = writeCArray("../../bpf/tests/aggregate_nid_out.txt", expectedOut)
73+
require.NoError(t, err)
74+
}
75+
}
76+
77+
func writeCArray(path string, nids []identity.NumericIdentity) error {
78+
fp, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
79+
if err != nil {
80+
return err
81+
}
82+
for i, nid := range nids {
83+
if _, err := fmt.Fprintf(fp, "%d, ", nid); err != nil {
84+
return err
85+
}
86+
if i%10 == 9 {
87+
if _, err := fmt.Fprintln(fp, ""); err != nil {
88+
return err
89+
}
90+
}
3491
}
92+
return fp.Close()
3593
}
3694

3795
func TestIsAggregate(t *testing.T) {

0 commit comments

Comments
 (0)