🐛 Fix backend util shell tests

This commit is contained in:
Andrey Antukh 2026-07-09 18:53:49 +02:00
parent 3400d6afbb
commit 23a9b4bdd9
2 changed files with 19 additions and 12 deletions

View File

@ -65,7 +65,6 @@
(assert (every? string? cmd) "the command should be a vector of strings") (assert (every? string? cmd) "the command should be a vector of strings")
(let [executor (::wrk/executor system) (let [executor (::wrk/executor system)
_ (assert (some? executor) "executor is required, check ::wrk/executor")
full-cmd (cond->> cmd full-cmd (cond->> cmd
(seq prlimit) (seq prlimit)
(into (prlimit-cmd prlimit))) (into (prlimit-cmd prlimit)))
@ -74,6 +73,9 @@
_ (reduce-kv set-env env-map env) _ (reduce-kv set-env env-map env)
process (.start builder)] process (.start builder)]
(when-not executor
(throw (IllegalArgumentException. "invalid system/cfg provided, missing ::wrk/executor")))
(if in (if in
(px/run! executor (px/run! executor
(fn [] (fn []

View File

@ -8,12 +8,17 @@
(:require (:require
[app.common.exceptions :as ex] [app.common.exceptions :as ex]
[app.util.shell :as shell] [app.util.shell :as shell]
[app.worker :as-alias wrk]
[clojure.string :as str] [clojure.string :as str]
[clojure.test :as t])) [clojure.test :as t]
[promesa.exec :as px]))
(def ^:private system
{::wrk/executor (px/cached-executor)})
(t/deftest exec-normal-completes (t/deftest exec-normal-completes
(t/testing "normal process completes within timeout" (t/testing "normal process completes within timeout"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["echo" "hello"] :cmd ["echo" "hello"]
:timeout 10)] :timeout 10)]
(t/is (= 0 (:exit result))) (t/is (= 0 (:exit result)))
@ -21,7 +26,7 @@
(t/deftest exec-captures-stderr (t/deftest exec-captures-stderr
(t/testing "stderr is captured separately" (t/testing "stderr is captured separately"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["bash" "-c" "echo out; echo err >&2"] :cmd ["bash" "-c" "echo out; echo err >&2"]
:timeout 10)] :timeout 10)]
(t/is (= 0 (:exit result))) (t/is (= 0 (:exit result)))
@ -30,14 +35,14 @@
(t/deftest exec-non-zero-exit (t/deftest exec-non-zero-exit
(t/testing "non-zero exit code is captured" (t/testing "non-zero exit code is captured"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["bash" "-c" "exit 42"] :cmd ["bash" "-c" "exit 42"]
:timeout 10)] :timeout 10)]
(t/is (= 42 (:exit result)))))) (t/is (= 42 (:exit result))))))
(t/deftest exec-with-env (t/deftest exec-with-env
(t/testing "environment variables are passed to the process" (t/testing "environment variables are passed to the process"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["bash" "-c" "echo $MY_VAR"] :cmd ["bash" "-c" "echo $MY_VAR"]
:env {"MY_VAR" "test-value"} :env {"MY_VAR" "test-value"}
:timeout 10)] :timeout 10)]
@ -46,7 +51,7 @@
(t/deftest exec-with-input (t/deftest exec-with-input
(t/testing "stdin input is passed to the process" (t/testing "stdin input is passed to the process"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["cat"] :cmd ["cat"]
:in "hello from stdin" :in "hello from stdin"
:timeout 10)] :timeout 10)]
@ -57,7 +62,7 @@
(t/testing "process that exceeds timeout is killed and raises exception" (t/testing "process that exceeds timeout is killed and raises exception"
(let [start (System/currentTimeMillis)] (let [start (System/currentTimeMillis)]
(try (try
(shell/exec! {} (shell/exec! system
:cmd ["sleep" "60"] :cmd ["sleep" "60"]
:timeout 1) :timeout 1)
(t/is false "should have thrown") (t/is false "should have thrown")
@ -72,14 +77,14 @@
(t/deftest exec-no-timeout-waits (t/deftest exec-no-timeout-waits
(t/testing "without timeout, process runs to completion" (t/testing "without timeout, process runs to completion"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["sleep" "0.1"] :cmd ["sleep" "0.1"]
:timeout nil)] :timeout nil)]
(t/is (= 0 (:exit result)))))) (t/is (= 0 (:exit result))))))
(t/deftest exec-prlimit-normal (t/deftest exec-prlimit-normal
(t/testing "normal process completes within prlimit" (t/testing "normal process completes within prlimit"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["echo" "hello"] :cmd ["echo" "hello"]
:prlimit {:mem 256 :cpu 10} :prlimit {:mem 256 :cpu 10}
:timeout 10)] :timeout 10)]
@ -88,7 +93,7 @@
(t/deftest exec-prlimit-cpu (t/deftest exec-prlimit-cpu
(t/testing "process exceeding CPU limit is killed" (t/testing "process exceeding CPU limit is killed"
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["bash" "-c" "while true; do :; done"] :cmd ["bash" "-c" "while true; do :; done"]
:prlimit {:cpu 2} :prlimit {:cpu 2}
:timeout 10)] :timeout 10)]
@ -98,7 +103,7 @@
(t/testing "process exceeding memory limit is killed" (t/testing "process exceeding memory limit is killed"
;; Use python3 to allocate more memory than the limit allows. ;; Use python3 to allocate more memory than the limit allows.
;; This test requires python3 to be available in the environment. ;; This test requires python3 to be available in the environment.
(let [result (shell/exec! {} (let [result (shell/exec! system
:cmd ["python3" "-c" :cmd ["python3" "-c"
"import sys; x = bytearray(600 * 1024 * 1024); sys.exit(0)"] "import sys; x = bytearray(600 * 1024 * 1024); sys.exit(0)"]
:prlimit {:mem 256} :prlimit {:mem 256}