...

Source file src/github.com/containerd/cgroups/v2/memoryv2_test.go

Documentation: github.com/containerd/cgroups/v2

     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 v2
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestCgroupv2MemoryStats(t *testing.T) {
    28  	checkCgroupMode(t)
    29  	group := "/memory-test-cg"
    30  	groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
    31  	res := Resources{
    32  		Memory: &Memory{
    33  			Max:  pointerInt64(629145600),
    34  			Swap: pointerInt64(314572800),
    35  			High: pointerInt64(524288000),
    36  		},
    37  	}
    38  	c, err := NewManager(defaultCgroup2Path, groupPath, &res)
    39  	if err != nil {
    40  		t.Fatal("failed to init new cgroup manager: ", err)
    41  	}
    42  	defer os.Remove(c.path)
    43  	stats, err := c.Stat()
    44  	if err != nil {
    45  		t.Fatal("failed to get cgroups stats: ", err)
    46  	}
    47  
    48  	assert.Equal(t, uint64(314572800), stats.Memory.SwapLimit)
    49  	assert.Equal(t, uint64(629145600), stats.Memory.UsageLimit)
    50  	checkFileContent(t, c.path, "memory.swap.max", "314572800")
    51  	checkFileContent(t, c.path, "memory.max", "629145600")
    52  }
    53  
    54  func TestSystemdCgroupMemoryController(t *testing.T) {
    55  	checkCgroupMode(t)
    56  	group := fmt.Sprintf("testing-memory-%d.scope", os.Getpid())
    57  	res := Resources{
    58  		Memory: &Memory{
    59  			Min: pointerInt64(16384),
    60  			Max: pointerInt64(629145600),
    61  		},
    62  	}
    63  	c, err := NewSystemd("", group, os.Getpid(), &res)
    64  	if err != nil {
    65  		t.Fatal("failed to init new cgroup systemd manager: ", err)
    66  	}
    67  	checkFileContent(t, c.path, "memory.min", "16384")
    68  	checkFileContent(t, c.path, "memory.max", "629145600")
    69  }
    70  

View as plain text