From fedab3648d80aed96aa6a1ae706d853a8f2eeb70 Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Wed, 23 Jul 2025 16:45:21 +0100 Subject: [PATCH 1/2] Resource quota returns 400 when contains decimals --- .../generic-container-resources-quota.test.ts | 12 ++++++++++++ .../src/generic-container/generic-container.ts | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) 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..c69377d67 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 @@ -44,6 +44,18 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => { expect(containerInfo.HostConfig.NanoCpus).toEqual(0); }); + it("should round values to match target int64 type", async () => { + await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") + .withResourcesQuota({ memory: 0.2, cpu: 0.3 }) + .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; } From 22ca73a0841dfece5d4a6678c7b3d5748211581d Mon Sep 17 00:00:00 2001 From: Cristian Greco Date: Wed, 23 Jul 2025 16:58:33 +0100 Subject: [PATCH 2/2] Exclusion for rootless --- .../generic-container-resources-quota.test.ts | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) 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 c69377d67..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,17 +44,19 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => { expect(containerInfo.HostConfig.NanoCpus).toEqual(0); }); - it("should round values to match target int64 type", async () => { - await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14") - .withResourcesQuota({ memory: 0.2, cpu: 0.3 }) - .start(); + 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(); + 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); - }); + 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 () => {