Skip to content

Commit

Permalink
🚧 Work in progress: working on the integration tests of the FaaS mode
Browse files Browse the repository at this point in the history
  • Loading branch information
k33g committed Jun 24, 2023
1 parent 5d4510f commit 12e1531
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 16 deletions.
Empty file added capsule-http/tests/.gitignore
Empty file.
2 changes: 2 additions & 0 deletions capsule-http/tests/faas/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tmp/*.wasm
*.wasm


108 changes: 98 additions & 10 deletions capsule-http/tests/faas/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: '3'

description: >
# Integration tests of the FaaS mode of Capsule
tasks:

# Build of capsule-http
Expand All @@ -14,6 +17,100 @@ tasks:
sudo cp capsule-http /usr/local/bin/capsule-http
capsule-http --version
build-capsule-http:
description: >
Build capsule-http, and copy the binary to the current directory
`/capsule/capsule-http/tests/faas`
cmds:
- |
echo "📦 Building capsule-http..."
cd ../..
pwd
go build -ldflags="-s -w" -o capsule-http
ls -lh capsule-http
mv capsule-http ./tests/faas
cd ./tests/faas
./capsule-http --version
build-html-page:
cmds:
- |
cd functions/html-page
tinygo build -o html-page.wasm \
-scheduler=none \
--no-debug \
-target wasi ./main.go
ls -lh *.wasm
mv html-page.wasm ../..
build-index-page:
description: >
In "FaaS mode", if an `index.page.wasm` file exists
Capsule will serve it with the "/" route
Otherwise, Capsule will return a simple message with its version number
cmds:
- |
cd functions/index-page
tinygo build -o index.page.wasm \
-scheduler=none \
--no-debug \
-target wasi ./main.go
ls -lh *.wasm
mv index.page.wasm ../..
# Start main capsule process
start-capsule-http-with-file:
description: >
Start Capsule HTTP with an wasm module.
Capsule will load and serve this wasm module,
but in the same process. It means that if you want to update the `index.page.wasm` module,
you will need to stop and start again the Capsule HTTP process.
There is another solution:
- start the capsule http without any file
- start a function name `index.page` (as any other function)
Then, you will be able to redeploy a version of `index.page`
See these task: `start-capsule-http` and `start-index`
env:
HTTP_PORT: '8080'
#WASM_FILE: './functions/index-page/index.page.wasm'
WASM_FILE: './html-page.wasm'
CAPSULE_FAAS_TOKEN: "ILOVEPANDAS"
cmds:
- | # use --faas=true to activate the faas mode of Capsule HTTP
#capsule-http --wasm=${WASM_FILE} --httpPort=${HTTP_PORT} --faas=true
./capsule-http --wasm=${WASM_FILE} --httpPort=${HTTP_PORT} --faas=true
# you can use http://capsule-ide.local:8080/
start-capsule-http:
env:
HTTP_PORT: '8080'
CAPSULE_FAAS_TOKEN: "ILOVEPANDAS"
cmds:
- | # use --faas=true to activate the faas mode of Capsule HTTP
./capsule-http --httpPort=${HTTP_PORT} --faas=true
# you can use http://capsule-ide.local:8080/
start-index:
env:
CAPSULE_FAAS_TOKEN: "ILOVEPANDAS"
CAPSULE_MAIN_PROCESS_URL: "http://capsule-ide.local:8080"
cmds:
- |
capsctl \
--cmd=start \
--name=index.page \
--revision=default \
--wasm=./index.page.wasm
todo: >
- redeploy of index
# =======================================
# Without capsctl, only the API
# =======================================

build-hello-functions:
cmds:
- | # hello-default
Expand Down Expand Up @@ -105,16 +202,7 @@ tasks:
"https://gitlab.com/api/v4/projects/${GITLAB_WASM_PROJECT_ID}/packages/generic/${WASM_PACKAGE}/${WASM_VERSION}/${WASM_MODULE}"
# Start main capsule process
start-capsule-http:
env:
HTTP_PORT: '8080'
WASM_FILE: './functions/index-page/index-page.wasm'
CAPSULE_FAAS_TOKEN: "ILOVEPANDAS"
cmds:
- | # use --faas=true to activate the faas mode of Capsule HTTP
capsule-http --wasm=${WASM_FILE} --httpPort=${HTTP_PORT} --faas=true
# you can use http://capsule-ide.local:8080/
launch-hello-functions:
cmds:
Expand Down
9 changes: 9 additions & 0 deletions capsule-http/tests/faas/functions/html-page/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module index-page

go 1.20

require github.com/bots-garden/capsule-module-sdk v0.0.4

require github.com/valyala/fastjson v1.6.4 // indirect

replace github.com/bots-garden/capsule-module-sdk => ../../../../../../capsule-module-sdk
2 changes: 2 additions & 0 deletions capsule-http/tests/faas/functions/html-page/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
21 changes: 21 additions & 0 deletions capsule-http/tests/faas/functions/html-page/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Package main
package main

import (
capsule "github.com/bots-garden/capsule-module-sdk"
)

func main() {
capsule.SetHandleHTTP(func (param capsule.HTTPRequest) (capsule.HTTPResponse, error) {

return capsule.HTTPResponse{
TextBody: "<h1>👋 Hello World! 🌍</h1>",
Headers: `{
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
"X-Powered-By": "capsule-module-sdk"
}`,
StatusCode: 200,
}, nil
})
}
2 changes: 2 additions & 0 deletions capsule-http/tests/faas/functions/index-page/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Chronicles reborn

6 changes: 2 additions & 4 deletions capsule-http/tests/faas/functions/index-page/go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module index-page
module index.page

go 1.20

require github.com/bots-garden/capsule-module-sdk v0.0.4
require github.com/bots-garden/capsule-module-sdk v0.0.3

require github.com/valyala/fastjson v1.6.4 // indirect

replace github.com/bots-garden/capsule-module-sdk => ../../../../../../capsule-module-sdk
2 changes: 2 additions & 0 deletions capsule-http/tests/faas/functions/index-page/go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/bots-garden/capsule-module-sdk v0.0.3 h1:a0TRgwdiJjc+9raOe4zQoWPnzxKegqmInVmwzlaDuug=
github.com/bots-garden/capsule-module-sdk v0.0.3/go.mod h1:DtKYwanz4YvBwSpu0GuuhtkeFE6+jDgEucBOTWVBMy8=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
24 changes: 24 additions & 0 deletions capsule-http/tests/faas/functions/index-page/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8">
<title>Wasm is fantastic 😍</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container { min-height: 100vh; display: flex; justify-content: center; align-items: center; text-align: center; }
.title { font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif; display: block; font-weight: 300; font-size: 80px; color: #35495e; letter-spacing: 1px; }
.subtitle { font-family: "Source Sans Pro", "Helvetica Neue", Arial, sans-serif; font-weight: 300; font-size: 32px; color: #526488; word-spacing: 5px; padding-bottom: 15px; }
.links { padding-top: 15px; }
</style>
</head>

<body>
<section class="container">
<div>
<h1 class="title">👋 Hello World 🌍</h1>
<h2 class="subtitle">Served with 💜 by Capsule 💊</h2>
<h2 class="subtitle">[🚀 Faas mode]</h2>
</div>
</section>
</body>

</html>
12 changes: 10 additions & 2 deletions capsule-http/tests/faas/functions/index-page/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
// Package main
// Package main => serving an html resource
package main

import (
_ "embed"
capsule "github.com/bots-garden/capsule-module-sdk"
)

var (
//go:embed index.html
html []byte
)

func main() {

capsule.SetHandleHTTP(func (param capsule.HTTPRequest) (capsule.HTTPResponse, error) {

return capsule.HTTPResponse{
TextBody: "<h1>👋 Hello World! 🌍</h1>",
TextBody: string(html),
Headers: `{
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-cache",
Expand All @@ -19,3 +26,4 @@ func main() {
}, nil
})
}
//TODO: make something easier for the headers (see old version of Capsule)
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ use (
./capsule-http/tests/faas/functions/hello-green
./capsule-http/tests/faas/functions/hello-orange
./capsule-http/tests/faas/functions/index-page
./capsule-http/tests/faas/functions/html-page

)

0 comments on commit 12e1531

Please sign in to comment.