@@ -86,10 +86,10 @@ func setupLRP(t *testing.T, ctx context.Context) (*v1.Pod, func()) {
8686 // Write the updated content back to the file
8787 err = os .WriteFile (tempNodeLocalDNSDaemonsetPath , []byte (replaced ), 0o644 )
8888 require .NoError (t , err )
89- cleanUpFns = append ( cleanUpFns , func () {
89+ defer func () {
9090 err := os .Remove (tempNodeLocalDNSDaemonsetPath )
9191 require .NoError (t , err )
92- })
92+ }( )
9393
9494 // list out and select node of choice
9595 nodeList , err := kubernetes .GetNodeList (ctx , cs )
@@ -153,19 +153,9 @@ func setupLRP(t *testing.T, ctx context.Context) (*v1.Pod, func()) {
153153 return & selectedClientPod , cleanupFn
154154}
155155
156- // TestLRP tests if the local redirect policy in a cilium cluster is functioning
157- // The test assumes the current kubeconfig points to a cluster with cilium (1.16+), cns,
158- // and kube-dns already installed. The lrp feature flag should be enabled in the cilium config
159- // Resources created are automatically cleaned up
160- // From the lrp folder, run: go test ./lrp_test.go -v -tags "lrp" -run ^TestLRP$
161- func TestLRP (t * testing.T ) {
156+ func testLRPCase (t * testing.T , ctx context.Context , clientPod v1.Pod , clientCmd []string , expectResponse string , countShouldIncrease bool ) {
162157 config := kubernetes .MustGetRestConfig ()
163158 cs := kubernetes .MustGetClientset ()
164- ctx := context .Background ()
165-
166- selectedPod , cleanupFn := setupLRP (t , ctx )
167- defer cleanupFn ()
168- require .NotNil (t , selectedPod )
169159
170160 // labels for target lrp metric
171161 metricLabels := map [string ]string {
@@ -179,24 +169,43 @@ func TestLRP(t *testing.T) {
179169 beforeMetric , err := prometheus .GetMetric (promAddress , coreDNSRequestCountTotal , metricLabels )
180170 require .NoError (t , err )
181171
182- t .Log ("calling nslookup from client" )
172+ t .Log ("calling command from client" )
183173 // nslookup to 10.0.0.10 (coredns)
184- val , err := kubernetes .ExecCmdOnPod (ctx , cs , selectedPod .Namespace , selectedPod .Name , clientContainer , []string {
185- "nslookup" , "google.com" , "10.0.0.10" ,
186- }, config )
174+ val , err := kubernetes .ExecCmdOnPod (ctx , cs , clientPod .Namespace , clientPod .Name , clientContainer , clientCmd , config )
187175 require .NoError (t , err , string (val ))
188176 // can connect
189- require .Contains (t , string (val ), "Server:" )
177+ require .Contains (t , string (val ), expectResponse )
190178
191179 // in case there is time to propagate
192- time .Sleep (1 * time .Second )
180+ time .Sleep (500 * time .Millisecond )
193181
194182 // curl again and see count increases
195183 afterMetric , err := prometheus .GetMetric (promAddress , coreDNSRequestCountTotal , metricLabels )
196184 require .NoError (t , err )
197185
198186 // count should go up
199- require .Greater (t , afterMetric .GetCounter ().GetValue (), beforeMetric .GetCounter ().GetValue (), "dns metric count did not increase after nslookup" )
187+ if countShouldIncrease {
188+ require .Greater (t , afterMetric .GetCounter ().GetValue (), beforeMetric .GetCounter ().GetValue (), "dns metric count did not increase after command" )
189+ } else {
190+ require .Equal (t , afterMetric .GetCounter ().GetValue (), beforeMetric .GetCounter ().GetValue (), "dns metric count increased after command" )
191+ }
192+ }
193+
194+ // TestLRP tests if the local redirect policy in a cilium cluster is functioning
195+ // The test assumes the current kubeconfig points to a cluster with cilium (1.16+), cns,
196+ // and kube-dns already installed. The lrp feature flag should be enabled in the cilium config
197+ // Resources created are automatically cleaned up
198+ // From the lrp folder, run: go test ./lrp_test.go -v -tags "lrp" -run ^TestLRP$
199+ func TestLRP (t * testing.T ) {
200+ ctx := context .Background ()
201+
202+ selectedPod , cleanupFn := setupLRP (t , ctx )
203+ defer cleanupFn ()
204+ require .NotNil (t , selectedPod )
205+
206+ testLRPCase (t , ctx , * selectedPod , []string {
207+ "nslookup" , "google.com" , "10.0.0.10" ,
208+ }, "Server:" , true )
200209}
201210
202211// TakeOne takes one item from the slice randomly; if empty, it returns the empty value for the type
0 commit comments