forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_test.go
113 lines (107 loc) · 2.29 KB
/
provider_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package backend
import (
"errors"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
var (
pod1 = &PodMetrics{
Pod: Pod{Name: "pod1"},
Metrics: Metrics{
WaitingQueueSize: 0,
KVCacheUsagePercent: 0.2,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo": 1,
"bar": 1,
},
},
}
pod2 = &PodMetrics{
Pod: Pod{Name: "pod2"},
Metrics: Metrics{
WaitingQueueSize: 1,
KVCacheUsagePercent: 0.2,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo1": 1,
"bar1": 1,
},
},
}
)
func TestProvider(t *testing.T) {
tests := []struct {
name string
pmc PodMetricsClient
datastore *K8sDatastore
initErr bool
want []*PodMetrics
}{
{
name: "Init success",
datastore: &K8sDatastore{
Pods: populateMap(pod1.Pod, pod2.Pod),
},
pmc: &FakePodMetricsClient{
Res: map[Pod]*PodMetrics{
pod1.Pod: pod1,
pod2.Pod: pod2,
},
},
want: []*PodMetrics{pod1, pod2},
},
{
name: "Fetch metrics error",
pmc: &FakePodMetricsClient{
Err: map[Pod]error{
pod2.Pod: errors.New("injected error"),
},
Res: map[Pod]*PodMetrics{
pod1.Pod: pod1,
},
},
datastore: &K8sDatastore{
Pods: populateMap(pod1.Pod, pod2.Pod),
},
want: []*PodMetrics{
pod1,
// Failed to fetch pod2 metrics so it remains the default values.
{
Pod: Pod{Name: "pod2"},
Metrics: Metrics{
WaitingQueueSize: 0,
KVCacheUsagePercent: 0,
MaxActiveModels: 0,
ActiveModels: map[string]int{},
},
}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p := NewProvider(test.pmc, test.datastore)
err := p.Init(time.Millisecond, time.Millisecond)
if test.initErr != (err != nil) {
t.Fatalf("Unexpected error, got: %v, want: %v", err, test.initErr)
}
metrics := p.AllPodMetrics()
lessFunc := func(a, b *PodMetrics) bool {
return a.String() < b.String()
}
if diff := cmp.Diff(test.want, metrics, cmpopts.SortSlices(lessFunc)); diff != "" {
t.Errorf("Unexpected output (-want +got): %v", diff)
}
})
}
}
func populateMap(pods ...Pod) *sync.Map {
newMap := &sync.Map{}
for _, pod := range pods {
newMap.Store(pod, true)
}
return newMap
}