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

feat: Allow termination of non-appstream envs #655

Merged
merged 7 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,165 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

const ServicesContainer = require('@aws-ee/base-services-container/lib/services-container');

// Mocked dependencies
jest.mock('@aws-ee/base-services/lib/plugin-registry/plugin-registry-service');
const PluginRegistryService = require('@aws-ee/base-services/lib/plugin-registry/plugin-registry-service');

jest.mock('@aws-ee/base-services/lib/settings/env-settings-service');
const SettingsServiceMock = require('@aws-ee/base-services/lib/settings/env-settings-service');

jest.mock('@aws-ee/base-raas-services/lib/environment/service-catalog/environment-sc-service');
const EnvironmentScServiceMock = require('@aws-ee/base-raas-services/lib/environment/service-catalog/environment-sc-service');

jest.mock('@aws-ee/base-raas-services/lib/indexes/indexes-service');
const IndexesServiceMock = require('@aws-ee/base-raas-services/lib/indexes/indexes-service');

const plugin = require('../aws-account-mgmt-plugin');

// Tested Functions: getActiveNonAppStreamEnvs
describe('awsAccountMgmtPlugin', () => {
let container;
let settings;
let environmentScService;
let indexesService;
beforeEach(async () => {
// Initialize services container and register dependencies
container = new ServicesContainer();
container.register('pluginRegistryService', new PluginRegistryService());
container.register('settings', new SettingsServiceMock());
container.register('environmentScService', new EnvironmentScServiceMock());
container.register('indexesService', new IndexesServiceMock());

await container.initServices();
settings = await container.find('settings');
environmentScService = await container.find('environmentScService');
indexesService = await container.find('indexesService');
});

describe('getActiveNonAppStreamEnvs', () => {
const requestContext = { principalIdentifier: { uid: 'u-testuser' } };
it('should return empty list if AppStream is disabled', async () => {
// BUILD
const awsAccountId = 'sampleAwsAccountId';
settings.getBoolean = jest.fn(() => {
return false;
});
const expected = [];

// OPERATE
const retVal = await plugin.getActiveNonAppStreamEnvs({ awsAccountId }, { requestContext, container });

// TEST
SanketD92 marked this conversation as resolved.
Show resolved Hide resolved
expect(retVal).toEqual(expected);
});

it('should return a list of active non-AppStream environments for an account if AppStream is enabled', async () => {
// BUILD
const awsAccountId = 'sampleAwsAccountId';
settings.getBoolean = jest.fn(() => {
return true;
});
const scEnvs = [
{ id: 'env1', indexId: 'index1', isAppStreamConfigured: true, status: 'COMPLETED' },
{ id: 'env2', indexId: 'index1', isAppStreamConfigured: false, status: 'COMPLETED' }, // This will be returned
{ id: 'env3', indexId: 'index1', isAppStreamConfigured: false, status: 'FAILED' },
{ id: 'env4', indexId: 'index1', isAppStreamConfigured: false, status: 'TERMINATED' },
{ id: 'env5', indexId: 'index1', isAppStreamConfigured: false, status: 'UNKNOWN' },
];
const indexes = [
{ id: 'index1', awsAccountId },
{ id: 'index2', awsAccountId: 'someOtherAccount' },
];
environmentScService.list = jest.fn(() => {
return scEnvs;
});
indexesService.list = jest.fn(() => {
return indexes;
});

const expected = [{ id: 'env2', indexId: 'index1', isAppStreamConfigured: false, status: 'COMPLETED' }];

// OPERATE
const retVal = await plugin.getActiveNonAppStreamEnvs({ awsAccountId }, { requestContext, container });

// TEST
expect(retVal).toEqual(expected);
});

it('should return an empty list if no active non-AppStream environments for an account are found', async () => {
// BUILD
const awsAccountId = 'sampleAwsAccountId';
settings.getBoolean = jest.fn(() => {
return true;
});
const scEnvs = [
{ id: 'env1', indexId: 'index1', isAppStreamConfigured: true, status: 'COMPLETED' },
{ id: 'env2', indexId: 'index1', isAppStreamConfigured: false, status: 'TERMINATED' },
{ id: 'env3', indexId: 'index1', isAppStreamConfigured: false, status: 'FAILED' },
{ id: 'env4', indexId: 'index1', isAppStreamConfigured: false, status: 'UNKNOWN' },
];
const indexes = [
{ id: 'index1', awsAccountId },
{ id: 'index2', awsAccountId: 'someOtherAccount' },
];
environmentScService.list = jest.fn(() => {
return scEnvs;
});
indexesService.list = jest.fn(() => {
return indexes;
});

const expected = [];

// OPERATE
const retVal = await plugin.getActiveNonAppStreamEnvs({ awsAccountId }, { requestContext, container });

// TEST
expect(retVal).toEqual(expected);
});

it('should return an empty list if active non-AppStream environments exist but for a different account', async () => {
// BUILD
const awsAccountId = 'sampleAwsAccountId';
settings.getBoolean = jest.fn(() => {
return true;
});
const scEnvs = [
{ id: 'env1', indexId: 'index1', isAppStreamConfigured: true, status: 'COMPLETED' },
{ id: 'env2', indexId: 'index1', isAppStreamConfigured: false, status: 'STOPPED' },
];
const indexes = [
{ id: 'index1', awsAccountId: 'someOtherAccount' },
{ id: 'index2', awsAccountId },
];
environmentScService.list = jest.fn(() => {
return scEnvs;
});
indexesService.list = jest.fn(() => {
return indexes;
});

const expected = [];

// OPERATE
const retVal = await plugin.getActiveNonAppStreamEnvs({ awsAccountId }, { requestContext, container });

// TEST
expect(retVal).toEqual(expected);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AppStreamScService = require('../../appstream/appstream-sc-service');

const plugin = require('../env-sc-connection-url-plugin');

// Tested Functions: create, update, delete
// Tested Functions: createConnectionUrl
describe('envScConnectionUrlPlugin', () => {
let container;
let appStreamScService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const settingKeys = {
* Returns a list of active non-AppStream environments linked to a given AWS Account ID
* This check is only performed when the deployment has AppStream enabled,
* and is triggered if the user attempts to update the AWS account using SWB APIs.
* A similar check is performed on the UI components as well.
* A similar check is performed on the UI components (AccountUtils) as well.
*/
async function getActiveNonAppStreamEnvs({ awsAccountId }, { requestContext, container }) {
const settings = await container.find('settings');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ScEnvironmentButtons extends React.Component {
1. AppStream is not enabled and environment can be connected to
2. AppStream is enabled, environment is linked to an AppStream-configured account, and environment can be connected to
*/}
{canConnect && !(isAppStreamEnabled && !env.isAppStreamConfigured) && (
{canConnect && (!isAppStreamEnabled || env.isAppStreamConfigured) && (
<Button
floated="left"
basic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe('AwsAccountService', () => {
let dbService = null;
let s3Service = null;
let lockService = null;
let pluginService = null;
let settingsService = null;
beforeEach(async () => {
// Initialize services container and register dependencies
const container = new ServicesContainer();
Expand All @@ -71,6 +73,8 @@ describe('AwsAccountService', () => {
dbService = await container.find('dbService');
s3Service = await container.find('s3Service');
lockService = await container.find('lockService');
pluginService = await container.find('pluginRegistryService');
settingsService = await container.find('settings');

// Skip authorization by default
service.assertAuthorized = jest.fn();
Expand Down Expand Up @@ -373,4 +377,56 @@ describe('AwsAccountService', () => {
);
});
});

describe('checkForActiveNonAppStreamEnvs', () => {
it('should not throw error if plugin returns empty array', async () => {
// BUILD
const requestContext = {};
settingsService.getBoolean = jest.fn(() => {
return true;
});
pluginService.visitPlugins = jest.fn(() => {
return [];
});
const awsAccountId = 'sampleAwsAccountId';

// OPERATE & CHECK
await service.checkForActiveNonAppStreamEnvs(requestContext, awsAccountId);
});

it('should not throw error if AppStream is disabled', async () => {
// BUILD
const requestContext = {};
settingsService.getBoolean = jest.fn(() => {
return false;
});
const awsAccountId = 'sampleAwsAccountId';

// OPERATE & CHECK
await service.checkForActiveNonAppStreamEnvs(requestContext, awsAccountId);
});

it('should throw error if AppStream is enabled and plugin returns non-empty array', async () => {
// BUILD
const requestContext = {};
settingsService.getBoolean = jest.fn(() => {
return true;
});
pluginService.visitPlugins = jest.fn(() => {
return [{ id: 'env1' }];
});
const awsAccountId = 'sampleAwsAccountId';

// OPERATE
try {
await service.checkForActiveNonAppStreamEnvs(requestContext, awsAccountId);
expect.hasAssertions();
} catch (err) {
// CHECK
expect(err.message).toEqual(
'This account has active non-AppStream environments. Please terminate them and retry this operation',
);
}
});
});
});