test(portforward): deflake TestGetListener

Signed-off-by: knight42 <anonymousknight96@gmail.com>

Kubernetes-commit: bb68741fcbe82a7b17e05e82f7a71bd0c4815e02
This commit is contained in:
knight42 2020-09-09 15:40:56 +08:00 committed by Kubernetes Publisher
parent f0b431a6e0
commit 9ff85ef8ef

View File

@ -17,6 +17,7 @@ limitations under the License.
package portforward package portforward
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -309,12 +310,13 @@ func TestGetListener(t *testing.T) {
} }
for i, testCase := range testCases { for i, testCase := range testCases {
expectedListenerPort := "12345" forwardedPort := &ForwardedPort{Local: 0, Remote: 12345}
listener, err := pf.getListener(testCase.Protocol, testCase.Hostname, &ForwardedPort{12345, 12345}) listener, err := pf.getListener(testCase.Protocol, testCase.Hostname, forwardedPort)
if err != nil && strings.Contains(err.Error(), "cannot assign requested address") { if err != nil && strings.Contains(err.Error(), "cannot assign requested address") {
t.Logf("Can't test #%d: %v", i, err) t.Logf("Can't test #%d: %v", i, err)
continue continue
} }
expectedListenerPort := fmt.Sprintf("%d", forwardedPort.Local)
errorRaised := err != nil errorRaised := err != nil
if testCase.ShouldRaiseError != errorRaised { if testCase.ShouldRaiseError != errorRaised {
@ -331,7 +333,7 @@ func TestGetListener(t *testing.T) {
} }
host, port, _ := net.SplitHostPort(listener.Addr().String()) host, port, _ := net.SplitHostPort(listener.Addr().String())
t.Logf("Asked a %s forward for: %s:%v, got listener %s:%s, expected: %s", testCase.Protocol, testCase.Hostname, 12345, host, port, expectedListenerPort) t.Logf("Asked a %s forward for: %s:0, got listener %s:%s, expected: %s", testCase.Protocol, testCase.Hostname, host, port, expectedListenerPort)
if host != testCase.ExpectedListenerAddress { if host != testCase.ExpectedListenerAddress {
t.Errorf("Test case #%d failed: Listener does not listen on expected address: asked '%v' got '%v'", i, testCase.ExpectedListenerAddress, host) t.Errorf("Test case #%d failed: Listener does not listen on expected address: asked '%v' got '%v'", i, testCase.ExpectedListenerAddress, host)
} }
@ -340,7 +342,6 @@ func TestGetListener(t *testing.T) {
} }
listener.Close() listener.Close()
} }
} }