11import { API_PROXY_PORTS } from './types' ;
22import {
33 execaError ,
4+ execaMissingCommandError ,
45 execaResult ,
56 mockedExeca ,
67 setupDefaultIptablesMocks ,
@@ -28,6 +29,8 @@ describe('host-iptables (setup)', () => {
2829 stderr : '' ,
2930 exitCode : 0 ,
3031 } ) )
32+ // Mock iptables --version
33+ . mockResolvedValueOnce ( execaResult ( ) )
3134 // Mock iptables -L DOCKER-USER (permission check)
3235 . mockRejectedValueOnce ( permissionError ) ;
3336
@@ -36,6 +39,21 @@ describe('host-iptables (setup)', () => {
3639 ) ;
3740 } ) ;
3841
42+ it ( 'should throw a clear error if iptables is not installed' , async ( ) => {
43+ mockedExeca
44+ // Mock getNetworkBridgeName
45+ . mockResolvedValueOnce ( execaResult ( { stdout : 'fw-bridge' , stderr : '' , exitCode : 0 } ) )
46+ // Mock iptables --version (missing binary)
47+ . mockRejectedValueOnce ( execaMissingCommandError ( ) ) ;
48+
49+ await expect ( setupHostIptables ( '172.30.0.10' , 3128 , [ '8.8.8.8' , '8.8.4.4' ] ) ) . rejects . toThrow (
50+ 'iptables is required but was not found'
51+ ) ;
52+
53+ expect ( mockedExeca ) . not . toHaveBeenCalledWith ( 'iptables' , [ '-t' , 'filter' , '-L' , 'DOCKER-USER' , '-n' ] , { timeout : 5000 } ) ;
54+ expect ( mockedExeca ) . not . toHaveBeenCalledWith ( 'iptables' , [ '-t' , 'filter' , '-N' , 'DOCKER-USER' ] ) ;
55+ } ) ;
56+
3957 it ( 'should create FW_WRAPPER chain and add rules' , async ( ) => {
4058 setupDefaultIptablesMocks ( { catchAllStdout : 'Chain DOCKER-USER\nChain FW_WRAPPER' } ) ;
4159
@@ -218,6 +236,8 @@ describe('host-iptables (setup)', () => {
218236 mockedExeca
219237 // Mock getNetworkBridgeName
220238 . mockResolvedValueOnce ( execaResult ( { stdout : 'fw-bridge' , stderr : '' , exitCode : 0 } ) )
239+ // Mock iptables --version
240+ . mockResolvedValueOnce ( execaResult ( ) )
221241 // Mock iptables -L DOCKER-USER (chain doesn't exist)
222242 . mockRejectedValueOnce ( noChainError )
223243 // Mock iptables -N DOCKER-USER (create chain)
@@ -475,6 +495,8 @@ describe('host-iptables (setup)', () => {
475495 mockedExeca
476496 // getNetworkBridgeName
477497 . mockResolvedValueOnce ( execaResult ( { stdout : 'fw-bridge' , stderr : '' , exitCode : 0 } ) )
498+ // iptables --version
499+ . mockResolvedValueOnce ( execaResult ( ) )
478500 // iptables -L DOCKER-USER (chain doesn't exist)
479501 . mockRejectedValueOnce ( noChainError )
480502 // iptables -N DOCKER-USER (creation fails)
@@ -512,6 +534,8 @@ describe('host-iptables (setup)', () => {
512534 mockedExeca
513535 // getNetworkBridgeName
514536 . mockResolvedValueOnce ( execaResult ( { stdout : 'fw-bridge' , exitCode : 0 } ) )
537+ // iptables --version
538+ . mockResolvedValueOnce ( execaResult ( { exitCode : 0 , stdout : '' } ) )
515539 // iptables -L DOCKER-USER (permission check) — success
516540 . mockResolvedValueOnce ( execaResult ( { exitCode : 0 , stdout : '' } ) )
517541 // iptables -L FW_WRAPPER (check if chain exists) — exists
0 commit comments