Skip to content

Commit

Permalink
fix: Static namespace fix linted (#629)
Browse files Browse the repository at this point in the history
fix: linted static namespace fix
  • Loading branch information
maghirardelli authored Aug 4, 2021
1 parent 0ea3b4d commit f81a04f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,106 +13,105 @@
* permissions and limitations under the License.
*/

const { iteratee } = require('lodash');
const WorkflowPayload = require('@aws-ee/workflow-engine/lib/workflow-payload');
const LaunchProduct = require('../launch-product/launch-product');

describe('LaunchProduct', ()=> {
const meta = {};
const input = {};
const lp = new LaunchProduct({
step: { config: {} },
workflowPayload: new WorkflowPayload({ meta, input, workflowInstance: { steps: [] } }),
});

describe('checkNamespace', () => {
it('Static name should be transformed to start with analysis- and end with number string and be unique between calls', async () => {
// Build
const resolvedInputParams = [{Key: 'Namespace', Value: 'staticname'}];
const datetime = Date.now();
const resolvedInputParams2 = [{Key: 'Namespace', Value: 'staticname'}];
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;
// Operate
const {namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const {namespaceParam2, namespaceIndex2 } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);
// Check
expect(namespaceParam).not.toBe(namespaceParam2);
expect(namespaceParam).toBe(`analysis-${originalNamespace}-${datetime}`);
});
it('Static name that begins with analysis- should be transformed to end with number string and be unique between calls', async () => {
// Build
const resolvedInputParams = [{Key: 'Namespace', Value: 'analysis-staticname'}];
const resolvedInputParams2 = [{Key: 'Namespace', Value: 'analysis-staticname'}];
const datetime = Date.now();
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;
// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const { namespaceParam2, namespaceIndex2 } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);
// Check
expect(namespaceParam).not.toBe(namespaceParam2);
expect(namespaceParam).toBe(`${originalNamespace}-${datetime}`);
});
it('Dynamic name should not be altered', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{Key: 'Namespace', Value: `analysis-${datetime}`}];
// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
// Check
expect(namespaceParam).toBe(resolvedInputParams[namespaceIndex].Value);
});
it('Static name that ends with a number sequence should be transformed to start with analysis- and end with number string that is unique and be unique between calls', async () => {
// Build
const resolvedInputParams = [{Key: 'Namespace', Value: 'staticname-2626262626'}];
const resolvedInputParams2 = [{Key: 'Namespace', Value: 'staticname-2626262626'}];
const datetime = Date.now();
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;
// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const { namespaceParam2, namespaceIndex2 } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);
// Check
expect(namespaceParam).not.toBe(namespaceParam2);
expect(namespaceParam).toBe(`analysis-${originalNamespace}-${datetime}`);
});

it('Static name that ends with a datetime string should be transformed to start with analysis-', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{Key: 'Namespace', Value: `staticname-${datetime}`}];
const originalNamespace = resolvedInputParams[0].Value;
// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
// Check
expect(namespaceParam).toBe(`analysis-${originalNamespace}`);
});

it('getNamespaceAndIndexIfNecessary does not change the original resolvedInputParams array when the namespace is static', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{Key: 'Namespace', Value: 'staticname'}];

// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);

// Check
expect(namespaceParam).not.toBe(resolvedInputParams[namespaceIndex]);
})
})
})
describe('LaunchProduct', () => {
const meta = {};
const input = {};

const lp = new LaunchProduct({
step: { config: {} },
workflowPayload: new WorkflowPayload({ meta, input, workflowInstance: { steps: [] } }),
});

describe('checkNamespace', () => {
it('Static name should be transformed to start with analysis- and end with number string and be unique between calls', async () => {
// Build
const resolvedInputParams = [{ Key: 'Namespace', Value: 'staticname' }];
const datetime = Date.now();
const resolvedInputParams2 = [{ Key: 'Namespace', Value: 'staticname' }];
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;

// Operate
const updatedParams = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const updatedParams2 = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);

// Check
expect(updatedParams.namespaceParam).not.toBe(updatedParams2.namespaceParam);
expect(updatedParams.namespaceParam).toBe(`analysis-${originalNamespace}-${datetime}`);
});

it('Static name that begins with analysis- should be transformed to end with number string and be unique between calls', async () => {
// Build
const resolvedInputParams = [{ Key: 'Namespace', Value: 'analysis-staticname' }];
const resolvedInputParams2 = [{ Key: 'Namespace', Value: 'analysis-staticname' }];
const datetime = Date.now();
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;

// Operate
const updatedParams = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const updatedParams2 = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);

// Check
expect(updatedParams.namespaceParam).not.toBe(updatedParams2.namespaceParam);
expect(updatedParams.namespaceParam).toBe(`${originalNamespace}-${datetime}`);
});

it('Dynamic name should not be altered', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{ Key: 'Namespace', Value: `analysis-${datetime}` }];

// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);

// Check
expect(namespaceParam).toBe(resolvedInputParams[namespaceIndex].Value);
});

