Skip to content

Commit

Permalink
fix: updated awsRegion setting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Chih-Ying committed Feb 12, 2020
1 parent 8a192a1 commit e2807aa
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 24 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<<<<<<< Updated upstream
### [0.7.1](https://github.com/alexa-labs/ask-cli/compare/v0.7.0...v0.7.1) (2020-02-05)


Expand All @@ -15,6 +16,14 @@ All notable changes to this project will be documented in this file. See [standa
### Features

* add "ask api get-metrics" and "ask api export-package" commands ([eb94433](https://github.com/alexa-labs/ask-cli/commit/eb94433911a9f040db6f91afef7994345601c487))
=======
### [0.6.2](https://github.com/alexa-labs/ask-cli/compare/v0.6.1...v0.6.2) (2020-02-12)


### Bug Fixes

* updated awsRegion setting logic ([07b5fa9](https://github.com/alexa-labs/ask-cli/commit/07b5fa9e17b2d4cdc4f8967d0f3bb913c4bca24c))
>>>>>>> Stashed changes
### [0.6.1](https://github.com/alexa-labs/ask-cli/compare/v0.6.0...v0.6.1) (2019-12-25)

Expand Down
4 changes: 3 additions & 1 deletion lib/builtins/deploy-delegates/cfn-deployer/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module.exports = {
};

function getAwsInformation(awsProfile, alexaRegion, userConfig, deployState) {
const awsRegion = R.view(R.lensPath(['regionalOverrides', alexaRegion, 'awsRegion']), userConfig) || alexaAwsRegionMap[alexaRegion];
let awsRegion = alexaRegion === 'default' ? userConfig.awsRegion
: R.view(R.lensPath(['regionalOverrides', alexaRegion, 'awsRegion']), userConfig);
awsRegion = awsRegion || alexaAwsRegionMap[alexaRegion];
if (!stringUtils.isNonBlankString(awsRegion)) {
throw `Unsupported Alexa region: ${alexaRegion}. Please check your region name or use "regionalOverrides" to specify AWS region.`;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/builtins/deploy-delegates/lambda-deployer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ function invoke(reporter, options, callback) {
const { profile, alexaRegion, skillId, skillName, code, userConfig, deployState } = options;
const awsProfile = awsUtil.getAWSProfile(profile);
if (!stringUtils.isNonBlankString(awsProfile)) {
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your porfile.`);
return callback(`Profile [${profile}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your profile.`);
}
let currentRegionDeployState = deployState[alexaRegion];
if (!currentRegionDeployState) {
currentRegionDeployState = {};
deployState[alexaRegion] = currentRegionDeployState;
}
const awsRegion = R.view(R.lensPath(['regionalOverrides', alexaRegion, 'awsRegion']), userConfig) || alexaAwsRegionMap[alexaRegion];
let awsRegion = alexaRegion === 'default' ? userConfig.awsRegion
: R.view(R.lensPath(['regionalOverrides', alexaRegion, 'awsRegion']), userConfig);
awsRegion = awsRegion || alexaAwsRegionMap[alexaRegion];
if (!stringUtils.isNonBlankString(awsRegion)) {
return callback(`Unsupported Alexa region: ${alexaRegion}. Please check your region name or use "regionalOverrides" to specify AWS region.`);
}
Expand Down
162 changes: 141 additions & 21 deletions test/unit/builtins/lambda-deployer/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ const lambdaDeployer = require('@src/builtins/deploy-delegates/lambda-deployer/i

describe('Builtins test - lambda-deployer index.js test', () => {
const TEST_PROFILE = 'default'; // test file uses 'default' profile
const TEST_ALEXA_REGION = 'default';
const TEST_ALEXA_REGION_DEFAULT = 'default';
const TEST_AWS_REGION_DEFAULT = 'us-east-1';
const TEST_SKILL_NAME = 'skill_name';
const TEST_IAM_ROLE_ARN = 'IAM role arn';

describe('# test class method: bootstrap', () => {
afterEach(() => {
Expand Down Expand Up @@ -39,14 +41,15 @@ describe('Builtins test - lambda-deployer index.js test', () => {

describe('# test case method: invoke', () => {
const REPORTER = {};
const NULL_PROFILE = null;
const TEST_OPTIONS = {
profile: TEST_PROFILE,
alexaRegion: TEST_ALEXA_REGION,
alexaRegion: TEST_ALEXA_REGION_DEFAULT,
skillId: '',
skillName: TEST_SKILL_NAME,
code: {},
userConfig: {},
userConfig: {
awsRegion: TEST_AWS_REGION_DEFAULT
},
deployState: {}
};
const TEST_VALIDATED_DEPLOY_STATE = {
Expand All @@ -60,10 +63,10 @@ describe('Builtins test - lambda-deployer index.js test', () => {
it('| profile is not set, expect an error return', (done) => {
// setup
const TEST_OPTIONS_WITHOUT_PROFILE = {
profile: NULL_PROFILE,
profile: null,
};
const TEST_ERROR = `Profile [${NULL_PROFILE}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your porfile.`;
sinon.stub(awsUtil, 'getAWSProfile').withArgs(NULL_PROFILE).returns(NULL_PROFILE);
const TEST_ERROR = `Profile [${null}] doesn't have AWS profile linked to it. Please run "ask init" to re-configure your profile.`;
sinon.stub(awsUtil, 'getAWSProfile').withArgs(null).returns(null);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS_WITHOUT_PROFILE, (err) => {
// verify
Expand All @@ -72,7 +75,7 @@ describe('Builtins test - lambda-deployer index.js test', () => {
});
});

it('| profile is set, awsRegion is blank, expect an error return', (done) => {
it('| profile is set, alexaRegion is not set correctly, expect an error return', (done) => {
// setup
const TEST_OPTIONS_WITHOUT_REGION = {
profile: TEST_PROFILE,
Expand All @@ -93,7 +96,7 @@ describe('Builtins test - lambda-deployer index.js test', () => {
});
});

it('| profile is set, validate Lambda deploy state fails, expect an error return', (done) => {
it('| alexaRegion is set correctly, validate Lambda deploy state fails, expect an error return', (done) => {
// setup
const TEST_ERROR = 'validateLambdaDeployState error message';
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
Expand All @@ -106,7 +109,7 @@ describe('Builtins test - lambda-deployer index.js test', () => {
});
});

it('| profile is set, validate Lambda deploy state passes, deploy IAM role fails, expect an error return', (done) => {
it('| validate Lambda deploy state passes, deploy IAM role fails, expect an error return', (done) => {
// setup
const TEST_ERROR = 'IAMRole error message';
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
Expand All @@ -122,50 +125,167 @@ describe('Builtins test - lambda-deployer index.js test', () => {

it('| deploy IAM role passes, deploy Lambda Function fails, expect IAM role arn and an error message return', (done) => {
// setup
const IAM_ROLE_ARN = 'IAM role arn';
const TEST_ERROR = 'LambdaFunction error message';
const TEST_ERROR_MESSAGE_RESPONSE = `The lambda deploy failed for Alexa region "default": ${TEST_ERROR}`;
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, IAM_ROLE_ARN);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, TEST_ERROR);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS, (err, data) => {
// verify
expect(data.reasons).to.be.an.instanceOf(Array);
expect(data.reasons.length).equal(1);
expect(data.message).equal(TEST_ERROR_MESSAGE_RESPONSE);
expect(data.deployState.iamRole).equal(IAM_ROLE_ARN);
expect(data.deployState.iamRole).equal(TEST_IAM_ROLE_ARN);
done();
});
});

it('| deploy IAM role and Lambda Function pass, expect correct data return', (done) => {
// setup
const IAM_ROLE_ARN = 'iam_role_arn';
const LAMBDA_ARN = 'lambda_arn';
const LAST_MODIFIED = 'last_modified';
const REVISIONID = '1';
const TEST_SUCCESS_RESPONSE = `The lambda deploy succeeded for Alexa region "${TEST_ALEXA_REGION}" \
const REVISION_ID = '1';
const TEST_SUCCESS_RESPONSE = `The lambda deploy succeeded for Alexa region "${TEST_ALEXA_REGION_DEFAULT}" \
with output Lambda ARN: ${LAMBDA_ARN}.`;
const LAMDBA_RESUALT = {
const LAMDBA_RESULT = {
arn: LAMBDA_ARN,
lastModified: LAST_MODIFIED,
revisionId: REVISIONID
revisionId: REVISION_ID
};
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESUALT);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESULT);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS, (err, res) => {
// verify
expect(res.endpoint.uri).equal(LAMBDA_ARN);
expect(res.deployState.iamRole).equal(IAM_ROLE_ARN);
expect(res.deployState.iamRole).equal(TEST_IAM_ROLE_ARN);
expect(res.message).equal(TEST_SUCCESS_RESPONSE);
expect(err).equal(null);
done();
});
});

it('| alexaRegion is default, userConfig awsRegion is set, expect awsRegion is retrieved correctly.', (done) => {
// setup
const USER_CONFIG_AWS_REGION = 'sa-east-1';
const TEST_OPTIONS_WITHOUT_AWS_REGION = {
profile: TEST_PROFILE,
alexaRegion: TEST_ALEXA_REGION_DEFAULT,
skillId: '',
skillName: TEST_SKILL_NAME,
code: {},
userConfig: {
awsRegion: USER_CONFIG_AWS_REGION
},
deployState: {}
};
const LAMDBA_RESULT = {};
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESULT);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS_WITHOUT_AWS_REGION, (err) => {
// verify
expect(err).equal(null);
expect(helper.validateLambdaDeployState.args[0][2]).equal(USER_CONFIG_AWS_REGION);
expect(helper.deployIAMRole.args[0][4]).equal(USER_CONFIG_AWS_REGION);
done();
});
});

it('| alexaRegion is default, userConfig awsRegion is NOT set, expect awsRegion is set based on Alexa and AWS region map.', (done) => {
// setup
const MAPPING_ALEXA_DEFAULT_AWS_REGION = 'us-east-1';
const TEST_OPTIONS_WITHOUT_AWS_REGION = {
profile: TEST_PROFILE,
alexaRegion: TEST_ALEXA_REGION_DEFAULT,
skillId: '',
skillName: TEST_SKILL_NAME,
code: {},
userConfig: {},
deployState: {}
};
const LAMDBA_RESULT = {};
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESULT);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS_WITHOUT_AWS_REGION, (err) => {
// verify
expect(err).equal(null);
expect(helper.validateLambdaDeployState.args[0][2]).equal(MAPPING_ALEXA_DEFAULT_AWS_REGION);
expect(helper.deployIAMRole.args[0][4]).equal(MAPPING_ALEXA_DEFAULT_AWS_REGION);
done();
});
});

it('| alexaRegion is not default, userConfig regionalOverrides awsRegion is set, expect awsRegion is retrieved correctly.', (done) => {
// setup
const TEST_ALEXA_REGION_EU = 'EU';
const USER_CONFIG_AWS_REGION = 'eu-west-2';
const TEST_OPTIONS_WITHOUT_EU_REGION = {
profile: TEST_PROFILE,
alexaRegion: TEST_ALEXA_REGION_EU,
skillId: '',
skillName: TEST_SKILL_NAME,
code: {},
userConfig: {
regionalOverrides: {
EU: {
awsRegion: USER_CONFIG_AWS_REGION
}
}
},
deployState: {}
};
const LAMDBA_RESULT = {};
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESULT);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS_WITHOUT_EU_REGION, (err) => {
// verify
expect(err).equal(null);
expect(helper.validateLambdaDeployState.args[0][2]).equal(USER_CONFIG_AWS_REGION);
expect(helper.deployIAMRole.args[0][4]).equal(USER_CONFIG_AWS_REGION);
done();
});
});

it('| alexaRegion is not default, userConfig regionalOverrides awsRegion is NOT set, '
+ 'expect awsRegion is set based on Alexa and AWS region map.', (done) => {
// setup
const TEST_ALEXA_REGION_EU = 'EU';
const MAPPING_ALEXA_EU_AWS_REGION = 'eu-west-1';
const TEST_OPTIONS_WITHOUT_AWS_REGION = {
profile: TEST_PROFILE,
alexaRegion: TEST_ALEXA_REGION_EU,
skillId: '',
skillName: TEST_SKILL_NAME,
code: {},
userConfig: {},
deployState: {}
};
const LAMDBA_RESULT = {};
sinon.stub(awsUtil, 'getAWSProfile').withArgs(TEST_PROFILE).returns(TEST_PROFILE);
sinon.stub(helper, 'validateLambdaDeployState').callsArgWith(4, null, TEST_VALIDATED_DEPLOY_STATE);
sinon.stub(helper, 'deployIAMRole').callsArgWith(6, null, TEST_IAM_ROLE_ARN);
sinon.stub(helper, 'deployLambdaFunction').callsArgWith(2, null, LAMDBA_RESULT);
// call
lambdaDeployer.invoke(REPORTER, TEST_OPTIONS_WITHOUT_AWS_REGION, (err) => {
// verify
expect(err).equal(null);
expect(helper.validateLambdaDeployState.args[0][2]).equal(MAPPING_ALEXA_EU_AWS_REGION);
expect(helper.deployIAMRole.args[0][4]).equal(MAPPING_ALEXA_EU_AWS_REGION);
done();
});
});
});
});

0 comments on commit e2807aa

Please sign in to comment.