Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icarus-sullivan committed Jan 22, 2022
1 parent 0cb9256 commit 01401e9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
71 changes: 17 additions & 54 deletions src/lambda/handler-runner/go-runner/GoRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@ import { EOL } from 'os'
import { promises as fsPromises } from 'fs'
import { sep, resolve, parse as pathParse } from 'path'
import execa, { sync } from 'execa'
import STS from 'aws-sdk/clients/sts'

const { writeFile, readFile, mkdir, rmdir } = fsPromises
const { parse, stringify } = JSON
const { cwd } = process

const MIN_ALLOWED_SESSION_DURATION_S = 900
const SESSION_TIMEOUT_MS = 850 * 1000
const PAYLOAD_IDENTIFIER = 'offline_payload'

export default class GoRunner {
#env = null
#handlerPath = null
#tmpPath = null
#tmpFile = null
#profile = null
#handle = null
#credentials = null
#goEnv = null

constructor(funOptions, env, v3Utils) {
const { handlerPath, provider } = funOptions
const { handlerPath } = funOptions

this.#env = env
this.#handlerPath = handlerPath
this.#profile = provider.profile || 'default'

if (v3Utils) {
this.log = v3Utils.log
Expand All @@ -41,12 +34,14 @@ export default class GoRunner {
}

async cleanup() {
// Clean up session timeout
if (this.#handle !== null) {
clearTimeout(this.#handle)
try {
await rmdir(this.#tmpPath, { recursive: true })
} catch (e) {
// @ignore
}

await this.cleanArtifacts()
this.#tmpFile = null
this.#tmpPath = null
}

_parsePayload(value) {
Expand Down Expand Up @@ -82,22 +77,6 @@ export default class GoRunner {
return payload
}

async cleanArtifacts() {
try {
await rmdir(this.#tmpPath, { recursive: true })
} catch (e) {
// @ignore
}

this.#tmpFile = null
this.#tmpPath = null
}

clearCredentials() {
this.#credentials = null
this.#handle = null
}

async run(event, context) {
const { dir } = pathParse(this.#handlerPath)
const handlerCodeRoot = dir.split(sep).slice(0, -1).join(sep)
Expand Down Expand Up @@ -138,40 +117,20 @@ export default class GoRunner {
}, {})
}

// Get session credentials to pass to go
if (!this.#credentials) {
const sts = new STS()

const { Credentials } = await sts
.getSessionToken({
DurationSeconds: MIN_ALLOWED_SESSION_DURATION_S, // 900-inf
})
.promise()

this.#credentials = Credentials
this.#handle = setTimeout(
this.clearCredentials.bind(this),
SESSION_TIMEOUT_MS,
)
}

// Remove our root, since we want to invoke go relatively
const cwdPath = `${this.#tmpFile}`.replace(`${cwd()}${sep}`, '')
const cp = await execa(`go`, ['run', cwdPath], {
const { stdout, stderr } = await execa(`go`, ['run', cwdPath], {
stdio: 'pipe',
env: {
...this.#env,
...this.#goEnv,
AWS_ACCESS_KEY_ID: this.#credentials.AccessKeyId,
AWS_SECRET_ACCESS_KEY: this.#credentials.SecretAccessKey,
AWS_SESSION_TOKEN: this.#credentials.SessionToken,
AWS_PROFILE: this.#profile,
AWS_LAMBDA_LOG_GROUP_NAME: context.logGroupName,
AWS_LAMBDA_LOG_STREAM_NAME: context.logStreamName,
AWS_LAMBDA_FUNCTION_NAME: context.functionName,
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: context.memoryLimitInMB,
AWS_LAMBDA_FUNCTION_VERSION: context.functionVersion,
LAMBDA_EVENT: stringify(event),
LAMBDA_TEST_EVENT: `${event}`,
LAMBDA_CONTEXT: stringify(context),
IS_LAMBDA_AUTHORIZER:
event.type === 'REQUEST' || event.type === 'TOKEN',
Expand All @@ -183,12 +142,16 @@ export default class GoRunner {
})

// Clean up after we created the temporary file
await this.cleanArtifacts()
await this.cleanup()

if (cp.stderr) {
throw new Error(cp.stderr)
console.log({
stdout,
stderr,
})
if (stderr) {
return stderr
}

return this._parsePayload(cp.stdout)
return this._parsePayload(stdout)
}
}
2 changes: 1 addition & 1 deletion tests/integration/go/go1.x/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.13

require (
github.com/aws/aws-lambda-go v1.28.0
github.com/icarus-sullivan/mock-lambda v0.0.0-20220114085425-44091545252e // indirect
github.com/icarus-sullivan/mock-lambda v0.0.0-20220115083805-e065469e964a // indirect
github.com/urfave/cli v1.22.1 // indirect
)
2 changes: 2 additions & 0 deletions tests/integration/go/go1.x/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/icarus-sullivan/mock-lambda v0.0.0-20220114085425-44091545252e h1:cPv6jHZPqHlu73UmtFEVPRNHGnSrd43OKwpQKVktLcs=
github.com/icarus-sullivan/mock-lambda v0.0.0-20220114085425-44091545252e/go.mod h1:2iuLAENWZqxe/B6XUDWw/3ioQ9d1fwhgFTlwVeIBpzY=
github.com/icarus-sullivan/mock-lambda v0.0.0-20220115083805-e065469e964a h1:gmFO6gLHZkdJlkZ41QiQ5tzH8LORPVJCuKk6YKyquU0=
github.com/icarus-sullivan/mock-lambda v0.0.0-20220115083805-e065469e964a/go.mod h1:2iuLAENWZqxe/B6XUDWw/3ioQ9d1fwhgFTlwVeIBpzY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/go/go1.x/go1.x.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ _describe('Go 1.x with GoRunner', () => {
//
;[
{
description: 'should work with go1.x without docker',
description: 'should work with go1.x',
expected: {
message: 'Hello Go 1.x!',
},
Expand Down

0 comments on commit 01401e9

Please sign in to comment.