Skip to content

Commit

Permalink
Support testing Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ccfontes committed Nov 9, 2023
1 parent cc20089 commit ca066e5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,25 @@ Namespace for each babashka source file created side by side with `handler.clj`

See the link:examples[examples] directory to find a fully working set of OpenFaaS Functions written in Babashka.

== Tests
== Function tests

To test the code of a Function, create a `test` directory in the Function top-level directory, containing:

- `run-tests.clj`: A script that runs the tests. You can use any test library and test runner you like.
- `bb.edn`: with `{:paths ["../.."]}`

To disable running existing tests, set the `TEST` build time argument to `false`:
[source, yml]
----
my-function:
lang: bb
handler: ./anything/my-function
image: ${DOCKER_REGISTRY_IMG_ORG_PATH}/my-function
build_args:
TEST: false
----

== faas-bb tests

=== CI tests

Expand Down
2 changes: 2 additions & 0 deletions examples/http/bb-hello/test/bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:paths ["../.."]
:deps {eg/eg {:mvn/version "0.5.6-alpha"}}}
11 changes: 11 additions & 0 deletions examples/http/bb-hello/test/run-tests.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns function.test.run-tests
(:require [eg :refer [eg]]
[clojure.test :refer [run-tests]]
[function.handler :refer [handler]]))

(eg handler
{:body "foo"} => {:body "Hello, foo"})

(let [{:keys [fail error]} (run-tests 'function.test.run-tests)]
(when (pos? (+ fail error))
(System/exit 1)))
9 changes: 9 additions & 0 deletions template/bb/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ FROM clojure:tools-deps-1.11.1.1347

RUN set -e

ARG TEST=true

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
COPY --from=babashka /usr/local/bin/bb /usr/local/bin/bb

Expand All @@ -27,6 +29,13 @@ RUN rm function/bb.edn
COPY --chown=app:app index.clj ./
COPY --chown=app:app lib ./

ENV TEST_DIR $HOME/function/test

RUN if [ $TEST != false ] && [ -d $TEST_DIR ]; then \
/bin/sh -c "cd $TEST_DIR && bb run-tests.clj" && \
rm -rf $TEST_DIR; \
fi

ENV mode="http"
ENV upstream_url="http://127.0.0.1:8082"

Expand Down

0 comments on commit ca066e5

Please sign in to comment.