Skip to content

Commit

Permalink
Reset pipelines after pipelines are generated in every unit test (#4325)
Browse files Browse the repository at this point in the history
When generating pipelines, every table ID is allocated by calling
`binding.NextTableID()`. Note that, every time the function is called,
its return value (uint8) is increased by 1. After each unit test,
if not calling `binding.ResetTableID()` to reset the return value,
there is a chance that the return value overflows when function to
generate pipelines is called enough times. When overflowing, two
consecutive return values are 255 and 0, causing test TestBuildPipeline
to fail since 255 is not less than 0 (in TestBuildPipeline, there is
an asserting about the table of previous table should be less than
that of the current table).

Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl committed Nov 8, 2022
1 parent 339ed6f commit e516aca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/agent/openflow/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestIdempotentFlowInstallation(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()

m.EXPECT().AddAll(gomock.Any()).Return(nil).Times(1)
// Installing the flows should succeed, and all the flows should be added into the cache.
Expand Down Expand Up @@ -152,6 +153,7 @@ func TestIdempotentFlowInstallation(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()

errorCall := m.EXPECT().AddAll(gomock.Any()).Return(errors.New("Bundle error")).Times(1)
m.EXPECT().AddAll(gomock.Any()).Return(nil).After(errorCall)
Expand Down Expand Up @@ -198,6 +200,7 @@ func TestFlowInstallationFailed(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()

// We generate an error for AddAll call.
m.EXPECT().AddAll(gomock.Any()).Return(errors.New("Bundle error"))
Expand Down Expand Up @@ -237,6 +240,7 @@ func TestConcurrentFlowInstallation(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()

var concurrentCalls atomic.Value // set to true if we observe concurrent calls
timeoutCh := make(chan struct{})
Expand Down Expand Up @@ -434,6 +438,7 @@ func prepareTraceflowFlow(ctrl *gomock.Controller) *client {
c.serviceConfig = serviceConfig
c.ipProtocols = []binding.Protocol{binding.ProtocolIP}
c.generatePipelines()
defer resetPipelines()

m.EXPECT().AddAll(gomock.Any()).Return(nil).Times(1)
c.bridge = ovsoftest.NewMockBridge(ctrl)
Expand Down Expand Up @@ -564,6 +569,7 @@ func TestMulticlusterFlowsInstallation(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()

m.EXPECT().AddAll(gomock.Any()).Return(nil).Times(1)
clusterID := "cluster-a"
Expand Down Expand Up @@ -600,6 +606,7 @@ func TestServiceGroupInstallAndUninstall(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()
endpoints := []k8sproxy.Endpoint{
&k8sproxy.BaseEndpointInfo{
Endpoint: net.JoinHostPort("192.168.1.2", "8081"),
Expand Down Expand Up @@ -666,6 +673,7 @@ func TestMulticastGroupInstallAndUninstall(t *testing.T) {
client.serviceConfig = serviceConfig
client.ipProtocols = []binding.Protocol{binding.ProtocolIP}
client.generatePipelines()
defer resetPipelines()
localReceivers := []uint32{101, 102}
remoteIPs := []net.IP{
net.ParseIP("1.1.1.2"),
Expand Down
9 changes: 9 additions & 0 deletions pkg/agent/openflow/network_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestPolicyRuleConjunction(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)

ruleID1 := uint32(1001)
Expand Down Expand Up @@ -163,6 +164,7 @@ func TestInstallPolicyRuleFlows(t *testing.T) {
defer ctrl.Finish()

preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)
c.nodeConfig = &config.NodeConfig{PodIPv4CIDR: podIPv4CIDR, PodIPv6CIDR: nil}
c.networkConfig = &config.NetworkConfig{}
Expand Down Expand Up @@ -308,6 +310,7 @@ func TestInstallPolicyRuleFlows(t *testing.T) {

func TestBatchInstallPolicyRuleFlows(t *testing.T) {
preparePipelines()
defer resetPipelines()
tests := []struct {
name string
rules []*types.PolicyRule
Expand Down Expand Up @@ -582,6 +585,7 @@ func BenchmarkBatchInstallPolicyRuleFlows(b *testing.B) {
defer ctrl.Finish()

preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)
// Make it return error so no change gets committed to cache.
mockOperations := oftest.NewMockOFEntryOperations(ctrl)
Expand Down Expand Up @@ -627,6 +631,7 @@ func TestConjMatchFlowContextKeyConflict(t *testing.T) {
defer ctrl.Finish()

preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)
mockEgressDefaultTable.EXPECT().BuildFlow(gomock.Any()).Return(newMockDropFlowBuilder(ctrl)).AnyTimes()
mockEgressRuleTable.EXPECT().BuildFlow(gomock.Any()).Return(newMockRuleFlowBuilder(ctrl)).AnyTimes()
Expand Down Expand Up @@ -672,6 +677,7 @@ func TestInstallPolicyRuleFlowsInDualStackCluster(t *testing.T) {
defer ctrl.Finish()

preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, true)
c.nodeConfig = &config.NodeConfig{PodIPv4CIDR: podIPv4CIDR, PodIPv6CIDR: podIPv6CIDR}
c.networkConfig = &config.NetworkConfig{IPv4Enabled: true, IPv6Enabled: true}
Expand Down Expand Up @@ -1160,6 +1166,7 @@ func TestNetworkPolicyMetrics(t *testing.T) {
defer ctrl.Finish()

preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)
mockOVSClient := ovsctltest.NewMockOVSCtlClient(ctrl)
c.ovsctlClient = mockOVSClient
Expand All @@ -1177,6 +1184,7 @@ func TestGetMatchFlowUpdates(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)
c.nodeConfig = &config.NodeConfig{PodIPv4CIDR: podIPv4CIDR, PodIPv6CIDR: nil}
c.networkConfig = &config.NetworkConfig{TrafficEncapMode: config.TrafficEncapModeEncap, IPv4Enabled: true}
Expand Down Expand Up @@ -1268,6 +1276,7 @@ func TestClient_GetPolicyInfoFromConjunction(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
preparePipelines()
defer resetPipelines()
c = prepareClient(ctrl, false)

ruleID1 := uint32(101)
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/openflow/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ func TestBuildPipeline(t *testing.T) {
}
require.NotNil(t, tables[len(tables)-1].ofTable, "table %q should be initialized", tables[len(tables)-1].name)
}
reset()
resetPipelines()
}
}

func reset() {
func resetPipelines() {
objs := tableCache.List()
for i := 0; i < len(objs); i++ {
tableCache.Delete(objs[i])
Expand Down

0 comments on commit e516aca

Please sign in to comment.