Skip to content

Commit

Permalink
Simplify test code
Browse files Browse the repository at this point in the history
Decrease the number of files in /runtime.
Use a single github workflow.
  • Loading branch information
hayd committed Nov 9, 2019
1 parent 6fcdb2e commit f803ced
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 77 deletions.
23 changes: 20 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,25 @@ jobs:
- name: Build docker image
working-directory: ./tests
run: |
docker build -f Dockerfile -t bootstrap ..
docker build -f Dockerfile -t test-runner ..
docker create --name extract test-runner
- name: Run tests
working-directory: ./tests
run: |
docker run bootstrap
docker run test-runner
- name: Create artifacts
run: |
docker cp extract:/bin/deno amz-deno
docker cp extract:/src/runtime/deno-lambda-layer.zip deno-lambda-layer.zip
docker cp extract:/src/runtime/deno-lambda-example.zip deno-lambda-example.zip
gzip -9 amz-deno
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') && github.repository == 'hayd/deno_lambda'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
files: |
amz-deno.gz
deno-lambda-layer.zip
deno-lambda-example.zip
draft: true
27 changes: 0 additions & 27 deletions .github/workflows/release.yml

This file was deleted.

8 changes: 0 additions & 8 deletions example.ts

This file was deleted.

File renamed without changes.
8 changes: 6 additions & 2 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ RUN mkdir -p /src/runtime /src/tests /var/task /opt \
&& cp /bin/deno /src/runtime/amz-deno \
&& cp /bin/deno /src/tests/amz-deno

ADD tests/deps.ts src/tests/deps.ts
RUN deno fetch src/tests/deps.ts
ADD tests/deps.ts /src/tests/deps.ts
RUN deno fetch /src/tests/deps.ts

ADD hello.ts /src/runtime/hello.ts
ADD hello.ts /src/tests/hello.ts
ADD tests/bundle.ts /src/runtime/bundle.ts

ADD runtime /src/runtime

Expand Down
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
In order to run the tests you run the `Dockerfile`:

```
docker build -f Dockerfile -t bootstrap .. && docker run bootstrap
docker build -f Dockerfile -t test-runner .. && docker run test-runner
```

_Note: it runs in the `..` (root) context._
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export async function handler(event, context) {
return {
statusCode: 200,
body: `Welcome to deno ${Deno.version.deno} 🦕`
};
}
8 changes: 0 additions & 8 deletions tests/hello.ts

This file was deleted.

40 changes: 16 additions & 24 deletions tests/server.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { serve } from "./deps.ts";

const encode = new TextEncoder().encode;
const decode = new TextDecoder();
const dec = new TextDecoder();

const testFilename = Deno.args[1];
const test = JSON.parse(decode.decode(Deno.readFileSync(testFilename)));

function* eventsGen() {
for (const e of test["events"]) {
yield e;
}
}

const env = test.env || {};
const testJson = JSON.parse(dec.decode(Deno.readFileSync(Deno.args[1])));

// start the server prior to running bootstrap.
const PORT = 1993;
const s = serve(`0.0.0.0:${PORT}`);

const statusOK = encode('{"status":"OK"}\n');

const bootstrap = Deno.readDirSync("/var/task/")
.map(x => x.name)
.includes("bootstrap")
Expand All @@ -29,32 +19,33 @@ const p = Deno.run({
args: [bootstrap],
stdout: "piped",
stderr: "piped", // comment this out to debug
env,
env: testJson.env,
cwd: "/var/task"
});

const events = eventsGen();
const statusOK = encode('{"status":"OK"}\n');

const events = testJson["events"][Symbol.iterator]();
// iterate through the events until done.
let reqId = 0;
for await (const req of s) {
if (req.method == "POST") {
if (req.url.endsWith("/response")) {
const body = decode.decode(await req.body());
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "ok", content: body }));
} else if (req.url.endsWith("/init/error")) {
const body = decode.decode(await req.body());
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "error", content: body }));
await req.respond({ body: statusOK });
p.kill(9);
s.close();
Deno.exit();
} else if (req.url.endsWith(`/${reqId}/error`)) {
const body = decode.decode(await req.body());
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "error", content: body }));
} else {
throw new Error("Unreachable!")
throw new Error("Unreachable!");
}
// raise on other?
await req.respond({ body: statusOK });
} else {
// assert endsWith /next
Expand All @@ -66,10 +57,11 @@ for await (const req of s) {
} else {
reqId++;
const headers = new Headers({
'lambda-runtime-invoked-function-arn': "arn:aws:lambda:us-east-1:776893852117:function:test",
'lambda-runtime-aws-request-id': reqId.toString(),
'lambda-runtime-deadline-ms': (Date.now() + 300000).toString()
})
"lambda-runtime-invoked-function-arn":
"arn:aws:lambda:us-east-1:776893852117:function:test",
"lambda-runtime-aws-request-id": reqId.toString(),
"lambda-runtime-deadline-ms": (Date.now() + 300000).toString()
});
await req.respond({ body: encode(JSON.stringify(e.value)), headers });
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const testFiles = Deno.readDirSync("/src/tests")
.filter(x => x.startsWith("test_"))
.filter(x => x.endsWith(".json"))
.map(x => x.split("/").slice(-1)[0]);
//const testFiles = ["test_self_contained.json"];

if (!Deno.env("_IN_DOCKER")) {
console.error("test.ts must be called inside a docker container");
Expand Down Expand Up @@ -35,7 +34,7 @@ async function addFiles(
}
}

async function clearDir(dir: string) {
async function emptyDir(dir: string) {
for (const f of Deno.readDirSync(dir)) {
await Deno.remove(`${dir}/${f.name}`, { recursive: true });
}
Expand All @@ -50,11 +49,11 @@ for (const t of testFiles) {
const testJson = JSON.parse(dec.decode(await Deno.readFile(testPath)));

// This is the layer
await clearDir("/opt");
await emptyDir("/opt");
await addFiles(testJson["layer"], "/opt");

// This is the function code
await clearDir("/var/task");
await emptyDir("/var/task");
await addFiles(testJson["files"], "/var/task");

const p = Deno.run({
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions tests/test_js.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"env": { "_HANDLER": "hello.handler", "HANDLER_EXT": "js" },
"events": [{ "hello": "deno" }, { "hello": "again" }],
"expected": [
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.23.0 🦕\"}"
},
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.23.0 🦕\"}"
}
],
"files": ["hello.js"],
"layer": "deno-lambda-layer.zip"
}

0 comments on commit f803ced

Please sign in to comment.