Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wolfinj opened this issue Mar 21, 2024 · 0 comments
Labels
triage Investigation required

Comments

@wolfinj
Copy link

wolfinj commented Mar 21, 2024

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\docker-modem@3.0.8\node_modules\docker-modem\lib\modem.js:343:17
           at getCause (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\docker-modem@3.0.8\node_modules\docker-modem\lib\modem.js:373:7)
           at Modem.buildPayload (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\docker-modem@3.0.8\node_modules\docker-modem\lib\modem.js:342:5)
           at IncomingMessage.<anonymous> (C:\projects\node.js\***.FunctionalTests\node_modules\.pnpm\docker-modem@3.0.8\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.
@cristianrgreco cristianrgreco added the triage Investigation required label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Investigation required
Projects
None yet
Development

No branches or pull requests

2 participants