Skip to content

Commit a6599d3

Browse files
authored
refactor(tests): extract setupDefaultIptablesMocks to eliminate repeated iptables mock sequences (#3140)
* Initial plan * refactor: extract setupDefaultIptablesMocks helper to eliminate duplicate iptables mock setup --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 66849f0 commit a6599d3

3 files changed

Lines changed: 45 additions & 259 deletions

File tree

src/host-iptables-host-access.test.ts

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execaResult, mockedExeca, setupHostIptablesTestSuite } from './test-helpers/host-iptables-test-setup';
1+
import { mockedExeca, setupDefaultIptablesMocks, setupHostIptablesTestSuite } from './test-helpers/host-iptables-test-setup';
22
import { HostAccessConfig, setupHostIptables } from './host-iptables';
33
import { _testing } from './host-iptables-shared';
44

@@ -7,13 +7,7 @@ describe('host-iptables (host access)', () => {
77

88
describe('setupHostIptables with host access', () => {
99
it('should add gateway ACCEPT rules when hostAccess is enabled', async () => {
10-
mockedExeca
11-
// Mock getNetworkBridgeName
12-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
13-
// Mock iptables -L DOCKER-USER (permission check)
14-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
15-
// Mock chain existence check (doesn't exist)
16-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
10+
setupDefaultIptablesMocks();
1711

1812
// Default mock for all subsequent calls; getDockerBridgeGateway returns 172.17.0.1
1913
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
@@ -55,12 +49,7 @@ describe('host-iptables (host access)', () => {
5549
});
5650

5751
it('should not add gateway rules when hostAccess is undefined', async () => {
58-
mockedExeca
59-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
60-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
61-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
62-
63-
mockedExeca.mockResolvedValue(execaResult({ stdout: '', stderr: '', exitCode: 0 }));
52+
setupDefaultIptablesMocks();
6453

6554
await setupHostIptables('172.30.0.10', 3128, ['8.8.8.8', '8.8.4.4']);
6655

@@ -74,10 +63,7 @@ describe('host-iptables (host access)', () => {
7463
});
7564

7665
it('should add custom port rules when allowHostPorts is specified', async () => {
77-
mockedExeca
78-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
79-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
80-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
66+
setupDefaultIptablesMocks();
8167

8268
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
8369
if (cmd === 'docker' && args.includes('bridge')) {
@@ -115,10 +101,7 @@ describe('host-iptables (host access)', () => {
115101
});
116102

117103
it('should only use AWF gateway when Docker bridge gateway is null', async () => {
118-
mockedExeca
119-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
120-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
121-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
104+
setupDefaultIptablesMocks();
122105

123106
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
124107
// Make getDockerBridgeGateway return null (docker network inspect bridge fails)
@@ -147,10 +130,7 @@ describe('host-iptables (host access)', () => {
147130
});
148131

149132
it('should only add default ports when allowHostPorts is empty', async () => {
150-
mockedExeca
151-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
152-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
153-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
133+
setupDefaultIptablesMocks();
154134

155135
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
156136
if (cmd === 'docker' && args.includes('bridge')) {
@@ -176,10 +156,7 @@ describe('host-iptables (host access)', () => {
176156
});
177157

178158
it('should support port ranges in allowHostPorts', async () => {
179-
mockedExeca
180-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
181-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
182-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
159+
setupDefaultIptablesMocks();
183160

184161
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
185162
if (cmd === 'docker' && args.includes('bridge')) {
@@ -205,10 +182,7 @@ describe('host-iptables (host access)', () => {
205182
});
206183

207184
it('should skip invalid ports in allowHostPorts', async () => {
208-
mockedExeca
209-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
210-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
211-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
185+
setupDefaultIptablesMocks();
212186

213187
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
214188
if (cmd === 'docker' && args.includes('bridge')) {
@@ -245,10 +219,7 @@ describe('host-iptables (host access)', () => {
245219
});
246220

247221
it('should deduplicate ports when custom ports overlap with defaults', async () => {
248-
mockedExeca
249-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
250-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
251-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
222+
setupDefaultIptablesMocks();
252223

253224
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
254225
if (cmd === 'docker' && args.includes('bridge')) {
@@ -282,10 +253,7 @@ describe('host-iptables (host access)', () => {
282253
});
283254

284255
it('should add service port rules when allowHostServicePorts is specified', async () => {
285-
mockedExeca
286-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
287-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
288-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
256+
setupDefaultIptablesMocks();
289257

290258
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
291259
if (cmd === 'docker' && args.includes('bridge')) {
@@ -321,10 +289,7 @@ describe('host-iptables (host access)', () => {
321289
});
322290

323291
it('should deduplicate service ports with regular host ports', async () => {
324-
mockedExeca
325-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
326-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
327-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
292+
setupDefaultIptablesMocks();
328293

329294
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
330295
if (cmd === 'docker' && args.includes('bridge')) {
@@ -364,10 +329,7 @@ describe('host-iptables (host access)', () => {
364329

365330
describe('getDockerBridgeGateway invalid IPv4', () => {
366331
it('should skip gateway rule when Docker bridge returns non-IPv4 gateway', async () => {
367-
mockedExeca
368-
.mockResolvedValueOnce(execaResult({ stdout: 'fw-bridge', stderr: '', exitCode: 0 }))
369-
.mockResolvedValueOnce(execaResult({ stdout: '', stderr: '', exitCode: 0 }))
370-
.mockResolvedValueOnce(execaResult({ exitCode: 1 }));
332+
setupDefaultIptablesMocks();
371333

372334
mockedExeca.mockImplementation(((cmd: string, args: string[]) => {
373335
// Return non-IPv4 (e.g. IPv6 address) from Docker bridge gateway

0 commit comments

Comments
 (0)