@@ -2,17 +2,18 @@ package backend
2
2
3
3
import (
4
4
"errors"
5
- "sync"
6
5
"testing"
7
- "time"
8
6
9
7
"github.com/google/go-cmp/cmp"
10
8
"github.com/google/go-cmp/cmp/cmpopts"
9
+ "inference.networking.x-k8s.io/gateway-api-inference-extension/api/v1alpha1"
10
+ testingutil "inference.networking.x-k8s.io/gateway-api-inference-extension/pkg/ext-proc/util/testing"
11
+ corev1 "k8s.io/api/core/v1"
11
12
)
12
13
13
14
var (
14
15
pod1 = & PodMetrics {
15
- Pod : Pod {Name : "pod1" },
16
+ Pod : Pod {Name : "pod1" , Address : "address1:9009" },
16
17
Metrics : Metrics {
17
18
WaitingQueueSize : 0 ,
18
19
KVCacheUsagePercent : 0.2 ,
24
25
},
25
26
}
26
27
pod2 = & PodMetrics {
27
- Pod : Pod {Name : "pod2" },
28
+ Pod : Pod {Name : "pod2" , Address : "address2:9009" },
28
29
Metrics : Metrics {
29
30
WaitingQueueSize : 1 ,
30
31
KVCacheUsagePercent : 0.2 ,
@@ -38,44 +39,67 @@ var (
38
39
)
39
40
40
41
func TestProvider (t * testing.T ) {
42
+ allPodsLister := & testingutil.FakePodLister {
43
+ PodsList : []* corev1.Pod {
44
+ testingutil .MakePod (pod1 .Pod .Name ).SetReady ().SetPodIP ("address1" ).Obj (),
45
+ testingutil .MakePod (pod2 .Pod .Name ).SetReady ().SetPodIP ("address2" ).Obj (),
46
+ },
47
+ }
48
+ allPodsMetricsClient := & FakePodMetricsClient {
49
+ Res : map [string ]* PodMetrics {
50
+ pod1 .Pod .Name : pod1 ,
51
+ pod2 .Pod .Name : pod2 ,
52
+ },
53
+ }
54
+
41
55
tests := []struct {
42
- name string
43
- pmc PodMetricsClient
44
- datastore * K8sDatastore
45
- initErr bool
46
- want []* PodMetrics
56
+ name string
57
+ initPodMetrics []* PodMetrics
58
+ lister * testingutil.FakePodLister
59
+ pmc PodMetricsClient
60
+ step func (* Provider )
61
+ want []* PodMetrics
47
62
}{
48
63
{
49
- name : "Init success" ,
50
- datastore : & K8sDatastore {
51
- pods : populateMap (pod1 .Pod , pod2 .Pod ),
64
+ name : "Init without refreshing pods" ,
65
+ initPodMetrics : []* PodMetrics {pod1 , pod2 },
66
+ lister : allPodsLister ,
67
+ pmc : allPodsMetricsClient ,
68
+ step : func (p * Provider ) {
69
+ _ = p .refreshMetricsOnce ()
52
70
},
53
- pmc : & FakePodMetricsClient {
54
- Res : map [Pod ]* PodMetrics {
55
- pod1 .Pod : pod1 ,
56
- pod2 .Pod : pod2 ,
57
- },
71
+ want : []* PodMetrics {pod1 , pod2 },
72
+ },
73
+ {
74
+ name : "Fetching all success" ,
75
+ lister : allPodsLister ,
76
+ pmc : allPodsMetricsClient ,
77
+ step : func (p * Provider ) {
78
+ p .refreshPodsOnce ()
79
+ _ = p .refreshMetricsOnce ()
58
80
},
59
81
want : []* PodMetrics {pod1 , pod2 },
60
82
},
61
83
{
62
- name : "Fetch metrics error" ,
84
+ name : "Fetch metrics error" ,
85
+ lister : allPodsLister ,
63
86
pmc : & FakePodMetricsClient {
64
- Err : map [Pod ]error {
65
- pod2 .Pod : errors .New ("injected error" ),
87
+ Err : map [string ]error {
88
+ pod2 .Pod . Name : errors .New ("injected error" ),
66
89
},
67
- Res : map [Pod ]* PodMetrics {
68
- pod1 .Pod : pod1 ,
90
+ Res : map [string ]* PodMetrics {
91
+ pod1 .Pod . Name : pod1 ,
69
92
},
70
93
},
71
- datastore : & K8sDatastore {
72
- pods : populateMap (pod1 .Pod , pod2 .Pod ),
94
+ step : func (p * Provider ) {
95
+ p .refreshPodsOnce ()
96
+ _ = p .refreshMetricsOnce ()
73
97
},
74
98
want : []* PodMetrics {
75
99
pod1 ,
76
100
// Failed to fetch pod2 metrics so it remains the default values.
77
101
{
78
- Pod : Pod { Name : " pod2" } ,
102
+ Pod : pod2 . Pod ,
79
103
Metrics : Metrics {
80
104
WaitingQueueSize : 0 ,
81
105
KVCacheUsagePercent : 0 ,
@@ -85,30 +109,73 @@ func TestProvider(t *testing.T) {
85
109
},
86
110
},
87
111
},
112
+ {
113
+ name : "A new pod added" ,
114
+ initPodMetrics : []* PodMetrics {pod2 },
115
+ lister : allPodsLister ,
116
+ pmc : allPodsMetricsClient ,
117
+ step : func (p * Provider ) {
118
+ p .refreshPodsOnce ()
119
+ _ = p .refreshMetricsOnce ()
120
+ },
121
+ want : []* PodMetrics {pod1 , pod2 },
122
+ },
123
+ {
124
+ name : "A pod removed" ,
125
+ initPodMetrics : []* PodMetrics {pod1 , pod2 },
126
+ lister : & testingutil.FakePodLister {
127
+ PodsList : []* corev1.Pod {
128
+ testingutil .MakePod (pod2 .Pod .Name ).SetReady ().SetPodIP ("address2" ).Obj (),
129
+ },
130
+ },
131
+ pmc : allPodsMetricsClient ,
132
+ step : func (p * Provider ) {
133
+ p .refreshPodsOnce ()
134
+ _ = p .refreshMetricsOnce ()
135
+ },
136
+ want : []* PodMetrics {pod2 },
137
+ },
138
+ {
139
+ name : "A pod removed, another added" ,
140
+ initPodMetrics : []* PodMetrics {pod1 },
141
+ lister : & testingutil.FakePodLister {
142
+ PodsList : []* corev1.Pod {
143
+ testingutil .MakePod (pod1 .Pod .Name ).SetReady ().SetPodIP ("address1" ).Obj (),
144
+ },
145
+ },
146
+ pmc : allPodsMetricsClient ,
147
+ step : func (p * Provider ) {
148
+ p .refreshPodsOnce ()
149
+ _ = p .refreshMetricsOnce ()
150
+ },
151
+ want : []* PodMetrics {pod1 },
152
+ },
88
153
}
89
154
90
155
for _ , test := range tests {
91
156
t .Run (test .name , func (t * testing.T ) {
92
- p := NewProvider (test .pmc , test .datastore )
93
- err := p .Init (time .Millisecond , time .Millisecond )
94
- if test .initErr != (err != nil ) {
95
- t .Fatalf ("Unexpected error, got: %v, want: %v" , err , test .initErr )
157
+ datastore := NewK8sDataStore (WithPodListerFactory (
158
+ func (pool * v1alpha1.InferencePool ) * PodLister {
159
+ return & PodLister {
160
+ Lister : test .lister ,
161
+ }
162
+ }))
163
+ datastore .setInferencePool (& v1alpha1.InferencePool {
164
+ Spec : v1alpha1.InferencePoolSpec {TargetPortNumber : 9009 },
165
+ })
166
+ p := NewProvider (test .pmc , datastore )
167
+ for _ , m := range test .initPodMetrics {
168
+ p .UpdatePodMetrics (m .Pod , m )
96
169
}
170
+ test .step (p )
97
171
metrics := p .AllPodMetrics ()
98
172
lessFunc := func (a , b * PodMetrics ) bool {
99
173
return a .String () < b .String ()
100
174
}
101
- if diff := cmp .Diff (test .want , metrics , cmpopts .SortSlices (lessFunc )); diff != "" {
175
+ if diff := cmp .Diff (test .want , metrics , cmpopts .SortSlices (lessFunc ),
176
+ cmpopts .IgnoreFields (PodMetrics {}, "revision" )); diff != "" {
102
177
t .Errorf ("Unexpected output (-want +got): %v" , diff )
103
178
}
104
179
})
105
180
}
106
181
}
107
-
108
- func populateMap (pods ... Pod ) * sync.Map {
109
- newMap := & sync.Map {}
110
- for _ , pod := range pods {
111
- newMap .Store (pod , true )
112
- }
113
- return newMap
114
- }
0 commit comments