logging_storage_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package logging_test
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. "github.com/kopia/kopia/internal/blobtesting"
  9. "github.com/kopia/kopia/internal/testlogging"
  10. "github.com/kopia/kopia/repo/blob"
  11. "github.com/kopia/kopia/repo/blob/logging"
  12. )
  13. func TestLoggingStorage(t *testing.T) {
  14. outputCount := new(int32)
  15. myPrefix := "myprefix"
  16. myOutput := func(msg string, args ...any) {
  17. msg = fmt.Sprintf(msg, args...)
  18. if !strings.HasPrefix(msg, myPrefix) {
  19. t.Errorf("unexpected prefix %v", msg)
  20. }
  21. atomic.AddInt32(outputCount, 1)
  22. }
  23. data := blobtesting.DataMap{}
  24. kt := map[blob.ID]time.Time{}
  25. underlying := blobtesting.NewMapStorage(data, kt, nil)
  26. st := logging.NewWrapper(underlying, testlogging.Printf(myOutput, ""), myPrefix)
  27. if st == nil {
  28. t.Fatalf("unexpected result: %v", st)
  29. }
  30. ctx := testlogging.Context(t)
  31. blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
  32. if err := st.Close(ctx); err != nil {
  33. t.Fatalf("err: %v", err)
  34. }
  35. if *outputCount == 0 {
  36. t.Errorf("did not write any output!")
  37. }
  38. if got, want := st.ConnectionInfo().Type, underlying.ConnectionInfo().Type; got != want {
  39. t.Errorf("unexpected connection info %v, want %v", got, want)
  40. }
  41. }