allocation_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. {"department", "department", false},
  22. {"environment", "environment", false},
  23. {"owner", "owner", false},
  24. {"product", "product", false},
  25. {"team", "team", false},
  26. {"", "", true},
  27. {"bad", "", true},
  28. }
  29. for _, tt := range tests {
  30. got, err := ValidateField(tt.in)
  31. if tt.err {
  32. if err == nil {
  33. t.Fatalf("ValidateField(%q) expected error", tt.in)
  34. }
  35. continue
  36. }
  37. if err != nil || got != tt.want {
  38. t.Fatalf("ValidateField(%q) = %q, %v", tt.in, got, err)
  39. }
  40. }
  41. }
  42. func TestNormalizeRequest(t *testing.T) {
  43. start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  44. req := &autocomplete.Request{
  45. Field: "cluster",
  46. Search: " x ",
  47. Limit: 0,
  48. Window: opencost.NewClosedWindow(start, start.Add(24*time.Hour)),
  49. }
  50. field, err := autocomplete.NormalizeRequest(req, ValidateField, autocomplete.NormalizeOptions{EnsureLabelConfig: true})
  51. if err != nil {
  52. t.Fatalf("unexpected error: %v", err)
  53. }
  54. if field != "cluster" || req.Search != "x" || req.LabelConfig == nil {
  55. t.Fatalf("unexpected normalized request: %+v", req)
  56. }
  57. openReq := &autocomplete.Request{Field: "cluster", Window: opencost.NewWindow(&start, nil)}
  58. _, err = autocomplete.NormalizeRequest(openReq, ValidateField, autocomplete.NormalizeOptions{EnsureLabelConfig: true})
  59. if err == nil || !errors.Is(err, autocomplete.ErrBadRequest) {
  60. t.Fatalf("expected open window error, got %v", err)
  61. }
  62. }
  63. func TestParseRequest(t *testing.T) {
  64. windowStr := "2023-01-01T00:00:00Z,2023-01-02T00:00:00Z"
  65. qp := httputil.NewQueryParams(map[string][]string{
  66. "window": {windowStr},
  67. "field": {"account"},
  68. "search": {" ns "},
  69. })
  70. got, err := ParseRequest(qp, autocomplete.ParseOptions{})
  71. if err != nil {
  72. t.Fatalf("ParseRequest() error = %v", err)
  73. }
  74. if got.Field != "account" || got.Search != "ns" {
  75. t.Fatalf("unexpected request: %+v", got)
  76. }
  77. }
  78. func TestRouteField(t *testing.T) {
  79. tests := []struct {
  80. field string
  81. route Route
  82. key string
  83. }{
  84. {"namespacelabel:Team", RouteNamespaceLabelValue, "Team"},
  85. {"label", RouteLabelKeys, ""},
  86. {"label:App", RouteLabelValue, "App"},
  87. {"namespacelabel", RouteNamespaceLabelKeys, ""},
  88. {"cluster", RouteDefault, ""},
  89. {"department", RouteAlias, ""},
  90. {"environment", RouteAlias, ""},
  91. {"owner", RouteAlias, ""},
  92. {"product", RouteAlias, ""},
  93. {"team", RouteAlias, ""},
  94. }
  95. for _, tt := range tests {
  96. route, key, err := RouteField(tt.field)
  97. if err != nil || route != tt.route || key != tt.key {
  98. t.Fatalf("RouteField(%q) = %v, %q, %v; want %v, %q", tt.field, route, key, err, tt.route, tt.key)
  99. }
  100. }
  101. }
  102. func TestResolveAliasLabelKey(t *testing.T) {
  103. tests := []struct {
  104. field string
  105. labelConfig *opencost.LabelConfig
  106. expected string
  107. }{
  108. {"department", nil, "department"},
  109. {"environment", nil, "env"},
  110. {"owner", nil, "owner"},
  111. {"product", nil, "app"},
  112. {"team", nil, "team"},
  113. {"department", &opencost.LabelConfig{DepartmentLabel: "dept"}, "dept"},
  114. {"does-not-exist", &opencost.LabelConfig{DepartmentLabel: "dept"}, ""},
  115. {"owner", &opencost.LabelConfig{OwnerLabel: "alpha.test.io/owner-name"}, "alpha.test.io/owner-name"},
  116. }
  117. for _, tt := range tests {
  118. actual, _ := ResolveAliasLabelKey(tt.field, tt.labelConfig)
  119. if actual != tt.expected {
  120. t.Fatalf("TestResolveAliasLabelKey(%q, %v) = %q; want %q", tt.field, tt.labelConfig, actual, tt.expected)
  121. }
  122. }
  123. }