Skip to content

Commit 70145df

Browse files
authoredDec 5, 2024··
[test]: add test case on package cgroup (#1868)
* [test]: add test case on package cgroup Signed-off-by: Sam Yuan <yy19902439@126.com> * [fix]: update cache setting logic when error happen Signed-off-by: Sam Yuan <yy19902439@126.com> * [fix]: fix lint Signed-off-by: Sam Yuan <yy19902439@126.com> --------- Signed-off-by: Sam Yuan <yy19902439@126.com>
1 parent 8ab7ff8 commit 70145df

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed
 

‎pkg/cgroup/resolve_container.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,17 @@ func (c *cache) getContainerIDFromCache(pid uint64) (string, bool) {
119119
}
120120

121121
func (c *cache) getGetContainerIDFromPID(pid uint64) (string, error) {
122-
p, ok := instance.containerIDCache.Load(pid)
122+
containerID, ok := instance.getContainerIDFromCache(pid)
123123
if ok {
124-
containerID, ok := p.(string)
125-
if ok {
126-
return containerID, nil
127-
}
124+
return containerID, nil
128125
}
129126

130127
path, err := getPathFromPID(procPath, pid)
131128
if err != nil {
132129
return utils.SystemProcessName, err
133130
}
134131

135-
containerID, err := extractPodContainerIDfromPath(path)
132+
containerID, err = extractPodContainerIDfromPathWithCgroup(path)
136133
if err != nil {
137134
return utils.SystemProcessName, err
138135
}
@@ -152,7 +149,7 @@ func (c *cache) getContainerIDFromcGroupID(cGroupID uint64) (string, error) {
152149
return utils.SystemProcessName, err
153150
}
154151

155-
containerID, err := extractPodContainerIDfromPath(path)
152+
containerID, err := extractPodContainerIDfromPathWithCgroup(path)
156153
if err != nil {
157154
return utils.SystemProcessName, err
158155
}
@@ -180,20 +177,24 @@ func (c *cache) getPathFromcGroupID(cgroupID uint64) (string, error) {
180177
if err != nil {
181178
return fmt.Errorf("error resolving handle: %w", err)
182179
}
180+
// found a path and load into cache.
183181
instance.cGroupIDToPath.Store(getCgroupID, path)
184182
return nil
185183
})
186184

185+
// if error is not nil
187186
if err != nil {
188187
return unknownPath, fmt.Errorf("failed to find cgroup id: %v", err)
189188
}
190-
189+
// if path found and load from cache.
191190
p, ok = instance.cGroupIDToPath.Load(cgroupID)
192191
if ok {
193192
return p.(string), nil
194193
}
195-
194+
// if error is nil, but path not found
195+
// add mapping in cache
196196
instance.cGroupIDToPath.Store(cgroupID, unknownPath)
197+
// return
197198
return unknownPath, nil
198199
}
199200

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

294-
// extractPodContainerIDfromPath extracts the container ID from the provided cgroup path
295-
func extractPodContainerIDfromPath(path string) (string, error) {
296-
return extractPodContainerIDfromPathWithCgroup(path)
297-
}
298-
299295
// extractPodContainerIDfromPathWithCgroup extracts the container ID from a cgroup path
300296
func extractPodContainerIDfromPathWithCgroup(path string) (string, error) {
301297
if path == unknownPath {

‎pkg/cgroup/resolve_container_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
. "github.com/onsi/gomega"
23+
"github.com/sustainable-computing-io/kepler/pkg/utils"
2324

2425
corev1 "k8s.io/api/core/v1"
2526
)
@@ -119,6 +120,12 @@ func TestGetAliveContainers(t *testing.T) {
119120
c := GetCache()
120121
res := c.getAliveContainers(&testcase.pods)
121122
g.Expect(res).To(Equal(testcase.expectResults))
123+
containerID := testcase.pods[0].Status.InitContainerStatuses[0].ContainerID
124+
exists := c.hasContainerID(containerID)
125+
g.Expect(exists).To(BeTrue())
126+
containerInfo, err := c.getContainerInfo(containerID)
127+
g.Expect(containerInfo).NotTo(BeNil())
128+
g.Expect(err).NotTo(HaveOccurred())
122129
})
123130
}
124131
}
@@ -195,3 +202,37 @@ func TestExtractPodContainerIDfromPathWithCgroup(t *testing.T) {
195202
})
196203
}
197204
}
205+
206+
func TestGetPathFromcGroupID(t *testing.T) {
207+
g := NewWithT(t)
208+
c := GetCache()
209+
c.cGroupIDToPath.Store(uint64(123), "abc")
210+
path, err := c.getPathFromcGroupID(uint64(123))
211+
g.Expect(err).NotTo(HaveOccurred())
212+
g.Expect(path).To(Equal("abc"))
213+
}
214+
215+
func TestContainerIDCache(t *testing.T) {
216+
g := NewWithT(t)
217+
c := GetCache()
218+
c.setContainerIDCache(uint64(123), "ID")
219+
id, exists := c.getContainerIDFromCache(uint64(123))
220+
g.Expect(exists).To(BeTrue())
221+
g.Expect(id).To(Equal("ID"))
222+
id, err := c.getContainerIDFromcGroupID(uint64(123))
223+
g.Expect(err).NotTo(HaveOccurred())
224+
g.Expect(id).To(Equal("ID"))
225+
id, exists = c.getContainerIDFromCache(uint64(404))
226+
g.Expect(exists).To(BeFalse())
227+
g.Expect(id).To(Equal(""))
228+
AddContainerIDToCache(uint64(124), "ID1")
229+
id, err = GetContainerIDFromPID(uint64(124))
230+
g.Expect(err).NotTo(HaveOccurred())
231+
g.Expect(id).To(Equal("ID1"))
232+
}
233+
234+
func TestValidContainerID(t *testing.T) {
235+
g := NewWithT(t)
236+
result := validContainerID("")
237+
g.Expect(result).To(Equal(utils.SystemProcessName))
238+
}

0 commit comments

Comments
 (0)
Please sign in to comment.