Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit 40814d5

Browse files
committed
test: check container creation with resource limits and node task slots
1 parent 783358d commit 40814d5

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

internal/types/node_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package types
2+
3+
import "testing"
4+
5+
func TestNode_GetMaxTaskSlots(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
node Node
9+
wantSlots int
10+
}{
11+
{
12+
name: "nil resources returns default 10",
13+
node: Node{
14+
NodeID: "node1",
15+
Resources: nil,
16+
},
17+
wantSlots: 10,
18+
},
19+
{
20+
name: "CPU is limiting factor",
21+
node: Node{
22+
NodeID: "node2",
23+
Resources: &NodeResources{
24+
Capacity: ResourceList{
25+
CPU: 2000, // 2 cores
26+
Memory: 10 * 1024 * 1024 * 1024, // 10GB
27+
},
28+
},
29+
},
30+
wantSlots: 2,
31+
},
32+
{
33+
name: "Memory is limiting factor",
34+
node: Node{
35+
NodeID: "node3",
36+
Resources: &NodeResources{
37+
Capacity: ResourceList{
38+
CPU: 10000, // 10 cores
39+
Memory: 2 * 1024 * 1024 * 1024, // 2GB
40+
},
41+
},
42+
},
43+
wantSlots: 2,
44+
},
45+
{
46+
name: "Equal CPU and memory",
47+
node: Node{
48+
NodeID: "node4",
49+
Resources: &NodeResources{
50+
Capacity: ResourceList{
51+
CPU: 4000, // 4 cores
52+
Memory: 4 * 1024 * 1024 * 1024, // 4GB
53+
},
54+
},
55+
},
56+
wantSlots: 4,
57+
},
58+
{
59+
name: "Zero CPU",
60+
node: Node{
61+
NodeID: "node5",
62+
Resources: &NodeResources{
63+
Capacity: ResourceList{
64+
CPU: 0,
65+
Memory: 10 * 1024 * 1024 * 1024,
66+
},
67+
},
68+
},
69+
wantSlots: 0,
70+
},
71+
{
72+
name: "Zero Memory",
73+
node: Node{
74+
NodeID: "node6",
75+
Resources: &NodeResources{
76+
Capacity: ResourceList{
77+
CPU: 10000,
78+
Memory: 0,
79+
},
80+
},
81+
},
82+
wantSlots: 0,
83+
},
84+
}
85+
86+
for _, tt := range tests {
87+
t.Run(
88+
tt.name, func(t *testing.T) {
89+
got := tt.node.GetMaxTaskSlots()
90+
if got != tt.wantSlots {
91+
t.Errorf("GetMaxTaskSlots() = %v, want %v", got, tt.wantSlots)
92+
}
93+
},
94+
)
95+
}
96+
}

internal/worker/docker/client_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,90 @@ func TestCreateContainer(t *testing.T) {
129129
}
130130
}
131131

132+
func TestCreateContainerWithResources(t *testing.T) {
133+
client, err := NewClient()
134+
if err != nil {
135+
t.Skipf("Docker not available: %v", err)
136+
}
137+
defer func() { _ = client.Close() }()
138+
139+
ctx := context.Background()
140+
141+
// Pull alpine first
142+
if err := client.PullImage(ctx, "alpine:latest"); err != nil {
143+
t.Fatalf("failed to pull alpine image: %v", err)
144+
}
145+
146+
tests := []struct {
147+
name string
148+
imageName string
149+
env []string
150+
cpuQuota float64
151+
memoryLimit int64
152+
wantErr bool
153+
}{
154+
{
155+
name: "create with CPU and memory limits",
156+
imageName: "alpine:latest",
157+
env: []string{"TEST=value"},
158+
cpuQuota: 1.0,
159+
memoryLimit: 256 * 1024 * 1024, // 256MB
160+
wantErr: false,
161+
},
162+
{
163+
name: "create with only CPU limit",
164+
imageName: "alpine:latest",
165+
env: []string{},
166+
cpuQuota: 0.5,
167+
memoryLimit: 0,
168+
wantErr: false,
169+
},
170+
{
171+
name: "create with only memory limit",
172+
imageName: "alpine:latest",
173+
env: []string{},
174+
cpuQuota: 0,
175+
memoryLimit: 512 * 1024 * 1024, // 512MB
176+
wantErr: false,
177+
},
178+
{
179+
name: "create with no limits",
180+
imageName: "alpine:latest",
181+
env: []string{},
182+
cpuQuota: 0,
183+
memoryLimit: 0,
184+
wantErr: false,
185+
},
186+
{
187+
name: "create with invalid image",
188+
imageName: "nonexistent-image:latest",
189+
env: []string{},
190+
cpuQuota: 1.0,
191+
memoryLimit: 256 * 1024 * 1024,
192+
wantErr: true,
193+
},
194+
}
195+
196+
for _, tt := range tests {
197+
t.Run(
198+
tt.name, func(t *testing.T) {
199+
containerID, err := client.CreateContainerWithResources(
200+
ctx, tt.imageName, tt.env, tt.cpuQuota, tt.memoryLimit,
201+
)
202+
if (err != nil) != tt.wantErr {
203+
t.Errorf("CreateContainerWithResources() error = %v, wantErr %v", err, tt.wantErr)
204+
}
205+
if !tt.wantErr && containerID == "" {
206+
t.Error("CreateContainerWithResources() returned empty container ID")
207+
}
208+
if containerID != "" {
209+
_ = client.RemoveContainer(ctx, containerID)
210+
}
211+
},
212+
)
213+
}
214+
}
215+
132216
func TestStartContainer(t *testing.T) {
133217
client, err := NewClient()
134218
if err != nil {

0 commit comments

Comments
 (0)