...

Source file src/github.com/containerd/cgroups/pids_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  	"encoding/hex"
    21  	"os"
    22  	"path/filepath"
    23  	"strconv"
    24  	"testing"
    25  
    26  	v1 "github.com/containerd/cgroups/stats/v1"
    27  	"github.com/opencontainers/runtime-spec/specs-go"
    28  )
    29  
    30  func TestPids(t *testing.T) {
    31  	mock, err := newMock(t)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	defer mock.delete()
    36  	pids := NewPids(mock.root)
    37  	if pids == nil {
    38  		t.Fatal("pids is nil")
    39  	}
    40  	resources := specs.LinuxResources{}
    41  	resources.Pids = &specs.LinuxPids{}
    42  	resources.Pids.Limit = int64(10)
    43  	err = pids.Create("test", &resources)
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	current := filepath.Join(mock.root, "pids", "test", "pids.current")
    48  	if err = os.WriteFile(
    49  		current,
    50  		[]byte(strconv.Itoa(5)),
    51  		defaultFilePerm,
    52  	); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	metrics := v1.Metrics{}
    56  	err = pids.Stat("test", &metrics)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	if metrics.Pids.Limit != uint64(10) {
    61  		t.Fatalf("expected pids limit %q but received %q",
    62  			uint64(10), metrics.Pids.Limit)
    63  	}
    64  	if metrics.Pids.Current != uint64(5) {
    65  		t.Fatalf("expected pids limit %q but received %q",
    66  			uint64(5), metrics.Pids.Current)
    67  	}
    68  	resources.Pids.Limit = int64(15)
    69  	err = pids.Update("test", &resources)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	err = pids.Stat("test", &metrics)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	if metrics.Pids.Limit != uint64(15) {
    78  		t.Fatalf("expected pids limit %q but received %q",
    79  			uint64(15), metrics.Pids.Limit)
    80  	}
    81  	if metrics.Pids.Current != uint64(5) {
    82  		t.Fatalf("expected pids limit %q but received %q",
    83  			uint64(5), metrics.Pids.Current)
    84  	}
    85  }
    86  
    87  func TestPidsMissingCurrent(t *testing.T) {
    88  	mock, err := newMock(t)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	defer mock.delete()
    93  	pids := NewPids(mock.root)
    94  	if pids == nil {
    95  		t.Fatal("pids is nil")
    96  	}
    97  	metrics := v1.Metrics{}
    98  	err = pids.Stat("test", &metrics)
    99  	if err == nil {
   100  		t.Fatal("expected not nil err")
   101  	}
   102  }
   103  
   104  func TestPidsMissingMax(t *testing.T) {
   105  	mock, err := newMock(t)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  	defer mock.delete()
   110  	pids := NewPids(mock.root)
   111  	if pids == nil {
   112  		t.Fatal("pids is nil")
   113  	}
   114  	err = os.Mkdir(filepath.Join(mock.root, "pids", "test"), defaultDirPerm)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  	current := filepath.Join(mock.root, "pids", "test", "pids.current")
   119  	if err = os.WriteFile(
   120  		current,
   121  		[]byte(strconv.Itoa(5)),
   122  		defaultFilePerm,
   123  	); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	metrics := v1.Metrics{}
   127  	err = pids.Stat("test", &metrics)
   128  	if err == nil {
   129  		t.Fatal("expected not nil err")
   130  	}
   131  }
   132  
   133  func TestPidsOverflowMax(t *testing.T) {
   134  	mock, err := newMock(t)
   135  	if err != nil {
   136  		t.Fatal(err)
   137  	}
   138  	defer mock.delete()
   139  	pids := NewPids(mock.root)
   140  	if pids == nil {
   141  		t.Fatal("pids is nil")
   142  	}
   143  	err = os.Mkdir(filepath.Join(mock.root, "pids", "test"), defaultDirPerm)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	current := filepath.Join(mock.root, "pids", "test", "pids.current")
   148  	if err = os.WriteFile(
   149  		current,
   150  		[]byte(strconv.Itoa(5)),
   151  		defaultFilePerm,
   152  	); err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	max := filepath.Join(mock.root, "pids", "test", "pids.max")
   156  	bytes, err := hex.DecodeString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	if err = os.WriteFile(
   161  		max,
   162  		bytes,
   163  		defaultFilePerm,
   164  	); err != nil {
   165  		t.Fatal(err)
   166  	}
   167  	metrics := v1.Metrics{}
   168  	err = pids.Stat("test", &metrics)
   169  	if err == nil {
   170  		t.Fatal("expected not nil err")
   171  	}
   172  }
   173  

View as plain text