Skip to content

Commit 86013d6

Browse files
authored
fix(scripts): add file existence check in file_env (#5424)
Signed-off-by: majiayu000 <1835304752@qq.com>
1 parent cbbb585 commit 86013d6

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

scripts/entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ file_env() {
1515
if [ -n "$val_var" ]; then
1616
val="$val_var"
1717
elif [ -n "$val_fileVar" ]; then
18+
if [ ! -r "$val_fileVar" ]; then
19+
echo "error: file '$val_fileVar' does not exist or is not readable" >&2
20+
exit 1
21+
fi
1822
val="$(cat "$val_fileVar")"
1923
fi
2024

scripts/entrypoint_test.sh

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env sh
2+
3+
# Test script for entrypoint.sh file_env function
4+
# Run: ./scripts/entrypoint_test.sh
5+
6+
set -e
7+
8+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
9+
TEMP_DIR=$(mktemp -d)
10+
trap "rm -rf $TEMP_DIR" EXIT
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
NC='\033[0m' # No Color
16+
17+
pass_count=0
18+
fail_count=0
19+
20+
pass() {
21+
echo "${GREEN}PASS${NC}: $1"
22+
pass_count=$((pass_count + 1))
23+
}
24+
25+
fail() {
26+
echo "${RED}FAIL${NC}: $1"
27+
fail_count=$((fail_count + 1))
28+
}
29+
30+
# Test 1: Direct env var works
31+
test_direct_env_var() {
32+
unset MEMOS_DSN MEMOS_DSN_FILE
33+
export MEMOS_DSN="direct_value"
34+
35+
result=$("$SCRIPT_DIR/entrypoint.sh" sh -c 'echo $MEMOS_DSN' 2>&1)
36+
if [ "$result" = "direct_value" ]; then
37+
pass "Direct env var works"
38+
else
39+
fail "Direct env var: expected 'direct_value', got '$result'"
40+
fi
41+
unset MEMOS_DSN
42+
}
43+
44+
# Test 2: File env var works with readable file
45+
test_file_env_var_readable() {
46+
unset MEMOS_DSN MEMOS_DSN_FILE
47+
echo "file_value" > "$TEMP_DIR/dsn_file"
48+
export MEMOS_DSN_FILE="$TEMP_DIR/dsn_file"
49+
50+
result=$("$SCRIPT_DIR/entrypoint.sh" sh -c 'echo $MEMOS_DSN' 2>&1)
51+
if [ "$result" = "file_value" ]; then
52+
pass "File env var with readable file works"
53+
else
54+
fail "File env var readable: expected 'file_value', got '$result'"
55+
fi
56+
unset MEMOS_DSN_FILE
57+
}
58+
59+
# Test 3: Error when file doesn't exist
60+
test_file_env_var_missing() {
61+
unset MEMOS_DSN MEMOS_DSN_FILE
62+
export MEMOS_DSN_FILE="$TEMP_DIR/nonexistent_file"
63+
64+
if result=$("$SCRIPT_DIR/entrypoint.sh" sh -c 'echo $MEMOS_DSN' 2>&1); then
65+
fail "Missing file should fail, but succeeded with: $result"
66+
else
67+
if echo "$result" | grep -q "does not exist or is not readable"; then
68+
pass "Missing file returns error"
69+
else
70+
fail "Missing file error message unexpected: $result"
71+
fi
72+
fi
73+
unset MEMOS_DSN_FILE
74+
}
75+
76+
# Test 4: Error when file is not readable
77+
test_file_env_var_unreadable() {
78+
unset MEMOS_DSN MEMOS_DSN_FILE
79+
echo "secret" > "$TEMP_DIR/unreadable_file"
80+
chmod 000 "$TEMP_DIR/unreadable_file"
81+
export MEMOS_DSN_FILE="$TEMP_DIR/unreadable_file"
82+
83+
if result=$("$SCRIPT_DIR/entrypoint.sh" sh -c 'echo $MEMOS_DSN' 2>&1); then
84+
fail "Unreadable file should fail, but succeeded with: $result"
85+
else
86+
if echo "$result" | grep -q "does not exist or is not readable"; then
87+
pass "Unreadable file returns error"
88+
else
89+
fail "Unreadable file error message unexpected: $result"
90+
fi
91+
fi
92+
chmod 644 "$TEMP_DIR/unreadable_file" 2>/dev/null || true
93+
unset MEMOS_DSN_FILE
94+
}
95+
96+
# Test 5: Error when both var and file are set
97+
test_both_set_error() {
98+
unset MEMOS_DSN MEMOS_DSN_FILE
99+
echo "file_value" > "$TEMP_DIR/dsn_file"
100+
export MEMOS_DSN="direct_value"
101+
export MEMOS_DSN_FILE="$TEMP_DIR/dsn_file"
102+
103+
if result=$("$SCRIPT_DIR/entrypoint.sh" sh -c 'echo $MEMOS_DSN' 2>&1); then
104+
fail "Both set should fail, but succeeded with: $result"
105+
else
106+
if echo "$result" | grep -q "are set (but are exclusive)"; then
107+
pass "Both var and file set returns error"
108+
else
109+
fail "Both set error message unexpected: $result"
110+
fi
111+
fi
112+
unset MEMOS_DSN MEMOS_DSN_FILE
113+
}
114+
115+
# Run all tests
116+
echo "Running entrypoint.sh tests..."
117+
echo "================================"
118+
119+
test_direct_env_var
120+
test_file_env_var_readable
121+
test_file_env_var_missing
122+
test_file_env_var_unreadable
123+
test_both_set_error
124+
125+
echo "================================"
126+
echo "Tests completed: ${GREEN}$pass_count passed${NC}, ${RED}$fail_count failed${NC}"
127+
128+
if [ $fail_count -gt 0 ]; then
129+
exit 1
130+
fi
131+
exit 0

0 commit comments

Comments
 (0)