Skip to content

Commit 7ad76b5

Browse files
tixxdzjrfastab
authored andcommitted
tests:cgroups: assert c strings conversion to golang ones
Signed-off-by: Djalal Harouni <[email protected]>
1 parent a4b4a86 commit 7ad76b5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pkg/cgroups/cgroups_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"strings"
1011
"syscall"
1112
"testing"
1213

@@ -43,6 +44,63 @@ func isDirMountFsType(path string, mntType string) (bool, error) {
4344
return true, nil
4445
}
4546

47+
func TestCgroupNameFromCStr(t *testing.T) {
48+
type progTest struct {
49+
in []byte
50+
want string
51+
}
52+
53+
containerId := "docker-713516e64fa59fc6c7216b29b25d395a606083232bdaf07e53540cd8252ea3f7.scope"
54+
cgroupPath := "/system.slice/docker-713516e64fa59fc6c7216b29b25d395a606083232bdaf07e53540cd8252ea3f7.scope"
55+
bempty := []byte{0x00}
56+
emptycontainerId := []byte(containerId)
57+
emptycontainerId[0] = 0x00
58+
bcontainerId := []byte(containerId)
59+
cidx := strings.Index(containerId, "6e")
60+
bcontainerId[cidx] = 0x00
61+
pidx := strings.LastIndex(cgroupPath, "/")
62+
bcgroupPath := []byte(cgroupPath)
63+
bcgroupPath[pidx] = 0x00
64+
65+
testcases := []progTest{
66+
{
67+
in: []byte(""),
68+
want: "",
69+
},
70+
{
71+
in: bempty,
72+
want: "",
73+
},
74+
{
75+
in: emptycontainerId,
76+
want: "",
77+
},
78+
{
79+
in: []byte(containerId),
80+
want: containerId,
81+
},
82+
{
83+
in: []byte(cgroupPath),
84+
want: cgroupPath,
85+
},
86+
{
87+
in: bcontainerId,
88+
want: containerId[:cidx],
89+
},
90+
{
91+
in: bcgroupPath,
92+
want: "/system.slice",
93+
},
94+
}
95+
96+
for _, test := range testcases {
97+
out := CgroupNameFromCStr(test.in)
98+
if out != test.want {
99+
t.Errorf("CgroupNameFromCStr() mismatch - want:'%s' - got:'%s'\n", test.want, out)
100+
}
101+
}
102+
}
103+
46104
// Test cgroup mode detection on an invalid directory
47105
func TestDetectCgroupModeInvalid(t *testing.T) {
48106
mode, err := detectCgroupMode("invalid-cgroupfs-path")

0 commit comments

Comments
 (0)