...

Source file src/github.com/containerd/cgroups/paths_test.go

Documentation: github.com/containerd/cgroups

     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 cgroups
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestStaticPath(t *testing.T) {
    27  	path := StaticPath("test")
    28  	p, err := path("")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if p != "test" {
    33  		t.Fatalf("expected static path of \"test\" but received %q", p)
    34  	}
    35  }
    36  
    37  func TestSelfPath(t *testing.T) {
    38  	_, err := v1MountPoint()
    39  	if err == ErrMountPointNotExist {
    40  		t.Skip("skipping test that requires cgroup hierarchy")
    41  	} else if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	paths, err := ParseCgroupFile("/proc/self/cgroup")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	dp := strings.TrimPrefix(paths["devices"], "/")
    49  	path := NestedPath("test")
    50  	p, err := path("devices")
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if p != filepath.Join("/", dp, "test") {
    55  		t.Fatalf("expected self path of %q but received %q", filepath.Join("/", dp, "test"), p)
    56  	}
    57  }
    58  
    59  func TestPidPath(t *testing.T) {
    60  	_, err := v1MountPoint()
    61  	if err == ErrMountPointNotExist {
    62  		t.Skip("skipping test that requires cgroup hierarchy")
    63  	} else if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  	paths, err := ParseCgroupFile("/proc/self/cgroup")
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	dp := strings.TrimPrefix(paths["devices"], "/")
    71  	path := PidPath(os.Getpid())
    72  	p, err := path("devices")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if p != filepath.Join("/", dp) {
    77  		t.Fatalf("expected self path of %q but received %q", filepath.Join("/", dp), p)
    78  	}
    79  }
    80  
    81  func TestRootPath(t *testing.T) {
    82  	p, err := RootPath(Cpu)
    83  	if err != nil {
    84  		t.Error(err)
    85  		return
    86  	}
    87  	if p != "/" {
    88  		t.Errorf("expected / but received %q", p)
    89  	}
    90  }
    91  
    92  func TestEmptySubsystem(t *testing.T) {
    93  	const data = `10:devices:/user.slice
    94  	9:net_cls,net_prio:/
    95  	8:blkio:/
    96  	7:freezer:/
    97  	6:perf_event:/
    98  	5:cpuset:/
    99  	4:memory:/
   100  	3:pids:/user.slice/user-1000.slice/user@1000.service
   101  	2:cpu,cpuacct:/
   102  	1:name=systemd:/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service
   103  	0::/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service`
   104  	r := strings.NewReader(data)
   105  	paths, unified, err := parseCgroupFromReaderUnified(r)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	for subsystem, path := range paths {
   110  		if subsystem == "" {
   111  			t.Fatalf("empty subsystem for %q", path)
   112  		}
   113  	}
   114  	unifiedExpected := "/user.slice/user-1000.slice/user@1000.service/gnome-terminal-server.service"
   115  	if unified != unifiedExpected {
   116  		t.Fatalf("expected %q, got %q", unifiedExpected, unified)
   117  	}
   118  }
   119  
   120  func TestSystemd240(t *testing.T) {
   121  	if isUnified {
   122  		t.Skipf("requires the system to be running in legacy mode")
   123  	}
   124  	const data = `8:net_cls:/
   125  	7:memory:/system.slice/docker.service
   126  	6:freezer:/
   127  	5:blkio:/system.slice/docker.service
   128  	4:devices:/system.slice/docker.service
   129  	3:cpuset:/
   130  	2:cpu,cpuacct:/system.slice/docker.service
   131  	1:name=systemd:/system.slice/docker.service
   132  	0::/system.slice/docker.service`
   133  	r := strings.NewReader(data)
   134  	paths, unified, err := parseCgroupFromReaderUnified(r)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  
   139  	path := existingPath(paths, "")
   140  	_, err = path("net_prio")
   141  	if err == nil {
   142  		t.Fatal("error for net_prio should not be nil")
   143  	}
   144  	if err != ErrControllerNotActive {
   145  		t.Fatalf("expected error %q but received %q", ErrControllerNotActive, err)
   146  	}
   147  	unifiedExpected := "/system.slice/docker.service"
   148  	if unified != unifiedExpected {
   149  		t.Fatalf("expected %q, got %q", unifiedExpected, unified)
   150  	}
   151  }
   152  

View as plain text