| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package allocation
- import (
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/opencost"
- )
- func TestQueryAllocationAutocompleteFromSetRange(t *testing.T) {
- start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
- as := opencost.NewAllocationSet(start, start.Add(24*time.Hour))
- as.Set(opencost.NewMockUnitAllocation("a1", start, 24*time.Hour, &opencost.AllocationProperties{
- Cluster: "cluster-a",
- Namespace: "ns-a",
- Pod: "pod-a",
- Container: "container-a",
- ControllerKind: "deployment",
- Controller: "deploy-a",
- Node: "node-a",
- Labels: map[string]string{"team": "platform", "app": "api"},
- NamespaceLabels: map[string]string{"owner": "sre"},
- }))
- as.Set(opencost.NewMockUnitAllocation("a2", start, 24*time.Hour, &opencost.AllocationProperties{
- Cluster: "cluster-b",
- Namespace: "ns-b",
- Pod: "pod-b",
- Container: "container-b",
- ControllerKind: "statefulset",
- Controller: "db-a",
- Node: "node-b",
- Labels: map[string]string{"team": "data", "app": "db"},
- NamespaceLabels: map[string]string{"owner": "db"},
- }))
- asr := opencost.NewAllocationSetRange(as)
- resp, err := QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
- Field: "label",
- Limit: 10,
- })
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if len(resp.Data) != 2 || resp.Data[0] != "app" || resp.Data[1] != "team" {
- t.Fatalf("unexpected label autocomplete response: %+v", resp.Data)
- }
- valueResp, err := QueryAllocationAutocompleteFromSetRange(asr, AllocationAutocompleteRequest{
- Field: "label:team",
- Search: "plat",
- })
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- if len(valueResp.Data) != 1 || valueResp.Data[0] != "platform" {
- t.Fatalf("unexpected label value autocomplete response: %+v", valueResp.Data)
- }
- }
|