Add a dummy implementation of proxyutil.LineBuffer

Rather than actually assembling all of the rules we aren't going to
use, just count them and throw them away.
This commit is contained in:
Dan Winship
2023-07-06 14:31:00 -04:00
parent 68ed020b2a
commit 883d0c3b71
8 changed files with 344 additions and 229 deletions

View File

@@ -17,10 +17,8 @@ limitations under the License.
package util
import (
"math/rand"
"net"
"reflect"
"strings"
"testing"
v1 "k8s.io/api/core/v1"
@@ -707,145 +705,6 @@ func TestRevertPorts(t *testing.T) {
}
}
func TestLineBufferWrite(t *testing.T) {
testCases := []struct {
name string
input []interface{}
expected string
}{
{
name: "none",
input: []interface{}{},
expected: "\n",
},
{
name: "one string",
input: []interface{}{"test1"},
expected: "test1\n",
},
{
name: "one slice",
input: []interface{}{[]string{"test1", "test2"}},
expected: "test1 test2\n",
},
{
name: "mixed",
input: []interface{}{"s1", "s2", []string{"s3", "s4"}, "", "s5", []string{}, []string{"s6"}, "s7"},
expected: "s1 s2 s3 s4 s5 s6 s7\n",
},
}
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
testBuffer.Write(testCase.input...)
if want, got := testCase.expected, testBuffer.String(); !strings.EqualFold(want, got) {
t.Fatalf("write word is %v\n expected: %q, got: %q", testCase.input, want, got)
}
if testBuffer.Lines() != 1 {
t.Fatalf("expected 1 line, got: %d", testBuffer.Lines())
}
})
}
}
func TestLineBufferWritePanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("did not panic")
}
}()
testBuffer := LineBuffer{}
testBuffer.Write("string", []string{"a", "slice"}, 1234)
}
func TestLineBufferWriteBytes(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expected string
}{
{
name: "empty bytes",
bytes: []byte{},
expected: "\n",
},
{
name: "test bytes",
bytes: []byte("test write bytes line"),
expected: "test write bytes line\n",
},
}
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
testBuffer.WriteBytes(testCase.bytes)
if want, got := testCase.expected, testBuffer.String(); !strings.EqualFold(want, got) {
t.Fatalf("write bytes is %v\n expected: %s, got: %s", testCase.bytes, want, got)
}
})
}
}
func TestWriteCountLines(t *testing.T) {
testCases := []struct {
name string
expected int
}{
{
name: "write no line",
expected: 0,
},
{
name: "write one line",
expected: 1,
},
{
name: "write 100 lines",
expected: 100,
},
{
name: "write 1000 lines",
expected: 1000,
},
{
name: "write 10000 lines",
expected: 10000,
},
{
name: "write 100000 lines",
expected: 100000,
},
}
testBuffer := LineBuffer{}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
for i := 0; i < testCase.expected; i++ {
testBuffer.Write(randSeq())
}
n := testBuffer.Lines()
if n != testCase.expected {
t.Fatalf("lines expected: %d, got: %d", testCase.expected, n)
}
})
}
}
// obtained from https://stackoverflow.com/a/22892986
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq() string {
b := make([]rune, 30)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func mustParseIPAddr(str string) net.Addr {
a, err := net.ResolveIPAddr("ip", str)
if err != nil {