From d8373d3ba911a46e76f52750f75ac8616703ee5f Mon Sep 17 00:00:00 2001 From: Yehiyam Livneh Date: Mon, 1 Feb 2021 15:03:11 +0200 Subject: [PATCH 1/2] add optional api-server prefix --- core/api-server/config/main/config.base.js | 6 ++++-- core/api-server/tests/algorithms-store.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/api-server/config/main/config.base.js b/core/api-server/config/main/config.base.js index 8443e093c..a44e9790e 100644 --- a/core/api-server/config/main/config.base.js +++ b/core/api-server/config/main/config.base.js @@ -41,8 +41,10 @@ config.dataSourceService = { prefix: 'api/v1' }; +config.ingressPrefix = process.env.INGRESS_PREFIX || ''; + config.debugUrl = { - path: path.join(process.env.INGRESS_PREFIX || '', 'hkube/debug') + path: path.join(config.ingressPrefix, 'hkube/debug') }; config.addDefaultAlgorithms = process.env.ADD_DEFAULT_ALGORITHMS || true; @@ -51,7 +53,7 @@ config.swagger = { protocol: secured ? 'https' : 'http', host: process.env.BASE_URL_HOST || 'localhost', port: process.env.BASE_URL_PORT || config.rest.port, - path: (process.env.BASE_URL_PATH || '') + path: process.env.BASE_URL_PATH? path.join(config.ingressPrefix, process.env.BASE_URL_PATH): config.ingressPrefix }; config.jobs = { diff --git a/core/api-server/tests/algorithms-store.js b/core/api-server/tests/algorithms-store.js index bda96d710..95041da69 100644 --- a/core/api-server/tests/algorithms-store.js +++ b/core/api-server/tests/algorithms-store.js @@ -2,6 +2,7 @@ const { expect } = require('chai'); const fse = require('fs-extra'); const nock = require('nock'); const HttpStatus = require('http-status-codes'); +const pathLib = require('path'); const merge = require('lodash.merge'); const { uid: uuid } = require('@hkube/uid'); const stateManager = require('../lib/state/state-manager'); @@ -26,6 +27,7 @@ describe('Store/Algorithms', () => { restUrl = global.testParams.restUrl; restPath = `${restUrl}/store/algorithms`; applyPath = `${restPath}/apply`; + debugRestUrl = `${restPath}/debug`; versionsPath = `${restUrl}/versions/algorithms`; nock(baseApi).persist().get(emptyGit).query(true).reply(HttpStatus.BAD_REQUEST, 'Git Repository is empty'); nock(baseApi).persist().get(fullGit).query(true).reply(HttpStatus.OK, commit.data); @@ -1851,4 +1853,19 @@ describe('Store/Algorithms', () => { expect(response.body.algorithmImage).to.not.eql(body.algorithmImage); }); }); + describe('/store/algorithms/debug POST', () => { + it('should succeed to set debugUrl', async () => { + const body = { + name: uuid(), + } + const options = { + uri: debugRestUrl, + body + }; + const response = await request(options); + const { version, created, modified, data, ...algorithm } = response.body; + expect(response.response.statusCode).to.equal(HttpStatus.CREATED); + expect(data.path).to.eql(pathLib.join(process.env.INGRESS_PREFIX || '', 'hkube', 'debug', body.name)); + }); + }) }); From edbf405d4c32e5654fccf21e571297a233ace210 Mon Sep 17 00:00:00 2001 From: Yehiyam Livneh Date: Wed, 3 Feb 2021 09:36:11 +0200 Subject: [PATCH 2/2] add tests --- core/api-server/tests/configTest.js | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 core/api-server/tests/configTest.js diff --git a/core/api-server/tests/configTest.js b/core/api-server/tests/configTest.js new file mode 100644 index 000000000..b2135f746 --- /dev/null +++ b/core/api-server/tests/configTest.js @@ -0,0 +1,37 @@ +const { expect } = require('chai'); +const configIt = require('@hkube/config'); +const path = require('path'); +let envSave={}; +describe('config',()=>{ + beforeEach(() => { + envSave = {...process.env}; + delete require.cache[require.resolve(path.resolve('config/main/config.base.js'))]; + }); + afterEach(()=>{ + process.env = envSave + }) + it('empty env', () => { + const { main } = configIt.load(); + expect(main.debugUrl.path).to.eql('hkube/debug') + expect(main.swagger.path).to.eql('') + }); + it('ingress prefix env', () => { + process.env.INGRESS_PREFIX='/foo' + const { main } = configIt.load(); + expect(main.debugUrl.path).to.eql('/foo/hkube/debug') + expect(main.swagger.path).to.eql('/foo') + }); + it('base path env', () => { + process.env.BASE_URL_PATH='/hkube/api-server' + const { main } = configIt.load(); + expect(main.debugUrl.path).to.eql('hkube/debug') + expect(main.swagger.path).to.eql('/hkube/api-server') + }); + it('ingress and base path env', () => { + process.env.BASE_URL_PATH='/hkube/api-server' + process.env.INGRESS_PREFIX='/foo' + const { main } = configIt.load(); + expect(main.debugUrl.path).to.eql('/foo/hkube/debug') + expect(main.swagger.path).to.eql('/foo/hkube/api-server') + }); +}) \ No newline at end of file