...

Source file src/github.com/containerd/cgroups/blkio_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  	"strings"
    22  	"testing"
    23  
    24  	v1 "github.com/containerd/cgroups/stats/v1"
    25  )
    26  
    27  const data = `major minor  #blocks  name
    28  
    29     7        0          4 loop0
    30     7        1     163456 loop1
    31     7        2     149616 loop2
    32     7        3     147684 loop3
    33     7        4     122572 loop4
    34     7        5       8936 loop5
    35     7        6      31464 loop6
    36     7        7     182432 loop7
    37   259        0  937692504 nvme0n1
    38   259        1      31744 nvme0n1p1
    39  `
    40  
    41  func TestGetDevices(t *testing.T) {
    42  	r := strings.NewReader(data)
    43  	devices, err := getDevices(r)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	for dev, expected := range map[deviceKey]string{
    48  		deviceKey{7, 0}: "/dev/loop0",
    49  		deviceKey{259, 0}: "/dev/nvme0n1",
    50  		deviceKey{259, 1}: "/dev/nvme0n1p1",
    51  	} {
    52  		name, ok := devices[dev]
    53  		if !ok {
    54  			t.Fatalf("no device found for %d:%d", dev.major, dev.minor)
    55  		}
    56  		if name != expected {
    57  			t.Fatalf("expected device name %q but received %q", expected, name)
    58  		}
    59  	}
    60  }
    61  
    62  func TestNewBlkio(t *testing.T) {
    63  	const root = "/test/folder"
    64  	const expected = "/test/folder/blkio"
    65  	const expectedProc = "/proc"
    66  
    67  	ctrl := NewBlkio(root)
    68  	if ctrl.root != expected {
    69  		t.Fatalf("expected cgroups root %q but received %q", expected, ctrl.root)
    70  	}
    71  	if ctrl.procRoot != expectedProc {
    72  		t.Fatalf("expected proc FS root %q but received %q", expectedProc, ctrl.procRoot)
    73  	}
    74  }
    75  
    76  func TestBlkioStat(t *testing.T) {
    77  	_, err := os.Stat("/sys/fs/cgroup/blkio")
    78  	if os.IsNotExist(err) {
    79  		t.Skip("failed to find /sys/fs/cgroup/blkio")
    80  	}
    81  
    82  	ctrl := NewBlkio("/sys/fs/cgroup")
    83  
    84  	var metrics v1.Metrics
    85  	err = ctrl.Stat("", &metrics)
    86  	if err != nil {
    87  		t.Fatalf("failed to call Stat: %v", err)
    88  	}
    89  
    90  	if len(metrics.Blkio.IoServicedRecursive) == 0 {
    91  		t.Fatalf("IoServicedRecursive must not be empty")
    92  	}
    93  	if len(metrics.Blkio.IoServiceBytesRecursive) == 0 {
    94  		t.Fatalf("IoServiceBytesRecursive must not be empty")
    95  	}
    96  }
    97  
    98  func TestNewBlkio_Proc(t *testing.T) {
    99  	const root = "/test/folder"
   100  	const expected = "/test/folder/blkio"
   101  	const expectedProc = "/test/proc"
   102  
   103  	ctrl := NewBlkio(root, ProcRoot(expectedProc))
   104  	if ctrl.root != expected {
   105  		t.Fatalf("expected cgroups root %q but received %q", expected, ctrl.root)
   106  	}
   107  	if ctrl.procRoot != expectedProc {
   108  		t.Fatalf("expected proc FS root %q but received %q", expectedProc, ctrl.procRoot)
   109  	}
   110  }
   111  

View as plain text