Skip to content

Commit

Permalink
Improve the unit test coverage for portcache
Browse files Browse the repository at this point in the history
Signed-off-by: Pulkit Jain <jainpu@vmware.com>
  • Loading branch information
Pulkit Jain committed Dec 6, 2022
1 parent b0e9636 commit 26643e1
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 25 deletions.
25 changes: 0 additions & 25 deletions pkg/agent/nodeportlocal/portcache/port_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
152 changes: 152 additions & 0 deletions pkg/agent/nodeportlocal/portcache/port_table_windows_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
44 changes: 44 additions & 0 deletions pkg/agent/nodeportlocal/portcache/shared_data.go
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit 26643e1

Please sign in to comment.