datasource_wal_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package collector
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/source"
  7. )
  8. var _ source.WALStatusProvider = (*collectorDataSource)(nil)
  9. func TestWALDiagnosticDetails(t *testing.T) {
  10. healthy := source.WALStatus{Enabled: true, LastExportSuccess: time.Now(), RestoreCompleted: true, RestoreObjectsSeen: 3, RestoreObjectsApplied: 3}
  11. details, err := walDiagnosticDetails(healthy)
  12. if err != nil {
  13. t.Fatalf("expected healthy wal to pass, got %s", err)
  14. }
  15. if details["restoreObjectsApplied"] != 3 {
  16. t.Errorf("unexpected details: %v", details)
  17. }
  18. failing := healthy
  19. failing.ConsecutiveExportFailures = 4
  20. failing.LastExportError = "503"
  21. failing.RestoreErrors = 1
  22. failing.RestoreListError = "denied"
  23. _, err = walDiagnosticDetails(failing)
  24. if err == nil {
  25. t.Fatalf("expected failing wal to report an error")
  26. }
  27. for _, want := range []string{"4 consecutive write failures", "could not list", "failed to read 1 of 3"} {
  28. if !strings.Contains(err.Error(), want) {
  29. t.Errorf("expected %q in %q", want, err)
  30. }
  31. }
  32. }
  33. func TestCollectorDataSource_WALStatusWithoutWAL(t *testing.T) {
  34. c := &collectorDataSource{}
  35. if c.WALStatus().Enabled {
  36. t.Errorf("expected disabled status without a wal")
  37. }
  38. }