Skip to content

Commit 246dcc3

Browse files
committed
chore: add e2e tests
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent cd4599b commit 246dcc3

File tree

4 files changed

+145
-21
lines changed

4 files changed

+145
-21
lines changed

api/handlers/container/create.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
191191
}
192192

193193
memoryReservation := ""
194+
memoryReservationChanged := false
194195
if req.HostConfig.MemoryReservation != 0 {
195196
memoryReservation = fmt.Sprint(req.HostConfig.MemoryReservation)
197+
memoryReservationChanged = true
196198
}
197199

198200
memorySwap := ""
@@ -270,22 +272,23 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
270272
// #endregion
271273

272274
// #region for resource flags
273-
CPUShares: uint64(req.HostConfig.CPUShares), // CPU shares (relative weight)
274-
Memory: memory, // memory limit (in bytes)
275-
CPUQuota: CpuQuota, // nerdctl default.
276-
MemorySwappiness64: memorySwappiness, // Tuning container memory swappiness behaviour
277-
PidsLimit: pidLimit, // PidsLimit specifies the tune container pids limit
278-
Cgroupns: cgroupnsMode, // Cgroupns specifies the cgroup namespace to use
279-
BlkioWeight: req.HostConfig.BlkioWeight, // block IO weight (relative)
280-
CPUPeriod: uint64(req.HostConfig.CPUPeriod), // CPU CFS (Completely Fair Scheduler) period
281-
CPUSetCPUs: req.HostConfig.CPUSetCPUs, // CpusetCpus 0-2, 0,1
282-
CPUSetMems: req.HostConfig.CPUSetMems, // CpusetMems 0-2, 0,1
283-
MemoryReservation: memoryReservation, // Memory soft limit (in bytes)
284-
MemorySwap: memorySwap, // Total memory usage (memory + swap); set `-1` to enable unlimited swap
285-
IPC: req.HostConfig.IpcMode, // IPC namespace to use
286-
ShmSize: shmSize, // ShmSize set the size of /dev/shm
287-
Ulimit: ulimits, // List of ulimits to be set in the container
288-
Device: devices, // Device specifies add a host device to the container
275+
CPUShares: uint64(req.HostConfig.CPUShares), // CPU shares (relative weight)
276+
Memory: memory, // memory limit (in bytes)
277+
CPUQuota: CpuQuota, // nerdctl default.
278+
MemorySwappiness64: memorySwappiness, // Tuning container memory swappiness behaviour
279+
PidsLimit: pidLimit, // PidsLimit specifies the tune container pids limit
280+
Cgroupns: cgroupnsMode, // Cgroupns specifies the cgroup namespace to use
281+
BlkioWeight: req.HostConfig.BlkioWeight, // block IO weight (relative)
282+
CPUPeriod: uint64(req.HostConfig.CPUPeriod), // CPU CFS (Completely Fair Scheduler) period
283+
CPUSetCPUs: req.HostConfig.CPUSetCPUs, // CpusetCpus 0-2, 0,1
284+
CPUSetMems: req.HostConfig.CPUSetMems, // CpusetMems 0-2, 0,1
285+
MemoryReservation: memoryReservation, // Memory soft limit (in bytes)
286+
MemoryReservationChanged: memoryReservationChanged, // Specifies whether the memory soft limit has been changed
287+
MemorySwap: memorySwap, // Total memory usage (memory + swap); set `-1` to enable unlimited swap
288+
IPC: req.HostConfig.IpcMode, // IPC namespace to use
289+
ShmSize: shmSize, // ShmSize set the size of /dev/shm
290+
Ulimit: ulimits, // List of ulimits to be set in the container
291+
Device: devices, // Device specifies add a host device to the container
289292
// #endregion
290293

291294
// #region for user flags
@@ -386,6 +389,7 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) {
386389
default:
387390
code = http.StatusInternalServerError
388391
}
392+
fmt.Println("error = ", err)
389393
logrus.Debugf("Create Container API failed. Status code %d, Message: %s", code, err)
390394
response.SendErrorResponse(w, code, err)
391395
return

