|
| 1 | +// Copyright 2015 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 common |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + "github.com/opencontainers/cgroups" |
| 25 | + |
| 26 | + "github.com/google/cadvisor/integration/framework" |
| 27 | +) |
| 28 | + |
| 29 | +func TestMachineStatsIsReturned(t *testing.T) { |
| 30 | + fm := framework.New(t) |
| 31 | + defer fm.Cleanup() |
| 32 | + |
| 33 | + machineStats, err := fm.Cadvisor().ClientV2().MachineStats() |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + |
| 38 | + as := assert.New(t) |
| 39 | + for _, stat := range machineStats { |
| 40 | + as.NotEqual(stat.Timestamp, time.Time{}) |
| 41 | + as.True(stat.Cpu.Usage.Total > 0) |
| 42 | + // PerCPU CPU usage is not supported in cgroupv2 (cpuacct.usage_percpu) |
| 43 | + // https://github.com/google/cadvisor/issues/3065 |
| 44 | + if !cgroups.IsCgroup2UnifiedMode() { |
| 45 | + as.True(len(stat.Cpu.Usage.PerCpu) > 0) |
| 46 | + } |
| 47 | + if stat.CpuInst != nil { |
| 48 | + as.True(stat.CpuInst.Usage.Total > 0) |
| 49 | + } |
| 50 | + as.True(stat.Memory.Usage > 0) |
| 51 | + for _, nStat := range stat.Network.Interfaces { |
| 52 | + as.NotEqual(nStat.Name, "") |
| 53 | + as.NotEqual(nStat.RxBytes, 0) |
| 54 | + } |
| 55 | + for _, fsStat := range stat.Filesystem { |
| 56 | + as.NotEqual(fsStat.Device, "") |
| 57 | + as.NotNil(fsStat.Capacity) |
| 58 | + as.NotNil(fsStat.Usage) |
| 59 | + as.NotNil(fsStat.ReadsCompleted) |
| 60 | + require.NotEmpty(t, fsStat.Type) |
| 61 | + if fsStat.Type == "vfs" { |
| 62 | + as.NotNil(fsStat.InodesFree) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments