@@ -3,7 +3,7 @@ import { expect } from 'chai';
3
3
import * as nock from 'nock' ;
4
4
import { KubeConfig } from './config' ;
5
5
import { V1Status , HttpError } from './gen/api' ;
6
- import { Metrics , PodMetricsList } from './metrics' ;
6
+ import { Metrics , NodeMetricsList , PodMetricsList } from './metrics' ;
7
7
8
8
const emptyPodMetrics : PodMetricsList = {
9
9
kind : 'PodMetricsList' ,
@@ -39,7 +39,35 @@ const mockedPodMetrics: PodMetricsList = {
39
39
} ,
40
40
timestamp : '2021-09-26T11:57:21Z' ,
41
41
window : '30s' ,
42
- containers : [ { name : 'nginx' , usage : { cpu : '15' , memory : '4012Ki' } } , { name : 'sidecar' , usage : { cpu : '16' , memory : '3012Ki' } } ] ,
42
+ containers : [
43
+ { name : 'nginx' , usage : { cpu : '15' , memory : '4012Ki' } } ,
44
+ { name : 'sidecar' , usage : { cpu : '16' , memory : '3012Ki' } } ,
45
+ ] ,
46
+ } ,
47
+ ] ,
48
+ } ;
49
+
50
+ const emptyNodeMetrics : NodeMetricsList = {
51
+ kind : 'NodeMetricsList' ,
52
+ apiVersion : 'metrics.k8s.io/v1beta1' ,
53
+ metadata : { selfLink : '/apis/metrics.k8s.io/v1beta1/nodes/' } ,
54
+ items : [ ] ,
55
+ } ;
56
+
57
+ const mockedNodeMetrics : NodeMetricsList = {
58
+ kind : 'NodeMetricsList' ,
59
+ apiVersion : 'metrics.k8s.io/v1beta1' ,
60
+ metadata : { selfLink : '/apis/metrics.k8s.io/v1beta1/nodes/' } ,
61
+ items : [
62
+ {
63
+ metadata : {
64
+ name : 'a-node' ,
65
+ selfLink : '/apis/metrics.k8s.io/v1beta1/nodes/a-node' ,
66
+ creationTimestamp : '2021-09-26T16:01:53Z' ,
67
+ } ,
68
+ timestamp : '2021-09-26T16:01:11Z' ,
69
+ window : '30s' ,
70
+ usage : { cpu : '214650124n' , memory : '801480Ki' } ,
43
71
} ,
44
72
] ,
45
73
} ;
@@ -53,63 +81,55 @@ const testConfigOptions: any = {
53
81
currentContext : 'currentContext' ,
54
82
} ;
55
83
56
-
57
- const systemUnderTest = ( ) :[ Metrics , nock . Scope ] => {
84
+ const systemUnderTest = ( ) : [ Metrics , nock . Scope ] => {
58
85
const kc = new KubeConfig ( ) ;
59
86
kc . loadFromOptions ( testConfigOptions ) ;
60
87
const metricsClient = new Metrics ( kc ) ;
61
88
62
- const scope = nock ( testConfigOptions . clusters [ 0 ] . server )
89
+ const scope = nock ( testConfigOptions . clusters [ 0 ] . server ) ;
63
90
64
- return [ metricsClient , scope ]
65
- }
91
+ return [ metricsClient , scope ] ;
92
+ } ;
66
93
67
94
describe ( 'Metrics' , ( ) => {
68
-
69
95
describe ( 'getPodMetrics' , ( ) => {
70
96
it ( 'should return cluster scope empty pods list' , async ( ) => {
71
- const [ metricsClient , scope ] = systemUnderTest ( )
72
- const s = scope
73
- . get ( '/apis/metrics.k8s.io/v1beta1/pods' )
74
- . reply ( 200 , emptyPodMetrics ) ;
97
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
98
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , emptyPodMetrics ) ;
75
99
76
100
const response = await metricsClient . getPodMetrics ( ) ;
77
- expect ( response . items . length ) . to . equal ( 0 ) ;
101
+ expect ( response ) . to . deep . equal ( emptyPodMetrics ) ;
78
102
s . done ( ) ;
79
103
} ) ;
80
- it ( 'should return cluster scope empty pods list when namespace is empty string' , async ( ) => {
81
- const [ metricsClient , scope ] = systemUnderTest ( )
82
- const s = scope
83
- . get ( '/apis/metrics.k8s.io/v1beta1/pods' )
84
- . reply ( 200 , emptyPodMetrics ) ;
104
+ it ( 'should return cluster scope empty pods list when namespace is empty string' , async ( ) => {
105
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
106
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , emptyPodMetrics ) ;
85
107
86
- const response = await metricsClient . getPodMetrics ( "" ) ;
87
- expect ( response . items . length ) . to . equal ( 0 ) ;
108
+ const response = await metricsClient . getPodMetrics ( '' ) ;
109
+ expect ( response ) . to . deep . equal ( emptyPodMetrics ) ;
88
110
s . done ( ) ;
89
111
} ) ;
90
112
it ( 'should return namespace scope empty pods list' , async ( ) => {
91
- const [ metricsClient , scope ] = systemUnderTest ( )
113
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
92
114
const s = scope
93
115
. get ( `/apis/metrics.k8s.io/v1beta1/${ TEST_NAMESPACE } /default/pods` )
94
116
. reply ( 200 , emptyPodMetrics ) ;
95
117
96
118
const response = await metricsClient . getPodMetrics ( TEST_NAMESPACE ) ;
97
- expect ( response . items . length ) . to . equal ( 0 ) ;
119
+ expect ( response ) . to . deep . equal ( emptyPodMetrics ) ;
98
120
s . done ( ) ;
99
121
} ) ;
100
122
it ( 'should return cluster scope pods metrics list' , async ( ) => {
101
- const [ metricsClient , scope ] = systemUnderTest ( )
102
- const s = scope
103
- . get ( '/apis/metrics.k8s.io/v1beta1/pods' )
104
- . reply ( 200 , mockedPodMetrics ) ;
123
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
124
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 200 , mockedPodMetrics ) ;
105
125
106
126
const response = await metricsClient . getPodMetrics ( ) ;
107
127
expect ( response ) . to . deep . equal ( mockedPodMetrics ) ;
108
128
109
129
s . done ( ) ;
110
130
} ) ;
111
131
it ( 'should return namespace scope pods metric list' , async ( ) => {
112
- const [ metricsClient , scope ] = systemUnderTest ( )
132
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
113
133
const s = scope
114
134
. get ( `/apis/metrics.k8s.io/v1beta1/${ TEST_NAMESPACE } /default/pods` )
115
135
. reply ( 200 , mockedPodMetrics ) ;
@@ -119,25 +139,61 @@ describe('Metrics', () => {
119
139
s . done ( ) ;
120
140
} ) ;
121
141
it ( 'should resolve to error when 500' , async ( ) => {
122
-
123
142
const response : V1Status = {
124
143
code : 12345 ,
125
144
message : 'some message' ,
126
145
} ;
127
- const [ metricsClient , scope ] = systemUnderTest ( )
128
- const s = scope
129
- . get ( '/apis/metrics.k8s.io/v1beta1/pods' )
130
- . reply ( 500 , response ) ;
146
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
147
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 500 , response ) ;
131
148
132
149
try {
133
150
await metricsClient . getPodMetrics ( ) ;
134
- fail ( "expected thrown error" ) ;
151
+ fail ( 'expected thrown error' ) ;
152
+ } catch ( e ) {
153
+ if ( ! ( e instanceof HttpError ) ) {
154
+ fail ( 'expected HttpError error' ) ;
155
+ }
156
+ expect ( e . body . code ) . to . equal ( response . code ) ;
157
+ expect ( e . body . message ) . to . equal ( response . message ) ;
158
+ }
159
+ s . done ( ) ;
160
+ } ) ;
161
+ } ) ;
162
+ describe ( 'getNodeMetrics' , ( ) => {
163
+ it ( 'should return empty nodes list' , async ( ) => {
164
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
165
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/nodes' ) . reply ( 200 , emptyNodeMetrics ) ;
166
+
167
+ const response = await metricsClient . getNodeMetrics ( ) ;
168
+ expect ( response ) . to . deep . equal ( emptyNodeMetrics ) ;
169
+ s . done ( ) ;
170
+ } ) ;
171
+ it ( 'should return nodes metrics list' , async ( ) => {
172
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
173
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/nodes' ) . reply ( 200 , mockedNodeMetrics ) ;
174
+
175
+ const response = await metricsClient . getNodeMetrics ( ) ;
176
+ expect ( response ) . to . deep . equal ( mockedNodeMetrics ) ;
177
+
178
+ s . done ( ) ;
179
+ } ) ;
180
+ it ( 'should resolve to error when 500' , async ( ) => {
181
+ const response : V1Status = {
182
+ code : 12345 ,
183
+ message : 'some message' ,
184
+ } ;
185
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
186
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/nodes' ) . reply ( 500 , response ) ;
187
+
188
+ try {
189
+ await metricsClient . getNodeMetrics ( ) ;
190
+ fail ( 'expected thrown error' ) ;
135
191
} catch ( e ) {
136
- if ( ! ( e instanceof HttpError ) ) {
137
- fail ( " expected HttpError error" ) ;
138
- }
139
- expect ( e . body . code ) . to . equal ( response . code ) ;
140
- expect ( e . body . message ) . to . equal ( response . message ) ;
192
+ if ( ! ( e instanceof HttpError ) ) {
193
+ fail ( ' expected HttpError error' ) ;
194
+ }
195
+ expect ( e . body . code ) . to . equal ( response . code ) ;
196
+ expect ( e . body . message ) . to . equal ( response . message ) ;
141
197
}
142
198
s . done ( ) ;
143
199
} ) ;
0 commit comments