From 761473cd447791f343032bbc188915b24af265f7 Mon Sep 17 00:00:00 2001 From: jornshen Date: Tue, 19 Jan 2021 21:11:48 +0800 Subject: [PATCH] add ut for utils WriteLine WriteBytesLine --- pkg/proxy/util/utils_test.go | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/pkg/proxy/util/utils_test.go b/pkg/proxy/util/utils_test.go index fe3d6680fc1..aa1e6e18aba 100644 --- a/pkg/proxy/util/utils_test.go +++ b/pkg/proxy/util/utils_test.go @@ -17,10 +17,12 @@ limitations under the License. package util import ( + "bytes" "context" "fmt" "net" "reflect" + "strings" "testing" v1 "k8s.io/api/core/v1" @@ -929,3 +931,67 @@ func TestGetClusterIPByFamily(t *testing.T) { } } + +func TestWriteLine(t *testing.T) { + testCases := []struct { + name string + words []string + expected string + }{ + { + name: "write no word", + words: []string{}, + expected: "", + }, + { + name: "write one word", + words: []string{"test1"}, + expected: "test1\n", + }, + { + name: "write multi word", + words: []string{"test1", "test2", "test3"}, + expected: "test1 test2 test3\n", + }, + } + testBuffer := bytes.NewBuffer(nil) + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + testBuffer.Reset() + WriteLine(testBuffer, testCase.words...) + if !strings.EqualFold(testBuffer.String(), testCase.expected) { + t.Fatalf("write word is %v\n expected: %s, got: %s", testCase.words, testCase.expected, testBuffer.String()) + } + }) + } +} + +func TestWriteBytesLine(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 := bytes.NewBuffer(nil) + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + testBuffer.Reset() + WriteBytesLine(testBuffer, testCase.bytes) + if !strings.EqualFold(testBuffer.String(), testCase.expected) { + t.Fatalf("write word is %v\n expected: %s, got: %s", testCase.bytes, testCase.expected, testBuffer.String()) + } + }) + } +}