autocompletequeryservice_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package allocation
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/opencost"
  6. )
  7. func TestQueryAllocationAutocompleteFromSetRange(t *testing.T) {
  8. start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  9. as := opencost.NewAllocationSet(start, start.Add(24*time.Hour))
  10. as.Set(opencost.NewMockUnitAllocation("a1", start, 24*time.Hour, &opencost.AllocationProperties{
  11. Cluster: "cluster-a",
  12. Namespace: "ns-a",
  13. Pod: "pod-a",
  14. Container: "container-a",
  15. ControllerKind: "deployment",
  16. Controller: "deploy-a",
  17. Node: "node-a",
  18. Labels: map[string]string{"Team": "platform", "app": "api"},
  19. NamespaceLabels: map[string]string{"owner": "sre"},
  20. }))
  21. as.Set(opencost.NewMockUnitAllocation("a2", start, 24*time.Hour, &opencost.AllocationProperties{
  22. Cluster: "cluster-b",
  23. Namespace: "ns-b",
  24. Pod: "pod-b",
  25. Container: "container-b",
  26. ControllerKind: "statefulset",
  27. Controller: "db-a",
  28. Node: "node-b",
  29. Labels: map[string]string{"Team": "data", "app": "db"},
  30. NamespaceLabels: map[string]string{"owner": "db"},
  31. }))
  32. asr := opencost.NewAllocationSetRange(as)
  33. resp, err := QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
  34. Field: "label",
  35. Limit: 10,
  36. })
  37. if err != nil {
  38. t.Fatalf("unexpected error: %v", err)
  39. }
  40. if len(resp.Data) != 2 || resp.Data[0] != "Team" || resp.Data[1] != "app" {
  41. t.Fatalf("unexpected label autocomplete response: %+v", resp.Data)
  42. }
  43. valueResp, err := QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
  44. Field: "label:team",
  45. Search: "plat",
  46. })
  47. if err != nil {
  48. t.Fatalf("unexpected error: %v", err)
  49. }
  50. if len(valueResp.Data) != 1 || valueResp.Data[0] != "platform" {
  51. t.Fatalf("unexpected label value autocomplete response: %+v", valueResp.Data)
  52. }
  53. mixedCaseResp, err := QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
  54. Field: "label:Team",
  55. })
  56. if err != nil {
  57. t.Fatalf("unexpected error: %v", err)
  58. }
  59. if len(mixedCaseResp.Data) != 2 || mixedCaseResp.Data[0] != "data" || mixedCaseResp.Data[1] != "platform" {
  60. t.Fatalf("expected label:team to match Team label values, got %+v", mixedCaseResp.Data)
  61. }
  62. _, err = QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
  63. Field: "account",
  64. })
  65. if err == nil {
  66. t.Fatal("expected error for unsupported account field")
  67. }
  68. if !IsAutocompleteBadRequest(err) {
  69. t.Fatalf("expected bad request error, got: %v", err)
  70. }
  71. _, err = QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
  72. Field: "namespace",
  73. Limit: MaxAutocompleteResultLimit + 1,
  74. })
  75. if err == nil {
  76. t.Fatal("expected error for excessive limit")
  77. }
  78. if !IsAutocompleteBadRequest(err) {
  79. t.Fatalf("expected bad request error, got: %v", err)
  80. }
  81. }