Skip to content

Commit

Permalink
pkg/util: use random port to make test TestVerifyCommonNameAndRotate …
Browse files Browse the repository at this point in the history
…stable (#50369)

close #37819
  • Loading branch information
tiancaiamao authored Jan 12, 2024
1 parent e87d949 commit 61e0620
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pkg/util/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ func TestVerifyCommonNameAndRotate(t *testing.T) {
util.WithVerifyCommonName([]string{"client1"}),
)
require.NoError(t, err)
port := 9292
url := fmt.Sprintf("https://127.0.0.1:%d", port)
ctx, cancel := context.WithCancel(context.Background())
server := runServer(ctx, serverTLS, port, t)
server, port := runServer(ctx, serverTLS, t)
defer func() {
cancel()
server.Close()
}()
url := fmt.Sprintf("https://127.0.0.1:%d", port)

clientTLS1, err := util.NewTLSConfig(
util.WithCAContent(caData),
Expand Down Expand Up @@ -143,14 +142,13 @@ func TestCA(t *testing.T) {
util.WithCertAndKeyContent(serverCert, serverKey),
)
require.NoError(t, err)
port := 9293
url := fmt.Sprintf("https://127.0.0.1:%d", port)
ctx, cancel := context.WithCancel(context.Background())
server := runServer(ctx, serverTLS, port, t)
server, port := runServer(ctx, serverTLS, t)
defer func() {
cancel()
server.Close()
}()
url := fmt.Sprintf("https://127.0.0.1:%d", port)

// test only CA
clientTLS1, err := util.NewTLSConfig(
Expand Down Expand Up @@ -202,18 +200,19 @@ func handler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("This an example server"))
}

func runServer(ctx context.Context, tlsCfg *tls.Config, port int, t *testing.T) *http.Server {
func runServer(ctx context.Context, tlsCfg *tls.Config, t *testing.T) (*http.Server, int) {
http.HandleFunc("/", handler)
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: nil}
server := &http.Server{Addr: ":0", Handler: nil}

conn, err := net.Listen("tcp", server.Addr)
if err != nil {
require.NoError(t, err)
}
port := conn.Addr().(*net.TCPAddr).Port

tlsListener := tls.NewListener(conn, tlsCfg)
go server.Serve(tlsListener)
return server
return server, port
}

// generateCerts returns the PEM contents of a CA certificate and some certificates and private keys per Common Name in
Expand Down

0 comments on commit 61e0620

Please sign in to comment.