autocompletequeryservice_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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] != "app" || resp.Data[1] != "team" {
  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. }