|
| 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 | +} |
0 commit comments