package server import ( "context" "errors" "strings" "testing" "time" "scm.bstein.dev/bstein/soteria/internal/config" "scm.bstein.dev/bstein/soteria/internal/k8s" ) func TestWaitForLonghornBackupCoversLookupErrorAndTimeout(t *testing.T) { originalTimeout := longhornBackupWaitTimeout originalPoll := longhornBackupPollInterval longhornBackupWaitTimeout = 20 * time.Millisecond longhornBackupPollInterval = 5 * time.Millisecond defer func() { longhornBackupWaitTimeout = originalTimeout longhornBackupPollInterval = originalPoll }() srv := newBackupTestServer( &config.Config{AuthRequired: false, BackupDriver: "longhorn"}, &backupTestKubeClient{restoreTestKubeClient: &restoreTestKubeClient{fakeKubeClient: &fakeKubeClient{ longhornBackupLookupErr: errors.New("lookup exploded"), }}}, &backupTestLonghornClient{restoreTestLonghornClient: &restoreTestLonghornClient{fakeLonghornClient: &fakeLonghornClient{}}}, ) if _, result, err := srv.waitForLonghornBackup(context.Background(), "snap-a"); result != "backend_error" || err == nil || !strings.Contains(err.Error(), "lookup exploded") { t.Fatalf("expected lookup backend error, result=%q err=%v", result, err) } srv.client = &backupTestKubeClient{restoreTestKubeClient: &restoreTestKubeClient{fakeKubeClient: &fakeKubeClient{ longhornBackups: []k8s.LonghornBackupSummary{{ Name: "backup-running", SnapshotName: "snap-a", State: "InProgress", Progress: 42, }}, }}} if _, result, err := srv.waitForLonghornBackup(context.Background(), "snap-a"); result != "in_progress" || err == nil || !strings.Contains(err.Error(), "backup-running") { t.Fatalf("expected in-progress timeout with backup name, result=%q err=%v", result, err) } srv.client = &backupTestKubeClient{restoreTestKubeClient: &restoreTestKubeClient{fakeKubeClient: &fakeKubeClient{ longhornBackups: []k8s.LonghornBackupSummary{}, }}} if _, result, err := srv.waitForLonghornBackup(context.Background(), "snap-missing"); result != "in_progress" || err == nil || !strings.Contains(err.Error(), "did not appear") { t.Fatalf("expected missing backup timeout, result=%q err=%v", result, err) } }