This commit is contained in:
2026-07-28 19:57:39 -05:00
parent 7f24b64906
commit 8dcee4121b
6 changed files with 61 additions and 22 deletions
+34 -10
View File
@@ -24,12 +24,17 @@ func newFleetTestServer(t *testing.T) *Server {
}
db.SetMaxOpenConns(1)
t.Cleanup(func() { _ = db.Close() })
migration, err := os.ReadFile("../../migrations/002_fleet_registry.sql")
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(string(migration)); err != nil {
t.Fatal(err)
for _, migrationPath := range []string{
"../../migrations/002_fleet_registry.sql",
"../../migrations/003_fleet_hostname.sql",
} {
migration, err := os.ReadFile(migrationPath)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(string(migration)); err != nil {
t.Fatal(err)
}
}
return &Server{
db: db,
@@ -62,6 +67,7 @@ func TestFleetRegistrationAndAuthenticatedEvents(t *testing.T) {
registration := `{
"schema_version": 1,
"installation_id": "` + testInstallationID + `",
"hostname": "pve01",
"credential": "` + testFleetCredential + `",
"proxmenu_version": "2026.7.26-7",
"git_commit": "0123456789abcdef0123456789abcdef01234567",
@@ -79,6 +85,7 @@ func TestFleetRegistrationAndAuthenticatedEvents(t *testing.T) {
event := `{
"schema_version": 1,
"installation_id": "` + testInstallationID + `",
"hostname": "pve01-renamed",
"event": "run_completed",
"result": "success",
"proxmenu_version": "2026.7.26-7",
@@ -95,14 +102,17 @@ func TestFleetRegistrationAndAuthenticatedEvents(t *testing.T) {
t.Fatalf("event status = %d, body = %s", response.Code, response.Body.String())
}
var lastEvent, lastResult string
var hostname, lastEvent, lastResult string
var eventCount int
if err := server.db.QueryRow(
`SELECT last_event, last_result FROM fleet_hosts WHERE installation_id = ?`,
`SELECT hostname, last_event, last_result FROM fleet_hosts WHERE installation_id = ?`,
testInstallationID,
).Scan(&lastEvent, &lastResult); err != nil {
).Scan(&hostname, &lastEvent, &lastResult); err != nil {
t.Fatal(err)
}
if hostname != "pve01-renamed" {
t.Fatalf("hostname = %q, want updated hostname", hostname)
}
if lastEvent != "run_completed" || lastResult != "success" {
t.Fatalf("unexpected host state: event=%q result=%q", lastEvent, lastResult)
}
@@ -158,7 +168,21 @@ func TestFleetRequestRejectsUnexpectedSensitiveFields(t *testing.T) {
"schema_version": 1,
"installation_id": "` + testInstallationID + `",
"credential": "` + testFleetCredential + `",
"hostname": "customer-pve01"
"machine_id": "customer-machine-id"
}`
response := fleetRequestRecorder(t, server.handleFleetRegister, registration, "")
if response.Code != http.StatusBadRequest {
t.Fatalf("registration status = %d, want %d", response.Code, http.StatusBadRequest)
}
}
func TestFleetRequestRejectsInvalidHostname(t *testing.T) {
server := newFleetTestServer(t)
registration := `{
"schema_version": 1,
"installation_id": "` + testInstallationID + `",
"credential": "` + testFleetCredential + `",
"hostname": "customer name with spaces"
}`
response := fleetRequestRecorder(t, server.handleFleetRegister, registration, "")
if response.Code != http.StatusBadRequest {