diff --git a/lib/pods.js b/lib/pods.js index 96b9f7d..46ebe9b 100644 --- a/lib/pods.js +++ b/lib/pods.js @@ -2,17 +2,13 @@ const Client = require('./client-base'); class Pods extends Client { get({ podName, labelSelector, useNamespace = true } = {}) { - const namespace = useNamespace ? this._namespace : undefined; - return this._client.api.v1 - .namespaces(namespace) - .pods(podName) - .get({ qs: { labelSelector } }); + const prefix = useNamespace ? this._client.api.v1.namespaces(this._namespace).pods(podName) : this._client.api.v1.pods; + return prefix.get({ qs: { labelSelector } }); } - delete({ podName, useNamespace = true }) { - const namespace = useNamespace ? this._namespace : undefined; + delete({ podName }) { return this._client.api.v1 - .namespaces(namespace) + .namespaces(this._namespace) .pods(podName) .delete(); } diff --git a/lib/resourceQuotas.js b/lib/resourceQuotas.js index e654ac7..887e63f 100644 --- a/lib/resourceQuotas.js +++ b/lib/resourceQuotas.js @@ -3,8 +3,8 @@ const Client = require('./client-base'); class ResourceQuotas extends Client { async get({ name, labelSelector, useNamespace = true } = {}) { - const namespace = useNamespace ? this._namespace : undefined; - return this._client.api.v1.namespaces(namespace).resourcequotas(name).get({ qs: { labelSelector } }); + const prefix = useNamespace ? this._client.api.v1.namespaces(this._namespace).resourcequotas(name) : this._client.api.v1.resourcequotas; + return prefix.get({ qs: { labelSelector } }); } } diff --git a/tests/mocks/kubernetes-server.mock.js b/tests/mocks/kubernetes-server.mock.js index 7d01fbe..9d0d817 100644 --- a/tests/mocks/kubernetes-server.mock.js +++ b/tests/mocks/kubernetes-server.mock.js @@ -7,7 +7,8 @@ const configmaps = require('../stubs/configmaps.json'); const map = { '/api/kube/api/v1/namespaces/default/pods/worker': pod, - '/api/kube/api/v1/namespaces/default/configmaps/hkube-versions': configmaps + '/api/kube/api/v1/namespaces/default/configmaps/hkube-versions': configmaps, + '/api/kube/api/v1/namespaces/default/resourcequotas/foo': {body: {name: 'foo'}} } class MockClient { @@ -18,7 +19,11 @@ class MockClient { app.use(bodyParser.json()); app.use('/', (req, res) => { const p = map[req.path]; - res.json((p && p.body) || ({ status: 'ok' })); + const reply = (p && p.body) || ({ status: 'ok' }); + if (this.addPath) { + reply.path = req.path; + } + res.json(reply); }); this._server.listen(options.port, (err) => { diff --git a/tests/test.js b/tests/test.js index 8bd10f6..b406dcd 100644 --- a/tests/test.js +++ b/tests/test.js @@ -155,28 +155,39 @@ describe('KubernetesClient', () => { }); }); describe('Pods', () => { + before(() => { + kubernetesServerMock.addPath=true; + }); + after(() => { + kubernetesServerMock.addPath=false; + }); it('should get', async () => { const res = await client.pods.get({ podName, labelSelector }); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/pods/worker') expect(res).to.have.property('statusCode'); - expect(res).to.have.property('body'); + expect(res).to.have.property('body'); }); it('should get all', async () => { const res = await client.pods.get({ useNamespace: false }); + expect(res.body.path).to.eql('/api/kube/api/v1/pods') expect(res).to.have.property('statusCode'); expect(res).to.have.property('body'); }); it('should get all in namespace', async () => { const res = await client.pods.get(); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/pods/') expect(res).to.have.property('statusCode'); expect(res).to.have.property('body'); }); it('should get all backward compatibility', async () => { const res = await client.pods.all(); + expect(res.body.path).to.eql('/api/kube/api/v1/pods') expect(res).to.have.property('statusCode'); expect(res).to.have.property('body'); }); it('should get all in namespace backward compatibility', async () => { const res = await client.pods.all(true); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/pods/') expect(res).to.have.property('statusCode'); expect(res).to.have.property('body'); }); @@ -186,21 +197,27 @@ describe('KubernetesClient', () => { }); }); describe('ResourceQuotas', () => { + before(() => { + kubernetesServerMock.addPath=true; + }); + after(() => { + kubernetesServerMock.addPath=false; + }); it('should get', async () => { const res = await client.resourcequotas.get({ name: 'foo', labelSelector }); - expect(res).to.eql(response); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/resourcequotas/foo') }); it('should get all', async () => { const res = await client.resourcequotas.get({ useNamespace: false }); - expect(res).to.eql(response); + expect(res.body.path).to.eql('/api/kube/api/v1/resourcequotas') }); it('should get all in namespace', async () => { const res = await client.resourcequotas.get({}); - expect(res).to.eql(response); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/resourcequotas/') }); it('should get all in namespace no args', async () => { const res = await client.resourcequotas.get(); - expect(res).to.eql(response); + expect(res.body.path).to.eql('/api/kube/api/v1/namespaces/default/resourcequotas/') }); }); describe('Services', () => {