Skip to content

Commit

Permalink
Add test for discoverTrustedProxies
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Dec 15, 2023
1 parent 718859b commit db02814
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/smee/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (k *Kube) discoverTrustedProxies(ctx context.Context, l logr.Logger, truste
// For example, if a cluster has 3 nodes, each with a /24 podCIDR, and the cluster has a /16 podCIDR, combinedCIDRs will return 4 CIDR ranges.
func combinedCIDRs(ctx context.Context, l logr.Logger, c corev1client.CoreV1Interface, trustedProxies []string) []string {
var tp []string
copy(tp, trustedProxies)
tp = append(tp, trustedProxies...)
if podCIDRS, err := perNodePodCIDRs(ctx, c); err == nil {
tp = append(tp, podCIDRS...)
} else {
Expand Down
66 changes: 66 additions & 0 deletions cmd/smee/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,69 @@ func TestCombinedCIDRs(t *testing.T) {
})
}
}

func TestDiscoverTrustedProxies(t *testing.T) {
tests := map[string]struct {
spec []runtime.Object
want []string
}{
"no podCIDR": {},
"podCIDR": {
spec: []runtime.Object{
&v1.NodeList{
Items: []v1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
},
Spec: v1.NodeSpec{
PodCIDRs: []string{"10.42.0.0/24"},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "node2",
},
Spec: v1.NodeSpec{
PodCIDRs: []string{"10.42.1.0/24"},
},
},
},
},
&v1.PodList{
Items: []v1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"component": "kube-controller-manager",
},
Namespace: "kube-system",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Command: []string{
"kube-controller-manager",
"--allocate-node-cidrs=true",
"--authentication-kubeconfig=/etc/kubernetes/controller-manager.conf",
},
},
},
},
},
},
},
},
want: []string{"10.42.1.0/24", "10.42.0.0/24"},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
k := &Kube{}
got := k.discoverTrustedProxies(context.Background(), logr.Discard(), nil)
if diff := cmp.Diff(got, test.want); diff != "" {
t.Fatalf("unexpected result (+want -got):\n%s", diff)
}
})
}
}

0 comments on commit db02814

Please sign in to comment.