Skip to content

Commit 28144ad

Browse files
committed
add lrp fqdn test and yaml
1 parent 1231468 commit 28144ad

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//go:build lrp
2+
3+
package lrp
4+
5+
import (
6+
"context"
7+
"testing"
8+
9+
"github.com/Azure/azure-container-networking/test/internal/kubernetes"
10+
ciliumClientset "github.com/cilium/cilium/pkg/k8s/client/clientset/versioned"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
var (
15+
fqdnCNPPath = ciliumManifestsDir + "fqdn-cnp.yaml"
16+
enableFQDNFlag = "enable-l7-proxy"
17+
)
18+
19+
// TestLRPFQDN tests if the local redirect policy in a cilium cluster is functioning with a
20+
// FQDN Cilium Network Policy. As such, enable-l7-proxy should be enabled in the config
21+
// The test assumes the current kubeconfig points to a cluster with cilium, cns,
22+
// and kube-dns already installed. The lrp feature flag should also be enabled in the cilium config
23+
// Resources created are automatically cleaned up
24+
// From the lrp folder, run: go test ./ -v -tags "lrp" -run ^TestLRPFQDN$
25+
func TestLRPFQDN(t *testing.T) {
26+
ctx := context.Background()
27+
28+
selectedPod, cleanupFn := setupLRP(t, ctx)
29+
defer cleanupFn()
30+
require.NotNil(t, selectedPod)
31+
32+
cs := kubernetes.MustGetClientset()
33+
config := kubernetes.MustGetRestConfig()
34+
ciliumCS, err := ciliumClientset.NewForConfig(config)
35+
require.NoError(t, err)
36+
37+
// ensure enable l7 proxy flag is enabled
38+
ciliumCM, err := kubernetes.GetConfigmap(ctx, cs, kubeSystemNamespace, ciliumConfigmapName)
39+
require.NoError(t, err)
40+
require.Equal(t, "true", ciliumCM.Data[enableFQDNFlag], "enable-l7-proxy not set to true in cilium-config")
41+
42+
_, cleanupCNP := kubernetes.MustSetupCNP(ctx, ciliumCS, fqdnCNPPath)
43+
defer cleanupCNP()
44+
45+
tests := []struct {
46+
name string
47+
command []string
48+
expectedMsgContains string
49+
expectedErrMsgContains string
50+
countIncreases bool
51+
}{
52+
{
53+
name: "nslookup google succeeds",
54+
command: []string{"nslookup", "www.google.com", "10.0.0.10"},
55+
expectedMsgContains: "Server:",
56+
countIncreases: true,
57+
},
58+
{
59+
name: "wget google succeeds",
60+
command: []string{"wget", "-O", "index.html", "www.google.com", "--timeout=5"},
61+
expectedErrMsgContains: "saved",
62+
countIncreases: true,
63+
},
64+
{
65+
name: "nslookup bing succeeds",
66+
command: []string{"nslookup", "www.bing.com", "10.0.0.10"},
67+
expectedMsgContains: "Server:",
68+
countIncreases: true,
69+
},
70+
{
71+
name: "wget bing fails but dns succeeds",
72+
command: []string{"wget", "-O", "index.html", "www.bing.com", "--timeout=5"},
73+
expectedErrMsgContains: "timed out",
74+
countIncreases: true,
75+
},
76+
{
77+
name: "nslookup example fails",
78+
command: []string{"nslookup", "www.example.com", "10.0.0.10"},
79+
expectedMsgContains: "REFUSED",
80+
countIncreases: false,
81+
},
82+
{
83+
// won't be able to nslookup, let alone query the website
84+
name: "wget example fails",
85+
command: []string{"wget", "-O", "index.html", "www.example.com", "--timeout=5"},
86+
expectedErrMsgContains: "bad address",
87+
countIncreases: false,
88+
},
89+
}
90+
for _, tt := range tests {
91+
tt := tt
92+
t.Run(tt.name, func(t *testing.T) {
93+
testLRPCase(t, ctx, *selectedPod, tt.command, tt.expectedMsgContains, tt.expectedErrMsgContains, tt.countIncreases)
94+
})
95+
}
96+
}

test/integration/lrp/lrp_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func testLRPCase(t *testing.T, ctx context.Context, clientPod v1.Pod, clientCmd
195195
// The test assumes the current kubeconfig points to a cluster with cilium (1.16+), cns,
196196
// and kube-dns already installed. The lrp feature flag should be enabled in the cilium config
197197
// Resources created are automatically cleaned up
198-
// From the lrp folder, run: go test ./lrp_test.go -v -tags "lrp" -run ^TestLRP$
198+
// From the lrp folder, run: go test ./ -v -tags "lrp" -run ^TestLRP$
199199
func TestLRP(t *testing.T) {
200200
ctx := context.Background()
201201

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apiVersion: "cilium.io/v2"
2+
kind: CiliumNetworkPolicy
3+
metadata:
4+
name: "to-fqdn"
5+
namespace: "default"
6+
spec:
7+
endpointSelector:
8+
matchLabels:
9+
lrp-test: "true"
10+
egress:
11+
- toEndpoints:
12+
- matchLabels:
13+
"k8s:io.kubernetes.pod.namespace": kube-system
14+
"k8s:k8s-app": node-local-dns
15+
toPorts:
16+
- ports:
17+
- port: "53"
18+
protocol: UDP
19+
rules:
20+
dns:
21+
- matchPattern: "*.google.com"
22+
- matchPattern: "*.bing.com"
23+
- toFQDNs:
24+
- matchPattern: "*.google.com"

0 commit comments

Comments
 (0)