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.
208 lines
7.9 KiB
Go
208 lines
7.9 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"g.luoli.live/root/virt/agent/internal/capability"
|
|
"g.luoli.live/root/virt/agent/internal/config"
|
|
"g.luoli.live/root/virt/agent/internal/domaincreateplan"
|
|
"g.luoli.live/root/virt/agent/internal/domainpreflight"
|
|
"g.luoli.live/root/virt/agent/internal/hypervisor"
|
|
"g.luoli.live/root/virt/agent/internal/operation"
|
|
)
|
|
|
|
func createPlanRequest() domaincreateplan.Request {
|
|
readonly := true
|
|
secureBoot := false
|
|
return domaincreateplan.Request{
|
|
Domain: domainpreflight.Request{
|
|
UUID: "00000000-0000-4000-8000-000000000951",
|
|
Firmware: "efi",
|
|
Architecture: "x86_64",
|
|
Machine: "q35",
|
|
OSType: "hvm",
|
|
Loader: domainpreflight.LoaderPolicy{Type: "pflash", Readonly: &readonly, SecureBoot: &secureBoot},
|
|
},
|
|
Seed: domaincreateplan.SeedRequest{
|
|
ArtifactID: "00000000-0000-4000-8000-000000000952",
|
|
InstanceID: "instance-951",
|
|
Hostname: "vm-951",
|
|
NetworkConfig: `{"version":2,"ethernets":{}}`,
|
|
StartupScript: "#!/bin/sh\necho panel-secret\n",
|
|
},
|
|
Image: domaincreateplan.ImageRef{ID: "image-951", SHA256: strings.Repeat("c", 64)},
|
|
}
|
|
}
|
|
|
|
func createPlanBody(t *testing.T, operationID, idempotency string, request domaincreateplan.Request) []byte {
|
|
t.Helper()
|
|
payload, err := json.Marshal(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return []byte(fmt.Sprintf(`{"operation_id":%q,"idempotency_key":%q,"kind":"domain_create_plan","target":%q,"payload":%s}`, operationID, idempotency, request.Domain.UUID, payload))
|
|
}
|
|
|
|
func TestDomainCreatePlanIsReadOnlyAndRedactsJournal(t *testing.T) {
|
|
store := operation.NewMemory()
|
|
backend := hypervisor.NewFakeBackend(hypervisor.DefaultCapabilitySnapshot())
|
|
c := config.Defaults()
|
|
c.AgentID = "host-create-plan"
|
|
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)
|
|
}
|
|
request := createPlanRequest()
|
|
id := "00000000-0000-4000-8000-000000000953"
|
|
body := createPlanBody(t, id, "create-plan-0953", request)
|
|
first := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(first, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(body)))
|
|
if first.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", first.Code, first.Body.String())
|
|
}
|
|
var record operation.Record
|
|
if err := json.Unmarshal(first.Body.Bytes(), &record); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if record.Status != operation.StatusSucceeded || record.CommitBoundary != "domain_create_plan_confirmed" {
|
|
t.Fatalf("record=%+v", record)
|
|
}
|
|
for _, forbidden := range []string{request.Seed.StartupScript, request.Seed.NetworkConfig, "source", "/var/lib", `"normalized_xml"`, "<domain"} {
|
|
if bytes.Contains(record.Payload, []byte(forbidden)) || bytes.Contains(record.Result, []byte(forbidden)) {
|
|
t.Fatalf("plan journal/result contains forbidden %q: payload=%s result=%s", forbidden, record.Payload, record.Result)
|
|
}
|
|
}
|
|
var result domaincreateplan.Result
|
|
if err := json.Unmarshal(record.Result, &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.PlanHash == "" || result.XMLHash == "" || !result.LoaderPending || result.SeedArtifactID != request.Seed.ArtifactID || result.NormalizedXML != "" {
|
|
t.Fatalf("result=%+v", result)
|
|
}
|
|
if _, err := backend.ReadDomainXML(context.Background(), request.Domain.UUID); !errors.Is(err, hypervisor.ErrDomainNotFound) {
|
|
t.Fatalf("create plan defined a domain: %v", err)
|
|
}
|
|
|
|
replay := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(replay, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(body)))
|
|
if replay.Code != http.StatusOK {
|
|
t.Fatalf("replay status=%d body=%s", replay.Code, replay.Body.String())
|
|
}
|
|
var replayRecord operation.Record
|
|
if err := json.Unmarshal(replay.Body.Bytes(), &replayRecord); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if replayRecord.Version != record.Version || !bytes.Equal(replayRecord.Result, record.Result) {
|
|
t.Fatalf("replay changed durable result: first=%+v replay=%+v", record, replayRecord)
|
|
}
|
|
|
|
request.Seed.StartupScript = "#!/bin/sh\necho changed\n"
|
|
conflict := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(conflict, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(createPlanBody(t, id, "create-plan-0953", request))))
|
|
if conflict.Code != http.StatusConflict {
|
|
t.Fatalf("same operation with changed body status=%d body=%s", conflict.Code, conflict.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestDomainCreatePlanHostUnavailableIsDurableAndReconciles(t *testing.T) {
|
|
store := operation.NewMemory()
|
|
backend := &preflightProbeBackend{
|
|
FakeBackend: hypervisor.NewFakeBackend(hypervisor.DefaultCapabilitySnapshot()),
|
|
capabilityErr: errors.New("capability transport unavailable"),
|
|
}
|
|
c := config.Defaults()
|
|
c.AgentID = "host-create-plan-unavailable"
|
|
s, err := New(Options{
|
|
Config: c,
|
|
Store: store,
|
|
Capabilities: capability.BackendProvider{Backend: backend, AgentID: c.AgentID, ProtocolVersion: c.ProtocolVersion},
|
|
Backend: backend,
|
|
HostBackendReady: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := createPlanRequest()
|
|
id := "00000000-0000-4000-8000-000000000954"
|
|
first := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(first, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(createPlanBody(t, id, "create-plan-0954", request))))
|
|
if first.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("status=%d body=%s", first.Code, first.Body.String())
|
|
}
|
|
record, err := store.Get(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if record.Status != operation.StatusUncertain || record.ErrorCode != "domain_create_plan_host_unavailable" {
|
|
t.Fatalf("record=%+v", record)
|
|
}
|
|
if _, err := json.Marshal(record.Payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
reconcile := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(reconcile, httptest.NewRequest(http.MethodPost, "/v1/operations/"+id+"/reconcile", nil))
|
|
if reconcile.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("failed reconcile status=%d body=%s", reconcile.Code, reconcile.Body.String())
|
|
}
|
|
backend.capabilityErr = nil
|
|
reconciled := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(reconciled, httptest.NewRequest(http.MethodPost, "/v1/operations/"+id+"/reconcile", nil))
|
|
if reconciled.Code != http.StatusOK {
|
|
t.Fatalf("successful reconcile status=%d body=%s", reconciled.Code, reconciled.Body.String())
|
|
}
|
|
final, err := store.Get(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if final.Status != operation.StatusSucceeded || final.CommitBoundary != "domain_create_plan_confirmed" {
|
|
t.Fatalf("final=%+v", final)
|
|
}
|
|
if backend.probeCalls != 0 || backend.capabilityCalls != 3 {
|
|
t.Fatalf("unexpected host calls probe=%d capabilities=%d", backend.probeCalls, backend.capabilityCalls)
|
|
}
|
|
}
|
|
|
|
func TestDomainCreatePlanRejectsUnknownFieldsBeforeJournal(t *testing.T) {
|
|
store := operation.NewMemory()
|
|
s := testBackendServer(t, "host-create-plan-invalid", store)
|
|
request := createPlanRequest()
|
|
payload, err := json.Marshal(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var raw map[string]any
|
|
if err := json.Unmarshal(payload, &raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain := raw["domain"].(map[string]any)
|
|
domain["xml"] = "<domain/>"
|
|
payload, err = json.Marshal(raw)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := []byte(fmt.Sprintf(`{"operation_id":"00000000-0000-4000-8000-000000000955","idempotency_key":"create-plan-0955","kind":"domain_create_plan","target":%q,"payload":%s}`, request.Domain.UUID, payload))
|
|
recorder := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/v1/operations", bytes.NewReader(body)))
|
|
if recorder.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if _, err := store.Get("00000000-0000-4000-8000-000000000955"); !errors.Is(err, operation.ErrNotFound) {
|
|
t.Fatalf("invalid plan entered journal: %v", err)
|
|
}
|
|
}
|