Files
ilkerandClaude Opus 5 47db704f1b hpcod: the longest step in a build now says it is still working
Waiting for a head node's portal is where a build spends most of its
time - the machine is installing and configuring itself - and it
wrote one line and then nothing for up to ten minutes. Silence is
indistinguishable from a hang. Somebody watching the log concluded
the build had stopped, twice, and both times it was working
correctly and finished a few minutes later.

It now reports what it is waiting on and for how long, every few
attempts rather than on a clock of its own, so the pacing follows
whatever interval the caller chose: half a minute apart at the ten
seconds a build uses, and still present in a test whose whole budget
is shorter than that. A portal that answers at once still prints
nothing, because there was nothing to wait for.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-22 10:49:25 +02:00

173 lines
5.4 KiB
Go

package provision
import (
"context"
"encoding/pem"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func aPortal(t *testing.T) *httptest.Server {
t.Helper()
srv := httptest.NewTLSServer(nil)
t.Cleanup(srv.Close)
return srv
}
func registry(t *testing.T) Registry {
t.Helper()
dir := t.TempDir()
return Registry{Path: filepath.Join(dir, "clusters.yaml"), CertDir: filepath.Join(dir, "certs")}
}
// Publishing records where the portal is and pins what it presented, so
// the proxy can both find it and refuse anything else answering there.
func TestPublishPinsWhatThePortalPresented(t *testing.T) {
srv := aPortal(t)
r := registry(t)
addr := strings.TrimPrefix(srv.URL, "https://")
if err := r.Publish(context.Background(), 3, addr); err != nil {
t.Fatal(err)
}
body, err := os.ReadFile(r.Path)
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"id: 3", "portal: " + addr, "c003.crt"} {
if !strings.Contains(string(body), want) {
t.Errorf("the registry does not mention %q:\n%s", want, body)
}
}
pinned, err := os.ReadFile(filepath.Join(r.CertDir, "c003.crt"))
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(string(pinned), "-----BEGIN CERTIFICATE-----") {
t.Errorf("what was pinned is not a certificate: %q", pinned)
}
// And it is that portal's certificate, not some other one.
wantPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw})
if string(pinned) != string(wantPEM) {
t.Error("what was pinned is not the certificate the portal presented")
}
}
// Rebuilding replaces the entry rather than adding a second one for the
// same cluster, which would leave the proxy with two answers.
func TestPublishingTwiceReplacesTheEntry(t *testing.T) {
r := registry(t)
first, second := aPortal(t), aPortal(t)
if err := r.Publish(context.Background(), 3, strings.TrimPrefix(first.URL, "https://")); err != nil {
t.Fatal(err)
}
if err := r.Publish(context.Background(), 3, strings.TrimPrefix(second.URL, "https://")); err != nil {
t.Fatal(err)
}
body, _ := os.ReadFile(r.Path)
if n := strings.Count(string(body), "id: 3"); n != 1 {
t.Errorf("the cluster appears %d times:\n%s", n, body)
}
if !strings.Contains(string(body), strings.TrimPrefix(second.URL, "https://")) {
t.Errorf("the registry still points at the old address:\n%s", body)
}
}
func TestUnpublishRemovesOnlyThatCluster(t *testing.T) {
r := registry(t)
a, b := aPortal(t), aPortal(t)
if err := r.Publish(context.Background(), 1, strings.TrimPrefix(a.URL, "https://")); err != nil {
t.Fatal(err)
}
if err := r.Publish(context.Background(), 2, strings.TrimPrefix(b.URL, "https://")); err != nil {
t.Fatal(err)
}
if err := r.Unpublish(1); err != nil {
t.Fatal(err)
}
body, _ := os.ReadFile(r.Path)
if strings.Contains(string(body), "id: 1") {
t.Errorf("cluster 1 is still published:\n%s", body)
}
if !strings.Contains(string(body), "id: 2") {
t.Errorf("cluster 2 was removed too:\n%s", body)
}
}
// A portal that never answers must not be published. Half an entry - an
// address with no certificate - would take the proxy's route for that
// cluster with it.
func TestAPortalThatNeverAnswersIsNotPublished(t *testing.T) {
r := registry(t)
err := r.Publish(context.Background(), 3, "127.0.0.1:1") // nothing listens
if err == nil {
t.Fatal("publishing succeeded against nothing")
}
if _, statErr := os.Stat(r.Path); statErr == nil {
t.Error("a registry file was written for a cluster that was never reached")
}
}
func TestWaitForPortalGivesUp(t *testing.T) {
start := time.Now()
err := waitForPortal(context.Background(), "127.0.0.1:1", 20*time.Millisecond, 150*time.Millisecond, nil)
if err == nil {
t.Fatal("waiting succeeded against nothing")
}
if time.Since(start) > 3*time.Second {
t.Errorf("waited %v, far past the limit", time.Since(start))
}
}
func TestWaitForPortalReturnsWhenItAnswers(t *testing.T) {
srv := aPortal(t)
if err := waitForPortal(context.Background(), strings.TrimPrefix(srv.URL, "https://"),
20*time.Millisecond, 2*time.Second, nil); err != nil {
t.Fatal(err)
}
}
// The wait for a head node is the longest step in a build by a wide
// margin, and it used to write one line and then nothing for up to ten
// minutes. Silence is indistinguishable from a hang: the person watching
// the log reasonably concludes the build has stopped.
func TestTheWaitSaysItIsStillWaiting(t *testing.T) {
var out strings.Builder
// Nothing listens on port 1, so every attempt fails and the loop runs
// to its deadline - which is the case worth reporting on.
err := waitForPortal(context.Background(), "127.0.0.1:1",
5*time.Millisecond, 200*time.Millisecond, &out)
if err == nil {
t.Fatal("a portal that never came up was reported as up")
}
if !strings.Contains(out.String(), "still waiting after") {
t.Errorf("the wait said nothing while it waited:\n%q", out.String())
}
}
// And it stays quiet when there is nothing to wait for: a portal that
// answers immediately should not produce a progress line at all.
func TestAnImmediateAnswerSaysNothing(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
defer srv.Close()
var out strings.Builder
if err := waitForPortal(context.Background(), strings.TrimPrefix(srv.URL, "https://"),
10*time.Millisecond, 2*time.Second, &out); err != nil {
t.Fatal(err)
}
if out.Len() != 0 {
t.Errorf("a portal that answered at once still produced %q", out.String())
}
}