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 Nov 28, 2022
1 parent b977b1d commit a1aaa1c
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pkg/agent/nodeportlocal/portcache/port_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
"time"

"github.com/golang/mock/gomock"
"k8s.io/client-go/tools/cache"

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 (
Expand Down
178 changes: 178 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,178 @@
//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"
"k8s.io/client-go/tools/cache"

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"
)

const (
startPort = 61000
endPort = 65000
)

func newPortTable(mockPortRules 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: mockPortRules,
LocalPortOpener: mockPortOpener,
}
}

func TestRestoreRules(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl)
mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl)
portTable := newPortTable(mockPortRules, mockPortOpener)
podIP := "10.0.0.1"
nodePort1 := startPort
nodePort2 := startPort + 1
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)
assert.NoError(t, err)
}

func TestDeleteRule(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl)
mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl)
portTable := newPortTable(mockPortRules, mockPortOpener)
podIP := "10.0.0.1"
npData := &NodePortData{
NodePort: startPort,
PodIP: "10.0.0.1",
PodPort: 1001,
Protocol: ProtocolSocketData{
Protocol: "tcp",
},
}

portTable.addPortTableCache(npData)
mockPortRules.EXPECT().DeleteRule(startPort, podIP, 1001, "tcp")
err := portTable.DeleteRule(podIP, 1001, "tcp")
assert.NoError(t, err)
}

func TestDeleteRulesForPod(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockPortRules := rulestesting.NewMockPodPortRules(mockCtrl)
mockPortOpener := portcachetesting.NewMockLocalPortOpener(mockCtrl)
portTable := newPortTable(mockPortRules, mockPortOpener)
podIP := "10.0.0.1"
npData := &NodePortData{
NodePort: startPort,
PodIP: "10.0.0.1",
PodPort: 1001,
Protocol: ProtocolSocketData{
Protocol: "tcp",
},
}

portTable.addPortTableCache(npData)
mockPortRules.EXPECT().DeleteRule(startPort, podIP, 1001, "tcp")
err := portTable.DeleteRulesForPod(podIP)
assert.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)
tcs := []struct {
name string
podIP string
podPort int
nodePort int
protocol string
}{
{
name: "first rule",
podIP: "10.0.0.1",
podPort: 1001,
nodePort: startPort,
protocol: "udp",
},
{
name: "second rule",
podIP: "10.0.0.1",
podPort: 1001,
nodePort: startPort + 1,
protocol: "tcp",
},
{
name: "third rule",
podIP: "10.0.0.1",
podPort: 1002,
nodePort: startPort,
protocol: "udp",
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
mockPortRules.EXPECT().AddRule(tc.nodePort, tc.podIP, tc.podPort, tc.protocol)
_, err := portTable.AddRule(tc.podIP, tc.podPort, tc.protocol)
assert.NoError(t, err)
})
}
}

0 comments on commit a1aaa1c

Please sign in to comment.