pkg/util/iptables: use buf.String() instead of string(buf.Bytes())

Signed-off-by: TommyStarK <thomasmilox@gmail.com>
This commit is contained in:
TommyStarK 2023-06-04 15:52:44 +02:00
parent eca1f9d2d5
commit 1fcfd1d509
4 changed files with 14 additions and 9 deletions

View File

@ -4660,8 +4660,8 @@ func TestCreateAndLinkKubeChain(t *testing.T) {
:KUBE-SOURCE-RANGES-FIREWALL - [0:0]
:KUBE-IPVS-FILTER - [0:0]
`
assert.Equal(t, expectedNATChains, string(fp.natChains.Bytes()))
assert.Equal(t, expectedFilterChains, string(fp.filterChains.Bytes()))
assert.Equal(t, expectedNATChains, fp.natChains.String())
assert.Equal(t, expectedFilterChains, fp.filterChains.String())
}
// This test ensures that the iptables proxier supports translating Endpoints to

View File

@ -483,6 +483,11 @@ func (buf *LineBuffer) Bytes() []byte {
return buf.b.Bytes()
}
// String returns the contents of buf as a string
func (buf *LineBuffer) String() string {
return buf.b.String()
}
// Lines returns the number of lines in buf. Note that more precisely, this returns the
// number of times Write() or WriteBytes() was called; it assumes that you never wrote
// any newlines to the buffer yourself.

View File

@ -962,7 +962,7 @@ func TestLineBufferWrite(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
testBuffer.Write(testCase.input...)
if want, got := testCase.expected, string(testBuffer.Bytes()); !strings.EqualFold(want, got) {
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 {
@ -1005,7 +1005,7 @@ func TestLineBufferWriteBytes(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
testBuffer.Reset()
testBuffer.WriteBytes(testCase.bytes)
if want, got := testCase.expected, string(testBuffer.Bytes()); !strings.EqualFold(want, got) {
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)
}
})

View File

@ -49,7 +49,7 @@ func TestFakeIPTables(t *testing.T) {
*mangle
COMMIT
`, "\n"))
if string(buf.Bytes()) != expected {
if buf.String() != expected {
t.Fatalf("bad initial dump. expected:\n%s\n\ngot:\n%s\n", expected, buf.Bytes())
}
@ -143,7 +143,7 @@ func TestFakeIPTables(t *testing.T) {
*mangle
COMMIT
`, "\n"))
if string(buf.Bytes()) != expected {
if buf.String() != expected {
t.Fatalf("bad sanity-check dump. expected:\n%s\n\ngot:\n%s\n", expected, buf.Bytes())
}
@ -214,7 +214,7 @@ func TestFakeIPTables(t *testing.T) {
*mangle
COMMIT
`, "\n"))
if string(buf.Bytes()) != expected {
if buf.String() != expected {
t.Fatalf("bad post-restore dump. expected:\n%s\n\ngot:\n%s\n", expected, buf.Bytes())
}
@ -282,7 +282,7 @@ func TestFakeIPTables(t *testing.T) {
*mangle
COMMIT
`, "\n"))
if string(buf.Bytes()) != expected {
if buf.String() != expected {
t.Fatalf("bad post-second-restore dump. expected:\n%s\n\ngot:\n%s\n", expected, buf.Bytes())
}
@ -339,7 +339,7 @@ func TestFakeIPTables(t *testing.T) {
*mangle
COMMIT
`, "\n"))
if string(buf.Bytes()) != expected {
if buf.String() != expected {
t.Fatalf("bad post-restore-all dump. expected:\n%s\n\ngot:\n%s\n", expected, buf.Bytes())
}
}