Skip to content

Commit 0369070

Browse files
committed
fix(strongswan-connections): helpful error when --test points at legacy fixture path
Two follow-up cleanups on top of the three-file-to-single-file refactor in the previous commit: 1. Validate that the --test fixture file actually exists before feeding it to lib.lftest.test(), which otherwise silently returns the bogus path as stdout and leaves json.loads() to crash with an opaque "Expecting value" message. 2. Detect the three legacy suffixes (-possible_connection_keys, -active_connection_keys, -list_sas) on a missing path and include a pointer at the new single-file path in the error message, so anyone with a stale command from before the refactor gets a one-line "drop the suffix" hint instead of a generic FileNotFoundError. Also wrap the json.loads() in try/except and surface the filename on a JSONDecodeError so a corrupt fixture is no longer anonymous.
1 parent 29113c2 commit 0369070

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

check-plugins/strongswan-connections/strongswan-connections

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import argparse
1414
import json
15+
import os
1516
import re
1617
import socket
1718
import sys
@@ -312,8 +313,27 @@ def main():
312313
# derived from `list_sas` below via the same `_collect_keys`
313314
# helper the production path uses, so the test and the
314315
# production paths cannot drift.
316+
fixture_path = args.TEST[0] if args.TEST else ''
317+
if not fixture_path or not os.path.isfile(fixture_path):
318+
hint = ''
319+
for legacy in (
320+
'-possible_connection_keys',
321+
'-active_connection_keys',
322+
'-list_sas',
323+
):
324+
if fixture_path.endswith(legacy):
325+
hint = (
326+
f' (the three-file convention was removed; drop the '
327+
f'`{legacy}` suffix and pass '
328+
f'`{fixture_path[: -len(legacy)]}` instead)'
329+
)
330+
break
331+
lib.base.cu(f'Test fixture not found: {fixture_path}{hint}')
315332
stdout, _stderr, _retc = lib.lftest.test(args.TEST)
316-
fixture = json.loads(stdout)
333+
try:
334+
fixture = json.loads(stdout)
335+
except json.JSONDecodeError as e:
336+
lib.base.cu(f'Malformed JSON in test fixture {fixture_path}: {e}')
317337
list_conns = fixture.get('list_conns', [])
318338
list_sas = fixture.get('list_sas', [])
319339
possible_connection_keys = _collect_keys(list_conns)

0 commit comments

Comments
 (0)