Skip to content

Commit b6a3e6f

Browse files
committed
complete metrics coverage
1 parent b57a95a commit b6a3e6f

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

src/metrics.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,7 @@ export class Metrics {
6262
}
6363

6464
public async getNodeMetrics(): Promise<NodeMetricsList> {
65-
const path = '/apis/metrics.k8s.io/v1beta1/nodes';
66-
67-
const requestOptions = this.requestOptionsFromPath(path);
68-
69-
await this.config.applyToRequest(requestOptions);
70-
71-
return this.handleResponse<NodeMetricsList>(requestOptions);
65+
return this.metricsApiRequest<NodeMetricsList>('/apis/metrics.k8s.io/v1beta1/nodes');
7266
}
7367

7468
public async getPodMetrics(namespace?: string): Promise<PodMetricsList> {
@@ -80,26 +74,22 @@ export class Metrics {
8074
path = '/apis/metrics.k8s.io/v1beta1/pods';
8175
}
8276

83-
const requestOptions = this.requestOptionsFromPath(path);
84-
85-
await this.config.applyToRequest(requestOptions);
86-
87-
return this.handleResponse<PodMetricsList>(requestOptions);
77+
return this.metricsApiRequest<PodMetricsList>(path);
8878
}
8979

90-
private requestOptionsFromPath(path: string): request.Options {
80+
private async metricsApiRequest<T extends PodMetricsList | NodeMetricsList>(path: string): Promise<T> {
9181
const cluster = this.config.getCurrentCluster();
9282
if (!cluster) {
9383
throw new Error('No currently active cluster');
9484
}
9585

96-
return {
86+
const requestOptions: request.Options = {
9787
method: 'GET',
9888
uri: cluster.server + path,
9989
};
100-
}
10190

102-
private handleResponse<T>(requestOptions: request.Options): Promise<T> {
91+
await this.config.applyToRequest(requestOptions);
92+
10393
return new Promise((resolve, reject) => {
10494
const req = request(requestOptions, (error, response, body) => {
10595
if (error) {

src/metrics_test.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ const testConfigOptions: any = {
8181
currentContext: 'currentContext',
8282
};
8383

84-
const systemUnderTest = (): [Metrics, nock.Scope] => {
84+
const systemUnderTest = (options: any = testConfigOptions): [Metrics, nock.Scope] => {
8585
const kc = new KubeConfig();
86-
kc.loadFromOptions(testConfigOptions);
86+
kc.loadFromOptions(options);
8787
const metricsClient = new Metrics(kc);
8888

8989
const scope = nock(testConfigOptions.clusters[0].server);
@@ -138,7 +138,38 @@ describe('Metrics', () => {
138138
expect(response).to.deep.equal(mockedPodMetrics);
139139
s.done();
140140
});
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 () => {
142173
const response: V1Status = {
143174
code: 12345,
144175
message: 'some message',
@@ -158,6 +189,22 @@ describe('Metrics', () => {
158189
}
159190
s.done();
160191
});
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+
});
161208
});
162209
describe('getNodeMetrics', () => {
163210
it('should return empty nodes list', async () => {

0 commit comments

Comments
 (0)