Skip to content

Commit

Permalink
Merge branch 'develop' into sagemaker-too-many-requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyen102 authored Mar 9, 2022
2 parents 7added0 + 15eb4d3 commit 06d8b0f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const SettingsServiceMock = require('@aws-ee/base-services/lib/settings/env-sett

jest.mock('@aws-ee/base-services/lib/audit/audit-writer-service');
const AuditServiceMock = require('@aws-ee/base-services/lib/audit/audit-writer-service');
const environmentScStatus = require('../environent-sc-status-enum');

jest.mock('../../environment-authz-service.js');
const EnvironmentAuthZServiceMock = require('../../environment-authz-service.js');
Expand Down Expand Up @@ -1392,6 +1393,92 @@ Quisque egestas, eros nec feugiat venenatis, lorem turpis placerat tortor, ullam
}
});

it('should fail because the environment is already terminated', async () => {
// BUILD
const requestContext = {
principal: {
isExternalUser: false,
},
};
const existingEnv = {
id: 'abc',
name: 'exampleName',
envTypeId: 'exampleETI',
envTypeConfigId: 'exampleETCI',
status: environmentScStatus.TERMINATED,
};

service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv);

// OPERATE
try {
await service.delete(requestContext, { id: existingEnv.id });
expect.hasAssertions();
} catch (err) {
// CHECK
expect(service.boom.is(err, 'badRequest')).toBe(true);
expect(err.message).toContain(`Workspace '${existingEnv.id}' has already been terminated`);
}
});

it('should fail because the environment is being terminated', async () => {
// BUILD
const requestContext = {
principal: {
isExternalUser: false,
},
};
const existingEnv = {
id: 'abc',
name: 'exampleName',
envTypeId: 'exampleETI',
envTypeConfigId: 'exampleETCI',
status: environmentScStatus.TERMINATING,
};

service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv);

// OPERATE
try {
await service.delete(requestContext, { id: existingEnv.id });
expect.hasAssertions();
} catch (err) {
// CHECK
expect(service.boom.is(err, 'badRequest')).toBe(true);
expect(err.message).toContain(`Workspace '${existingEnv.id}' is already being terminated`);
}
});

it('should fail because the environment is in termination failed status', async () => {
// BUILD
const requestContext = {
principal: {
isExternalUser: false,
},
};
const existingEnv = {
id: 'abc',
name: 'exampleName',
envTypeId: 'exampleETI',
envTypeConfigId: 'exampleETCI',
status: environmentScStatus.TERMINATING_FAILED,
};

service.mustFind = jest.fn().mockResolvedValueOnce(existingEnv);

// OPERATE
try {
await service.delete(requestContext, { id: existingEnv.id });
expect.hasAssertions();
} catch (err) {
// CHECK
expect(service.boom.is(err, 'badRequest')).toBe(true);
expect(err.message).toContain(
`Workspace '${existingEnv.id}' can not be terminated while in ${environmentScStatus.TERMINATING_FAILED} status`,
);
}
});

it('should fail because the workflow failed to trigger', async () => {
// BUILD
const requestContext = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,21 @@ class EnvironmentScService extends Service {
existingEnvironment,
);

if (existingEnvironment.status === environmentScStatus.TERMINATED) {
throw this.boom.badRequest(`Workspace '${id}' has already been terminated`, true);
}

if (existingEnvironment.status === environmentScStatus.TERMINATING) {
throw this.boom.badRequest(`Workspace '${id}' is already being terminated`, true);
}

if (existingEnvironment.status === environmentScStatus.TERMINATING_FAILED) {
throw this.boom.badRequest(
`Workspace '${id}' can not be terminated while in ${environmentScStatus.TERMINATING_FAILED} status`,
true,
);
}

await this.update(requestContext, {
id,
rev: existingEnvironment.rev,
Expand Down

0 comments on commit 06d8b0f

Please sign in to comment.