api/handlers/container/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,15 +521,15 @@ var _ = Describe("Container Create API ", func() {
521521
body := []byte(`{
522522
"Image": "test-image",
523523
"HostConfig": {
524-
"MemoryReservation": 209715200,
524+
"MemoryReservation": 209710,
525525
"MemorySwap": 514288000,
526526
"MemorySwappiness": 25
527527
}
528528
}`)
529529
req, _ := http.NewRequest(http.MethodPost, "/containers/create", bytes.NewReader(body))
530530

531531
// expected create options
532-
createOpt.MemoryReservation = "209715200"
532+
createOpt.MemoryReservation = "209710"
533533
createOpt.MemorySwap = "514288000"
534534
createOpt.MemorySwappiness64 = 25
535535
service.EXPECT().Create(gomock.Any(), "test-image", nil, equalTo(createOpt), equalTo(netOpt)).Return(

api/types/container_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,17 +271,17 @@ type DeviceMapping struct {
271271
CgroupPermissions string
272272
}
273273

274-
// CgroupnsMode represents the cgroup namespace mode of the container
274+
// CgroupnsMode represents the cgroup namespace mode of the container.
275275
type CgroupnsMode string
276276

277-
// cgroup namespace modes for containers
277+
// cgroup namespace modes for containers.
278278
const (
279279
CgroupnsModeEmpty CgroupnsMode = ""
280280
CgroupnsModePrivate CgroupnsMode = "private"
281281
CgroupnsModeHost CgroupnsMode = "host"
282282
)
283283

284-
// Valid indicates whether the cgroup namespace mode is valid
284+
// Valid indicates whether the cgroup namespace mode is valid.
285285
func (c CgroupnsMode) Valid() bool {
286286
return c == CgroupnsModePrivate || c == CgroupnsModeHost
287287
}

e2e/tests/container_create.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,126 @@ func ContainerCreate(opt *option.Option) {
432432
// verify log path exists
433433
Expect(inspect[0].LogPath).ShouldNot(BeNil())
434434
})
435+
436+
It("should create a container with specified CPU qouta and period options", func() {
437+
// define options
438+
options.Cmd = []string{"sleep", "Infinity"}
439+
options.HostConfig.CPUQuota = 11111
440+
options.HostConfig.CPUShares = 2048
441+
options.HostConfig.CPUPeriod = 100000
442+
443+
// create container
444+
statusCode, ctr := createContainer(uClient, url, testContainerName, options)
445+
Expect(statusCode).Should(Equal(http.StatusCreated))
446+
Expect(ctr.ID).ShouldNot(BeEmpty())
447+
448+
nativeResp := command.Stdout(opt, "inspect", "--mode=native", testContainerName)
449+
var nativeInspect []map[string]interface{}
450+
err := json.Unmarshal([]byte(nativeResp), &nativeInspect)
451+
Expect(err).Should(BeNil())
452+
Expect(nativeInspect).Should(HaveLen(1))
453+
454+
// Navigate to the CPU quota value
455+
spec, ok := nativeInspect[0]["Spec"].(map[string]interface{})
456+
Expect(ok).Should(BeTrue())
457+
linux, ok := spec["linux"].(map[string]interface{})
458+
Expect(ok).Should(BeTrue())
459+
resources, ok := linux["resources"].(map[string]interface{})
460+
Expect(ok).Should(BeTrue())
461+
cpu, ok := resources["cpu"].(map[string]interface{})
462+
Expect(ok).Should(BeTrue())
463+
quota, ok := cpu["quota"].(float64)
464+
Expect(ok).Should(BeTrue())
465+
period, ok := cpu["period"].(float64)
466+
Expect(ok).Should(BeTrue())
467+
shares, ok := cpu["shares"].(float64)
468+
Expect(ok).Should(BeTrue())
469+
470+
// Verify the CPU quota
471+
Expect(int64(quota)).Should(Equal(int64(11111)))
472+
Expect(int64(shares)).Should(Equal(int64(2048)))
473+
Expect(int64(period)).Should(Equal(int64(100000)))
474+
475+
})
476+
477+
It("should create a container with specified Memory qouta and PidLimits options", func() {
478+
// define options
479+
options.Cmd = []string{"sleep", "Infinity"}
480+
options.HostConfig.Memory = 4048
481+
options.HostConfig.PidsLimit = 200
482+
options.HostConfig.MemoryReservation = 28
483+
options.HostConfig.MemorySwap = 514288000
484+
options.HostConfig.MemorySwappiness = 25
485+
486+
// create container
487+
statusCode, ctr := createContainer(uClient, url, testContainerName, options)
488+
Expect(statusCode).Should(Equal(http.StatusCreated))
489+
Expect(ctr.ID).ShouldNot(BeEmpty())
490+
491+
nativeResp := command.Stdout(opt, "inspect", "--mode=native", testContainerName)
492+
var nativeInspect []map[string]interface{}
493+
err := json.Unmarshal([]byte(nativeResp), &nativeInspect)
494+
Expect(err).Should(BeNil())
495+
Expect(nativeInspect).Should(HaveLen(1))
496+
497+
// Navigate to the CPU quota value
498+
spec, ok := nativeInspect[0]["Spec"].(map[string]interface{})
499+
Expect(ok).Should(BeTrue())
500+
linux, ok := spec["linux"].(map[string]interface{})
501+
Expect(ok).Should(BeTrue())
502+
resources, ok := linux["resources"].(map[string]interface{})
503+
Expect(ok).Should(BeTrue())
504+
memory, _ := resources["memory"].(map[string]interface{})
505+
506+
pids, _ := resources["pids"].(map[string]interface{})
507+
508+
Expect(int64(pids["limit"].(float64))).Should(Equal(options.HostConfig.PidsLimit))
509+
Expect(int64(memory["limit"].(float64))).Should(Equal(options.HostConfig.Memory))
510+
511+
})
512+
513+
It("should create a container with specified Ulimit options", func() {
514+
// define options
515+
options.Cmd = []string{"sleep", "Infinity"}
516+
517+
options.HostConfig.Ulimits = []*types.Ulimit{
518+
{
519+
Name: "nofile",
520+
Soft: 1024,
521+
Hard: 2048,
522+
},
523+
}
524+
525+
// create container
526+
statusCode, ctr := createContainer(uClient, url, testContainerName, options)
527+
Expect(statusCode).Should(Equal(http.StatusCreated))
528+
Expect(ctr.ID).ShouldNot(BeEmpty())
529+
530+
nativeResp := command.Stdout(opt, "inspect", "--mode=native", testContainerName)
531+
var nativeInspect []map[string]interface{}
532+
err := json.Unmarshal([]byte(nativeResp), &nativeInspect)
533+
Expect(err).Should(BeNil())
534+
Expect(nativeInspect).Should(HaveLen(1))
535+
536+
// Navigate to the CPU quota value
537+
spec, _ := nativeInspect[0]["Spec"].(map[string]interface{})
538+
rlimits := spec["process"].(map[string]interface{})["rlimits"].([]interface{})
539+
for _, ulimit := range options.HostConfig.Ulimits {
540+
found := false
541+
for _, rlimit := range rlimits {
542+
r := rlimit.(map[string]interface{})
543+
if r["type"] == "RLIMIT_NOFILE" {
544+
Expect(r["hard"]).To(Equal(float64(ulimit.Hard)))
545+
Expect(r["soft"]).To(Equal(float64(ulimit.Soft)))
546+
found = true
547+
break
548+
}
549+
}
550+
Expect(found).To(BeTrue())
551+
}
552+
553+
})
554+
435555
It("should create a container with specified network options", func() {
436556
// define options
437557
options.Cmd = []string{"sleep", "Infinity"}

0 commit comments

Comments
 (0)