@@ -8,6 +8,7 @@ import { expect } from 'chai';
88
99import { setupTest } from './interceptor-test-utils' ;
1010import { spawnToResult } from '../../src/util/process-management' ;
11+ import { delay } from '../../src/util/promise' ;
1112
1213import { getTerminalEnvVars } from '../../src/interceptors/terminal/terminal-env-overrides' ;
1314
@@ -325,12 +326,53 @@ Successfully built <hash>
325326
326327 await stopDockerInterceptionServices ( server . port , ruleParams ) ;
327328
329+ // Intercepted container is removed:
328330 const inspectResultAfterShutdown : unknown = await docker . getContainer ( interceptedContainerId ) . inspect ( ) . catch ( e => e ) ;
329331 expect ( inspectResultAfterShutdown ) . to . be . instanceOf ( Error )
330332 expect ( ( inspectResultAfterShutdown as Error ) . message ) . to . include ( "no such container" ) ;
331333
334+ // Unintercepted container is not touched:
332335 const uninterceptedContainerAfterShutdown = await docker . getContainer ( uninterceptedContainerId ) . inspect ( ) ;
333336 expect ( uninterceptedContainerAfterShutdown . Config . Image ) . to . equal ( 'node:14' ) ;
334337 } ) ;
335338
339+ it ( "should start & clean up tunnel automatically" , async ( ) => {
340+ const { server, httpsConfig } = await testSetup ;
341+ const docker = new Docker ( ) ;
342+
343+ const terminalEnvOverrides = getTerminalEnvVars ( server . port , httpsConfig , process . env , { } , {
344+ dockerEnabled : true
345+ } ) ;
346+
347+ // Tunnel doesn't start preemptively:
348+ await delay ( 500 ) ;
349+ let tunnel = await docker . getContainer ( `httptoolkit-docker-tunnel-${ server . port } ` ) . inspect ( ) . catch ( e => e ) ;
350+ expect ( tunnel ) . to . be . instanceOf ( Error )
351+ expect ( ( tunnel as Error ) . message ) . to . include ( "no such container" ) ;
352+
353+ // Then launch an intercepted container:
354+ const interceptedResult = await spawnToResult (
355+ 'docker' , [ 'create' , 'node:14' ] ,
356+ { env : { ...process . env , ...terminalEnvOverrides } } ,
357+ true
358+ ) ;
359+
360+ expect ( interceptedResult . exitCode ) . to . equal ( 0 ) ;
361+ expect ( interceptedResult . stderr ) . to . equal ( '' ) ;
362+
363+ await delay ( 100 ) ; // There can be a brief delay in CI in starting the tunnel itself
364+
365+ // Tunnel is now running:
366+ tunnel = await docker . getContainer ( `httptoolkit-docker-tunnel-${ server . port } ` ) . inspect ( ) ;
367+ expect ( tunnel . Config . Image . split ( ':' ) [ 0 ] ) . to . equal ( 'httptoolkit/docker-socks-tunnel' ) ;
368+
369+ // Then shut everything down:
370+ await stopDockerInterceptionServices ( server . port , ruleParams ) ;
371+
372+ // Tunnel is automatically removed:
373+ tunnel = await docker . getContainer ( `httptoolkit-docker-tunnel-${ server . port } ` ) . inspect ( ) . catch ( e => e ) ;
374+ expect ( tunnel ) . to . be . instanceOf ( Error )
375+ expect ( ( tunnel as Error ) . message ) . to . include ( "no such container" ) ;
376+ } ) ;
377+
336378} ) ;
0 commit comments