Skip to content

Commit 41f5647

Browse files
authored
Merge pull request #2019 from cjihrig/test-cleanup
test: clean up rejection and fail logic
2 parents 9a87778 + d51fba1 commit 41f5647

File tree

5 files changed

+73
-225
lines changed

5 files changed

+73
-225
lines changed

src/exec_auth_test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { OutgoingHttpHeaders } from 'node:http';
77

88
import { ExecAuth } from './exec_auth';
99
import { User } from './config_types';
10-
import { fail } from 'node:assert';
1110

1211
import child_process from 'node:child_process';
1312

@@ -336,10 +335,6 @@ describe('ExecAuth', () => {
336335
},
337336
opts,
338337
);
339-
if (!opts.headers) {
340-
fail('unexpected null headers!');
341-
} else {
342-
expect(opts.headers.Authorization).to.equal('Bearer foo');
343-
}
338+
expect(opts.headers?.Authorization).to.equal('Bearer foo');
344339
});
345340
});

src/metrics_test.ts

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { fail } from 'node:assert';
21
import { expect } from 'chai';
32
import nock from 'nock';
43
import { KubeConfig } from './config';
@@ -147,32 +146,21 @@ describe('Metrics', () => {
147146
currentContext: 'currentContext',
148147
});
149148
const metricsClient = new Metrics(kc);
150-
try {
151-
await metricsClient.getPodMetrics();
152-
fail('expected thrown error');
153-
} catch (e) {
154-
expect(e instanceof ApiException);
155-
if (e instanceof ApiException) {
156-
expect(e.message).to.include('connect ECONNREFUSED 127.0.0.1:51011');
157-
}
158-
}
149+
await expect(metricsClient.getPodMetrics()).to.be.rejectedWith(
150+
ApiException,
151+
'connect ECONNREFUSED 127.0.0.1:51011',
152+
);
159153
});
160154
it('should throw when no current cluster', async () => {
161155
const [metricsClient, scope] = systemUnderTest({
162156
clusters: [{ name: 'cluster', server: 'https://127.0.0.1:51010' }],
163157
users: [{ name: 'user', password: 'password' }],
164158
contexts: [{ name: 'currentContext', cluster: 'cluster', user: 'user' }],
165159
});
166-
167-
try {
168-
await metricsClient.getPodMetrics();
169-
fail('expected thrown error');
170-
} catch (e) {
171-
expect(e instanceof ApiException);
172-
if (e instanceof ApiException) {
173-
expect(e.message).to.equal('No currently active cluster');
174-
}
175-
}
160+
await expect(metricsClient.getPodMetrics()).to.be.rejectedWith(
161+
Error,
162+
'No currently active cluster',
163+
);
176164
scope.done();
177165
});
178166
it('should resolve to error when 500 - V1 Status', async () => {
@@ -183,33 +171,23 @@ describe('Metrics', () => {
183171
const [metricsClient, scope] = systemUnderTest();
184172
const s = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(500, response);
185173

186-
try {
187-
await metricsClient.getPodMetrics();
188-
fail('expected thrown error');
189-
} catch (e) {
190-
if (!(e instanceof ApiException)) {
191-
fail('expected ApiException error');
192-
}
174+
await expect(metricsClient.getPodMetrics()).to.be.rejected.then((e) => {
175+
expect(e).to.be.an.instanceOf(ApiException);
193176
expect(e.code).to.equal(response.code);
194177
expect(e.body.message).to.equal(response.message);
195-
}
178+
});
196179
s.done();
197180
});
198181
it('should resolve to error when 500 - non-V1Status', async () => {
199182
const response = 'some other response';
200183
const [metricsClient, scope] = systemUnderTest();
201184
const s = scope.get('/apis/metrics.k8s.io/v1beta1/pods').reply(500, response);
202185

203-
try {
204-
await metricsClient.getPodMetrics();
205-
fail('expected thrown error');
206-
} catch (e) {
207-
if (!(e instanceof ApiException)) {
208-
fail('expected ApiException error');
209-
}
186+
await expect(metricsClient.getPodMetrics()).to.be.rejected.then((e) => {
187+
expect(e).to.be.an.instanceOf(ApiException);
210188
expect(e.code).to.equal(500);
211189
expect(e.message).to.include('Error occurred in metrics request');
212-
}
190+
});
213191
s.done();
214192
});
215193
});
@@ -239,16 +217,11 @@ describe('Metrics', () => {
239217
const [metricsClient, scope] = systemUnderTest();
240218
const s = scope.get('/apis/metrics.k8s.io/v1beta1/nodes').reply(500, response);
241219

242-
try {
243-
await metricsClient.getNodeMetrics();
244-
fail('expected thrown error');
245-
} catch (e) {
246-
if (!(e instanceof ApiException)) {
247-
fail('expected ApiException error');
248-
}
220+
await expect(metricsClient.getNodeMetrics()).to.be.rejected.then((e) => {
221+
expect(e).to.be.an.instanceOf(ApiException);
249222
expect(e.code).to.equal(response.code);
250223
expect(e.body.message).to.equal(response.message);
251-
}
224+
});
252225
s.done();
253226
});
254227
});

0 commit comments

Comments
 (0)