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

     1  //go:build libpfm && cgo
     2  // +build libpfm,cgo
     3  
     4  // Copyright 2020 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  package api
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  
    24  	v1 "github.com/google/cadvisor/info/v1"
    25  	"github.com/google/cadvisor/integration/framework"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestPerfEvents(t *testing.T) {
    31  	fm := framework.New(t)
    32  	defer fm.Cleanup()
    33  
    34  	containerID := fm.Docker().RunPause()
    35  	waitForContainerInfo(fm, containerID)
    36  
    37  	info, err := fm.Cadvisor().Client().DockerContainer(containerID, &v1.ContainerInfoRequest{
    38  		NumStats: 1,
    39  	})
    40  
    41  	assert.Nil(t, err)
    42  	assert.Len(t, info.Stats, 1)
    43  	assert.Greater(t, len(info.Stats[0].PerfStats), 0, "Length of info.Stats[0].PerfStats is not greater than zero")
    44  	for k, stat := range info.Stats[0].PerfStats {
    45  		//Everything beyond name is non-deterministic unfortunately.
    46  		assert.Contains(t, []string{"context-switches", "cpu-migrations-custom"}, stat.Name, "Wrong metric name for key %d: %#v", k, stat)
    47  	}
    48  }
    49  
    50  func waitForContainerInfo(fm framework.Framework, containerID string) {
    51  	err := framework.RetryForDuration(func() error {
    52  		_, err := fm.Cadvisor().Client().DockerContainer(containerID, &v1.ContainerInfoRequest{NumStats: 1})
    53  		if err != nil {
    54  			return err
    55  		}
    56  		return nil
    57  	}, 5*time.Second)
    58  	assert.NoError(fm.T(), err)
    59  }