Skip to content

Commit

Permalink
Added tests for the nodeportlocal package
Browse files Browse the repository at this point in the history
This commit introduces new tests for the `pkg/agent/nodeportlocal/rules`
package.

Fixes: #5650

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
  • Loading branch information
VaibhavMalik4187 committed Jan 25, 2024
1 parent ca5dc45 commit 176e70a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/agent/nodeportlocal/rules/iptable_rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 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 rules

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestInitAndAddRule(t *testing.T) {
rules := InitRules()
require.NotNil(t, rules)

err := rules.Init()
require.ErrorContains(t, err, "initialization of NPL iptables rules failed")

err = rules.AddRule(7, "", 77, "tcp")
require.ErrorContains(t, err, "error checking if rule [-p")

nodePortRules := []PodNodePort{
{
Protocols: []string{"tcp"},
},
}
expectedErr := "error executing iptables-restore:"

err = rules.AddAllRules(nodePortRules)

require.ErrorContains(t, err, expectedErr)

err = rules.DeleteRule(7, "", 77, "tcp")
require.ErrorContains(t, err, "error checking if rule [-p")

err = rules.DeleteAllRules()
require.ErrorContains(t, err, "failed to check if NodePortLocal chain exists in NAT table:")
}

0 comments on commit 176e70a

Please sign in to comment.