Skip to content

Commit 3efb5cd

Browse files
committed
Solve bugs and fully implemented testing.
1 parent 686672a commit 3efb5cd

13 files changed

+117
-23
lines changed

Dockerfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ EXPOSE 9000
4949

5050
CMD ["node", "dist/index.js"]
5151

52-
# TODO: testing
52+
FROM base AS test
53+
54+
RUN apt-get update \
55+
&& apt-get install curl ca-certificates jq -y --no-install-recommends \
56+
&& npm install -g @metacall/deploy

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ metacall-deploy --dev
4444
- This project is developed using [MetaCall Core] itself in order to provide polyglot support, we are using its [Node Port](https://github.com/metacall/core/tree/develop/source/ports/node_port) of this library to use all the functions and methods `MetaCall Core C API` provides.
4545

4646
- Also, [Here](https://github.com/metacall/faas/blob/master/types/metacall.d.ts) are all the functions of `MetaCall Core` we are using.
47+
48+
### Testing
49+
50+
This will run a test, and if the `docker compose up test` command exits with exit code 0, it will mean that the test has passed.
51+
If you want to see the output of the FaaS, run `docker compose up` instead. But when running this, the command won't terminate once the test finishes and the FaaS will keep running. You will need to press `Ctrl+C` in order to close it.
52+
53+
```sh
54+
docker compose build
55+
docker compose up test
56+
docker compose down
57+
```

TODO.md

-1
This file was deleted.

docker-compose.yml

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
2-
# MetaCall Library by Parra Studios
3-
# Docker compose infrastructure for MetaCall.
2+
# MetaCall FaaS Script by Parra Studios
3+
# Reimplementation of MetaCall FaaS platform written in TypeScript.
44
#
55
# Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
66
#
@@ -30,4 +30,16 @@ services:
3030
ports:
3131
- "9000:9000"
3232

33-
# TODO: testing
33+
test:
34+
image: metacall/faas:test
35+
container_name: metacall_faas_test
36+
build:
37+
context: .
38+
dockerfile: Dockerfile
39+
target: test
40+
network_mode: host
41+
depends_on:
42+
- faas
43+
volumes:
44+
- ./test/:/metacall/
45+
command: /metacall/test.sh

src/app.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ const host = hostname();
1212
app.use(express.json());
1313
app.use(express.urlencoded({ extended: true }));
1414

15+
app.get('/readiness', (_req: Request, res: Response) => res.sendStatus(200));
1516
app.get('/validate', api.validate);
1617
app.get('/api/account/deploy-enabled', api.validate);
1718

19+
app.get(`/${host}/:appName/:version/call/:name`, api.callFunction);
1820
app.post(`/${host}/:appName/:version/call/:name`, api.callFunction);
1921
app.get(
2022
`/${host}/:appName/:version/static/.metacall/faas/apps/:app/:file`,

src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface Deployment {
1010
blob?: string;
1111
}
1212

13-
export const deploymentMap: Record<string, Deployment> = {};
13+
export const deploymentMap: Record<string, Promise<Deployment>> = {};
1414

1515
export const createInstallDependenciesScript = (
1616
runner: string,

src/controller/deploy.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import {
2020
logProcessOutput
2121
} from '../utils/utils';
2222

23-
import { PackageError } from '@metacall/protocol/package';
24-
2523
// TODO: Isn't this available inside protocol package? We MUST reuse it
2624
export type DeployBody = {
2725
suffix: string; // name of deployment
@@ -43,7 +41,7 @@ export const deploy = catchAsync(
4341
// req.body.resourceType == 'Repository' &&
4442
// (await calculatePackages(next));
4543

46-
const deployment = deploymentMap[req.body.suffix];
44+
const deployment = await deploymentMap[req.body.suffix];
4745

4846
if (deployment === undefined) {
4947
return next(
@@ -86,15 +84,11 @@ export const deploy = catchAsync(
8684
});
8785

8886
return res.status(200).json({
89-
suffix: hostname(),
90-
prefix: deployment.id,
87+
prefix: hostname(),
88+
suffix: deployment.id,
9189
version: 'v1'
9290
});
9391
} catch (err) {
94-
// Check if the error is PackageError.Empty
95-
if (err === PackageError.Empty) {
96-
return next(err);
97-
}
9892
return next(err);
9993
}
10094
}

src/controller/upload.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,30 @@ export const uploadPackage = (
162162

163163
const options: ParseOptions = { path: deployment.path };
164164

165+
let deployResolve: (
166+
value: Deployment | PromiseLike<Deployment>
167+
) => void;
168+
let deployReject: (reason?: unknown) => void;
169+
170+
deploymentMap[deployment.id] = new Promise((resolve, reject) => {
171+
deployResolve = resolve;
172+
deployReject = reject;
173+
});
174+
165175
fs.createReadStream(deployment.blob)
166176
.pipe(Extract(options))
167177
.on('close', () => {
168178
deleteBlob();
169-
deploymentMap[deployment.id] = deployment;
179+
deployResolve(deployment);
170180
})
171181
.on('error', error => {
172182
deleteBlob();
173-
errorHandler(
174-
new AppError(
175-
`Failed to unzip the deployment at: ${error.toString()}`,
176-
500
177-
)
183+
const appError = new AppError(
184+
`Failed to unzip the deployment at: ${error.toString()}`,
185+
500
178186
);
187+
errorHandler(appError);
188+
deployReject(appError);
179189
});
180190
});
181191

src/controller/validate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Request, Response } from 'express';
22

3-
export const validate = (req: Request, res: Response): Response =>
3+
export const validate = (_req: Request, res: Response): Response =>
44
res.status(200).json({
55
status: 'success',
66
data: true

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void (async (): Promise<void> => {
1717
// TODO: Refactor this
1818
await findJsonFilesRecursively(appsDirectory);
1919

20-
console.log('Previously deployed apllications deployed successfully');
20+
console.log('Previously deployed applications deployed successfully');
2121
// END-TODO
2222

2323
const port = process.env.PORT || 9000;

test/data/python-base-app/index.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python
2+
3+
def number():
4+
return 100
5+
6+
def text():
7+
return 'asd'
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"language_id": "py",
3+
"path": ".",
4+
"scripts": [
5+
"index.py"
6+
]
7+
}

test/test.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
#
4+
# MetaCall FaaS Script by Parra Studios
5+
# Reimplementation of MetaCall FaaS platform written in TypeScript.
6+
#
7+
# Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
#
21+
22+
set -exuo pipefail
23+
24+
# FaaS base URL
25+
BASE_URL="http://localhost:9000"
26+
27+
# Get the prefix of a deployment
28+
function getPrefix() {
29+
prefix=$(metacall-deploy --dev --inspect Raw | jq -r ".[] | select(.suffix == \"$1\") | .prefix")
30+
echo $prefix
31+
}
32+
33+
# Wait for the FaaS to be ready
34+
while [[ ! $(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/readiness) = "200" ]]; do
35+
sleep 1
36+
done
37+
38+
echo "FaaS ready, starting tests."
39+
40+
# Test deploy (Python) without dependencies
41+
app="python-base-app"
42+
pushd data/$app
43+
metacall-deploy --dev
44+
prefix=$(getPrefix $app)
45+
url=$BASE_URL/$prefix/$app/v1/call
46+
[[ $(curl -s $url/number) = 100 ]] || exit 1
47+
[[ $(curl -s $url/text) = '"asd"' ]] || exit 1
48+
popd

0 commit comments

Comments
 (0)