testcontainers-node icon indicating copy to clipboard operation
testcontainers-node copied to clipboard

Incorrect Memory Quota Interpretation and JSON Unmarshal Error in Testcontainers Resource Limit Setting

Open wolfinj opened this issue 1 year ago • 0 comments

Expected Behaviour When setting resource limits for a Docker container using the Testcontainers library, specifying a memory quota of 0.5 should limit the container's memory to 0.5 GB (or 512 MB), and specifying a CPU quota of 1 should allocate 1 unit of the CPU without causing any errors in JSON parsing or unmarshalling.

Actual Behaviour Specifying a memory quota of 0.2 and a CPU quota of 0.3 via the .withResourcesQuota({ memory: 0.5, cpu: 1 }) method results in an unexpected 400 HTTP code error related to invalid JSON parsing. The error message indicates an inability to unmarshal the number 214748364.8 into the expected Go struct for Docker's HostConfig.Memory, which is of type int64. This discrepancy indicates a potential issue with how the Testcontainers library or Docker API interprets and formats the memory quota values for the Docker engine.

Error Message

Error: (HTTP code 400) unexpected - invalid JSON: json: cannot unmarshal number 214748364.8 into Go struct field HostConfig.HostConfig.Memory of type int64 
           at C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\[email protected]\node_modules\docker-modem\lib\modem.js:343:17
           at getCause (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\[email protected]\node_modules\docker-modem\lib\modem.js:373:7)
           at Modem.buildPayload (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\[email protected]\node_modules\docker-modem\lib\modem.js:342:5)
           at IncomingMessage.<anonymous> (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\[email protected]\node_modules\docker-modem\lib\modem.js:310:16)
           at IncomingMessage.emit (node:events:530:35)
           at endReadableNT (node:internal/streams/readable:1696:12)
           at processTicksAndRejections (node:internal/process/task_queues:82:21)

Steps to Reproduce

Set up a Node.js environment with the Testcontainers library installed.
Configure a Docker container using the Testcontainers library with the following resource quota:

javascript

const container = await new GenericContainer("mongo:latest")
    .withExposedPorts(27017)
    .withWaitStrategy(Wait.forLogMessage(/Waiting for connections/))
    .withEnvironment({
        MONGO_INITDB_ROOT_USERNAME: "user",
        MONGO_INITDB_ROOT_PASSWORD: "password"
    })
    .withResourcesQuota({
        memory: 0.2,
        cpu: 0.3
    })
    .start();

Run the container setup script.
Observe the error related to invalid JSON and the incorrect handling of the memory quota.

Environment Information

Operating System: [Windows 10]
Docker Version: [4.27.2 (137060)]
Node version: [ v20.11.1.]
Testcontainers version: [10.7.1]

Additional Context

The documentation for Testcontainers suggests that memory should be specified in gigabytes and CPU quota in units of CPUs, which was followed in the setup that led to this issue.
The error indicates a mismatch between the expected data type for the Docker engine's memory configuration and the data provided by the Testcontainers library, suggesting a potential bug in the library's handling of resource quotas or a documentation mismatch on proper usage.

wolfinj avatar Mar 21 '24 12:03 wolfinj