Skip to content

Commit

Permalink
Add babashka smoke test
Browse files Browse the repository at this point in the history
Add a GitHub action that will run Fulcro Demo with each of the supported
databases and verify that it actually runs. It also verified that cljs
compiles.
  • Loading branch information
holyjak committed Jan 8, 2023
1 parent eb9ef3f commit 0bc6ed2
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/smoke-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: smoke-test

on:
- pull_request
#- push # for testing only

permissions: read-all

jobs:
smoke-test:
name: App smoke test
runs-on: ubuntu-latest
strategy:
matrix:
# NOTE: if sql is removed then update the `if` condition of 'Verify ClojureScript compiles'
db_deps_alias: [sql, asami, xtdb] # datomic TODO add after we solve access to dev-local datomic .jar
steps:

- name: Cache All The Things
uses: actions/cache@v2
with:
# TODO /opt/hostedtoolcache or ${{ RUNNER_TOOL_CACHE }} ? bb is downloaded here <> reset cache on version change
path: |
~/.m2/repository
~/.gitlibs
~/.clojure
~/.cpcache
/opt/hostedtoolcache
**/node_modules
key: ${{ runner.os }}-${{ hashFiles('**/deps.edn','**/yarn.lock') }}
- uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 11
- name: Setup Clojure
uses: DeLaGuardo/[email protected]
with:
cli: '1.11.1.1208'
# TODO Find out how to cache bb; see https://github.com/turtlequeue/setup-babashka/issues/15
- name: Setup Babashka
uses: turtlequeue/[email protected]
with:
babashka-version: 1.0.169

- uses: actions/checkout@v2
- uses: bahmutov/npm-install@v1
- name: Babashka version
run: bb --version
- name: Verify ClojureScript compiles
if: matrix.db_deps_alias == 'sql' # so that we only run this once
run: bb compile-cljs
- name: Run the actual smoke test
env:
DB_DEPS_ALIAS: ${{ matrix.db_deps_alias }}
run: bb run --prn smoke-test
4 changes: 4 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:paths ["." "smoke-test.bb"]
:tasks {compile-cljs (shell "npx shadow-cljs compile main")
smoke-test {:doc "Start the app, run smoke test"
:task (load-file "./smoke-test.bb")}}}
89 changes: 89 additions & 0 deletions smoke-test.bb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
(ns smoke-test
"Check that Demo is running, we can log in, and list accounts"
(:require [babashka.wait :as wait]
[babashka.process :as p]
[cognitect.transit :as transit]
[org.httpkit.client :as http]))

(def sigterm-exit-code "JVM exits with this when it gets SIGTERM from p/delete-tree" 143)
(def ok-exitcode? #{0 sigterm-exit-code})

(defn transit-str [data]
(with-open [out (java.io.ByteArrayOutputStream.)]
(let [writer (transit/writer out :json nil #_{:handlers nil})]
(transit/write writer data)
(.toString out "UTF-8"))))

(defn parse-transit-str [text]
(transit/read
(transit/reader (java.io.ByteArrayInputStream. (.getBytes text "UTF-8"))
:json {:handlers nil})))

(defn try-slurp [f]
(try (slurp f)
(catch Exception e
(println "Warning: Failed to slurp" e)
nil)))

(def db-deps-alias
(doto (System/getenv "DB_DEPS_ALIAS")
(assert "Missing env var DB_DEPS_ALIAS")))

(println "Starting the server for" db-deps-alias "...")
(def server-process (p/process {:shutdown p/destroy-tree
;; TODO It seems the exit-fn is never called?!
:exit-fn (fn [{:keys [exit err out] :as resp}]
(if (ok-exitcode? exit)
(println "Server exited OK, out:"
(slurp out))
(do
(println "Server failed with code" exit
(slurp out) "\n" (slurp err))
(System/exit exit))))}
(format "clojure -X:%s:dev development/cli-start" db-deps-alias)))

(def success? (atom false))

(try

(println "Waiting for the server to start...")
(assert (not= (wait/wait-for-port "localhost" 3000 {:timeout 60000 :default ::timeout})
::timeout)
"Timeout waiting for the server to start")
(println "OK, server is running")

(def res1
@(http/post "http://localhost:3000/api"
{:as :text
:headers {"Content-Type" "application/transit+json"}
:body (transit-str
'[{(com.example.model.account/login
{:username "[email protected]", :password "letmein"}) [*]}])}))

(assert (= 200 (:status res1)) (str "Expected 200, got " res1))
(println "OK, managed to log in")

(def res2
(->
@(http/post "http://localhost:3000/api"
{:as :text
:headers {"Content-Type" "application/transit+json"}
:body (transit-str '[{:account/all-accounts [:account/id :account/name]}])})
(select-keys [:status :body])))

(assert (= 200 (:status res2)) (str "Expected 200, got " res2))

(let [resp-data (parse-transit-str (:body res2))]
(assert (sequential? (:account/all-accounts resp-data))
(str "Expected an accounts list, got " resp-data))
;; NOTE The list may be empty if seed!, which runs async, has not finished yet
(println "OK, managed to list accounts:" (count (:account/all-accounts resp-data))))

(reset! success? true)
(finally
(p/destroy-tree server-process)
(let [{:keys [exit out err]} @server-process
out (not-empty (try-slurp out)), err (not-empty (try-slurp err))
problem? (or (not @success?) (not (ok-exitcode? exit)))]
(when problem?
(println "Server OUT:" out (when err (str "\nERR: " err)))))))
2 changes: 2 additions & 0 deletions src/asami/development.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
[]
(mount/stop))

(defn cli-start "Start & seed the app from the CLI using `clojure -X ..`" [_] (start))

(def go start)

(defn restart
Expand Down
2 changes: 2 additions & 0 deletions src/datomic/development.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
(seed!)
:ok)

(defn cli-start "Start & seed the app from the CLI using `clojure -X ..`" [_] (start))

(defn stop
"Stop the server."
[]
Expand Down
2 changes: 2 additions & 0 deletions src/sql/development.clj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
(seed!)
:ok)

(defn cli-start "Start & seed the app from the CLI using `clojure -X ..`" [_] (start))

(defn stop
"Stop the server."
[]
Expand Down
2 changes: 2 additions & 0 deletions src/xtdb/development.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
(seed!)
:ok)

(defn cli-start "Start & seed the app from the CLI using `clojure -X ..`" [_] (start))

(defn stop
"Stop the server."
[]
Expand Down

0 comments on commit 0bc6ed2

Please sign in to comment.