Skip to content
Open
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
53 changes: 47 additions & 6 deletions internal/mcpserver/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
// a certificate has to answer HTTPS and nothing else.
func TestServesTLSWhenGivenACertificate(t *testing.T) {
opts, roots := writeServingCertificate(t, t.TempDir(), 1)
addr, stop := startServing(t, opts)
addr, stop := startServing(t, opts, roots)
defer stop()

resp, err := httpsClient(roots).Get("https://" + addr + "/healthz")
Expand All @@ -56,8 +56,8 @@ func TestServesTLSWhenGivenACertificate(t *testing.T) {
// watched rather than loaded once: the CSI driver renews it in place.
func TestPicksUpARotatedCertificateWithoutARestart(t *testing.T) {
dir := t.TempDir()
opts, _ := writeServingCertificate(t, dir, 1)
addr, stop := startServing(t, opts)
opts, roots := writeServingCertificate(t, dir, 1)
addr, stop := startServing(t, opts, roots)
defer stop()

_, rotatedRoots := writeServingCertificate(t, dir, 2)
Expand All @@ -74,7 +74,7 @@ func TestPicksUpARotatedCertificateWithoutARestart(t *testing.T) {

// TestServesPlainHTTPWithoutACertificate keeps local runs and e2e working.
func TestServesPlainHTTPWithoutACertificate(t *testing.T) {
addr, stop := startServing(t, TLSOptions{})
addr, stop := startServing(t, TLSOptions{}, nil)
defer stop()

resp, err := http.Get("http://" + addr + "/healthz")
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestTLSFlagsDefaultFromTheBinarysEnvironment(t *testing.T) {
assert.Equal(t, "http", TLSOptions{}.Scheme())
}

func startServing(t *testing.T, opts TLSOptions) (addr string, stop func()) {
func startServing(t *testing.T, opts TLSOptions, roots *x509.CertPool) (addr string, stop func()) {
t.Helper()

listener, err := net.Listen("tcp", "127.0.0.1:0")
Expand All @@ -146,7 +146,10 @@ func startServing(t *testing.T, opts TLSOptions) (addr string, stop func()) {
done := make(chan error, 1)
go func() { done <- serve(ctx, listener, opts, mux) }()

return listener.Addr().String(), func() {
addr = listener.Addr().String()
awaitServing(t, addr, opts, roots, done)

return addr, func() {
cancel()
select {
case err := <-done:
Expand All @@ -157,6 +160,44 @@ func startServing(t *testing.T, opts TLSOptions) (addr string, stop func()) {
}
}

// awaitServing blocks until the server answers, because serve reads the
// certificate files on its own goroutine: a test that rewrote them before that
// read finished failed startup with "loading serving certificate" and left no
// server behind. The caller checks which certificate is served after this.
func awaitServing(t *testing.T, addr string, opts TLSOptions, roots *x509.CertPool, done chan error) {
t.Helper()

client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{DisableKeepAlives: true},
}
if opts.Enabled() {
client = httpsClient(roots)
}

deadline := time.Now().Add(serveReturnTimeout)
for {
resp, err := client.Get(opts.Scheme() + "://" + addr + "/healthz")
if err == nil {
_ = resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return
}
}

select {
case serveErr := <-done:
done <- serveErr
t.Fatalf("serve returned before it answered: %v", serveErr)
default:
}
if time.Now().After(deadline) {
t.Fatalf("the server never answered on %s", addr)
}
time.Sleep(20 * time.Millisecond)
}
}

func httpsClient(roots *x509.CertPool) *http.Client {
return &http.Client{
Timeout: 5 * time.Second,
Expand Down
Loading