github.com/google/cadvisor@v0.49.1/resctrl/manager_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  // Copyright 2021 Google Inc. All Rights Reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  // Manager tests.
    19  package resctrl
    20  
    21  import (
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestNewManager(t *testing.T) {
    29  	var testCases = []struct {
    30  		isResctrlInitialized bool
    31  		enabledCMT           bool
    32  		enabledMBM           bool
    33  		inHostNamespace      bool
    34  		err                  string
    35  		expected             Manager
    36  	}{
    37  		{
    38  			true,
    39  			true,
    40  			false,
    41  			true,
    42  			"",
    43  			&manager{interval: 0, inHostNamespace: true},
    44  		},
    45  		{
    46  			true,
    47  			false,
    48  			true,
    49  			true,
    50  			"",
    51  			&manager{interval: 0, inHostNamespace: true},
    52  		},
    53  		{
    54  			true,
    55  			true,
    56  			true,
    57  			false,
    58  			"",
    59  			&manager{interval: 0, inHostNamespace: false},
    60  		},
    61  		{
    62  			false,
    63  			true,
    64  			false,
    65  			false,
    66  			"the resctrl isn't initialized",
    67  			&NoopManager{},
    68  		},
    69  		{
    70  			false,
    71  			false,
    72  			true,
    73  			false,
    74  			"the resctrl isn't initialized",
    75  			&NoopManager{},
    76  		},
    77  		{
    78  			false,
    79  			true,
    80  			true,
    81  			true,
    82  			"the resctrl isn't initialized",
    83  			&NoopManager{},
    84  		},
    85  		{
    86  			false,
    87  			false,
    88  			false,
    89  			true,
    90  			"the resctrl isn't initialized",
    91  			&NoopManager{},
    92  		},
    93  		{
    94  			true,
    95  			false,
    96  			false,
    97  			true,
    98  			"there are no monitoring features available",
    99  			&NoopManager{},
   100  		},
   101  	}
   102  
   103  	for _, test := range testCases {
   104  		setup := func() error {
   105  			isResctrlInitialized = test.isResctrlInitialized
   106  			enabledCMT = test.enabledCMT
   107  			enabledMBM = test.enabledMBM
   108  			return nil
   109  		}
   110  		got, err := NewManager(0, setup, "", test.inHostNamespace)
   111  		assert.Equal(t, got, test.expected)
   112  		checkError(t, err, test.err)
   113  	}
   114  }
   115  
   116  func TestGetCollector(t *testing.T) {
   117  	rootResctrl = mockResctrl()
   118  	defer os.RemoveAll(rootResctrl)
   119  
   120  	pidsPath = mockContainersPids()
   121  	defer os.RemoveAll(pidsPath)
   122  
   123  	processPath = mockProcFs()
   124  	defer os.RemoveAll(processPath)
   125  
   126  	expectedID := "container"
   127  
   128  	setup := func() error {
   129  		isResctrlInitialized = true
   130  		enabledCMT = true
   131  		enabledMBM = true
   132  		return nil
   133  	}
   134  	manager, err := NewManager(0, setup, "", true)
   135  	assert.NoError(t, err)
   136  
   137  	_, err = manager.GetCollector(expectedID, mockGetContainerPids, 2)
   138  	assert.NoError(t, err)
   139  }