add unit tests to verify the fix

Kubernetes-commit: 79150d1ecf16833524f9795d9621e5cd9acd54a4
This commit is contained in:
Artur Zych 2025-04-04 13:55:24 +02:00 committed by Kubernetes Publisher
parent c7defa9550
commit a1589230c6
1 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,65 @@
package remotecommand
import (
"bytes"
"context"
"encoding/json"
"io"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"k8s.io/client-go/tools/remotecommand"
)
func TestHandleResizeEvents(t *testing.T) {
var testTerminalSize remotecommand.TerminalSize
rawTerminalSize, err := json.Marshal(&testTerminalSize)
require.NoError(t, err)
testCases := []struct {
name string
resizeStreamData []byte
cancelContext bool
readFromChannel bool
}{
{
name: "data attempted to be sent on the channel; channel not read; context canceled",
resizeStreamData: rawTerminalSize,
cancelContext: true,
},
{
name: "data attempted to be sent on the channel; channel read; context not canceled",
resizeStreamData: rawTerminalSize,
readFromChannel: true,
},
{
name: "no data attempted to be sent on the channel; context canceled",
cancelContext: true,
},
{
name: "no data attempted to be sent on the channel; context not canceled",
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.TODO())
connCtx := connectionContext{
resizeStream: io.NopCloser(bytes.NewReader(testCase.resizeStreamData)),
resizeChan: make(chan remotecommand.TerminalSize),
}
go handleResizeEvents(ctx, connCtx.resizeStream, connCtx.resizeChan)
if testCase.readFromChannel {
gotTerminalSize := <-connCtx.resizeChan
require.Equal(t, gotTerminalSize, testTerminalSize)
}
if testCase.cancelContext {
cancel()
}
goleak.VerifyNone(t)
cancel()
})
}
}