11import { ContainerRuntimeClient , getContainerRuntimeClient } from "../container-runtime" ;
2+ import { RandomUniquePortGenerator } from "../utils/port-generator" ;
23
34describe ( "Reaper" , { timeout : 120_000 } , ( ) => {
45 let client : ContainerRuntimeClient ;
@@ -12,6 +13,42 @@ describe("Reaper", { timeout: 120_000 }, () => {
1213 client = await getContainerRuntimeClient ( ) ;
1314 } ) ;
1415
16+ it ( "should create disabled reaper when TESTCONTAINERS_RYUK_DISABLED=true" , async ( ) => {
17+ vitest . stubEnv ( "TESTCONTAINERS_RYUK_DISABLED" , "true" ) ;
18+
19+ vi . spyOn ( client . container , "list" ) . mockResolvedValue ( [ ] ) ;
20+ const reaper = await getReaper ( ) ;
21+
22+ // DisabledReaper has an empty containerId
23+ expect ( reaper . containerId ) . toBe ( "" ) ;
24+
25+ // Should not throw exceptions when methods are called
26+ expect ( ( ) => reaper . addSession ( "test-session" ) ) . not . toThrow ( ) ;
27+ expect ( ( ) => reaper . addComposeProject ( "test-project" ) ) . not . toThrow ( ) ;
28+ } ) ;
29+
30+ it ( "should use custom port when TESTCONTAINERS_RYUK_PORT is set" , async ( ) => {
31+ const customPort = ( await new RandomUniquePortGenerator ( ) . generatePort ( ) ) . toString ( ) ;
32+ vitest . stubEnv ( "TESTCONTAINERS_RYUK_PORT" , customPort ) ;
33+
34+ vi . spyOn ( client . container , "list" ) . mockResolvedValue ( [ ] ) ;
35+ const reaper = await getReaper ( ) ;
36+
37+ const reaperContainer = client . container . getById ( reaper . containerId ) ;
38+ const ports = ( await reaperContainer . inspect ( ) ) . HostConfig . PortBindings ;
39+ expect ( ports [ "8080" ] [ 0 ] . HostPort ) . toBe ( customPort ) ;
40+ } ) ;
41+
42+ it ( "should reuse existing reaper container if one is already running" , async ( ) => {
43+ const reaper = await getReaper ( ) ;
44+ const reaperContainerInfo = ( await client . container . list ( ) ) . filter ( ( c ) => c . Id === reaper . containerId ) [ 0 ] ;
45+ vi . spyOn ( client . container , "list" ) . mockResolvedValue ( [ reaperContainerInfo ] ) ;
46+
47+ const reaper2 = await getReaper ( ) ;
48+
49+ expect ( reaper2 . containerId ) . toBe ( reaper . containerId ) ;
50+ } ) ;
51+
1552 it ( "should create Reaper container without RYUK_VERBOSE env var by default" , async ( ) => {
1653 vi . spyOn ( client . container , "list" ) . mockResolvedValue ( [ ] ) ;
1754 const reaper = await getReaper ( ) ;
0 commit comments