allocation_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package allocation
  2. import (
  3. "errors"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/autocomplete"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/util/httputil"
  9. )
  10. func TestValidateField(t *testing.T) {
  11. tests := []struct {
  12. in string
  13. want string
  14. err bool
  15. }{
  16. {"account", "account", false},
  17. {"cluster", "cluster", false},
  18. {"label", "label", false},
  19. {"label:App", "label:App", false},
  20. {"namespacelabel:Team", "namespacelabel:Team", false},
  21. {"", "", true},
  22. {"bad", "", true},
  23. }
  24. for _, tt := range tests {
  25. got, err := ValidateField(tt.in)
  26. if tt.err {
  27. if err == nil {
  28. t.Fatalf("ValidateField(%q) expected error", tt.in)
  29. }
  30. continue
  31. }
  32. if err != nil || got != tt.want {
  33. t.Fatalf("ValidateField(%q) = %q, %v", tt.in, got, err)
  34. }
  35. }
  36. }
  37. func TestNormalizeRequest(t *testing.T) {
  38. start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  39. req := &autocomplete.Request{
  40. Field: "cluster",
  41. Search: " x ",
  42. Limit: 0,
  43. Window: opencost.NewClosedWindow(start, start.Add(24*time.Hour)),
  44. }
  45. field, err := autocomplete.NormalizeRequest(req, ValidateField, autocomplete.NormalizeOptions{EnsureLabelConfig: true})
  46. if err != nil {
  47. t.Fatalf("unexpected error: %v", err)
  48. }
  49. if field != "cluster" || req.Search != "x" || req.LabelConfig == nil {
  50. t.Fatalf("unexpected normalized request: %+v", req)
  51. }
  52. openReq := &autocomplete.Request{Field: "cluster", Window: opencost.NewWindow(&start, nil)}
  53. _, err = autocomplete.NormalizeRequest(openReq, ValidateField, autocomplete.NormalizeOptions{EnsureLabelConfig: true})
  54. if err == nil || !errors.Is(err, autocomplete.ErrBadRequest) {
  55. t.Fatalf("expected open window error, got %v", err)
  56. }
  57. }
  58. func TestParseRequest(t *testing.T) {
  59. windowStr := "2023-01-01T00:00:00Z,2023-01-02T00:00:00Z"
  60. qp := httputil.NewQueryParams(map[string][]string{
  61. "window": {windowStr},
  62. "field": {"account"},
  63. "search": {" ns "},
  64. })
  65. got, err := ParseRequest(qp, autocomplete.ParseOptions{})
  66. if err != nil {
  67. t.Fatalf("ParseRequest() error = %v", err)
  68. }
  69. if got.Field != "account" || got.Search != "ns" {
  70. t.Fatalf("unexpected request: %+v", got)
  71. }
  72. }
  73. func TestRouteField(t *testing.T) {
  74. tests := []struct {
  75. field string
  76. route Route
  77. key string
  78. }{
  79. {"namespacelabel:Team", RouteNamespaceLabelValue, "Team"},
  80. {"label", RouteLabelKeys, ""},
  81. {"label:App", RouteLabelValue, "App"},
  82. {"namespacelabel", RouteNamespaceLabelKeys, ""},
  83. {"cluster", RouteDefault, ""},
  84. }
  85. for _, tt := range tests {
  86. route, key, err := RouteField(tt.field)
  87. if err != nil || route != tt.route || key != tt.key {
  88. t.Fatalf("RouteField(%q) = %v, %q, %v; want %v, %q", tt.field, route, key, err, tt.route, tt.key)
  89. }
  90. }
  91. }