Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fix cloud deployment ID check #68695

Merged
merged 6 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
isCloud,
getNewJobDefaults,
getNewJobLimits,
extractDeploymentId,
} from './ml_server_info';
import mockMlInfoResponse from './__mocks__/ml_info_response.json';

Expand All @@ -20,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);
});
Expand All @@ -33,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');
Expand All @@ -52,11 +53,37 @@ 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');
done();
});
});

describe('cloud extract deployment ID', () => {
const cloudIdWithDeploymentName =
'cloud_message_test:ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg==';

const cloudIdWithOutDeploymentName =
':ZXUtd2VzdC0yLmF3cy5jbG91ZC5lcy5pbyQ4NWQ2NjZmMzM1MGM0NjllOGMzMjQyZDc2YTdmNDU5YyQxNmI1ZDM2ZGE1Mzk0YjlkYjIyZWJlNDk1OWY1OGQzMg==';

const badCloudId = 'cloud_message_test:this_is_not_a_base64_string';

it('should extract cloud ID when deployment name is present', () => {
expect(extractDeploymentId(cloudIdWithDeploymentName)).toBe(
'85d666f3350c469e8c3242d76a7f459c'
);
});

it('should extract cloud ID when deployment name is not present', () => {
expect(extractDeploymentId(cloudIdWithOutDeploymentName)).toBe(
'85d666f3350c469e8c3242d76a7f459c'
);
});

it('should fail to extract cloud ID', () => {
expect(extractDeploymentId(badCloudId)).toBe(null);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^(.+)?:/, '');
Copy link
Contributor

@darnautov darnautov Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you can achieve it without regexp, const tempCloudId = cloudId.split(':')[1]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the deployment name isn't present at all then the array will only have a length of 1 and so an extra check will be needed.
i think using replace here is the simplest approach, even though it's not the most performant.

Copy link
Contributor

@darnautov darnautov Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you regexp relies on : anyway. It expects something like":xyz", so split will return 2 elements anyway, the first one will be an empty string. Not critical at all anyway, just suppose it's always easier without regexp

try {
const matches = atob(tempCloudId).match(/^.+\$(.+)(?=\$)/);
return matches !== null && matches.length === 2 ? matches[1] : null;
Expand Down