package statuspage import ( "context" "errors" "fmt" "net/http" "testing" "time" "git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi" ) func (f *fakeSource) callCount() int { f.mu.Lock() defer f.mu.Unlock() return f.calls } func TestUnknownNamesAreNotAskedAgainAtOnce(t *testing.T) { now := time.Now() src := newSource() h := newHarness(t, src, "", &now) for range 3 { if code, _ := h.get("nope.example.test", "/"); code != http.StatusNotFound { t.Fatalf("unknown host = %d, want 404", code) } } if n := src.callCount(); n != 1 { t.Errorf("the API was asked %d times about one unknown host, want 1", n) } now = now.Add(missFor) h.get("nope.example.test", "/") if n := src.callCount(); n != 2 { t.Errorf("after %v the API was asked %d times in all, want 2", missFor, n) } h.srv.mu.Lock() visited := len(h.srv.visited) h.srv.mu.Unlock() if visited != 0 { t.Errorf("unknown hosts were recorded as visited pages: %d", visited) } } func TestFloodOfMadeUpNamesIsThrottled(t *testing.T) { now := time.Now() src := newSource() h := newHarness(t, src, "", &now) h.srv.Sync(context.Background()) before := src.callCount() for i := range 200 { h.get(fmt.Sprintf("x%d.example.test", i), "/") } if n := src.callCount() - before; n > unknownPerSecond { t.Errorf("a flood of made-up hosts reached the API %d times in one second, want at most %d", n, unknownPerSecond) } // Pages the sync cached are still served during the flood. if code, _ := h.get("status.acme.test", "/"); code != http.StatusOK { t.Errorf("a known page during the flood = %d, want 200", code) } now = now.Add(time.Second) if err := h.srv.AllowCertificate(context.Background(), "status.acme.test"); err != nil { t.Errorf("a known custom domain after the flood: %v", err) } } type gatedSource struct { *fakeSource asked []string err error } func (g *gatedSource) AllowCertificate(_ context.Context, name string) error { g.asked = append(g.asked, name) return g.err } func TestNewCertificatesAreOrderedOnlyWhenTheAPIAllows(t *testing.T) { now := time.Now() g := &gatedSource{fakeSource: newSource()} h := newHarness(t, g.fakeSource, "", &now) h.srv.src = g ctx := context.Background() obtain := func(name string, renewal bool) error { data := map[string]any{"identifier": name} if renewal { data["renewal"] = true } return h.srv.OnCertEvent(ctx, "cert_obtaining", data) } if err := obtain("status.acme.test", false); err != nil { t.Errorf("allowed order: %v", err) } // Renewals, the default hosts and other events are not asked about. if err := obtain("status.acme.test", true); err != nil { t.Errorf("renewal: %v", err) } if err := obtain("status.example.test", false); err != nil { t.Errorf("default host: %v", err) } if err := h.srv.OnCertEvent(ctx, "cert_obtained", map[string]any{"identifier": "status.acme.test"}); err != nil { t.Errorf("other event: %v", err) } if len(g.asked) != 1 || g.asked[0] != "status.acme.test" { t.Errorf("asked about %v, want only the new order", g.asked) } for _, refuse := range []error{agentapi.ErrRateLimited, agentapi.ErrNotFound, errors.New("api down")} { g.err = refuse if err := obtain("status.acme.test", false); err == nil { t.Errorf("order went ahead although the API answered %v", refuse) } } }