...

Source file src/github.com/containerd/cgroups/v2/utils_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  	"strings"
    21  	"testing"
    22  
    23  	"github.com/opencontainers/runtime-spec/specs-go"
    24  	"github.com/stretchr/testify/assert"
    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 TestToResources(t *testing.T) {
    51  	var (
    52  		quota  int64  = 8000
    53  		period uint64 = 10000
    54  		shares uint64 = 5000
    55  	)
    56  	weight := 1 + ((shares-2)*9999)/262142
    57  	res := specs.LinuxResources{CPU: &specs.LinuxCPU{Quota: &quota, Period: &period, Shares: &shares}}
    58  	v2resources := ToResources(&res)
    59  
    60  	assert.Equal(t, weight, *v2resources.CPU.Weight)
    61  	assert.Equal(t, CPUMax("8000 10000"), v2resources.CPU.Max)
    62  
    63  	res2 := specs.LinuxResources{CPU: &specs.LinuxCPU{Period: &period}}
    64  	v2resources2 := ToResources(&res2)
    65  	assert.Equal(t, CPUMax("max 10000"), v2resources2.CPU.Max)
    66  }
    67  

View as plain text