You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.7 KiB
Go
149 lines
4.7 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"g.luoli.live/root/virt/agent/internal/capability"
|
|
"g.luoli.live/root/virt/agent/internal/config"
|
|
"g.luoli.live/root/virt/agent/internal/hypervisor"
|
|
"g.luoli.live/root/virt/agent/internal/operation"
|
|
)
|
|
|
|
func TestNewRequiredSigningWithoutAuthFailsClosed(t *testing.T) {
|
|
c := config.Defaults()
|
|
c.AgentID = "host-required-auth"
|
|
c.RequireSigned = true
|
|
store := operation.NewMemory()
|
|
backend := hypervisor.NewFakeBackend(hypervisor.DefaultCapabilitySnapshot())
|
|
s, err := New(Options{
|
|
Config: c,
|
|
Store: store,
|
|
Capabilities: capability.BackendProvider{Backend: backend, AgentID: c.AgentID, ProtocolVersion: c.ProtocolVersion},
|
|
Backend: backend,
|
|
HostBackendReady: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s.auth == nil || s.auth.Ready() {
|
|
t.Fatal("required signing without injected Auth must construct an unavailable verifier")
|
|
}
|
|
|
|
operationID := "00000000-0000-4000-8000-000000000091"
|
|
body := []byte(`{"operation_id":"` + operationID + `","idempotency_key":"idem-required-auth-0091","kind":"capability_probe","target":"` + c.AgentID + `","payload":{}}`)
|
|
recorder := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(body)))
|
|
if recorder.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status = %d, body = %s; required signing must fail closed", recorder.Code, recorder.Body.String())
|
|
}
|
|
if _, err := store.Get(operationID); !errors.Is(err, operation.ErrNotFound) {
|
|
t.Fatalf("unsigned request entered operation journal: err=%v", err)
|
|
}
|
|
}
|
|
|
|
// probeCountingBackend blocks the first host probe so concurrent health calls
|
|
// have a deterministic in-flight window in which to exercise single-flight.
|
|
type probeCountingBackend struct {
|
|
*hypervisor.FakeBackend
|
|
calls atomic.Int32
|
|
startedOnce sync.Once
|
|
started chan struct{}
|
|
release chan struct{}
|
|
}
|
|
|
|
func (b *probeCountingBackend) Probe(ctx context.Context) (hypervisor.ProbeResult, error) {
|
|
b.calls.Add(1)
|
|
b.startedOnce.Do(func() { close(b.started) })
|
|
select {
|
|
case <-b.release:
|
|
return b.FakeBackend.Probe(ctx)
|
|
case <-ctx.Done():
|
|
return hypervisor.ProbeResult{}, ctx.Err()
|
|
}
|
|
}
|
|
|
|
func TestHealthReadinessProbeSingleFlightAndThrottle(t *testing.T) {
|
|
c := config.Defaults()
|
|
c.AgentID = "host-health-probe"
|
|
base := hypervisor.NewFakeBackend(hypervisor.DefaultCapabilitySnapshot())
|
|
backend := &probeCountingBackend{
|
|
FakeBackend: base,
|
|
started: make(chan struct{}),
|
|
release: make(chan struct{}),
|
|
}
|
|
s, err := New(Options{
|
|
Config: c,
|
|
Store: operation.NewMemory(),
|
|
Capabilities: capability.BackendProvider{Backend: backend, AgentID: c.AgentID, ProtocolVersion: c.ProtocolVersion},
|
|
Backend: backend,
|
|
HostBackendReady: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
firstDone := make(chan struct{})
|
|
go func() {
|
|
s.refreshHostReadinessCached(context.Background())
|
|
close(firstDone)
|
|
}()
|
|
select {
|
|
case <-backend.started:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("timed out waiting for first host probe")
|
|
}
|
|
|
|
const concurrent = 16
|
|
var wg sync.WaitGroup
|
|
wg.Add(concurrent)
|
|
for i := 0; i < concurrent; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
s.refreshHostReadinessCached(context.Background())
|
|
}()
|
|
}
|
|
// Give the waiters a scheduling window while the first probe is blocked.
|
|
time.Sleep(25 * time.Millisecond)
|
|
if got := backend.calls.Load(); got != 1 {
|
|
t.Fatalf("concurrent health probes = %d, want one in-flight probe", got)
|
|
}
|
|
close(backend.release)
|
|
select {
|
|
case <-firstDone:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("first host probe did not finish")
|
|
}
|
|
wg.Wait()
|
|
if got := backend.calls.Load(); got != 1 {
|
|
t.Fatalf("coalesced health probes = %d, want one", got)
|
|
}
|
|
|
|
// The health handler must use the same short cache rather than bypassing it.
|
|
recorder := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/v1/health", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("health status = %d, body = %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := backend.calls.Load(); got != 1 {
|
|
t.Fatalf("health request inside throttle window started %d probes, want one", got)
|
|
}
|
|
|
|
time.Sleep(healthReadinessProbeTTL + 20*time.Millisecond)
|
|
recorder = httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/v1/health", nil))
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("second health status = %d, body = %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if got := backend.calls.Load(); got != 2 {
|
|
t.Fatalf("health probe after throttle window = %d, want two total probes", got)
|
|
}
|
|
}
|