2021-10-08 23:52:38 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-18 01:31:42 +03:00
|
|
|
"io"
|
2021-10-08 23:52:38 +03:00
|
|
|
"testing"
|
|
|
|
|
2022-06-14 21:58:17 +03:00
|
|
|
dataplane "github.com/Azure/azure-container-networking/npm/pkg/dataplane/debug"
|
|
|
|
"github.com/Azure/azure-container-networking/npm/util"
|
2021-10-08 23:52:38 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-06-14 21:58:17 +03:00
|
|
|
iptableSaveFile = "../pkg/dataplane/testdata/iptablesave-v1"
|
|
|
|
npmCacheFile = "../pkg/dataplane/testdata/npmcachev1.json"
|
2021-10-08 23:52:38 +03:00
|
|
|
nonExistingFile = "non-existing-iptables-file"
|
|
|
|
|
|
|
|
npmCacheFlag = "-c"
|
|
|
|
iptablesSaveFileFlag = "-i"
|
|
|
|
dstFlag = "-d"
|
|
|
|
srcFlag = "-s"
|
|
|
|
unknownShorthandFlag = "-z"
|
|
|
|
|
2022-06-14 21:58:17 +03:00
|
|
|
testIP1 = "10.224.0.87" // from npmCacheWithCustomFormat.json
|
|
|
|
testIP2 = "10.224.0.20" // ditto
|
2021-10-08 23:52:38 +03:00
|
|
|
|
|
|
|
debugCmdString = "debug"
|
|
|
|
convertIPTableCmdString = "convertiptable"
|
|
|
|
getTuplesCmdString = "gettuples"
|
|
|
|
parseIPTableCmdString = "parseiptable"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testCases struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
wantErr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCommand(t *testing.T, tests []*testCases) {
|
|
|
|
for _, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rootCMD := NewRootCmd()
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
rootCMD.SetOut(b)
|
|
|
|
rootCMD.SetArgs(tt.args)
|
|
|
|
err := rootCMD.Execute()
|
|
|
|
|
|
|
|
if tt.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-18 01:31:42 +03:00
|
|
|
out, err := io.ReadAll(b)
|
2021-10-08 23:52:38 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
if tt.wantErr {
|
|
|
|
require.NotEmpty(t, out)
|
|
|
|
} else {
|
|
|
|
require.Empty(t, out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatArgs(baseArgs []string, args ...string) []string {
|
|
|
|
return append(baseArgs, args...)
|
|
|
|
}
|
2022-06-14 21:58:17 +03:00
|
|
|
|
|
|
|
func TestPrettyPrint(t *testing.T) {
|
|
|
|
c := &dataplane.Converter{}
|
|
|
|
|
|
|
|
iptables, err := c.GetProtobufRulesFromIptableFile(
|
|
|
|
util.IptablesFilterTable,
|
|
|
|
npmCacheFile,
|
|
|
|
iptableSaveFile,
|
|
|
|
)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, prettyPrintIPTables(iptables))
|
|
|
|
}
|