|
7 | 7 | "fmt"
|
8 | 8 | "os"
|
9 | 9 | "path/filepath"
|
| 10 | + "strings" |
10 | 11 | "syscall"
|
11 | 12 | "testing"
|
12 | 13 |
|
@@ -43,6 +44,63 @@ func isDirMountFsType(path string, mntType string) (bool, error) {
|
43 | 44 | return true, nil
|
44 | 45 | }
|
45 | 46 |
|
| 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 | + |
46 | 104 | // Test cgroup mode detection on an invalid directory
|
47 | 105 | func TestDetectCgroupModeInvalid(t *testing.T) {
|
48 | 106 | mode, err := detectCgroupMode("invalid-cgroupfs-path")
|
|
0 commit comments