Harden host deletion and test stability
This commit is contained in:
@@ -13,12 +13,12 @@ import (
|
||||
func openTestDB(t *testing.T) *DB {
|
||||
t.Helper()
|
||||
dbPath := filepath.Join(t.TempDir(), "test.db")
|
||||
dsn := "file:" + filepath.ToSlash(dbPath) + "?_pragma=foreign_keys(0)"
|
||||
dsn := "file:" + filepath.ToSlash(dbPath) + "?_busy_timeout=5000&_pragma=foreign_keys(0)"
|
||||
store, err := Open(context.Background(), dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { store.Close() })
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
return store
|
||||
}
|
||||
|
||||
@@ -26,12 +26,12 @@ func openTestDB(t *testing.T) *DB {
|
||||
func openTestDBWithFK(t *testing.T) *DB {
|
||||
t.Helper()
|
||||
dbPath := filepath.Join(t.TempDir(), "test-fk.db")
|
||||
dsn := "file:" + filepath.ToSlash(dbPath)
|
||||
dsn := "file:" + filepath.ToSlash(dbPath) + "?_busy_timeout=5000"
|
||||
store, err := Open(context.Background(), dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { store.Close() })
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
return store
|
||||
}
|
||||
|
||||
@@ -279,6 +279,47 @@ func TestHostsRepoDeleteByHostID(t *testing.T) {
|
||||
t.Fatalf("ListAll() after delete len = %d, want 0", len(hosts))
|
||||
}
|
||||
}
|
||||
func TestHostsRepoDeleteByHostIDBlocksWhenRuntimeStateExists(t *testing.T) {
|
||||
store := openTestDBWithFK(t)
|
||||
batchID := createTestBatch(t, store)
|
||||
|
||||
hostRowID := mustHostRowIDForBatch(t, store, batchID)
|
||||
host, err := store.Hosts().GetByID(context.Background(), hostRowID)
|
||||
if err != nil {
|
||||
t.Fatalf("Hosts().GetByID() error = %v", err)
|
||||
}
|
||||
if _, err := store.ManagedResources().Create(context.Background(), ManagedResource{
|
||||
BatchID: batchID,
|
||||
HostID: host.ID,
|
||||
ResourceType: "group",
|
||||
HostResourceID: "group_1",
|
||||
ResourceName: "group",
|
||||
}); err != nil {
|
||||
t.Fatalf("ManagedResources().Create() error = %v", err)
|
||||
}
|
||||
providerID := mustProviderIDForBatch(t, store, batchID)
|
||||
if _, err := store.ReconcileRuns().Create(context.Background(), ReconcileRun{
|
||||
BatchID: batchID,
|
||||
HostID: host.ID,
|
||||
ProviderID: providerID,
|
||||
Status: "active",
|
||||
SummaryJSON: `{}`,
|
||||
}); err != nil {
|
||||
t.Fatalf("ReconcileRuns().Create() error = %v", err)
|
||||
}
|
||||
|
||||
err = store.Hosts().DeleteByHostID(context.Background(), host.HostID)
|
||||
if err == nil {
|
||||
t.Fatal("DeleteByHostID() error = nil, want blocked error")
|
||||
}
|
||||
var blocker *HostDeleteBlocker
|
||||
if !errors.As(err, &blocker) {
|
||||
t.Fatalf("DeleteByHostID() error = %T %v, want HostDeleteBlocker", err, err)
|
||||
}
|
||||
if blocker.ImportBatchCount != 1 || blocker.ManagedResourceCount != 1 || blocker.ReconcileRunCount != 1 {
|
||||
t.Fatalf("blocker = %+v, want all dependency counts = 1", blocker)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostsRepoUpdateProbeByHostID(t *testing.T) {
|
||||
store := openTestDB(t)
|
||||
@@ -361,3 +402,20 @@ func TestHostsRepoDeleteByHostIDEmptyError(t *testing.T) {
|
||||
t.Fatal("DeleteByHostID('') error = nil, want error")
|
||||
}
|
||||
}
|
||||
func mustHostRowIDForBatch(t *testing.T, store *DB, batchID int64) int64 {
|
||||
t.Helper()
|
||||
var hostID int64
|
||||
if err := store.SQLDB().QueryRow(`SELECT host_id FROM import_batches WHERE id = ?`, batchID).Scan(&hostID); err != nil {
|
||||
t.Fatalf("query host_id for batch %d error = %v", batchID, err)
|
||||
}
|
||||
return hostID
|
||||
}
|
||||
|
||||
func mustProviderIDForBatch(t *testing.T, store *DB, batchID int64) int64 {
|
||||
t.Helper()
|
||||
var providerID int64
|
||||
if err := store.SQLDB().QueryRow(`SELECT provider_id FROM import_batches WHERE id = ?`, batchID).Scan(&providerID); err != nil {
|
||||
t.Fatalf("query provider_id for batch %d error = %v", batchID, err)
|
||||
}
|
||||
return providerID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user