From 26643e16efb9ff53846e73d028030385941fbb7f Mon Sep 17 00:00:00 2001 From: Pulkit Jain Date: Wed, 12 Oct 2022 18:50:33 +0530 Subject: [PATCH] Improve the unit test coverage for portcache Signed-off-by: Pulkit Jain --- .../portcache/port_table_test.go | 25 --- .../portcache/port_table_windows_test.go | 152 ++++++++++++++++++ .../nodeportlocal/portcache/shared_data.go | 44 +++++ 3 files changed, 196 insertions(+), 25 deletions(-) create mode 100644 pkg/agent/nodeportlocal/portcache/port_table_windows_test.go create mode 100644 pkg/agent/nodeportlocal/portcache/shared_data.go diff --git a/pkg/agent/nodeportlocal/portcache/port_table_test.go b/pkg/agent/nodeportlocal/portcache/port_table_test.go index 250f808d17c..076bf59f0dd 100644 --- a/pkg/agent/nodeportlocal/portcache/port_table_test.go +++ b/pkg/agent/nodeportlocal/portcache/port_table_test.go @@ -26,39 +26,14 @@ import ( portcachetesting "antrea.io/antrea/pkg/agent/nodeportlocal/portcache/testing" "antrea.io/antrea/pkg/agent/nodeportlocal/rules" rulestesting "antrea.io/antrea/pkg/agent/nodeportlocal/rules/testing" - - "k8s.io/client-go/tools/cache" -) - -const ( - startPort = 61000 - endPort = 65000 ) -func newPortTable(mockIPTables rules.PodPortRules, mockPortOpener LocalPortOpener) *PortTable { - return &PortTable{ - PortTableCache: cache.NewIndexer(GetPortTableKey, cache.Indexers{ - NodePortIndex: NodePortIndexFunc, - PodEndpointIndex: PodEndpointIndexFunc, - PodIPIndex: PodIPIndexFunc, - }), - StartPort: startPort, - EndPort: endPort, - PortSearchStart: startPort, - PodPortRules: mockIPTables, - LocalPortOpener: mockPortOpener, - } -} - func TestRestoreRules(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockIPTables := rulestesting.NewMockPodPortRules(mockCtrl) mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl) portTable := newPortTable(mockIPTables, mockPortOpener) - podIP := "10.0.0.1" - nodePort1 := startPort - nodePort2 := startPort + 1 allNPLPorts := []rules.PodNodePort{ { NodePort: nodePort1, diff --git a/pkg/agent/nodeportlocal/portcache/port_table_windows_test.go b/pkg/agent/nodeportlocal/portcache/port_table_windows_test.go new file mode 100644 index 00000000000..0a018ffdcc0 --- /dev/null +++ b/pkg/agent/nodeportlocal/portcache/port_table_windows_test.go @@ -0,0 +1,152 @@ +//go:build windows +// +build windows + +// Copyright 2022 Antrea Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package portcache + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + portcachetesting "antrea.io/antrea/pkg/agent/nodeportlocal/portcache/testing" + "antrea.io/antrea/pkg/agent/nodeportlocal/rules" + rulestesting "antrea.io/antrea/pkg/agent/nodeportlocal/rules/testing" +) + +func TestRestoreRules(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl) + mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl) + portTable := newPortTable(mockPortRules, mockPortOpener) + allNPLPorts := []rules.PodNodePort{ + { + NodePort: nodePort1, + PodPort: 1001, + PodIP: podIP, + Protocol: "tcp", + }, + { + NodePort: nodePort1, + PodPort: 1001, + PodIP: podIP, + Protocol: "udp", + }, + { + NodePort: nodePort2, + PodPort: 1002, + PodIP: podIP, + Protocol: "udp", + }, + } + + mockPortRules.EXPECT().AddRule(nodePort1, podIP, 1001, "tcp") + mockPortRules.EXPECT().AddRule(nodePort1, podIP, 1001, "udp") + mockPortRules.EXPECT().AddRule(nodePort2, podIP, 1002, "udp") + + syncedCh := make(chan struct{}) + err := portTable.RestoreRules(allNPLPorts, syncedCh) + require.NoError(t, err) +} + +func TestDeleteRule(t *testing.T) { + var err error + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl) + mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl) + portTable := newPortTable(mockPortRules, mockPortOpener) + npData := &NodePortData{ + NodePort: startPort, + PodIP: podIP, + PodPort: 1001, + Protocol: ProtocolSocketData{ + Protocol: "tcp", + }, + } + + tcs := []struct { + name string + testForPod bool + }{ + { + name: "Delete Rule", + testForPod: true, + }, + { + name: "Delete Rules For Pod", + testForPod: false, + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + portTable.addPortTableCache(npData) + mockPortRules.EXPECT().DeleteRule(startPort, podIP, 1001, "tcp") + if tc.testForPod { + err = portTable.DeleteRule(podIP, 1001, "tcp") + } else { + err = portTable.DeleteRulesForPod(podIP) + } + require.NoError(t, err) + }) + } +} + +func TestAddRule(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl) + mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl) + portTable := newPortTable(mockPortRules, mockPortOpener) + podPort := 1001 + tcs := []struct { + name string + nodePort int + protocol string + expectedErr string + }{ + { + name: "valid entry for rule", + nodePort: startPort, + protocol: "udp", + }, + { + name: "duplicate rule", + nodePort: startPort + 1, + protocol: "udp", + expectedErr: "existing Windows Nodeport entry for", + }, + } + + for _, tc := range tcs { + t.Run(tc.name, func(t *testing.T) { + if tc.expectedErr == "" { + mockPortRules.EXPECT().AddRule(tc.nodePort, podIP, podPort, tc.protocol) + } + nodePort, err := portTable.AddRule(podIP, podPort, tc.protocol) + if tc.expectedErr != "" { + assert.ErrorContains(t, err, tc.expectedErr) + } else { + require.NoError(t, err) + assert.Equal(t, tc.nodePort, nodePort) + } + }) + } +} diff --git a/pkg/agent/nodeportlocal/portcache/shared_data.go b/pkg/agent/nodeportlocal/portcache/shared_data.go new file mode 100644 index 00000000000..7a1fe3b86ec --- /dev/null +++ b/pkg/agent/nodeportlocal/portcache/shared_data.go @@ -0,0 +1,44 @@ +// Copyright 2022 Antrea Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package portcache + +import ( + "k8s.io/client-go/tools/cache" + + "antrea.io/antrea/pkg/agent/nodeportlocal/rules" +) + +const ( + startPort = 61000 + endPort = 65000 + podIP = "10.0.0.1" + nodePort1 = startPort + nodePort2 = startPort + 1 +) + +func newPortTable(mockIPTables rules.PodPortRules, mockPortOpener LocalPortOpener) *PortTable { + return &PortTable{ + PortTableCache: cache.NewIndexer(GetPortTableKey, cache.Indexers{ + NodePortIndex: NodePortIndexFunc, + PodEndpointIndex: PodEndpointIndexFunc, + PodIPIndex: PodIPIndexFunc, + }), + StartPort: startPort, + EndPort: endPort, + PortSearchStart: startPort, + PodPortRules: mockIPTables, + LocalPortOpener: mockPortOpener, + } +}