From 0f09af23e0a8adc5089ad5f490bed6dfe7921f18 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 9 Jun 2020 19:56:31 +0100 Subject: [PATCH 1/4] [ML] Fix cloud ID check --- x-pack/plugins/ml/public/application/services/ml_server_info.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.ts index 8ab955b479108..66fd491b59731 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.ts @@ -56,7 +56,7 @@ export function getCloudDeploymentId(): string | null { if (cloudInfo.cloudId === null) { return null; } - const tempCloudId = cloudInfo.cloudId.replace(/^.+:/, ''); + const tempCloudId = cloudInfo.cloudId.replace(/^(.+)?:/, ''); try { const matches = atob(tempCloudId).match(/^.+\$(.+)(?=\$)/); return matches !== null && matches.length === 2 ? matches[1] : null; From ec910b882f69675c5213e78f6f9aab383c325afd Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Tue, 9 Jun 2020 20:07:20 +0100 Subject: [PATCH 2/4] updates for tests --- .../services/ml_server_info.test.ts | 21 +++++++++++++++++++ .../application/services/ml_server_info.ts | 9 ++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts index bd91995b6efc3..7383a996b32b6 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts @@ -10,6 +10,7 @@ import { isCloud, getNewJobDefaults, getNewJobLimits, + extractDeploymentId, } from './ml_server_info'; import mockMlInfoResponse from './__mocks__/ml_info_response.json'; @@ -59,4 +60,24 @@ describe('ml_server_info', () => { done(); }); }); + + describe('cloud extract deployment ID', () => { + const cloudIdWithDeploymentName = + 'cloud_message_test:ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg=='; + + const cloudIdWithOutDeploymentName = + ':ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg=='; + + it('cloud ID with deployment name', () => { + expect(extractDeploymentId(cloudIdWithDeploymentName)).toBe( + '85d666f3350c469e8c3242d76a7f459c' + ); + }); + + it('cloud ID without deployment name', () => { + expect(extractDeploymentId(cloudIdWithOutDeploymentName)).toBe( + '85d666f3350c469e8c3242d76a7f459c' + ); + }); + }); }); diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.ts index 66fd491b59731..d1f92df01f061 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.ts @@ -53,10 +53,11 @@ export function isCloud(): boolean { } export function getCloudDeploymentId(): string | null { - if (cloudInfo.cloudId === null) { - return null; - } - const tempCloudId = cloudInfo.cloudId.replace(/^(.+)?:/, ''); + return cloudInfo.cloudId === null ? null : extractDeploymentId(cloudInfo.cloudId); +} + +export function extractDeploymentId(cloudId: string) { + const tempCloudId = cloudId.replace(/^(.+)?:/, ''); try { const matches = atob(tempCloudId).match(/^.+\$(.+)(?=\$)/); return matches !== null && matches.length === 2 ? matches[1] : null; From 860bc1ed82c93c7592f4cd1d5ad21d7565f2fb72 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Wed, 10 Jun 2020 08:22:47 +0100 Subject: [PATCH 3/4] adding extra test --- .../ml/public/application/services/ml_server_info.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts index 7383a996b32b6..8a8743c60fe04 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts @@ -68,6 +68,8 @@ describe('ml_server_info', () => { const cloudIdWithOutDeploymentName = ':ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg=='; + const badCloudId = 'cloud_message_test:this_is_not_a_base64_string'; + it('cloud ID with deployment name', () => { expect(extractDeploymentId(cloudIdWithDeploymentName)).toBe( '85d666f3350c469e8c3242d76a7f459c' @@ -79,5 +81,9 @@ describe('ml_server_info', () => { '85d666f3350c469e8c3242d76a7f459c' ); }); + + it('bad cloud ID', () => { + expect(extractDeploymentId(badCloudId)).toBe(null); + }); }); }); From 23102b2cd3c0789001630145de1c400740bfc623 Mon Sep 17 00:00:00 2001 From: James Gowdy Date: Wed, 10 Jun 2020 09:57:24 +0100 Subject: [PATCH 4/4] updating test titles --- .../application/services/ml_server_info.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts index 8a8743c60fe04..cd0f10bb7f577 100644 --- a/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts +++ b/x-pack/plugins/ml/public/application/services/ml_server_info.test.ts @@ -21,7 +21,7 @@ jest.mock('./ml_api_service', () => ({ })); describe('ml_server_info initial state', () => { - it('server info not loaded ', () => { + it('should fail to get server info ', () => { expect(isCloud()).toBe(false); expect(getCloudDeploymentId()).toBe(null); }); @@ -34,14 +34,14 @@ describe('ml_server_info', () => { }); describe('cloud information', () => { - it('can get could deployment id', () => { + it('should get could deployment id', () => { expect(isCloud()).toBe(true); expect(getCloudDeploymentId()).toBe('85d666f3350c469e8c3242d76a7f459c'); }); }); describe('defaults', () => { - it('can get defaults', async (done) => { + it('should get defaults', async (done) => { const defaults = getNewJobDefaults(); expect(defaults.anomaly_detectors.model_memory_limit).toBe('128mb'); @@ -53,7 +53,7 @@ describe('ml_server_info', () => { }); describe('limits', () => { - it('can get limits', async (done) => { + it('should get limits', async (done) => { const limits = getNewJobLimits(); expect(limits.max_model_memory_limit).toBe('128mb'); @@ -70,19 +70,19 @@ describe('ml_server_info', () => { const badCloudId = 'cloud_message_test:this_is_not_a_base64_string'; - it('cloud ID with deployment name', () => { + it('should extract cloud ID when deployment name is present', () => { expect(extractDeploymentId(cloudIdWithDeploymentName)).toBe( '85d666f3350c469e8c3242d76a7f459c' ); }); - it('cloud ID without deployment name', () => { + it('should extract cloud ID when deployment name is not present', () => { expect(extractDeploymentId(cloudIdWithOutDeploymentName)).toBe( '85d666f3350c469e8c3242d76a7f459c' ); }); - it('bad cloud ID', () => { + it('should fail to extract cloud ID', () => { expect(extractDeploymentId(badCloudId)).toBe(null); }); });