Skip to content

Commit bb68741

Browse files
committed
test(portforward): deflake TestGetListener
Signed-off-by: knight42 <[email protected]>
1 parent b16070f commit bb68741

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

staging/src/k8s.io/client-go/tools/portforward/portforward_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package portforward
1818

1919
import (
20+
"fmt"
2021
"net"
2122
"net/http"
2223
"os"
@@ -309,12 +310,13 @@ func TestGetListener(t *testing.T) {
309310
}
310311

311312
for i, testCase := range testCases {
312-
expectedListenerPort := "12345"
313-
listener, err := pf.getListener(testCase.Protocol, testCase.Hostname, &ForwardedPort{12345, 12345})
313+
forwardedPort := &ForwardedPort{Local: 0, Remote: 12345}
314+
listener, err := pf.getListener(testCase.Protocol, testCase.Hostname, forwardedPort)
314315
if err != nil && strings.Contains(err.Error(), "cannot assign requested address") {
315316
t.Logf("Can't test #%d: %v", i, err)
316317
continue
317318
}
319+
expectedListenerPort := fmt.Sprintf("%d", forwardedPort.Local)
318320
errorRaised := err != nil
319321

320322
if testCase.ShouldRaiseError != errorRaised {
@@ -331,7 +333,7 @@ func TestGetListener(t *testing.T) {
331333
}
332334

333335
host, port, _ := net.SplitHostPort(listener.Addr().String())
334-
t.Logf("Asked a %s forward for: %s:%v, got listener %s:%s, expected: %s", testCase.Protocol, testCase.Hostname, 12345, host, port, expectedListenerPort)
336+
t.Logf("Asked a %s forward for: %s:0, got listener %s:%s, expected: %s", testCase.Protocol, testCase.Hostname, host, port, expectedListenerPort)
335337
if host != testCase.ExpectedListenerAddress {
336338
t.Errorf("Test case #%d failed: Listener does not listen on expected address: asked '%v' got '%v'", i, testCase.ExpectedListenerAddress, host)
337339
}
@@ -340,7 +342,6 @@ func TestGetListener(t *testing.T) {
340342

341343
}
342344
listener.Close()
343-
344345
}
345346
}
346347

0 commit comments

Comments
 (0)