Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function waitForTestExecutionStart({
workflowName,
stackName,
bucket,
findExecutionFn,
findExecutionFnParams,
startTask,
maxWaitSeconds = maxWaitForStartedExecutionSecs
}) {
let timeWaitedSecs = 0;
/* eslint-disable no-await-in-loop */
while (timeWaitedSecs < maxWaitSeconds) {
await sleep(waitPeriodMs);
timeWaitedSecs += (waitPeriodMs / 1000);
const executions = await getExecutions(workflowName, stackName, bucket);
for (let executionCtr = 0; executionCtr < executions.length; executionCtr += 1) {
const execution = executions[executionCtr];
let taskInput = await lambdaStep.getStepInput(execution.executionArn, startTask);
if (taskInput) {
taskInput = await pullStepFunctionEvent(taskInput);
}
if (taskInput && findExecutionFn(taskInput, findExecutionFnParams)) {
return execution;
}
}
}
/* eslint-enable no-await-in-loop */
throw new Error('Never found started workflow.');
it('performs ECS Service Autoscaling', async () => {
// wait for the event messages
await sleep(180 * 1000);
const { cluster, service } = await getEcsClusterService(awsConfig.stackName, 'EcsTaskHelloWorldKesCma');
console.log('getEcsServiceEvents', cluster, service, startTime);
const serviceEvents = await getEcsServiceEvents(cluster, service, startTime);
expect(serviceEvents.length).toBeGreaterThan(2);
expect(serviceEvents.filter((event) => event.message.includes('has started 1 tasks')).length).toBeGreaterThanOrEqual(1);
expect(serviceEvents.filter((event) => event.message.includes('has stopped 1 running tasks')).length).toBeGreaterThanOrEqual(1);
});
});
async function getNewScalingActivity() {
const params = {
AutoScalingGroupName: autoScalingGroupName,
MaxRecords: 1
};
let activities = await autoscaling().describeScalingActivities(params).promise();
const startingActivity = activities.Activities[0];
let mostRecentActivity = Object.assign({}, startingActivity);
/* eslint-disable no-await-in-loop */
while (startingActivity.ActivityId === mostRecentActivity.ActivityId) {
activities = await autoscaling().describeScalingActivities(params).promise();
mostRecentActivity = activities.Activities[0];
console.log(`No new activity found. Sleeping for ${waitPeriod / 1000} seconds.`);
await sleep(waitPeriod);
}
/* eslint-enable no-await-in-loop */
return mostRecentActivity;
}
async function waitForAllTestSf(recordIdentifier, workflowName, maxWaitTimeSecs, numExecutions, firstStep) {
const config = await loadConfig();
let timeWaitedSecs = 0;
const workflowExecutions = [];
const startTime = moment();
/* eslint-disable no-await-in-loop */
while (timeWaitedSecs < maxWaitTimeSecs && workflowExecutions.length < numExecutions) {
await sleep(waitPeriodMs);
timeWaitedSecs = (moment.duration(moment().diff(startTime)).asSeconds());
const executions = await getExecutions(workflowName, config.stackName, config.bucket, maxExecutionResults);
// Search all recent executions for target recordIdentifier
for (let ctr = 0; ctr < executions.length; ctr += 1) {
const execution = executions[ctr];
if (!workflowExecutions.find((e) => e.executionArn === execution.executionArn)) {
const taskInput = await lambdaStep.getStepInput(execution.executionArn, firstStep);
if (taskInput !== null && taskInput.payload.identifier === recordIdentifier) {
workflowExecutions.push(execution);
if (workflowExecutions.length === numExecutions) {
break;
}
}
}
}
}
async function waitForAsyncOperationStatus({
TableName,
id,
status,
retries = 10
}) {
const { Item } = await dynamodb().getItem({
TableName,
Key: { id: { S: id } }
}).promise();
if (Item.status.S === status || retries <= 0) return Item;
await sleep(2000);
return waitForAsyncOperationStatus({
TableName,
id,
status,
retries: retries - 1
});
}
const waitForCmrToBeConsistent = () => sleep(ONE_SECOND);
async function waitForActiveStream(streamName, initialDelaySecs = 10, maxRetries = 10) {
const kinesis = new Kinesis({ apiVersion: '2013-12-02', region: getRegion() });
let streamStatus = 'UNDEFINED';
let stream;
const displayName = streamName.split('-').pop();
await sleep(initialDelaySecs * 1000);
return pRetry(
async () => {
stream = await kinesis.describeStream({ StreamName: streamName }).promise();
streamStatus = stream.StreamDescription.StreamStatus;
if (streamStatus === 'ACTIVE') return streamStatus;
throw new Error(`Stream never became active: status: ${streamStatus}: ${streamName}`);
},
{
minTimeout: 3 * 1000,
factor: 1.45,
retries: maxRetries,
onFailedAttempt: (error) => {
console.log(`Stream in state ${streamStatus} retrying. ${error.attemptsLeft} remain on ${displayName} at ${new Date().toString()}`);
}
}
it('can handle the load (has the expected number of running tasks)', async () => {
sleep(5000);
const stats = await getClusterStats(config.stackName);
console.log(`stats are ${JSON.stringify(stats, null, 2)}\n`);
console.log(`numExecutions ${numExecutions}\n`);
console.log(`numActivityTasks ${numActivityTasks}\n`);
const runningEC2TasksCount = parseInt(find(stats, ['name', 'runningEC2TasksCount']).value);
const pendingEC2TasksCount = parseInt(find(stats, ['name', 'pendingEC2TasksCount']).value);
expect(runningEC2TasksCount + pendingEC2TasksCount).toEqual(numExecutions + numActivityTasks);
});
async redeployApiGateWay(name, restApiId, stageName) {
const waitTime = 20;
if (restApiId) {
try {
const apigateway = new this.AWS.APIGateway();
await apigateway.createDeployment({ restApiId, stageName }).promise();
console.log(`${name} endpoints with the id ${restApiId} redeployed.`);
} catch (e) {
if (e.message && e.message.includes('Too Many Requests')) {
console.log(
`Redeploying ${restApiId} was throttled. `
+ `Another attempt will be made in ${waitTime} seconds`
);
await sleep(waitTime * 1000);
return this.redeployApiGateWay(name, restApiId, stageName);
}
throw e;
}
}
return true;
}
async function getRecords(shardIterator, records = []) {
const kinesis = new Kinesis({ apiVersion: '2013-12-02', region: getRegion() });
const data = await kinesis.getRecords({ ShardIterator: shardIterator }).promise();
records.push(...data.Records);
if ((data.NextShardIterator !== null) && (data.MillisBehindLatest > 0)) {
await sleep(waitPeriodMs);
return getRecords(data.NextShardIterator, records);
}
return records;
}