...

Source file src/github.com/containerd/cgroups/mock_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  	"testing"
    23  )
    24  
    25  func init() {
    26  	defaultFilePerm = 0666
    27  }
    28  
    29  func newMock(tb testing.TB) (*mockCgroup, error) {
    30  	root := tb.TempDir()
    31  	subsystems, err := defaults(root)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	for _, s := range subsystems {
    36  		if err := os.MkdirAll(filepath.Join(root, string(s.Name())), defaultDirPerm); err != nil {
    37  			return nil, err
    38  		}
    39  	}
    40  	// make cpuset root files
    41  	for _, v := range []struct {
    42  		name  string
    43  		value []byte
    44  	}{
    45  		{
    46  			name:  "cpuset.cpus",
    47  			value: []byte("0-3"),
    48  		},
    49  		{
    50  			name:  "cpuset.mems",
    51  			value: []byte("0-3"),
    52  		},
    53  	} {
    54  		if err := os.WriteFile(filepath.Join(root, "cpuset", v.name), v.value, defaultFilePerm); err != nil {
    55  			return nil, err
    56  		}
    57  	}
    58  	return &mockCgroup{
    59  		root:       root,
    60  		subsystems: subsystems,
    61  	}, nil
    62  }
    63  
    64  type mockCgroup struct {
    65  	root       string
    66  	subsystems []Subsystem
    67  }
    68  
    69  func (m *mockCgroup) delete() error {
    70  	return os.RemoveAll(m.root)
    71  }
    72  
    73  func (m *mockCgroup) hierarchy() ([]Subsystem, error) {
    74  	return m.subsystems, nil
    75  }
    76  

View as plain text