...

Source file src/github.com/opencontainers/runc/libcontainer/cgroups/fs2/defaultpath_test.go

Documentation: github.com/opencontainers/runc/libcontainer/cgroups/fs2

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package fs2
    18  
    19  import (
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/opencontainers/runc/libcontainer/cgroups"
    25  )
    26  
    27  func TestParseCgroupFromReader(t *testing.T) {
    28  	cases := map[string]string{
    29  		"0::/user.slice/user-1001.slice/session-1.scope\n":                                  "/user.slice/user-1001.slice/session-1.scope",
    30  		"2:cpuset:/foo\n1:name=systemd:/\n":                                                 "",
    31  		"2:cpuset:/foo\n1:name=systemd:/\n0::/user.slice/user-1001.slice/session-1.scope\n": "/user.slice/user-1001.slice/session-1.scope",
    32  	}
    33  	for s, expected := range cases {
    34  		g, err := parseCgroupFromReader(strings.NewReader(s))
    35  		if expected != "" {
    36  			if g != expected {
    37  				t.Errorf("expected %q, got %q", expected, g)
    38  			}
    39  			if err != nil {
    40  				t.Error(err)
    41  			}
    42  		} else {
    43  			if err == nil {
    44  				t.Error("error is expected")
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func TestDefaultDirPath(t *testing.T) {
    51  	if !cgroups.IsCgroup2UnifiedMode() {
    52  		t.Skip("need cgroupv2")
    53  	}
    54  	// same code as in defaultDirPath()
    55  	ownCgroup, err := parseCgroupFile("/proc/self/cgroup")
    56  	if err != nil {
    57  		// Not a test failure, but rather some weird
    58  		// environment so we can't run this test.
    59  		t.Skipf("can't get own cgroup: %v", err)
    60  	}
    61  	ownCgroup = filepath.Dir(ownCgroup)
    62  
    63  	cases := []struct {
    64  		cgPath   string
    65  		cgParent string
    66  		cgName   string
    67  		expected string
    68  	}{
    69  		{
    70  			cgPath:   "/foo/bar",
    71  			expected: "/sys/fs/cgroup/foo/bar",
    72  		},
    73  		{
    74  			cgPath:   "foo/bar",
    75  			expected: filepath.Join(UnifiedMountpoint, ownCgroup, "foo/bar"),
    76  		},
    77  	}
    78  	for _, c := range cases {
    79  		got, err := _defaultDirPath(UnifiedMountpoint, c.cgPath, c.cgParent, c.cgName)
    80  		if err != nil {
    81  			t.Fatal(err)
    82  		}
    83  		if got != c.expected {
    84  			t.Fatalf("expected %q, got %q", c.expected, got)
    85  		}
    86  	}
    87  }
    88  

View as plain text