github.com/google/cadvisor@v0.49.1/integration/tests/api/test_utils.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	info "github.com/google/cadvisor/info/v1"
    22  
    23  	"github.com/opencontainers/runc/libcontainer/cgroups"
    24  	"github.com/stretchr/testify/assert"
    25  	"k8s.io/klog/v2"
    26  )
    27  
    28  func init() {
    29  	klog.InitFlags(nil)
    30  }
    31  
    32  // Checks that expected and actual are within delta of each other.
    33  func inDelta(t *testing.T, expected, actual, delta uint64, description string) {
    34  	var diff uint64
    35  	if expected > actual {
    36  		diff = expected - actual
    37  	} else {
    38  		diff = actual - expected
    39  	}
    40  	if diff > delta {
    41  		t.Errorf("%s (%d and %d) are not within %d of each other", description, expected, actual, delta)
    42  	}
    43  }
    44  
    45  // Checks that CPU stats are valid.
    46  func checkCPUStats(t *testing.T, stat info.CpuStats) {
    47  	assert := assert.New(t)
    48  
    49  	assert.NotEqual(0, stat.Usage.Total, "Total CPU usage should not be zero")
    50  
    51  	// PerCPU CPU usage is not supported in cgroupv2 (cpuacct.usage_percpu)
    52  	// https://github.com/google/cadvisor/issues/3065
    53  	if !cgroups.IsCgroup2UnifiedMode() {
    54  		assert.NotEmpty(stat.Usage.PerCpu, "Per-core usage should not be empty")
    55  
    56  		totalUsage := uint64(0)
    57  		for _, usage := range stat.Usage.PerCpu {
    58  			totalUsage += usage
    59  		}
    60  		inDelta(t, stat.Usage.Total, totalUsage, uint64((5 * time.Millisecond).Nanoseconds()), "Per-core CPU usage")
    61  	}
    62  
    63  	inDelta(t, stat.Usage.Total, stat.Usage.User+stat.Usage.System, uint64((500 * time.Millisecond).Nanoseconds()), "User + system CPU usage")
    64  	// TODO(rjnagal): Add verification for cpu load.
    65  }
    66  
    67  func checkMemoryStats(t *testing.T, stat info.MemoryStats) {
    68  	assert := assert.New(t)
    69  
    70  	assert.NotEqual(0, stat.Usage, "Memory usage should not be zero")
    71  	assert.NotEqual(0, stat.WorkingSet, "Memory working set should not be zero")
    72  	if stat.WorkingSet > stat.Usage {
    73  		t.Errorf("Memory working set (%d) should be at most equal to memory usage (%d)", stat.WorkingSet, stat.Usage)
    74  	}
    75  	// TODO(vmarmol): Add checks for ContainerData and HierarchicalData
    76  }