Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 RuntimeSDK/client: use random port for unit test server #6665

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions internal/runtime/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -514,7 +513,6 @@ func Test_defaultAndValidateDiscoveryResponse(t *testing.T) {
}

func TestClient_CallExtension(t *testing.T) {
testHostPort := "127.0.0.1:9090"
ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -530,7 +528,8 @@ func TestClient_CallExtension(t *testing.T) {
validExtensionHandlerWithFailPolicy := runtimev1.ExtensionConfig{
Spec: runtimev1.ExtensionConfigSpec{
ClientConfig: runtimev1.ClientConfig{
URL: pointer.String(fmt.Sprintf("https://%s/", testHostPort)),
// Set a fake URL, in test cases where we start the test server the URL will be overridden.
URL: pointer.String("https://127.0.0.1/"),
CABundle: testcerts.CACert,
},
NamespaceSelector: &metav1.LabelSelector{},
Expand All @@ -552,7 +551,8 @@ func TestClient_CallExtension(t *testing.T) {
validExtensionHandlerWithIgnorePolicy := runtimev1.ExtensionConfig{
Spec: runtimev1.ExtensionConfigSpec{
ClientConfig: runtimev1.ClientConfig{
URL: pointer.String(fmt.Sprintf("https://%s/", testHostPort)),
// Set a fake URL, in test cases where we start the test server the URL will be overridden.
URL: pointer.String("https://127.0.0.1/"),
CABundle: testcerts.CACert,
},
NamespaceSelector: &metav1.LabelSelector{}},
Expand Down Expand Up @@ -726,12 +726,14 @@ func TestClient_CallExtension(t *testing.T) {
g := NewWithT(t)

if tt.testServer.start {
if tt.testServer.hostPort == "" {
tt.testServer.hostPort = testHostPort
}
srv := createSecureTestServer(g, tt.testServer)
srv := createSecureTestServer(tt.testServer)
srv.StartTLS()
defer srv.Close()

// Set the URL to the real address of the test server.
for i := range tt.registeredExtensionConfigs {
tt.registeredExtensionConfigs[i].Spec.ClientConfig.URL = pointer.String(fmt.Sprintf("https://%s/", srv.Listener.Addr().String()))
}
}

cat := runtimecatalog.New()
Expand Down Expand Up @@ -765,7 +767,6 @@ func TestClient_CallExtension(t *testing.T) {
}

func TestClient_CallAllExtensions(t *testing.T) {
testHostPort := "127.0.0.1:9090"
ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -780,7 +781,8 @@ func TestClient_CallAllExtensions(t *testing.T) {
extensionConfig := runtimev1.ExtensionConfig{
Spec: runtimev1.ExtensionConfigSpec{
ClientConfig: runtimev1.ClientConfig{
URL: pointer.String(fmt.Sprintf("https://%s/", testHostPort)),
// Set a fake URL, in test cases where we start the test server the URL will be overridden.
URL: pointer.String("https://127.0.0.1/"),
CABundle: testcerts.CACert,
},
NamespaceSelector: &metav1.LabelSelector{},
Expand Down Expand Up @@ -930,12 +932,14 @@ func TestClient_CallAllExtensions(t *testing.T) {
g := NewWithT(t)

if tt.testServer.start {
if tt.testServer.hostPort == "" {
tt.testServer.hostPort = testHostPort
}
srv := createSecureTestServer(g, tt.testServer)
srv := createSecureTestServer(tt.testServer)
srv.StartTLS()
defer srv.Close()

// Set the URL to the real address of the test server.
for i := range tt.registeredExtensionConfigs {
tt.registeredExtensionConfigs[i].Spec.ClientConfig.URL = pointer.String(fmt.Sprintf("https://%s/", srv.Listener.Addr().String()))
}
}

cat := runtimecatalog.New()
Expand Down Expand Up @@ -1131,7 +1135,6 @@ func Test_aggregateResponses(t *testing.T) {
type testServerConfig struct {
start bool
responses map[string]testServerResponse
hostPort string
}

type testServerResponse struct {
Expand All @@ -1150,10 +1153,7 @@ func response(status runtimehooksv1.ResponseStatus) testServerResponse {
}
}

func createSecureTestServer(g *WithT, server testServerConfig) *httptest.Server {
l, err := net.Listen("tcp", server.hostPort)
g.Expect(err).NotTo(HaveOccurred())

func createSecureTestServer(server testServerConfig) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Write the response for the first match in tt.testServer.responses.
Expand All @@ -1177,11 +1177,6 @@ func createSecureTestServer(g *WithT, server testServerConfig) *httptest.Server

srv := newUnstartedTLSServer(mux)

// NewUnstartedServer creates a listener. Close that listener and replace
// with the one we created.
g.Expect(srv.Listener.Close()).To(Succeed())
srv.Listener = l

return srv
}

Expand Down