Skip to content

Commit 29113c2

Browse files
committed
refactor(strongswan-connections): consolidate 3-file test fixtures into single JSON
The previous test-mode layout stored three separate files per scenario (-possible_connection_keys, -active_connection_keys, -list_sas) and the plugin manipulated args.TEST by hand before passing it to lib.lftest.test(): lib.lftest.test([args.TEST[0] + '-possible_connection_keys', args.TEST[1], args.TEST[2]]) That direct index access crashed with IndexError on the natural --test=path invocation (no trailing commas), because args.TEST only had one element. Manually padding also meant the plugin bypassed lib.lftest.test's own defensive length handling. Collapse the three fixture files into a single JSON object with `list_conns` and `list_sas` keys, both holding the raw VICI "list of single-key dicts" shape. The test-mode path now calls lib.lftest.test(args.TEST) directly and derives both key lists from the fixture via the same _collect_keys() helper the production path uses, so the test and production paths cannot drift and the active_connection_keys fixture that could be out-of-sync with list_sas is gone. User-facing effect: strongswan-connections --test=stdout/all-connections-active strongswan-connections --test=stdout/all-connections-active,,0 both now work, and the table + perfdata rendering that the old fixtures silently skipped (because format_sas_data was reading bytes from str fixtures and crashing into a swallowed except) are finally exercised. Dead-code cleanup: drop get_possible_connection_keys() and get_active_connection_keys() wrappers that only existed to feed the deleted three-file convention; production path calls _collect_keys() directly on session.list_conns() and list(session.list_sas()).
1 parent 9fc0e78 commit 29113c2

21 files changed

+342
-306
lines changed

check-plugins/strongswan-connections/strongswan-connections

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def _collect_keys(entries):
126126
"""Return the sorted list of dict keys from a sequence of
127127
single-key dicts. VICI's `list_conns()` and `list_sas()` both
128128
yield one-key-per-dict entries, which makes them interchangeable
129-
for a simple key collection.
129+
for a simple key collection. The production path passes the
130+
VICI generator directly; the test path passes a pre-loaded
131+
list from the JSON fixture.
130132
"""
131133
keys = []
132134
for entry in entries:
@@ -135,16 +137,6 @@ def _collect_keys(entries):
135137
return keys
136138

137139

138-
def get_possible_connection_keys(session):
139-
"""Return the sorted list of configured VICI connection names."""
140-
return _collect_keys(session.list_conns())
141-
142-
143-
def get_active_connection_keys(session):
144-
"""Return the sorted list of currently active VICI SA names."""
145-
return _collect_keys(session.list_sas())
146-
147-
148140
def keep_connection(name, match_patterns, ignore_patterns):
149141
"""Return True if `name` should be kept by the --match / --ignore
150142
filter pair, False if it should be dropped. Include first, then
@@ -306,27 +298,26 @@ def main():
306298
lib.base.cu(f'Failed to connect to the VICI socket at {args.SOCKET}: {e}')
307299
try:
308300
session = vici.Session(s)
309-
possible_connection_keys = get_possible_connection_keys(session)
310-
active_connection_keys = get_active_connection_keys(session)
301+
list_conns = list(session.list_conns())
311302
# list_sas() returns a generator backed by the VICI
312303
# socket; materialise it to a list now so we can close
313304
# the socket cleanly before processing the data.
314305
list_sas = list(session.list_sas())
315306
finally:
316307
s.close()
317308
else:
318-
possible_connection_keys, _stderr, _retc = lib.lftest.test(
319-
[args.TEST[0] + '-possible_connection_keys', args.TEST[1], args.TEST[2]]
320-
)
321-
possible_connection_keys = json.loads(possible_connection_keys)
322-
active_connection_keys, _stderr, _retc = lib.lftest.test(
323-
[args.TEST[0] + '-active_connection_keys', args.TEST[1], args.TEST[2]]
324-
)
325-
active_connection_keys = json.loads(active_connection_keys)
326-
list_sas, _stderr, _retc = lib.lftest.test(
327-
[args.TEST[0] + '-list_sas', args.TEST[1], args.TEST[2]]
328-
)
329-
list_sas = json.loads(list_sas)
309+
# Single-file test fixture: a JSON object with `list_conns`
310+
# and `list_sas` keys, both holding the raw VICI "list of
311+
# single-key dicts" shape. `active_connection_keys` is
312+
# derived from `list_sas` below via the same `_collect_keys`
313+
# helper the production path uses, so the test and the
314+
# production paths cannot drift.
315+
stdout, _stderr, _retc = lib.lftest.test(args.TEST)
316+
fixture = json.loads(stdout)
317+
list_conns = fixture.get('list_conns', [])
318+
list_sas = fixture.get('list_sas', [])
319+
possible_connection_keys = _collect_keys(list_conns)
320+
active_connection_keys = _collect_keys(list_sas)
330321

331322
# Apply the --match / --ignore filter to both the configured and
332323
# the active connection lists. Filtering before the "configured
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"list_conns": [
3+
{"conn2": {}}
4+
],
5+
"list_sas": [
6+
{
7+
"conn2": {
8+
"uniqueid": "17",
9+
"version": "2",
10+
"state": "ESTABLISHED",
11+
"local-host": "x.x.x.x",
12+
"local-port": "500",
13+
"local-id": "local.vpn.id",
14+
"remote-host": "x.x.x.x",
15+
"remote-port": "500",
16+
"remote-id": "remote.vpn.id",
17+
"initiator-spi": "508fc57fbeaba4dc",
18+
"responder-spi": "9f58965873215d33",
19+
"if-id-in": "0000126a",
20+
"if-id-out": "0000126a",
21+
"encr-alg": "AES_GCM_16",
22+
"encr-keysize": "256",
23+
"prf-alg": "PRF_HMAC_SHA2_256",
24+
"dh-group": "ECP_521",
25+
"established": "22668",
26+
"rekey-time": "61895",
27+
"child-sas": {
28+
"xxxx-11": {
29+
"name": "xxxx",
30+
"uniqueid": "11",
31+
"reqid": "1",
32+
"state": "INSTALLED",
33+
"mode": "TUNNEL",
34+
"protocol": "ESP",
35+
"spi-in": "c442df3d",
36+
"spi-out": "91165bb6",
37+
"if-id-in": "0000126a",
38+
"if-id-out": "0000126a",
39+
"encr-alg": "AES_GCM_16",
40+
"encr-keysize": "256",
41+
"dh-group": "ECP_521",
42+
"bytes-in": "114397277",
43+
"packets-in": "85669",
44+
"use-in": "1",
45+
"bytes-out": "4114924",
46+
"packets-out": "54769",
47+
"use-out": "1",
48+
"rekey-time": "62222",
49+
"life-time": "72596",
50+
"install-time": "22444",
51+
"local-ts": ["x.x.x.x/24"],
52+
"remote-ts": ["x.x.x.x/24"]
53+
}
54+
}
55+
}
56+
}
57+
]
58+
}

check-plugins/strongswan-connections/unit-test/stdout/all-connections-active-active_connection_keys

Lines changed: 0 additions & 1 deletion
This file was deleted.

check-plugins/strongswan-connections/unit-test/stdout/all-connections-active-list_sas

Lines changed: 0 additions & 53 deletions
This file was deleted.

check-plugins/strongswan-connections/unit-test/stdout/all-connections-active-possible_connection_keys

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)