Skip to content

Commit

Permalink
Merge pull request #35 from byu-oit/dev
Browse files Browse the repository at this point in the history
dev -> master
  • Loading branch information
Mark Roth authored Jun 18, 2021
2 parents bcf619e + 8de4fdc commit ded51a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 61 deletions.
Binary file modified lambda/dist/function.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion lambda/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lambda/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-test-lambda",
"version": "3.0.1",
"version": "3.0.2",
"description": "Lambda function that runs postman collection tests",
"repository": {
"type": "git",
Expand Down
131 changes: 72 additions & 59 deletions lambda/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const os = require('os')
const { sep } = require('path')
const newman = require('newman')
const AWS = require('aws-sdk')
const path = require('path')
const codedeploy = new AWS.CodeDeploy({ apiVersion: '2014-10-06', region: 'us-west-2' })
const s3 = new AWS.S3({ apiVersion: '2014-10-06', region: 'us-west-2' })

Expand All @@ -28,70 +29,53 @@ exports.handler = async function (event, context) {
// store the error so that we can update codedeploy lifecycle if there are any errors including errors from downloading files
let error

const postmanCollections = process.env.POSTMAN_COLLECTIONS
if (!postmanCollections) {
error = new Error('Env variable POSTMAN_COLLECTIONS is required')
} else {
const postmanList = JSON.parse(postmanCollections)
const promises = [timer]
for (const each of postmanList) {
if (each.collection.includes('.json')) {
promises.push(downloadFileFromBucket(each.collection))
each.collection = `${tmpDir}${sep}${each.collection}`
} else {
promises.push(downloadFileFromPostman('collection', each.collection))
each.collection = `${tmpDir}${sep}${each.collection}.json`
}
if (each.environment) { // environment can be null
if (each.environment.includes('.json')) {
promises.push(downloadFileFromBucket(each.environment))
each.environment = `${tmpDir}${sep}${each.environment}`
try {
const postmanCollections = process.env.POSTMAN_COLLECTIONS
if (!postmanCollections) {
error = new Error('Env variable POSTMAN_COLLECTIONS is required')
} else {
const postmanList = JSON.parse(postmanCollections)
const promises = [timer]
for (const each of postmanList) {
if (each.collection.includes('.json')) {
promises.push(downloadFileFromBucket(each.collection))
each.collection = `${tmpDir}${sep}${path.basename(each.collection)}`
} else {
promises.push(downloadFileFromPostman('environment', each.environment))
each.environment = `${tmpDir}${sep}${each.environment}.json`
promises.push(downloadFileFromPostman('collection', each.collection))
each.collection = `${tmpDir}${sep}${path.basename(each.collection)}.json`
}
if (each.environment) { // environment can be null
if (each.environment.includes('.json')) {
promises.push(downloadFileFromBucket(each.environment))
each.environment = `${tmpDir}${sep}${path.basename(each.environment)}`
} else {
promises.push(downloadFileFromPostman('environment', each.environment))
each.environment = `${tmpDir}${sep}${path.basename(each.environment)}.json`
}
}
}
}
// make sure all files are downloaded and we wait for 10 seconds before executing postman tests
await Promise.all(promises)

console.log('starting postman tests ...')
if (!error) {
// no need to run tests if files weren't downloaded correctly
for (const each of postmanList) {
if (!error) {
// don't run later collections if previous one errored out
await runTest(each.collection, each.environment).catch(err => {
error = err
})
// make sure all files are downloaded and we wait for 10 seconds before executing postman tests
await Promise.all(promises)

console.log('starting postman tests ...')
if (!error) {
// no need to run tests if files weren't downloaded correctly
for (const each of postmanList) {
if (!error) {
// don't run later collections if previous one errored out
await runTest(each.collection, each.environment).catch(err => {
error = err
})
}
}
}
console.log('postman tests finished')
}
console.log('postman tests finished')
}

if (deploymentId) {
console.log('starting to update CodeDeploy lifecycle event hook status...')
const params = {
deploymentId: event.DeploymentId,
lifecycleEventHookExecutionId: event.LifecycleEventHookExecutionId,
status: error ? 'Failed' : 'Succeeded'
}
try {
const data = await codedeploy.putLifecycleEventHookExecutionStatus(params).promise()
console.log(data)
} catch (err) {
console.log(err, err.stack)
throw err
}
} else if (combinedRunner) {
return {
passed: !error
}
} else {
console.log('No deployment ID found in the event. Skipping update to CodeDeploy lifecycle hook...')
await updateRunner(deploymentId, combinedRunner, event, error)
} catch (e) {
await updateRunner(deploymentId, combinedRunner, event, true)
throw e
}

if (error) throw error // Cause the lambda to "fail"
}

Expand All @@ -116,6 +100,9 @@ async function downloadFileFromPostman (type, id) {
}

async function downloadFileFromBucket (key) {
// Stripping relative path off of key.
key = path.basename(key)

const filename = `${tmpDir}${sep}${key}`
console.log(`started download for ${key} from s3 bucket`)

Expand All @@ -137,7 +124,9 @@ async function downloadFileFromBucket (key) {

function newmanRun (options) {
return new Promise((resolve, reject) => {
newman.run(options, err => { err ? reject(err) : resolve() })
newman.run(options, err => {
err ? reject(err) : resolve()
})
})
}

Expand All @@ -157,6 +146,30 @@ async function runTest (postmanCollection, postmanEnvironment) {
}
}

async function updateRunner (deploymentId, combinedRunner, event, error) {
if (deploymentId) {
console.log('starting to update CodeDeploy lifecycle event hook status...')
const params = {
deploymentId: deploymentId,
lifecycleEventHookExecutionId: event.LifecycleEventHookExecutionId,
status: error ? 'Failed' : 'Succeeded'
}
try {
const data = await codedeploy.putLifecycleEventHookExecutionStatus(params).promise()
console.log(data)
} catch (err) {
console.log(err, err.stack)
throw err
}
} else if (combinedRunner) {
return {
passed: !error
}
} else {
console.log('No deployment ID found in the event. Skipping update to CodeDeploy lifecycle hook...')
}
}

function sleep (ms) {
console.log('started sleep timer')
return new Promise(resolve => setTimeout(args => {
Expand All @@ -165,4 +178,4 @@ function sleep (ms) {
}, ms))
}

// exports.handler({}, {})
// exports.handler({}, {}).then(() => {})

0 comments on commit ded51a4

Please sign in to comment.