| 1 | // Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroups |
| 25 | |
| 26 | import ( |
| 27 | "strconv" |
| 28 | "testing" |
| 29 | |
| 30 | "github.com/stretchr/testify/assert" |
| 31 | ) |
| 32 | |
| 33 | func TestNewCGroupSubsysFromLine(t *testing.T) { |
| 34 | testTable := []struct { |
| 35 | name string |
| 36 | line string |
| 37 | expectedSubsys *CGroupSubsys |
| 38 | }{ |
| 39 | { |
| 40 | name: "single-subsys", |
| 41 | line: "1:cpu:/", |
| 42 | expectedSubsys: &CGroupSubsys{ |
| 43 | ID: 1, |
| 44 | Subsystems: []string{"cpu"}, |
| 45 | Name: "/", |
| 46 | }, |
| 47 | }, |
| 48 | { |
| 49 | name: "multi-subsys", |
| 50 | line: "8:cpu,cpuacct,cpuset:/docker/1234567890abcdef", |
| 51 | expectedSubsys: &CGroupSubsys{ |
| 52 | ID: 8, |
| 53 | Subsystems: []string{"cpu", "cpuacct", "cpuset"}, |
| 54 | Name: "/docker/1234567890abcdef", |
| 55 | }, |
| 56 | }, |
| 57 | { |
| 58 | name: "multi-subsys", |
| 59 | line: "12:cpu,cpuacct:/system.slice/containerd.service/kubepods-besteffort-podb41662f7_b03a_4c65_8ef9_6e4e55c3cf27.slice:cri-containerd:1753b7cbbf62734d812936961224d5bc0cf8f45214e0d5cdd1a781a053e7c48f", |
| 60 | expectedSubsys: &CGroupSubsys{ |
| 61 | ID: 12, |
| 62 | Subsystems: []string{"cpu", "cpuacct"}, |
| 63 | Name: "/system.slice/containerd.service/kubepods-besteffort-podb41662f7_b03a_4c65_8ef9_6e4e55c3cf27.slice:cri-containerd:1753b7cbbf62734d812936961224d5bc0cf8f45214e0d5cdd1a781a053e7c48f", |
| 64 | }, |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | for _, tt := range testTable { |
| 69 | subsys, err := NewCGroupSubsysFromLine(tt.line) |
| 70 | assert.Equal(t, tt.expectedSubsys, subsys, tt.name) |
| 71 | assert.NoError(t, err, tt.name) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestNewCGroupSubsysFromLineErr(t *testing.T) { |
| 76 | lines := []string{ |
| 77 | "1:cpu", |
| 78 | "not-a-number:cpu:/", |
| 79 | } |
| 80 | _, parseError := strconv.Atoi("not-a-number") |
| 81 | |
| 82 | testTable := []struct { |
| 83 | name string |
| 84 | line string |
| 85 | expectedError error |
| 86 | }{ |
| 87 | { |
| 88 | name: "fewer-fields", |
| 89 | line: lines[0], |
| 90 | expectedError: cgroupSubsysFormatInvalidError{lines[0]}, |
| 91 | }, |
| 92 | { |
| 93 | name: "illegal-id", |
| 94 | line: lines[1], |
| 95 | expectedError: parseError, |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | for _, tt := range testTable { |
| 100 | subsys, err := NewCGroupSubsysFromLine(tt.line) |
| 101 | assert.Nil(t, subsys, tt.name) |
| 102 | assert.Equal(t, tt.expectedError, err, tt.name) |
| 103 | } |
| 104 | } |