Skip to content

Commit fe5d9b7

Browse files
committed
Enhance alert management logic in test_alertManager.sh
- Introduced flags to track the presence of -t and -A arguments, ensuring proper handling of tabular output format. - Improved query normalization for better matching of alert status updates, specifically for acknowledged and resolved states. - Updated logic to check for RETURNING clauses in queries, allowing for accurate return values based on the expected output format. - Enhanced backward compatibility by refining how arguments are parsed and processed, ensuring consistent behavior across different input scenarios.
1 parent 15d9e52 commit fe5d9b7

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

tests/unit/alerts/test_alertManager.sh

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ setup() {
7070
local query=""
7171
local is_tab_format=false
7272
local all_args="${*}"
73+
local has_t=false
74+
local has_a=false
7375

7476
# Extract query from arguments
7577
# psql can be called as: psql -h host -p port -U user -d db -c "query"
@@ -83,13 +85,26 @@ setup() {
8385
break
8486
fi
8587
;;
86-
-t|-A)
87-
is_tab_format=true
88+
-t)
89+
has_t=true
90+
;;
91+
-A)
92+
has_a=true
8893
;;
8994
esac
9095
((i++))
9196
done
9297

98+
# Set is_tab_format only if both -t and -A are present
99+
if [[ "${has_t}" == "true" ]] && [[ "${has_a}" == "true" ]]; then
100+
is_tab_format=true
101+
fi
102+
103+
# Also check all_args as fallback for cases where args are parsed differently
104+
if [[ "${all_args}" =~ -t.*-A ]] || [[ "${all_args}" =~ -A.*-t ]]; then
105+
is_tab_format=true
106+
fi
107+
93108
# If no -c found, treat all args as query (for backward compatibility)
94109
# This handles cases where psql is called without -c flag
95110
if [[ -z "${query}" ]]; then
@@ -198,46 +213,57 @@ setup() {
198213

199214
# Handle UPDATE operations (for acknowledge/resolve)
200215
if [[ "${query}" =~ UPDATE.*alerts ]]; then
216+
# Normalize query for matching (handle multiline queries)
217+
local query_normalized
218+
query_normalized=$(echo "${query}" | tr '\n' ' ' | tr -s ' ')
219+
201220
# Read current mock status from file
202221
local current_status
203222
current_status=$(cat "${MOCK_STATUS_FILE:-${TEST_LOG_DIR}/.mock_alert_status}" 2>/dev/null || echo "active")
204223

205224
# Check if it's a valid UUID
206-
if [[ "${query}" =~ 00000000-0000-0000-0000-000000000000 ]] || [[ "${query}" =~ invalid-uuid ]]; then
225+
if [[ "${query_normalized}" =~ 00000000-0000-0000-0000-000000000000 ]] || [[ "${query_normalized}" =~ invalid-uuid ]]; then
207226
# Invalid UUID - return empty (no rows updated)
208227
return 0
209228
fi
210-
# Check if updating to acknowledged
211-
if [[ "${query}" =~ status.*=.*acknowledged ]]; then
212-
# acknowledge_alert only updates if status = 'active'
213-
# Check if alert is already acknowledged or resolved
214-
if [[ "${current_status}" == "acknowledged" ]] || [[ "${current_status}" == "resolved" ]]; then
215-
# Already acknowledged/resolved - return empty (no rows updated)
216-
return 0
217-
fi
218-
# Update mock status in file
219-
echo "acknowledged" > "${MOCK_STATUS_FILE:-${TEST_LOG_DIR}/.mock_alert_status}"
220-
# Return alert ID (RETURNING id) - check for -t -A format in args or all_args
221-
if [[ "${is_tab_format}" == "true" ]] || [[ "${all_args}" =~ -t.*-A ]] || [[ "${all_args}" =~ -A.*-t ]] || [[ "${*}" =~ -t.*-A ]] || [[ "${*}" =~ -A.*-t ]]; then
222-
echo "00000000-0000-0000-0000-000000000001"
223-
fi
224-
return 0
225-
fi
226-
# Check if updating to resolved
227-
if [[ "${query}" =~ status.*=.*resolved ]]; then
229+
# Check if updating to resolved FIRST (before acknowledged, since resolved query contains "acknowledged" in WHERE clause)
230+
# Check both normalized and original query, and ensure it's in SET clause, not WHERE clause
231+
if [[ "${query_normalized}" =~ SET.*status.*resolved ]] || [[ "${query}" =~ SET.*status.*[\"']resolved[\"'] ]] || ([[ "${query_normalized}" =~ resolved ]] && [[ "${query_normalized}" =~ SET.*status ]] && ! [[ "${query_normalized}" =~ WHERE.*resolved ]]); then
228232
# resolve_alert only updates if status IN ('active', 'acknowledged')
229233
# Check if alert is already resolved
230234
if [[ "${current_status}" == "resolved" ]]; then
231235
# Already resolved - return empty (no rows updated)
232-
if [[ "${is_tab_format}" == "true" ]] || [[ "${all_args}" =~ -t.*-A ]] || [[ "${all_args}" =~ -A.*-t ]] || [[ "${*}" =~ -t.*-A ]] || [[ "${*}" =~ -A.*-t ]]; then
236+
# If query has RETURNING, return empty string for -t -A format
237+
if [[ "${query}" =~ RETURNING ]] || [[ "${query_normalized}" =~ RETURNING ]]; then
233238
echo "" # Return empty string for -t -A format
234239
fi
235240
return 0
236241
fi
237242
# Update mock status in file BEFORE returning result
238243
echo "resolved" > "${MOCK_STATUS_FILE:-${TEST_LOG_DIR}/.mock_alert_status}"
239-
# Return alert ID (RETURNING id) - check for -t -A format in args or all_args
240-
if [[ "${is_tab_format}" == "true" ]] || [[ "${all_args}" =~ -t.*-A ]] || [[ "${all_args}" =~ -A.*-t ]] || [[ "${*}" =~ -t.*-A ]] || [[ "${*}" =~ -A.*-t ]]; then
244+
# Return alert ID if query has RETURNING (which indicates -t -A format is expected)
245+
if [[ "${query}" =~ RETURNING ]] || [[ "${query_normalized}" =~ RETURNING ]]; then
246+
echo "00000000-0000-0000-0000-000000000001"
247+
fi
248+
return 0
249+
fi
250+
# Check if updating to acknowledged (check both normalized and original query)
251+
# Make sure it's in SET clause, not WHERE clause
252+
if [[ "${query_normalized}" =~ SET.*status.*acknowledged ]] || [[ "${query}" =~ SET.*status.*[\"']acknowledged[\"'] ]] || ([[ "${query_normalized}" =~ acknowledged ]] && [[ "${query_normalized}" =~ SET.*status ]] && ! [[ "${query_normalized}" =~ WHERE.*acknowledged ]]); then
253+
# acknowledge_alert only updates if status = 'active'
254+
# Check if alert is already acknowledged or resolved
255+
if [[ "${current_status}" == "acknowledged" ]] || [[ "${current_status}" == "resolved" ]]; then
256+
# Already acknowledged/resolved - return empty (no rows updated)
257+
# If query has RETURNING, return empty string for -t -A format
258+
if [[ "${query}" =~ RETURNING ]] || [[ "${query_normalized}" =~ RETURNING ]]; then
259+
echo "" # Return empty string for -t -A format
260+
fi
261+
return 0
262+
fi
263+
# Update mock status in file
264+
echo "acknowledged" > "${MOCK_STATUS_FILE:-${TEST_LOG_DIR}/.mock_alert_status}"
265+
# Return alert ID if query has RETURNING (which indicates -t -A format is expected)
266+
if [[ "${query}" =~ RETURNING ]] || [[ "${query_normalized}" =~ RETURNING ]]; then
241267
echo "00000000-0000-0000-0000-000000000001"
242268
fi
243269
return 0

0 commit comments

Comments
 (0)