diff --git a/packages/testcontainers/src/generic-container/generic-container-resources-quota.test.ts b/packages/testcontainers/src/generic-container/generic-container-resources-quota.test.ts index d03e2f315..2b0d08213 100644 --- a/packages/testcontainers/src/generic-container/generic-container-resources-quota.test.ts +++ b/packages/testcontainers/src/generic-container/generic-container-resources-quota.test.ts @@ -11,7 +11,7 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => { if (!process.env["CI_ROOTLESS"]) { it("should set resources quota", async () => { await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") - .withResourcesQuota({ memory: 0.5, cpu: 1 }) + .withResourcesQuota({ cpu: 1, memory: 0.5 }) .start(); const dockerContainer = await client.container.getById(container.getId()); @@ -44,6 +44,20 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => { expect(containerInfo.HostConfig.NanoCpus).toEqual(0); }); + if (!process.env["CI_ROOTLESS"]) { + it("should round values to match target int64 type", async () => { + await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") + .withResourcesQuota({ cpu: 0.3, memory: 0.2 }) + .start(); + + const dockerContainer = await client.container.getById(container.getId()); + const containerInfo = await dockerContainer.inspect(); + + expect(containerInfo.HostConfig.Memory).toEqual(214748365); + expect(containerInfo.HostConfig.NanoCpus).toEqual(300000000); + }); + } + if (!process.env["CI_ROOTLESS"]) { it("should set resources quota cpu only, memory should be 0", async () => { await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") diff --git a/packages/testcontainers/src/generic-container/generic-container.ts b/packages/testcontainers/src/generic-container/generic-container.ts index 93ae90792..19109dd3d 100644 --- a/packages/testcontainers/src/generic-container/generic-container.ts +++ b/packages/testcontainers/src/generic-container/generic-container.ts @@ -484,8 +484,8 @@ export class GenericContainer implements TestContainer { } public withResourcesQuota({ memory, cpu }: ResourcesQuota): this { - this.hostConfig.Memory = memory !== undefined ? memory * 1024 ** 3 : undefined; - this.hostConfig.NanoCpus = cpu !== undefined ? cpu * 10 ** 9 : undefined; + this.hostConfig.Memory = memory !== undefined ? Math.ceil(memory * 1024 ** 3) : undefined; + this.hostConfig.NanoCpus = cpu !== undefined ? Math.ceil(cpu * 10 ** 9) : undefined; return this; }