...

Source file src/k8s.io/kubernetes/pkg/proxy/util/linebuffer_test.go

Documentation: k8s.io/kubernetes/pkg/proxy/util

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"math/rand"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestLineBufferWrite(t *testing.T) {
    26  	testCases := []struct {
    27  		name     string
    28  		input    []interface{}
    29  		expected string
    30  	}{
    31  		{
    32  			name:     "none",
    33  			input:    []interface{}{},
    34  			expected: "\n",
    35  		},
    36  		{
    37  			name:     "one string",
    38  			input:    []interface{}{"test1"},
    39  			expected: "test1\n",
    40  		},
    41  		{
    42  			name:     "one slice",
    43  			input:    []interface{}{[]string{"test1", "test2"}},
    44  			expected: "test1 test2\n",
    45  		},
    46  		{
    47  			name:     "mixed",
    48  			input:    []interface{}{"s1", "s2", []string{"s3", "s4"}, "", "s5", []string{}, []string{"s6"}, "s7"},
    49  			expected: "s1 s2 s3 s4  s5  s6 s7\n",
    50  		},
    51  	}
    52  	testBuffer := NewLineBuffer()
    53  	for _, testCase := range testCases {
    54  		t.Run(testCase.name, func(t *testing.T) {
    55  			testBuffer.Reset()
    56  			testBuffer.Write(testCase.input...)
    57  			if want, got := testCase.expected, testBuffer.String(); !strings.EqualFold(want, got) {
    58  				t.Fatalf("write word is %v\n expected: %q, got: %q", testCase.input, want, got)
    59  			}
    60  			if testBuffer.Lines() != 1 {
    61  				t.Fatalf("expected 1 line, got: %d", testBuffer.Lines())
    62  			}
    63  		})
    64  	}
    65  }
    66  
    67  func TestLineBufferWritePanic(t *testing.T) {
    68  	defer func() {
    69  		if r := recover(); r == nil {
    70  			t.Errorf("did not panic")
    71  		}
    72  	}()
    73  	testBuffer := NewLineBuffer()
    74  	testBuffer.Write("string", []string{"a", "slice"}, 1234)
    75  }
    76  
    77  func TestLineBufferWriteBytes(t *testing.T) {
    78  	testCases := []struct {
    79  		name     string
    80  		bytes    []byte
    81  		expected string
    82  	}{
    83  		{
    84  			name:     "empty bytes",
    85  			bytes:    []byte{},
    86  			expected: "\n",
    87  		},
    88  		{
    89  			name:     "test bytes",
    90  			bytes:    []byte("test write bytes line"),
    91  			expected: "test write bytes line\n",
    92  		},
    93  	}
    94  
    95  	testBuffer := NewLineBuffer()
    96  	for _, testCase := range testCases {
    97  		t.Run(testCase.name, func(t *testing.T) {
    98  			testBuffer.Reset()
    99  			testBuffer.WriteBytes(testCase.bytes)
   100  			if want, got := testCase.expected, testBuffer.String(); !strings.EqualFold(want, got) {
   101  				t.Fatalf("write bytes is %v\n expected: %s, got: %s", testCase.bytes, want, got)
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  // obtained from https://stackoverflow.com/a/22892986
   108  var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
   109  
   110  func randSeq() string {
   111  	b := make([]rune, 30)
   112  	for i := range b {
   113  		b[i] = letters[rand.Intn(len(letters))]
   114  	}
   115  	return string(b)
   116  }
   117  
   118  func TestWriteCountLines(t *testing.T) {
   119  	testCases := []struct {
   120  		name     string
   121  		expected int
   122  	}{
   123  		{
   124  			name:     "write no line",
   125  			expected: 0,
   126  		},
   127  		{
   128  			name:     "write one line",
   129  			expected: 1,
   130  		},
   131  		{
   132  			name:     "write 100 lines",
   133  			expected: 100,
   134  		},
   135  		{
   136  			name:     "write 1000 lines",
   137  			expected: 1000,
   138  		},
   139  		{
   140  			name:     "write 10000 lines",
   141  			expected: 10000,
   142  		},
   143  		{
   144  			name:     "write 100000 lines",
   145  			expected: 100000,
   146  		},
   147  	}
   148  	testBuffer := NewLineBuffer()
   149  	discardBuffer := NewDiscardLineBuffer()
   150  	for _, testCase := range testCases {
   151  		t.Run(testCase.name, func(t *testing.T) {
   152  			testBuffer.Reset()
   153  			discardBuffer.Reset()
   154  			for i := 0; i < testCase.expected; i++ {
   155  				testBuffer.Write(randSeq())
   156  				discardBuffer.Write(randSeq())
   157  			}
   158  			n := testBuffer.Lines()
   159  			if n != testCase.expected {
   160  				t.Fatalf("lines expected: %d, got: %d", testCase.expected, n)
   161  			}
   162  			n = discardBuffer.Lines()
   163  			if n != testCase.expected {
   164  				t.Fatalf("discardBuffer lines expected: %d, got: %d", testCase.expected, n)
   165  			}
   166  		})
   167  	}
   168  }
   169  

View as plain text