@@ -81,9 +81,9 @@ const testConfigOptions: any = {
81
81
currentContext : 'currentContext' ,
82
82
} ;
83
83
84
- const systemUnderTest = ( ) : [ Metrics , nock . Scope ] => {
84
+ const systemUnderTest = ( options : any = testConfigOptions ) : [ Metrics , nock . Scope ] => {
85
85
const kc = new KubeConfig ( ) ;
86
- kc . loadFromOptions ( testConfigOptions ) ;
86
+ kc . loadFromOptions ( options ) ;
87
87
const metricsClient = new Metrics ( kc ) ;
88
88
89
89
const scope = nock ( testConfigOptions . clusters [ 0 ] . server ) ;
@@ -138,7 +138,38 @@ describe('Metrics', () => {
138
138
expect ( response ) . to . deep . equal ( mockedPodMetrics ) ;
139
139
s . done ( ) ;
140
140
} ) ;
141
- it ( 'should resolve to error when 500' , async ( ) => {
141
+ it ( 'should when connection refused' , async ( ) => {
142
+ const kc = new KubeConfig ( ) ;
143
+ kc . loadFromOptions ( {
144
+ clusters : [ { name : 'cluster' , server : 'https://127.0.0.1:51011' } ] ,
145
+ users : [ { name : 'user' , password : 'password' } ] ,
146
+ contexts : [ { name : 'currentContext' , cluster : 'cluster' , user : 'user' } ] ,
147
+ currentContext : 'currentContext' ,
148
+ } ) ;
149
+ const metricsClient = new Metrics ( kc ) ;
150
+ try {
151
+ await metricsClient . getPodMetrics ( ) ;
152
+ fail ( 'expected thrown error' ) ;
153
+ } catch ( e ) {
154
+ expect ( e . message ) . to . equal ( 'connect ECONNREFUSED 127.0.0.1:51011' ) ;
155
+ }
156
+ } ) ;
157
+ it ( 'should throw when no current cluster' , async ( ) => {
158
+ const [ metricsClient , scope ] = systemUnderTest ( {
159
+ clusters : [ { name : 'cluster' , server : 'https://127.0.0.1:51010' } ] ,
160
+ users : [ { name : 'user' , password : 'password' } ] ,
161
+ contexts : [ { name : 'currentContext' , cluster : 'cluster' , user : 'user' } ] ,
162
+ } ) ;
163
+
164
+ try {
165
+ await metricsClient . getPodMetrics ( ) ;
166
+ fail ( 'expected thrown error' ) ;
167
+ } catch ( e ) {
168
+ expect ( e . message ) . to . equal ( 'No currently active cluster' ) ;
169
+ }
170
+ scope . done ( ) ;
171
+ } ) ;
172
+ it ( 'should resolve to error when 500 - V1 Status' , async ( ) => {
142
173
const response : V1Status = {
143
174
code : 12345 ,
144
175
message : 'some message' ,
@@ -158,6 +189,22 @@ describe('Metrics', () => {
158
189
}
159
190
s . done ( ) ;
160
191
} ) ;
192
+ it ( 'should resolve to error when 500 - non-V1Status' , async ( ) => {
193
+ const response = 'some other response' ;
194
+ const [ metricsClient , scope ] = systemUnderTest ( ) ;
195
+ const s = scope . get ( '/apis/metrics.k8s.io/v1beta1/pods' ) . reply ( 500 , response ) ;
196
+
197
+ try {
198
+ await metricsClient . getPodMetrics ( ) ;
199
+ fail ( 'expected thrown error' ) ;
200
+ } catch ( e ) {
201
+ if ( ! ( e instanceof HttpError ) ) {
202
+ fail ( 'expected HttpError error' ) ;
203
+ }
204
+ expect ( e . message ) . to . equal ( 'HTTP request failed' ) ;
205
+ }
206
+ s . done ( ) ;
207
+ } ) ;
161
208
} ) ;
162
209
describe ( 'getNodeMetrics' , ( ) => {
163
210
it ( 'should return empty nodes list' , async ( ) => {
0 commit comments