Skip to content

Commit 1e4adf8

Browse files
committed
test: add additional CRI-O integration tests
Add new CRI-O integration tests to match Docker test coverage: - TestCrioContainerNetworkStats: tests network statistics collection - TestCrioContainerSpec: tests container spec properties - TestCrioContainerDeletionExitCode: tests exit code in deletion events - TestCrioFilesystemStats: tests filesystem statistics collection Signed-off-by: Davanum Srinivas <davanum@gmail.com>
1 parent ed6d3b7 commit 1e4adf8

File tree

11 files changed

+493
-6
lines changed

11 files changed

+493
-6
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ docker-test: container-test
3838
test-integration:
3939
GO_FLAGS=$(or $(GO_FLAGS),-race) ./build/build.sh
4040
$(GO_TEST) -c github.com/google/cadvisor/integration/tests/api
41-
$(GO_TEST) -c github.com/google/cadvisor/integration/tests/healthz
41+
$(GO_TEST) -c github.com/google/cadvisor/integration/tests/common
4242
@./build/integration.sh
4343

4444
docker-test-integration:
@@ -50,6 +50,7 @@ docker-test-integration-crio:
5050
test-integration-crio:
5151
GO_FLAGS=$(or $(GO_FLAGS),-race) ./build/build.sh
5252
$(GO_TEST) -c github.com/google/cadvisor/integration/tests/crio
53+
$(GO_TEST) -c github.com/google/cadvisor/integration/tests/common
5354
@./build/integration-crio.sh
5455

5556
test-runner:

build/integration-crio.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,10 @@ if ! [ -f ./crio.test ]; then
251251
exit 1
252252
fi
253253
./crio.test --vmodule=*=2 -test.v
254+
255+
echo ">> running common integration tests against local cAdvisor"
256+
if [ -f ./common.test ]; then
257+
./common.test -test.v
258+
else
259+
echo "Skipping common tests (./common.test not found)"
260+
fi

build/integration-in-docker-crio.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ function run_tests() {
3636
# Add safe.directory as workaround for https://github.com/actions/runner/issues/2033
3737
# Build for amd64 to match the test container
3838
BUILD_CMD="git config --global safe.directory /go/src/github.com/google/cadvisor && env GOOS=linux GOARCH=amd64 GO_FLAGS='$GO_FLAGS' CGO_ENABLED=0 ./build/build.sh && \
39-
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c github.com/google/cadvisor/integration/tests/crio"
39+
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c github.com/google/cadvisor/integration/tests/crio && \
40+
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c github.com/google/cadvisor/integration/tests/common"
4041

4142
if [ "$BUILD_PACKAGES" != "" ]; then
4243
BUILD_CMD="apt update && apt install -y $BUILD_PACKAGES && \

build/integration-in-docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function run_tests() {
3333
# Add safe.directory as workaround for https://github.com/actions/runner/issues/2033
3434
BUILD_CMD="git config --global safe.directory /go/src/github.com/google/cadvisor && env GOOS=linux GOARCH=amd64 GO_FLAGS='$GO_FLAGS' ./build/build.sh && \
3535
env GOOS=linux GOFLAGS='$GO_FLAGS' go test -c github.com/google/cadvisor/integration/tests/api && \
36-
env GOOS=linux GOFLAGS='$GO_FLAGS' go test -c github.com/google/cadvisor/integration/tests/healthz"
36+
env GOOS=linux GOFLAGS='$GO_FLAGS' go test -c github.com/google/cadvisor/integration/tests/common"
3737

3838
if [ "$BUILD_PACKAGES" != "" ]; then
3939
BUILD_CMD="apt update && apt install -y $BUILD_PACKAGES && \

build/integration.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ if [[ "${DOCKER_IN_DOCKER_ENABLED:-}" == "true" ]]; then
117117
fi
118118

119119
echo ">> running integration tests against local cAdvisor"
120-
if ! [ -f ./api.test ] || ! [ -f ./healthz.test ]; then
121-
echo You must compile the ./api.test binary and ./healthz.test binary before
120+
if ! [ -f ./api.test ] || ! [ -f ./common.test ]; then
121+
echo You must compile the ./api.test binary and ./common.test binary before
122122
echo running the integration tests.
123123
exit 1
124124
fi
125125
./api.test --vmodule=*=2 -test.v
126-
./healthz.test --vmodule=*=2 -test.v
126+
./common.test -test.v
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
20+
"github.com/google/cadvisor/integration/framework"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestAttributeInformationIsReturned(t *testing.T) {
26+
fm := framework.New(t)
27+
defer fm.Cleanup()
28+
29+
attributes, err := fm.Cadvisor().ClientV2().Attributes()
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
vp := `\d+\.\d+\.\d+`
35+
assert.True(t, assert.Regexp(t, vp, attributes.KernelVersion),
36+
"Expected %s to match %s", attributes.KernelVersion, vp)
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 common
16+
17+
import (
18+
"io"
19+
"net/http"
20+
"testing"
21+
22+
"github.com/google/cadvisor/integration/framework"
23+
)
24+
25+
func TestHealthzOk(t *testing.T) {
26+
fm := framework.New(t)
27+
defer fm.Cleanup()
28+
29+
// Ensure that /healthz returns "ok"
30+
resp, err := http.Get(fm.Hostname().FullHostname() + "healthz")
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
defer resp.Body.Close()
35+
body, err := io.ReadAll(resp.Body)
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
40+
if string(body) != "ok" {
41+
t.Fatalf("cAdvisor returned unexpected healthz status of %q", body)
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 common contains integration tests that are runtime-independent.
16+
// These tests work with any container runtime (Docker, CRI-O, containerd, etc.)
17+
// and test cAdvisor's core functionality like machine info, attributes, and health.
18+
package common
19+
20+
import (
21+
"testing"
22+
23+
"github.com/google/cadvisor/integration/framework"
24+
)
25+
26+
func TestMachineInformationIsReturned(t *testing.T) {
27+
fm := framework.New(t)
28+
defer fm.Cleanup()
29+
30+
machineInfo, err := fm.Cadvisor().Client().MachineInfo()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
35+
// Check for "sane" values. Note these can change with time.
36+
if machineInfo.NumCores <= 0 || machineInfo.NumCores >= 1000000 {
37+
t.Errorf("Machine info has unexpected number of cores: %v", machineInfo.NumCores)
38+
}
39+
if machineInfo.MemoryCapacity == 0 || machineInfo.MemoryCapacity >= (1<<50 /* 1PB */) {
40+
t.Errorf("Machine info has unexpected amount of memory: %v", machineInfo.MemoryCapacity)
41+
}
42+
if len(machineInfo.Filesystems) == 0 {
43+
t.Errorf("Expected to have some filesystems, found none")
44+
}
45+
for _, fs := range machineInfo.Filesystems {
46+
if fs.Device == "" {
47+
t.Errorf("Expected a non-empty device name in: %+v", fs)
48+
}
49+
if fs.Capacity >= (1 << 60 /* 1 EB*/) {
50+
t.Errorf("Unexpected capacity in device %q: %v", fs.Device, fs.Capacity)
51+
}
52+
if fs.Type == "" {
53+
t.Errorf("Filesystem type is not set")
54+
} else if fs.Type == "vfs" && fs.Inodes == 0 {
55+
if fs.Device != "devpts" {
56+
t.Errorf("Inodes not available for device %q", fs.Device)
57+
}
58+
}
59+
}
60+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)