Skip to content

Commit 40829ec

Browse files
Fix resource quota being rejected when contains decimals (#1088)
1 parent 98a81b3 commit 40829ec

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/testcontainers/src/generic-container/generic-container-resources-quota.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => {
1111
if (!process.env["CI_ROOTLESS"]) {
1212
it("should set resources quota", async () => {
1313
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
14-
.withResourcesQuota({ memory: 0.5, cpu: 1 })
14+
.withResourcesQuota({ cpu: 1, memory: 0.5 })
1515
.start();
1616

1717
const dockerContainer = await client.container.getById(container.getId());
@@ -44,6 +44,20 @@ describe("GenericContainer resources quota", { timeout: 180_000 }, () => {
4444
expect(containerInfo.HostConfig.NanoCpus).toEqual(0);
4545
});
4646

47+
if (!process.env["CI_ROOTLESS"]) {
48+
it("should round values to match target int64 type", async () => {
49+
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
50+
.withResourcesQuota({ cpu: 0.3, memory: 0.2 })
51+
.start();
52+
53+
const dockerContainer = await client.container.getById(container.getId());
54+
const containerInfo = await dockerContainer.inspect();
55+
56+
expect(containerInfo.HostConfig.Memory).toEqual(214748365);
57+
expect(containerInfo.HostConfig.NanoCpus).toEqual(300000000);
58+
});
59+
}
60+
4761
if (!process.env["CI_ROOTLESS"]) {
4862
it("should set resources quota cpu only, memory should be 0", async () => {
4963
await using container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")

packages/testcontainers/src/generic-container/generic-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ export class GenericContainer implements TestContainer {
488488
}
489489

490490
public withResourcesQuota({ memory, cpu }: ResourcesQuota): this {
491-
this.hostConfig.Memory = memory !== undefined ? memory * 1024 ** 3 : undefined;
492-
this.hostConfig.NanoCpus = cpu !== undefined ? cpu * 10 ** 9 : undefined;
491+
this.hostConfig.Memory = memory !== undefined ? Math.ceil(memory * 1024 ** 3) : undefined;
492+
this.hostConfig.NanoCpus = cpu !== undefined ? Math.ceil(cpu * 10 ** 9) : undefined;
493493
return this;
494494
}
495495

0 commit comments

Comments
 (0)