Skip to content

Commit

Permalink
[test]: add test case on package cgroup (#1868)
Browse files Browse the repository at this point in the history
* [test]: add test case on package cgroup

Signed-off-by: Sam Yuan <[email protected]>

* [fix]: update cache setting logic when error happen

Signed-off-by: Sam Yuan <[email protected]>

* [fix]: fix lint

Signed-off-by: Sam Yuan <[email protected]>

---------

Signed-off-by: Sam Yuan <[email protected]>
  • Loading branch information
SamYuan1990 authored Dec 5, 2024
1 parent 8ab7ff8 commit 70145df
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
24 changes: 10 additions & 14 deletions pkg/cgroup/resolve_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,17 @@ func (c *cache) getContainerIDFromCache(pid uint64) (string, bool) {
}

func (c *cache) getGetContainerIDFromPID(pid uint64) (string, error) {
p, ok := instance.containerIDCache.Load(pid)
containerID, ok := instance.getContainerIDFromCache(pid)
if ok {
containerID, ok := p.(string)
if ok {
return containerID, nil
}
return containerID, nil
}

path, err := getPathFromPID(procPath, pid)
if err != nil {
return utils.SystemProcessName, err
}

containerID, err := extractPodContainerIDfromPath(path)
containerID, err = extractPodContainerIDfromPathWithCgroup(path)
if err != nil {
return utils.SystemProcessName, err
}
Expand All @@ -152,7 +149,7 @@ func (c *cache) getContainerIDFromcGroupID(cGroupID uint64) (string, error) {
return utils.SystemProcessName, err
}

containerID, err := extractPodContainerIDfromPath(path)
containerID, err := extractPodContainerIDfromPathWithCgroup(path)
if err != nil {
return utils.SystemProcessName, err
}
Expand Down Expand Up @@ -180,20 +177,24 @@ func (c *cache) getPathFromcGroupID(cgroupID uint64) (string, error) {
if err != nil {
return fmt.Errorf("error resolving handle: %w", err)
}
// found a path and load into cache.
instance.cGroupIDToPath.Store(getCgroupID, path)
return nil
})

// if error is not nil
if err != nil {
return unknownPath, fmt.Errorf("failed to find cgroup id: %v", err)
}

// if path found and load from cache.
p, ok = instance.cGroupIDToPath.Load(cgroupID)
if ok {
return p.(string), nil
}

// if error is nil, but path not found
// add mapping in cache
instance.cGroupIDToPath.Store(cgroupID, unknownPath)
// return
return unknownPath, nil
}

Expand Down Expand Up @@ -291,11 +292,6 @@ func getContainerIDFromPath(cGroupID, pid uint64, withCGroupID bool) (string, er
return instance.getGetContainerIDFromPID(pid)
}

// extractPodContainerIDfromPath extracts the container ID from the provided cgroup path
func extractPodContainerIDfromPath(path string) (string, error) {
return extractPodContainerIDfromPathWithCgroup(path)
}

// extractPodContainerIDfromPathWithCgroup extracts the container ID from a cgroup path
func extractPodContainerIDfromPathWithCgroup(path string) (string, error) {
if path == unknownPath {
Expand Down
41 changes: 41 additions & 0 deletions pkg/cgroup/resolve_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

. "github.com/onsi/gomega"
"github.com/sustainable-computing-io/kepler/pkg/utils"

corev1 "k8s.io/api/core/v1"
)
Expand Down Expand Up @@ -119,6 +120,12 @@ func TestGetAliveContainers(t *testing.T) {
c := GetCache()
res := c.getAliveContainers(&testcase.pods)
g.Expect(res).To(Equal(testcase.expectResults))
containerID := testcase.pods[0].Status.InitContainerStatuses[0].ContainerID
exists := c.hasContainerID(containerID)
g.Expect(exists).To(BeTrue())
containerInfo, err := c.getContainerInfo(containerID)
g.Expect(containerInfo).NotTo(BeNil())
g.Expect(err).NotTo(HaveOccurred())
})
}
}
Expand Down Expand Up @@ -195,3 +202,37 @@ func TestExtractPodContainerIDfromPathWithCgroup(t *testing.T) {
})
}
}

func TestGetPathFromcGroupID(t *testing.T) {
g := NewWithT(t)
c := GetCache()
c.cGroupIDToPath.Store(uint64(123), "abc")
path, err := c.getPathFromcGroupID(uint64(123))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(path).To(Equal("abc"))
}

func TestContainerIDCache(t *testing.T) {
g := NewWithT(t)
c := GetCache()
c.setContainerIDCache(uint64(123), "ID")
id, exists := c.getContainerIDFromCache(uint64(123))
g.Expect(exists).To(BeTrue())
g.Expect(id).To(Equal("ID"))
id, err := c.getContainerIDFromcGroupID(uint64(123))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(id).To(Equal("ID"))
id, exists = c.getContainerIDFromCache(uint64(404))
g.Expect(exists).To(BeFalse())
g.Expect(id).To(Equal(""))
AddContainerIDToCache(uint64(124), "ID1")
id, err = GetContainerIDFromPID(uint64(124))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(id).To(Equal("ID1"))
}

func TestValidContainerID(t *testing.T) {
g := NewWithT(t)
result := validContainerID("")
g.Expect(result).To(Equal(utils.SystemProcessName))
}

0 comments on commit 70145df

Please sign in to comment.