Skip to content

Commit

Permalink
refactor: Upgrade to @serverless/test in v10
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Feb 14, 2022
1 parent b47cdb4 commit 4201b30
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 108 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -78,7 +78,7 @@
"devDependencies": {
"@commitlint/cli": "^12.1.4",
"@serverless/eslint-config": "^4.0.0",
"@serverless/test": "^9.0.0",
"@serverless/test": "^10.0.1",
"adm-zip": "^0.5.9",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
Expand All @@ -92,7 +92,7 @@
"lint-staged": "^10.5.4",
"log": "^6.3.1",
"log-node": "^8.0.3",
"mocha": "^8.4.0",
"mocha": "^9.2.0",
"mock-require": "^3.0.3",
"ncjsm": "^4.3.0",
"nyc": "^15.1.0",
Expand Down
7 changes: 4 additions & 3 deletions test/integration-basic.test.js
Expand Up @@ -10,6 +10,7 @@ const spawn = require('child-process-ext/spawn');
const resolveAwsEnv = require('@serverless/test/resolve-aws-env');
const hasFailed = require('@serverless/test/has-failed');
const awsRequest = require('@serverless/test/aws-request');
const CloudFormationService = require('aws-sdk').CloudFormation;
const { getTmpDirPath } = require('./utils/fs');

const serverlessExec = require('./serverless-binary');
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('Service Lifecyle Integration Test', function () {

after(async () => {
try {
await awsRequest('CloudFormation', 'describeStacks', { StackName });
await awsRequest(CloudFormationService, 'describeStacks', { StackName });
} catch (error) {
if (error.message.indexOf('does not exist') > -1) return;
throw error;
Expand All @@ -64,7 +65,7 @@ describe('Service Lifecyle Integration Test', function () {
it('should deploy service to aws', async () => {
await spawn(serverlessExec, ['deploy'], { cwd: tmpDir, env });

const d = await awsRequest('CloudFormation', 'describeStacks', { StackName });
const d = await awsRequest(CloudFormationService, 'describeStacks', { StackName });
expect(d.Stacks[0].StackStatus).to.be.equal('UPDATE_COMPLETE');
});

Expand Down Expand Up @@ -135,7 +136,7 @@ describe('Service Lifecyle Integration Test', function () {

const d = await (async () => {
try {
return await awsRequest('CloudFormation', 'describeStacks', { StackName });
return await awsRequest(CloudFormationService, 'describeStacks', { StackName });
} catch (error) {
if (error.message.indexOf('does not exist') > -1) return null;
throw error;
Expand Down
5 changes: 4 additions & 1 deletion test/integration/aws/api-gateway-external.test.js
Expand Up @@ -3,6 +3,7 @@
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const awsRequest = require('@serverless/test/aws-request');
const CloudFormationService = require('aws-sdk').CloudFormation;
const fixtures = require('../../fixtures/programmatic');

const { deployService, removeService, fetch } = require('../../utils/integration');
Expand All @@ -19,7 +20,9 @@ describe('AWS - API Gateway with External REST API Integration Test', function (
const stage = 'dev';

const resolveEndpoint = async () => {
const result = await awsRequest('CloudFormation', 'describeStacks', { StackName: stackName });
const result = await awsRequest(CloudFormationService, 'describeStacks', {
StackName: stackName,
});
const endpointOutput = result.Stacks[0].Outputs.find(
(output) => output.OutputKey === 'ServiceEndpoint'
).OutputValue;
Expand Down
5 changes: 4 additions & 1 deletion test/integration/aws/api-gateway.test.js
Expand Up @@ -3,6 +3,7 @@
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const awsRequest = require('@serverless/test/aws-request');
const CloudFormationService = require('aws-sdk').CloudFormation;
const fixtures = require('../../fixtures/programmatic');

const { confirmCloudWatchLogs } = require('../../utils/misc');
Expand All @@ -20,7 +21,9 @@ describe('AWS - API Gateway Integration Test', function () {
const stage = 'dev';

const resolveEndpoint = async () => {
const result = await awsRequest('CloudFormation', 'describeStacks', { StackName: stackName });
const result = await awsRequest(CloudFormationService, 'describeStacks', {
StackName: stackName,
});
const endpointOutput = result.Stacks[0].Outputs.find(
(output) => output.OutputKey === 'ServiceEndpoint'
).OutputValue;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/aws/custom-deployment-bucket.test.js
Expand Up @@ -4,6 +4,7 @@ const uuid = require('uuid');
const { expect } = require('chai');
const fixtures = require('../../fixtures/programmatic');
const awsRequest = require('@serverless/test/aws-request');
const S3Service = require('aws-sdk').S3;
const { deployService, removeService } = require('../../utils/integration');
const { createBucket, deleteBucket } = require('../../utils/s3');

Expand All @@ -23,7 +24,7 @@ describe('Base AWS provider test', function () {

it('should deploy in the configured aws bucket', async () => {
// we cannot deploy an empty fixture like aws so we go for a small one
const res = await awsRequest('S3', 'listObjects', { Bucket: bucketName });
const res = await awsRequest(S3Service, 'listObjects', { Bucket: bucketName });
expect(
res.Contents.filter((obj) => /compiled-cloudformation-template.json$/.test(obj.Key)).length
).to.equal(1);
Expand Down
5 changes: 3 additions & 2 deletions test/integration/aws/function.test.js
Expand Up @@ -2,6 +2,7 @@

const { expect } = require('chai');
const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const fixtures = require('../../fixtures/programmatic');
const { confirmCloudWatchLogs } = require('../../utils/misc');

Expand Down Expand Up @@ -46,7 +47,7 @@ describe('test/integration/aws/function.test.js', function () {
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-target`,
async () => {
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-trigger`,
InvocationType: 'Event',
});
Expand All @@ -60,7 +61,7 @@ describe('test/integration/aws/function.test.js', function () {
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-arch`,
async () => {
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-arch`,
InvocationType: 'Event',
});
Expand Down
26 changes: 17 additions & 9 deletions test/integration/aws/http-api.test.js
Expand Up @@ -3,6 +3,9 @@
const { expect } = require('chai');
const log = require('log').get('serverless:test');
const awsRequest = require('@serverless/test/aws-request');
const CloudFormationService = require('aws-sdk').CloudFormation;
const CognitoIdentityServiceProviderService = require('aws-sdk').CognitoIdentityServiceProvider;
const ApiGatewayV2Service = require('aws-sdk').ApiGatewayV2;
const fixtures = require('../../fixtures/programmatic');
const { confirmCloudWatchLogs } = require('../../utils/misc');

Expand All @@ -16,7 +19,9 @@ describe('HTTP API Integration Test', function () {
const stage = 'dev';

const resolveEndpoint = async () => {
const result = await awsRequest('CloudFormation', 'describeStacks', { StackName: stackName });
const result = await awsRequest(CloudFormationService, 'describeStacks', {
StackName: stackName,
});
const endpointOutput = result.Stacks[0].Outputs.find(
(output) => output.OutputKey === 'HttpApiUrl'
).OutputValue;
Expand All @@ -31,22 +36,22 @@ describe('HTTP API Integration Test', function () {

before(async () => {
poolId = (
await awsRequest('CognitoIdentityServiceProvider', 'createUserPool', {
await awsRequest(CognitoIdentityServiceProviderService, 'createUserPool', {
PoolName: `test-http-api-${process.hrtime()[1]}`,
})
).UserPool.Id;
[clientId] = await Promise.all([
awsRequest('CognitoIdentityServiceProvider', 'createUserPoolClient', {
awsRequest(CognitoIdentityServiceProviderService, 'createUserPoolClient', {
ClientName: 'test-http-api',
UserPoolId: poolId,
ExplicitAuthFlows: ['ALLOW_USER_PASSWORD_AUTH', 'ALLOW_REFRESH_TOKEN_AUTH'],
PreventUserExistenceErrors: 'ENABLED',
}).then((result) => result.UserPoolClient.ClientId),
awsRequest('CognitoIdentityServiceProvider', 'adminCreateUser', {
awsRequest(CognitoIdentityServiceProviderService, 'adminCreateUser', {
UserPoolId: poolId,
Username: userName,
}).then(() =>
awsRequest('CognitoIdentityServiceProvider', 'adminSetUserPassword', {
awsRequest(CognitoIdentityServiceProviderService, 'adminSetUserPassword', {
UserPoolId: poolId,
Username: userName,
Password: userPassword,
Expand Down Expand Up @@ -127,7 +132,9 @@ describe('HTTP API Integration Test', function () {
});

after(async () => {
await awsRequest('CognitoIdentityServiceProvider', 'deleteUserPool', { UserPoolId: poolId });
await awsRequest(CognitoIdentityServiceProviderService, 'deleteUserPool', {
UserPoolId: poolId,
});
if (!serviceDir) return;
await removeService(serviceDir);
});
Expand Down Expand Up @@ -189,7 +196,7 @@ describe('HTTP API Integration Test', function () {
expect(responseUnauthorized.status).to.equal(401);

const token = (
await awsRequest('CognitoIdentityServiceProvider', 'initiateAuth', {
await awsRequest(CognitoIdentityServiceProviderService, 'initiateAuth', {
AuthFlow: 'USER_PASSWORD_AUTH',
AuthParameters: { USERNAME: userName, PASSWORD: userPassword },
ClientId: clientId,
Expand Down Expand Up @@ -295,11 +302,12 @@ describe('HTTP API Integration Test', function () {
const exportServiceName = exportServiceData.serviceConfig.service;
await deployService(exportServicePath);
const httpApiId = (
await awsRequest('CloudFormation', 'describeStacks', {
await awsRequest(CloudFormationService, 'describeStacks', {
StackName: `${exportServiceName}-${stage}`,
})
).Stacks[0].Outputs[0].OutputValue;
endpoint = (await awsRequest('ApiGatewayV2', 'getApi', { ApiId: httpApiId })).ApiEndpoint;
endpoint = (await awsRequest(ApiGatewayV2Service, 'getApi', { ApiId: httpApiId }))
.ApiEndpoint;

const serviceData = await fixtures.setup('http-api', {
configExt: {
Expand Down
9 changes: 6 additions & 3 deletions test/integration/aws/infra-dependent/active-mq.test.js
Expand Up @@ -11,6 +11,9 @@ const {
} = require('../../../utils/cloudformation');

const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const MQService = require('aws-sdk').MQ;
const SecretsManagerService = require('aws-sdk').SecretsManager;
const crypto = require('crypto');
const { deployService, removeService } = require('../../../utils/integration');

Expand All @@ -31,14 +34,14 @@ describe.skip('AWS - Active MQ Integration Test', function () {
const outputMap = await getDependencyStackOutputMap();

log.notice('Getting Active MQ Credentials ARN');
const getSecretValueResponse = await awsRequest('SecretsManager', 'getSecretValue', {
const getSecretValueResponse = await awsRequest(SecretsManagerService, 'getSecretValue', {
SecretId: SHARED_INFRA_TESTS_ACTIVE_MQ_CREDENTIALS_NAME,
});
const { username: mqUsername, password: mqPassword } = JSON.parse(
getSecretValueResponse.SecretString
);

const describeBrokerResponse = await awsRequest('MQ', 'describeBroker', {
const describeBrokerResponse = await awsRequest(MQService, 'describeBroker', {
BrokerId: outputMap.get('ActiveMQBrokerId'),
});
const stompEndpoint = describeBrokerResponse.BrokerInstances[0].Endpoints.find((endpoint) =>
Expand Down Expand Up @@ -95,7 +98,7 @@ describe.skip('AWS - Active MQ Integration Test', function () {
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
async () =>
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-producer`,
InvocationType: 'RequestResponse',
}),
Expand Down
Expand Up @@ -5,6 +5,7 @@ const log = require('log').get('serverless:test');
const fixtures = require('../../../fixtures/programmatic');

const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const crypto = require('crypto');
const { deployService, removeService } = require('../../../utils/integration');
const {
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('AWS - FileSystemConfig Integration Test', function () {

it('should be able to write to efs and read from it in a separate function', async function self() {
try {
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-writer`,
InvocationType: 'RequestResponse',
});
Expand All @@ -85,7 +86,7 @@ describe('AWS - FileSystemConfig Integration Test', function () {
throw e;
}

const readerResult = await awsRequest('Lambda', 'invoke', {
const readerResult = await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-reader`,
InvocationType: 'RequestResponse',
});
Expand Down
6 changes: 4 additions & 2 deletions test/integration/aws/infra-dependent/msk.test.js
Expand Up @@ -10,6 +10,8 @@ const {
} = require('../../../utils/cloudformation');

const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const KafkaService = require('aws-sdk').Kafka;
const crypto = require('crypto');
const { deployService, removeService } = require('../../../utils/integration');

Expand All @@ -30,7 +32,7 @@ describe('AWS - MSK Integration Test', function () {
const outputMap = await getDependencyStackOutputMap();

log.notice('Getting MSK Boostrap Brokers URLs...');
const getBootstrapBrokersResponse = await awsRequest('Kafka', 'getBootstrapBrokers', {
const getBootstrapBrokersResponse = await awsRequest(KafkaService, 'getBootstrapBrokers', {
ClusterArn: outputMap.get('MSKCluster'),
});
const brokerUrls = getBootstrapBrokersResponse.BootstrapBrokerStringTls;
Expand Down Expand Up @@ -84,7 +86,7 @@ describe('AWS - MSK Integration Test', function () {
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
async () =>
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-producer`,
InvocationType: 'RequestResponse',
}),
Expand Down
9 changes: 6 additions & 3 deletions test/integration/aws/infra-dependent/rabbit-mq.test.js
Expand Up @@ -11,6 +11,9 @@ const {
} = require('../../../utils/cloudformation');

const awsRequest = require('@serverless/test/aws-request');
const LambdaService = require('aws-sdk').Lambda;
const MQService = require('aws-sdk').MQ;
const SecretsManagerService = require('aws-sdk').SecretsManager;
const crypto = require('crypto');
const { deployService, removeService } = require('../../../utils/integration');

Expand All @@ -31,14 +34,14 @@ describe.skip('AWS - RabbitMQ Integration Test', function () {
const outputMap = await getDependencyStackOutputMap();

log.notice('Getting RabbitMQ Credentials ARN');
const getSecretValueResponse = await awsRequest('SecretsManager', 'getSecretValue', {
const getSecretValueResponse = await awsRequest(SecretsManagerService, 'getSecretValue', {
SecretId: SHARED_INFRA_TESTS_RABBITMQ_CREDENTIALS_NAME,
});
const { username: mqUsername, password: mqPassword } = JSON.parse(
getSecretValueResponse.SecretString
);

const describeBrokerResponse = await awsRequest('MQ', 'describeBroker', {
const describeBrokerResponse = await awsRequest(MQService, 'describeBroker', {
BrokerId: outputMap.get('RabbitMQBrokerId'),
});
const amqpEndpoint = describeBrokerResponse.BrokerInstances[0].Endpoints.find((endpoint) =>
Expand Down Expand Up @@ -99,7 +102,7 @@ describe.skip('AWS - RabbitMQ Integration Test', function () {
const events = await confirmCloudWatchLogs(
`/aws/lambda/${stackName}-${functionName}`,
async () =>
await awsRequest('Lambda', 'invoke', {
await awsRequest(LambdaService, 'invoke', {
FunctionName: `${stackName}-producer`,
InvocationType: 'RequestResponse',
}),
Expand Down

0 comments on commit 4201b30

Please sign in to comment.