Skip to content

Commit 14ff881

Browse files
committed
Allow plus symbol in SERVER_NAME validation
- Update regex in validate_server_name to include '+' - Update corresponding validation error message - Add BATS test case for SERVER_NAME containing '+' - Apply shfmt auto-formatting across BATS test files
1 parent 2a0f989 commit 14ff881

7 files changed

Lines changed: 226 additions & 209 deletions

File tree

opt/monitoring/lib/validation.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ validate_server_name() {
355355
printf "SERVER_NAME is not set\n" >&2
356356
return 1
357357
}
358-
[[ ${val} =~ ^[a-zA-Z0-9\ ._-]+$ ]] || {
359-
printf "SERVER_NAME format is invalid (allowed: a-z, A-Z, 0-9, space, ., _, -)\n" >&2
358+
[[ ${val} =~ ^[a-zA-Z0-9\ ._+-]+$ ]] || {
359+
printf "SERVER_NAME format is invalid (allowed: a-z, A-Z, 0-9, space, ., _, -, +)\n" >&2
360360
return 1
361361
}
362362
return 0

tests/common.bats

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ setup() {
2222
@test "common: die prints ERROR to stderr and exits 1" {
2323
run die "something broke"
2424
[ "$status" -eq 1 ]
25-
[[ "$output" == *"ERROR: something broke"* ]]
25+
[[ $output == *"ERROR: something broke"* ]]
2626
}
2727

2828
@test "common: die handles multiple arguments" {
2929
run die "disk full" "on /dev/sda1"
3030
[ "$status" -eq 1 ]
31-
[[ "$output" == *"ERROR: disk full on /dev/sda1"* ]]
31+
[[ $output == *"ERROR: disk full on /dev/sda1"* ]]
3232
}
3333

3434
@test "common: die handles empty message" {
3535
run die
3636
[ "$status" -eq 1 ]
37-
[[ "$output" == *"ERROR:"* ]]
37+
[[ $output == *"ERROR:"* ]]
3838
}
3939

4040
@test "common: info prints INFO to stderr" {
4141
run info "startup complete"
4242
[ "$status" -eq 0 ]
43-
[[ "$output" == *"INFO: startup complete"* ]]
43+
[[ $output == *"INFO: startup complete"* ]]
4444
}
4545

4646
@test "common: info handles multiple arguments" {
4747
run info "loaded" "5 modules"
4848
[ "$status" -eq 0 ]
49-
[[ "$output" == *"INFO: loaded 5 modules"* ]]
49+
[[ $output == *"INFO: loaded 5 modules"* ]]
5050
}
5151

5252
@test "common: debug is silent when VERBOSE=0" {
@@ -69,7 +69,7 @@ setup() {
6969
VERBOSE=1
7070
run debug "trace message"
7171
[ "$status" -eq 0 ]
72-
[[ "$output" == *"DEBUG: trace message"* ]]
72+
[[ $output == *"DEBUG: trace message"* ]]
7373
}
7474

7575
@test "common: check_deps succeeds with no arguments" {
@@ -79,7 +79,7 @@ setup() {
7979

8080
@test "common: check_deps succeeds when all commands exist" {
8181
command() {
82-
if [[ "$1" == "-v" ]]; then return 0; fi
82+
if [[ $1 == "-v" ]]; then return 0; fi
8383
builtin command "$@"
8484
}
8585
run check_deps bash cat ls
@@ -88,25 +88,25 @@ setup() {
8888

8989
@test "common: check_deps fails with single missing command" {
9090
command() {
91-
if [[ "$1" == "-v" && "$2" == "nonexistent_cmd" ]]; then return 1; fi
92-
if [[ "$1" == "-v" ]]; then return 0; fi
91+
if [[ $1 == "-v" && $2 == "nonexistent_cmd" ]]; then return 1; fi
92+
if [[ $1 == "-v" ]]; then return 0; fi
9393
builtin command "$@"
9494
}
9595
run check_deps bash nonexistent_cmd
9696
[ "$status" -eq 1 ]
97-
[[ "$output" == *"Missing required commands: nonexistent_cmd"* ]]
97+
[[ $output == *"Missing required commands: nonexistent_cmd"* ]]
9898
}
9999

100100
@test "common: check_deps reports multiple missing commands" {
101101
command() {
102-
if [[ "$1" == "-v" && ("$2" == "foo" || "$2" == "bar") ]]; then return 1; fi
103-
if [[ "$1" == "-v" ]]; then return 0; fi
102+
if [[ $1 == "-v" && ($2 == "foo" || $2 == "bar") ]]; then return 1; fi
103+
if [[ $1 == "-v" ]]; then return 0; fi
104104
builtin command "$@"
105105
}
106106
run check_deps foo bar
107107
[ "$status" -eq 1 ]
108-
[[ "$output" == *"foo"* ]]
109-
[[ "$output" == *"bar"* ]]
108+
[[ $output == *"foo"* ]]
109+
[[ $output == *"bar"* ]]
110110
}
111111

112112
@test "common: load_senders sources sender modules" {
@@ -162,25 +162,25 @@ setup() {
162162
@test "common: get_available_notifiers without prefix returns all notifiers" {
163163
run get_available_notifiers
164164
[ "$status" -eq 0 ]
165-
[[ "$output" == *"telegram"* ]]
166-
[[ "$output" == *"matrix"* ]]
167-
[[ "$output" == *"ntfy"* ]]
165+
[[ $output == *"telegram"* ]]
166+
[[ $output == *"matrix"* ]]
167+
[[ $output == *"ntfy"* ]]
168168
}
169169

170170
@test "common: get_available_notifiers with login- prefix returns login notifiers" {
171171
run get_available_notifiers "login-"
172172
[ "$status" -eq 0 ]
173-
[[ "$output" == *"telegram"* ]]
174-
[[ "$output" == *"matrix"* ]]
175-
[[ "$output" == *"ntfy"* ]]
173+
[[ $output == *"telegram"* ]]
174+
[[ $output == *"matrix"* ]]
175+
[[ $output == *"ntfy"* ]]
176176
}
177177

178178
@test "common: get_available_notifiers with high-load- prefix returns high-load notifiers" {
179179
run get_available_notifiers "high-load-"
180180
[ "$status" -eq 0 ]
181-
[[ "$output" == *"telegram"* ]]
182-
[[ "$output" == *"matrix"* ]]
183-
[[ "$output" == *"ntfy"* ]]
181+
[[ $output == *"telegram"* ]]
182+
[[ $output == *"matrix"* ]]
183+
[[ $output == *"ntfy"* ]]
184184
}
185185

186186
@test "common: get_available_notifiers with nonexistent prefix returns empty" {
@@ -192,20 +192,20 @@ setup() {
192192
@test "common: get_configured_notifiers returns telegram when BOT_TOKEN set" {
193193
BOT_TOKEN="000000000:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
194194
run get_configured_notifiers
195-
[[ "$output" == *"telegram"* ]]
195+
[[ $output == *"telegram"* ]]
196196
}
197197

198198
@test "common: get_configured_notifiers returns matrix when MATRIX_URL set" {
199199
MATRIX_URL="https://matrix.example.com"
200200
run get_configured_notifiers
201-
[[ "$output" == *"matrix"* ]]
201+
[[ $output == *"matrix"* ]]
202202
}
203203

204204
@test "common: get_configured_notifiers returns ntfy when NTFY_URL set" {
205205
NTFY_URL="https://ntfy.sh"
206206
NTFY_TOPIC="test"
207207
run get_configured_notifiers
208-
[[ "$output" == *"ntfy"* ]]
208+
[[ $output == *"ntfy"* ]]
209209
}
210210

211211
@test "common: get_configured_notifiers returns all when all configured" {
@@ -214,9 +214,9 @@ setup() {
214214
NTFY_URL="https://ntfy.sh"
215215
NTFY_TOPIC="test"
216216
run get_configured_notifiers
217-
[[ "$output" == *"telegram"* ]]
218-
[[ "$output" == *"matrix"* ]]
219-
[[ "$output" == *"ntfy"* ]]
217+
[[ $output == *"telegram"* ]]
218+
[[ $output == *"matrix"* ]]
219+
[[ $output == *"ntfy"* ]]
220220
}
221221

222222
@test "common: get_configured_notifiers returns empty when nothing set" {

tests/high-load-messages.bats

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ setup() {
2828
@test "high-load-messages: _tg_escape escapes underscores" {
2929
run _tg_escape "my_server_name"
3030
[ "$status" -eq 0 ]
31-
[[ "$output" == *"my\_server\_name"* ]]
31+
[[ $output == *"my\_server\_name"* ]]
3232
}
3333

3434
@test "high-load-messages: _tg_escape escapes asterisks and backticks" {
35-
run _tg_escape "test*bold*and\`code\`"
35+
run _tg_escape 'test*bold*and`code`'
3636
[ "$status" -eq 0 ]
37-
[[ "$output" != *'*bold*'* ]] || [[ "$output" == *'\*'* ]]
37+
[[ $output != *'*bold*'* ]] || [[ $output == *'\*'* ]]
3838
}
3939

4040
@test "high-load-messages: _tg_escape preserves plain text" {
@@ -47,140 +47,140 @@ setup() {
4747
PSI_AVAILABLE=1
4848
run _tg_psi_section
4949
[ "$status" -eq 0 ]
50-
[[ "$output" == *"PSI"* ]]
51-
[[ "$output" == *"CPU some"* ]]
52-
[[ "$output" == *"IO some"* ]]
53-
[[ "$output" == *"Mem some"* ]]
54-
[[ "$output" == *"25.50"* ]]
50+
[[ $output == *"PSI"* ]]
51+
[[ $output == *"CPU some"* ]]
52+
[[ $output == *"IO some"* ]]
53+
[[ $output == *"Mem some"* ]]
54+
[[ $output == *"25.50"* ]]
5555
}
5656

5757
@test "high-load-messages: _tg_psi_section shows unavailable when PSI off" {
5858
PSI_AVAILABLE=0
5959
run _tg_psi_section
6060
[ "$status" -eq 0 ]
61-
[[ "$output" == *"unavailable"* ]]
61+
[[ $output == *"unavailable"* ]]
6262
}
6363

6464
@test "high-load-messages: telegram message contains all metric fields" {
6565
run high_load_message_telegram
6666
[ "$status" -eq 0 ]
67-
[[ "$output" == *"High Load"* ]]
68-
[[ "$output" == *"12.50"* ]]
69-
[[ "$output" == *"92.5"* ]]
70-
[[ "$output" == *"14.20"* ]]
71-
[[ "$output" == *"16.00"* ]]
72-
[[ "$output" == *"25.30"* ]]
73-
[[ "$output" == *"150.20"* ]]
74-
[[ "$output" == *"18.50"* ]]
67+
[[ $output == *"High Load"* ]]
68+
[[ $output == *"12.50"* ]]
69+
[[ $output == *"92.5"* ]]
70+
[[ $output == *"14.20"* ]]
71+
[[ $output == *"16.00"* ]]
72+
[[ $output == *"25.30"* ]]
73+
[[ $output == *"150.20"* ]]
74+
[[ $output == *"18.50"* ]]
7575
}
7676

7777
@test "high-load-messages: telegram message includes failed services when set" {
7878
FAILED_SERVICES="nginx.service, mysql.service"
7979
run high_load_message_telegram
8080
[ "$status" -eq 0 ]
81-
[[ "$output" == *"Failed Services"* ]]
82-
[[ "$output" == *"nginx"* ]]
81+
[[ $output == *"Failed Services"* ]]
82+
[[ $output == *"nginx"* ]]
8383
}
8484

8585
@test "high-load-messages: telegram message omits failed services when empty" {
8686
FAILED_SERVICES=""
8787
run high_load_message_telegram
8888
[ "$status" -eq 0 ]
89-
[[ "$output" != *"Failed Services"* ]]
89+
[[ $output != *"Failed Services"* ]]
9090
}
9191

9292
@test "high-load-messages: _mx_psi_html contains HTML tags when PSI available" {
9393
PSI_AVAILABLE=1
9494
run _mx_psi_html
9595
[ "$status" -eq 0 ]
96-
[[ "$output" == *"<strong>"* ]]
97-
[[ "$output" == *"<br>"* ]]
98-
[[ "$output" == *"CPU some"* ]]
96+
[[ $output == *"<strong>"* ]]
97+
[[ $output == *"<br>"* ]]
98+
[[ $output == *"CPU some"* ]]
9999
}
100100

101101
@test "high-load-messages: _mx_psi_html shows unavailable when PSI off" {
102102
PSI_AVAILABLE=0
103103
run _mx_psi_html
104104
[ "$status" -eq 0 ]
105-
[[ "$output" == *"<em>"* ]]
106-
[[ "$output" == *"unavailable"* ]]
105+
[[ $output == *"<em>"* ]]
106+
[[ $output == *"unavailable"* ]]
107107
}
108108

109109
@test "high-load-messages: _mx_psi_plain shows plain text when PSI available" {
110110
PSI_AVAILABLE=1
111111
run _mx_psi_plain
112112
[ "$status" -eq 0 ]
113-
[[ "$output" == *"CPU some"* ]]
114-
[[ "$output" == *"IO full"* ]]
115-
[[ "$output" == *"Mem full"* ]]
113+
[[ $output == *"CPU some"* ]]
114+
[[ $output == *"IO full"* ]]
115+
[[ $output == *"Mem full"* ]]
116116
}
117117

118118
@test "high-load-messages: _mx_psi_plain shows unavailable when PSI off" {
119119
PSI_AVAILABLE=0
120120
run _mx_psi_plain
121121
[ "$status" -eq 0 ]
122-
[[ "$output" == *"unavailable"* ]]
122+
[[ $output == *"unavailable"* ]]
123123
}
124124

125125
@test "high-load-messages: matrix plain contains all fields" {
126126
run high_load_message_matrix_plain
127127
[ "$status" -eq 0 ]
128-
[[ "$output" == *"prod-server-01"* ]]
129-
[[ "$output" == *"12.50"* ]]
130-
[[ "$output" == *"92.5"* ]]
131-
[[ "$output" == *"Root FS"* ]]
128+
[[ $output == *"prod-server-01"* ]]
129+
[[ $output == *"12.50"* ]]
130+
[[ $output == *"92.5"* ]]
131+
[[ $output == *"Root FS"* ]]
132132
}
133133

134134
@test "high-load-messages: matrix html contains HTML markup" {
135135
run high_load_message_matrix_html
136136
[ "$status" -eq 0 ]
137-
[[ "$output" == *"<strong>"* ]]
138-
[[ "$output" == *"<br>"* ]]
139-
[[ "$output" == *"prod-server-01"* ]]
140-
[[ "$output" == *"CPU"* ]]
137+
[[ $output == *"<strong>"* ]]
138+
[[ $output == *"<br>"* ]]
139+
[[ $output == *"prod-server-01"* ]]
140+
[[ $output == *"CPU"* ]]
141141
}
142142

143143
@test "high-load-messages: matrix plain includes failed services when set" {
144144
FAILED_SERVICES="redis.service"
145145
run high_load_message_matrix_plain
146-
[[ "$output" == *"Failed Services"* ]]
147-
[[ "$output" == *"redis"* ]]
146+
[[ $output == *"Failed Services"* ]]
147+
[[ $output == *"redis"* ]]
148148
}
149149

150150
@test "high-load-messages: _ntfy_psi_section shows data when PSI available" {
151151
PSI_AVAILABLE=1
152152
run _ntfy_psi_section
153153
[ "$status" -eq 0 ]
154-
[[ "$output" == *"CPU some"* ]]
155-
[[ "$output" == *"IO full"* ]]
154+
[[ $output == *"CPU some"* ]]
155+
[[ $output == *"IO full"* ]]
156156
}
157157

158158
@test "high-load-messages: _ntfy_psi_section shows unavailable when PSI off" {
159159
PSI_AVAILABLE=0
160160
run _ntfy_psi_section
161161
[ "$status" -eq 0 ]
162-
[[ "$output" == *"unavailable"* ]]
162+
[[ $output == *"unavailable"* ]]
163163
}
164164

165165
@test "high-load-messages: ntfy message contains all fields" {
166166
run high_load_message_ntfy
167167
[ "$status" -eq 0 ]
168-
[[ "$output" == *"12.50"* ]]
169-
[[ "$output" == *"92.5"* ]]
170-
[[ "$output" == *"14.20"* ]]
171-
[[ "$output" == *"Root FS"* ]]
168+
[[ $output == *"12.50"* ]]
169+
[[ $output == *"92.5"* ]]
170+
[[ $output == *"14.20"* ]]
171+
[[ $output == *"Root FS"* ]]
172172
}
173173

174174
@test "high-load-messages: ntfy title contains server name" {
175175
run high_load_title_ntfy
176176
[ "$status" -eq 0 ]
177-
[[ "$output" == *"High Load"* ]]
178-
[[ "$output" == *"prod-server-01"* ]]
177+
[[ $output == *"High Load"* ]]
178+
[[ $output == *"prod-server-01"* ]]
179179
}
180180

181181
@test "high-load-messages: ntfy message includes failed services" {
182182
FAILED_SERVICES="postgres.service"
183183
run high_load_message_ntfy
184-
[[ "$output" == *"Failed Services"* ]]
185-
[[ "$output" == *"postgres"* ]]
184+
[[ $output == *"Failed Services"* ]]
185+
[[ $output == *"postgres"* ]]
186186
}

0 commit comments

Comments
 (0)