@@ -3,7 +3,7 @@ import { expect } from 'chai';
3
3
import nock = require( 'nock' ) ;
4
4
import { KubeConfig } from './config' ;
5
5
import { V1Status , HttpError } from './gen/api' ;
6
- import { Metrics , NodeMetricsList , PodMetricsList , SinglePodMetrics } from './metrics' ;
6
+ import { Metrics , NodeMetricsList , PodMetricsList , SingleNodeMetrics , SinglePodMetrics } from './metrics' ;
7
7
8
8
const emptyPodMetrics : PodMetricsList = {
9
9
kind : 'PodMetricsList' ,
@@ -47,6 +47,28 @@ const mockedPodMetrics: PodMetricsList = {
47
47
] ,
48
48
} ;
49
49
50
+ const mockedPodMetricsWithLabels : PodMetricsList = {
51
+ kind : 'PodMetricsList' ,
52
+ apiVersion : 'metrics.k8s.io/v1beta1' ,
53
+ metadata : { selfLink : '/apis/metrics.k8s.io/v1beta1/pods/' } ,
54
+ items : [
55
+ {
56
+ metadata : {
57
+ name : 'dice-roller-7c76898b4d-shm9p' ,
58
+ namespace : 'default' ,
59
+ selfLink : '/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/dice-roller-7c76898b4d-shm9p' ,
60
+ creationTimestamp : '2021-09-26T11:57:27Z' ,
61
+ labels : {
62
+ label : 'aLabel' ,
63
+ } ,
64
+ } ,
65
+ timestamp : '2021-09-26T11:57:21Z' ,
66
+ window : '30s' ,
67
+ containers : [ { name : 'nginx' , usage : { cpu : '10' , memory : '3912Ki' } } ] ,
68
+ } ,
69
+ ] ,
70
+ } ;
71
+
50
72
const emptyNodeMetrics : NodeMetricsList = {
51
73
kind : 'NodeMetricsList' ,
52
74
apiVersion : 'metrics.k8s.io/v1beta1' ,
@@ -64,6 +86,9 @@ const mockedNodeMetrics: NodeMetricsList = {
64
86
name : 'a-node' ,
65
87
selfLink : '/apis/metrics.k8s.io/v1beta1/nodes/a-node' ,
66
88
creationTimestamp : '2021-09-26T16:01:53Z' ,
89
+ labels : {
90
+ label : 'aLabel' ,
91
+ } ,
67
92
} ,
68
93
timestamp : '2021-09-26T16:01:11Z' ,
69
94
window : '30s' ,
@@ -72,6 +97,21 @@ const mockedNodeMetrics: NodeMetricsList = {
72
97
] ,
73
98
} ;
74
99
100
+ const mockedSingleNodeMetrics : SingleNodeMetrics = {
101
+ kind : 'NodeMetrics' ,
102
+ apiVersion : 'metrics.k8s.io/v1beta1' ,
103
+ metadata : {
104
+ name : 'a-node' ,
105
+ creationTimestamp : '2021-09-26T16:01:53Z' ,
106
+ labels : {
107
+ label : 'aLabel' ,
108
+ } ,
109
+ } ,
110
+ timestamp : '2021-09-26T16:01:11Z' ,
111
+ window : '30s' ,
112
+ usage : { cpu : '214650124n' , memory : '801480Ki' } ,
113
+ } ;
114
+
75
115
const mockedSinglePodMetrics : SinglePodMetrics = {
76
116
kind : 'PodMetrics' ,
77
117
apiVersion : 'metrics.k8s.io/v1beta1' ,
@@ -167,6 +207,54 @@ describe('Metrics', () => {
167
207
168
208
s . done ( ) ;
169
209
} ) ;
210
+
211
+ it ( 'should return specified cluster scope pods metric list if given options' , async ( ) => {
212
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
213
+ const options = {
214
+ labelSelector : 'label=aLabel' ,
215
+ } ;
216
+ const s = scope
217
+ . get ( '/apis/metrics.k8s.io/v1beta1/pods' )
218
+ . query ( options )
219
+ . reply ( 200 , mockedPodMetricsWithLabels ) ;
220
+
221
+ const response = await metricsClient . getPodMetrics ( options ) ;
222
+ expect ( response ) . to . deep . equal ( mockedPodMetricsWithLabels ) ;
223
+ s . done ( ) ;
224
+ } ) ;
225
+
226
+ it ( 'should return specified namespace scope pods metric list if given options' , async ( ) => {
227
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
228
+ const options = {
229
+ labelSelector : 'label=aLabel' ,
230
+ } ;
231
+ const s = scope
232
+ . get ( `/apis/metrics.k8s.io/v1beta1/namespaces/${ TEST_NAMESPACE } /pods` )
233
+ . query ( options )
234
+ . reply ( 200 , mockedPodMetricsWithLabels ) ;
235
+
236
+ const response = await metricsClient . getPodMetrics ( TEST_NAMESPACE , options ) ;
237
+ expect ( response ) . to . deep . equal ( mockedPodMetricsWithLabels ) ;
238
+ s . done ( ) ;
239
+ } ) ;
240
+
241
+ it ( 'should return specified single pod metrics if given namespace and pod name and options' , async ( ) => {
242
+ const podName = 'pod-name' ;
243
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
244
+ const options = {
245
+ labelSelector : 'label=aLabel' ,
246
+ } ;
247
+ const s = scope
248
+ . get ( `/apis/metrics.k8s.io/v1beta1/namespaces/${ TEST_NAMESPACE } /pods/${ podName } ` )
249
+ . query ( options )
250
+ . reply ( 200 , mockedSinglePodMetrics ) ;
251
+
252
+ const response = await metricsClient . getPodMetrics ( TEST_NAMESPACE , podName , options ) ;
253
+ expect ( response ) . to . deep . equal ( mockedSinglePodMetrics ) ;
254
+
255
+ s . done ( ) ;
256
+ } ) ;
257
+
170
258
it ( 'should when connection refused' , async ( ) => {
171
259
const kc = new KubeConfig ( ) ;
172
260
kc . loadFromOptions ( {
@@ -261,6 +349,54 @@ describe('Metrics', () => {
261
349
262
350
s . done ( ) ;
263
351
} ) ;
352
+
353
+ it ( 'should return single node metrics if given node name' , async ( ) => {
354
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
355
+ const nodeName = 'a-node' ;
356
+
357
+ const s = scope
358
+ . get ( `/apis/metrics.k8s.io/v1beta1/nodes/${ nodeName } ` )
359
+ . reply ( 200 , mockedSingleNodeMetrics ) ;
360
+
361
+ const response = await metricsClient . getNodeMetrics ( nodeName ) ;
362
+ expect ( response ) . to . deep . equal ( mockedSingleNodeMetrics ) ;
363
+
364
+ s . done ( ) ;
365
+ } ) ;
366
+
367
+ it ( 'should return specified nodes metrics list if given options' , async ( ) => {
368
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
369
+ const options = {
370
+ labelSelector : 'label=aLabel' ,
371
+ } ;
372
+ const s = scope
373
+ . get ( '/apis/metrics.k8s.io/v1beta1/nodes' )
374
+ . query ( options )
375
+ . reply ( 200 , mockedNodeMetrics ) ;
376
+
377
+ const response = await metricsClient . getNodeMetrics ( options ) ;
378
+ expect ( response ) . to . deep . equal ( mockedNodeMetrics ) ;
379
+
380
+ s . done ( ) ;
381
+ } ) ;
382
+
383
+ it ( 'should return specified single node metrics if given node name and options' , async ( ) => {
384
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
385
+ const nodeName = 'a-node' ;
386
+ const options = {
387
+ labelSelector : 'label=aLabel' ,
388
+ } ;
389
+ const s = scope
390
+ . get ( `/apis/metrics.k8s.io/v1beta1/nodes/${ nodeName } ` )
391
+ . query ( options )
392
+ . reply ( 200 , mockedSingleNodeMetrics ) ;
393
+
394
+ const response = await metricsClient . getNodeMetrics ( nodeName , options ) ;
395
+ expect ( response ) . to . deep . equal ( mockedSingleNodeMetrics ) ;
396
+
397
+ s . done ( ) ;
398
+ } ) ;
399
+
264
400
it ( 'should resolve to error when 500' , async ( ) => {
265
401
const response : V1Status = {
266
402
code : 12345 ,
0 commit comments