it('Static name that ends with a number sequence should be transformed to start with analysis- and end with number string that is unique and be unique between calls', async () => {
// Build
const resolvedInputParams = [{ Key: 'Namespace', Value: 'staticname-2626262626' }];
const resolvedInputParams2 = [{ Key: 'Namespace', Value: 'staticname-2626262626' }];
const datetime = Date.now();
const datetime2 = Date.now() + 1;
const originalNamespace = resolvedInputParams[0].Value;

// Operate
const updatedParams = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);
const updatedParams2 = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams2, datetime2);

// Check
expect(updatedParams.namespaceParam).not.toBe(updatedParams2.namespaceParam);
expect(updatedParams.namespaceParam).toBe(`analysis-${originalNamespace}-${datetime}`);
});

it('Static name that ends with a datetime string should be transformed to start with analysis-', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{ Key: 'Namespace', Value: `staticname-${datetime}` }];
const originalNamespace = resolvedInputParams[0].Value;

// Operate
const { namespaceParam } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);

// Check
expect(namespaceParam).toBe(`analysis-${originalNamespace}`);
});

it('getNamespaceAndIndexIfNecessary does not change the original resolvedInputParams array when the namespace is static', async () => {
// Build
const datetime = Date.now();
const resolvedInputParams = [{ Key: 'Namespace', Value: 'staticname' }];

// Operate
const { namespaceParam, namespaceIndex } = lp.getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime);

// Check
expect(namespaceParam).not.toBe(resolvedInputParams[namespaceIndex]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class LaunchProduct extends StepBase {

// Read input params specified in the environment type configuration
// The params may include variable expressions, resolve the expressions by using the resolveVars
// By doing this resolution, we might overwrite the dynamic, unique value defined above with a static name
// By doing this resolution, we might overwrite the dynamic, unique value defined above with a static name
// that would not be unique between deployments of the same workspace configuration
const resolvedInputParams = await this.resolveVarExpressions(envTypeConfig.params, resolvedVars);
// Additional layer to check the namespace is valid and unique. If not, make new namespace from
Expand Down Expand Up @@ -365,26 +365,26 @@ class LaunchProduct extends StepBase {
}

/**
* Check if the resolved input parameter contains a static namespace param. If so, augments the namespace to begin with
* 'analysis-' for permissions purposes (if it does not already start with that) and to end with a unique datetime string
* Check if the resolved input parameter contains a static namespace param. If so, augments the namespace to begin with
* 'analysis-' for permissions purposes (if it does not already start with that) and to end with a unique datetime string
* so Cloudformation doesn't create duplicate stacks for separate workspaces.
*
* @param resolvedInputParams
* @param datetime
*
* @param resolvedInputParams
* @param datetime
* @returns {namespaceParam:string, namespaceIndex:string}
*/
getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime){
getNamespaceAndIndexIfNecessary(resolvedInputParams, datetime) {
const namespaceIndex = resolvedInputParams.findIndex(element => element.Key === 'Namespace');
let namespaceParam = resolvedInputParams[namespaceIndex].Value;

// Check to make sure the resolved namespace variable begins with 'analysis-' so our templates will allow it
if(!namespaceParam.startsWith('analysis-')){
namespaceParam = 'analysis-' + namespaceParam;
if (!namespaceParam.startsWith('analysis-')) {
namespaceParam = `analysis-${namespaceParam}`;
}

// Check to make sure the resolved namespace variable ends with a unique datetime string so it will be unique for each deployment of a configuration with a static namespace
if(namespaceParam.split('-').pop() !== datetime.toString()){
namespaceParam += '-' + datetime;
if (namespaceParam.split('-').pop() !== datetime.toString()) {
namespaceParam += `-${datetime}`;
}

return { namespaceParam, namespaceIndex };
Expand Down

0 comments on commit f81a04f

Please sign in to comment.