exporter_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package exporter
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/exporter/pathing"
  6. "github.com/opencost/opencost/core/pkg/exporter/validator"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/pipelines"
  9. "github.com/opencost/opencost/core/pkg/storage"
  10. "github.com/opencost/opencost/core/pkg/util/json"
  11. )
  12. const (
  13. TestClusterId = "test-cluster"
  14. TestEventName = "test-event-path"
  15. )
  16. type TestData struct {
  17. Message string `json:"message"`
  18. }
  19. func TestStorageExporters(t *testing.T) {
  20. t.Run("test event storage exporter", func(t *testing.T) {
  21. store := storage.NewMemoryStorage()
  22. p, err := pathing.NewEventStoragePathFormatter("root", TestClusterId, TestEventName)
  23. if err != nil {
  24. t.Fatalf("failed to create path formatter: %v", err)
  25. }
  26. encoder := NewJSONEncoder[TestData]()
  27. export := NewEventStorageExporter(p, encoder, store)
  28. ts := time.Now().UTC().Truncate(time.Minute)
  29. export.Export(ts, &TestData{
  30. Message: "TestMessage-1",
  31. })
  32. expectedPath := p.ToFullPath("", ts, "json")
  33. t.Logf("expected path: %s", expectedPath)
  34. data, err := store.Read(expectedPath)
  35. if err != nil {
  36. t.Fatalf("failed to read data from store: %v", err)
  37. }
  38. if len(data) == 0 {
  39. t.Fatalf("expected data to be non-empty, got empty")
  40. }
  41. t.Logf("Data: %s", string(data))
  42. var td *TestData = new(TestData)
  43. if err := json.Unmarshal(data, td); err != nil {
  44. t.Fatalf("failed to unmarshal data: %v", err)
  45. }
  46. if td.Message != "TestMessage-1" {
  47. t.Fatalf("expected message to be 'TestMessage-1', got '%s'", td.Message)
  48. }
  49. })
  50. t.Run("test compute storage exporter", func(t *testing.T) {
  51. res := 24 * time.Hour
  52. store := storage.NewMemoryStorage()
  53. p, err := pathing.NewDefaultStoragePathFormatter(TestClusterId, pipelines.AllocationPipelineName, &res)
  54. if err != nil {
  55. t.Fatalf("failed to create path formatter: %v", err)
  56. }
  57. encoder := NewBingenEncoder[opencost.AllocationSet]()
  58. export := NewComputeStorageExporter[opencost.AllocationSet](
  59. p,
  60. encoder,
  61. store,
  62. validator.NewSetValidator[opencost.AllocationSet](24*time.Hour),
  63. )
  64. start := time.Now().UTC().Truncate(res)
  65. end := start.Add(res)
  66. toExport := opencost.GenerateMockAllocationSet(start)
  67. err = export.Export(opencost.NewClosedWindow(start, end), toExport)
  68. if err != nil {
  69. t.Fatalf("failed to export data: %v", err)
  70. }
  71. expectedPath := p.ToFullPath("", opencost.NewClosedWindow(start, end), "")
  72. data, err := store.Read(expectedPath)
  73. if err != nil {
  74. t.Fatalf("failed to read data from store: %v", err)
  75. }
  76. if len(data) == 0 {
  77. t.Fatalf("expected data to be non-empty, got empty")
  78. }
  79. var as *opencost.AllocationSet = new(opencost.AllocationSet)
  80. err = as.UnmarshalBinary(data)
  81. if err != nil {
  82. t.Fatalf("failed to unmarshal data: %v", err)
  83. }
  84. if as.IsEmpty() {
  85. t.Fatalf("expected allocation set to be non-empty, got empty")
  86. }
  87. })
  88. }