github.com/google/cadvisor@v0.49.1/metrics/prometheus_machine_test.go (about)

     1  // Copyright 2020 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 metrics
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/prometheus/client_golang/prometheus"
    25  	"github.com/prometheus/common/expfmt"
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"github.com/google/cadvisor/container"
    29  )
    30  
    31  const machineMetricsFile = "testdata/prometheus_machine_metrics"
    32  const machineMetricsFailureFile = "testdata/prometheus_machine_metrics_failure"
    33  
    34  func TestPrometheusMachineCollector(t *testing.T) {
    35  	collector := NewPrometheusMachineCollector(testSubcontainersInfoProvider{}, container.AllMetrics)
    36  	registry := prometheus.NewRegistry()
    37  	registry.MustRegister(collector)
    38  
    39  	metricsFamily, err := registry.Gather()
    40  	assert.Nil(t, err)
    41  
    42  	var metricBuffer bytes.Buffer
    43  	for _, metricFamily := range metricsFamily {
    44  		_, err := expfmt.MetricFamilyToText(&metricBuffer, metricFamily)
    45  		assert.Nil(t, err)
    46  	}
    47  	collectedMetrics := metricBuffer.String()
    48  
    49  	expectedMetrics, err := os.ReadFile(machineMetricsFile)
    50  	assert.Nil(t, err)
    51  	assert.Equal(t, string(expectedMetrics), collectedMetrics)
    52  }
    53  
    54  func TestPrometheusMachineCollectorWithFailure(t *testing.T) {
    55  	provider := &erroringSubcontainersInfoProvider{
    56  		successfulProvider: testSubcontainersInfoProvider{},
    57  		shouldFail:         true,
    58  	}
    59  	collector := NewPrometheusMachineCollector(provider, container.AllMetrics)
    60  	registry := prometheus.NewRegistry()
    61  	registry.MustRegister(collector)
    62  
    63  	metricsFamily, err := registry.Gather()
    64  	assert.Nil(t, err)
    65  
    66  	var metricBuffer bytes.Buffer
    67  	for _, metricFamily := range metricsFamily {
    68  		_, err := expfmt.MetricFamilyToText(&metricBuffer, metricFamily)
    69  		assert.Nil(t, err)
    70  	}
    71  	collectedMetrics := metricBuffer.String()
    72  	expectedMetrics, err := os.ReadFile(machineMetricsFailureFile)
    73  	assert.Nil(t, err)
    74  	assert.Equal(t, string(expectedMetrics), collectedMetrics)
    75  }
    76  
    77  func TestGetMemoryByType(t *testing.T) {
    78  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
    79  	assert.Nil(t, err)
    80  
    81  	capacityMetrics := getMemoryByType(machineInfo, memoryByTypeDimmCapacityKey)
    82  	assert.Equal(t, 2, len(capacityMetrics))
    83  
    84  	countMetrics := getMemoryByType(machineInfo, memoryByTypeDimmCountKey)
    85  	assert.Equal(t, 2, len(countMetrics))
    86  }
    87  
    88  func TestGetMemoryByTypeWithWrongProperty(t *testing.T) {
    89  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
    90  	assert.Nil(t, err)
    91  
    92  	metricVals := getMemoryByType(machineInfo, "wrong_property_name")
    93  	assert.Equal(t, 0, len(metricVals))
    94  }
    95  
    96  func TestGetCaches(t *testing.T) {
    97  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
    98  	assert.Nil(t, err)
    99  
   100  	metricVals := getCaches(machineInfo)
   101  
   102  	assert.Equal(t, 25, len(metricVals))
   103  	expectedMetricVals := []metricValue{
   104  		{value: 32768, labels: []string{"0", "0", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   105  		{value: 32768, labels: []string{"0", "0", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   106  		{value: 262144, labels: []string{"0", "0", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   107  		{value: 32764, labels: []string{"0", "1", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   108  		{value: 32764, labels: []string{"0", "1", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   109  		{value: 262148, labels: []string{"0", "1", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   110  		{value: 32768, labels: []string{"0", "2", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   111  		{value: 32768, labels: []string{"0", "2", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   112  		{value: 262144, labels: []string{"0", "2", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   113  		{value: 32764, labels: []string{"0", "3", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   114  		{value: 32764, labels: []string{"0", "3", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   115  		{value: 262148, labels: []string{"0", "3", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   116  		{value: 32768, labels: []string{"1", "4", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   117  		{value: 32768, labels: []string{"1", "4", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   118  		{value: 262144, labels: []string{"1", "4", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   119  		{value: 32764, labels: []string{"1", "5", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   120  		{value: 32764, labels: []string{"1", "5", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   121  		{value: 262148, labels: []string{"1", "5", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   122  		{value: 32768, labels: []string{"1", "6", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   123  		{value: 32768, labels: []string{"1", "6", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   124  		{value: 262144, labels: []string{"1", "6", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   125  		{value: 32764, labels: []string{"1", "7", "Data", "1"}, timestamp: time.Unix(1395066363, 0)},
   126  		{value: 32764, labels: []string{"1", "7", "Instruction", "1"}, timestamp: time.Unix(1395066363, 0)},
   127  		{value: 262148, labels: []string{"1", "7", "Unified", "2"}, timestamp: time.Unix(1395066363, 0)},
   128  		{value: 8388608, labels: []string{"1", "", "Unified", "3"}, timestamp: time.Unix(1395066363, 0)},
   129  	}
   130  	assertMetricValues(t, expectedMetricVals, metricVals, "Unexpected information about Node memory")
   131  }
   132  
   133  func TestGetThreadsSiblingsCount(t *testing.T) {
   134  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
   135  	assert.Nil(t, err)
   136  
   137  	metricVals := getThreadsSiblingsCount(machineInfo)
   138  
   139  	assert.Equal(t, 16, len(metricVals))
   140  	expectedMetricVals := []metricValue{
   141  		{value: 2, labels: []string{"0", "0", "0"}, timestamp: time.Unix(1395066363, 0)},
   142  		{value: 2, labels: []string{"0", "0", "1"}, timestamp: time.Unix(1395066363, 0)},
   143  		{value: 2, labels: []string{"0", "1", "2"}, timestamp: time.Unix(1395066363, 0)},
   144  		{value: 2, labels: []string{"0", "1", "3"}, timestamp: time.Unix(1395066363, 0)},
   145  		{value: 2, labels: []string{"0", "2", "4"}, timestamp: time.Unix(1395066363, 0)},
   146  		{value: 2, labels: []string{"0", "2", "5"}, timestamp: time.Unix(1395066363, 0)},
   147  		{value: 2, labels: []string{"0", "3", "6"}, timestamp: time.Unix(1395066363, 0)},
   148  		{value: 2, labels: []string{"0", "3", "7"}, timestamp: time.Unix(1395066363, 0)},
   149  		{value: 2, labels: []string{"1", "4", "8"}, timestamp: time.Unix(1395066363, 0)},
   150  		{value: 2, labels: []string{"1", "4", "9"}, timestamp: time.Unix(1395066363, 0)},
   151  		{value: 2, labels: []string{"1", "5", "10"}, timestamp: time.Unix(1395066363, 0)},
   152  		{value: 2, labels: []string{"1", "5", "11"}, timestamp: time.Unix(1395066363, 0)},
   153  		{value: 2, labels: []string{"1", "6", "12"}, timestamp: time.Unix(1395066363, 0)},
   154  		{value: 2, labels: []string{"1", "6", "13"}, timestamp: time.Unix(1395066363, 0)},
   155  		{value: 2, labels: []string{"1", "7", "14"}, timestamp: time.Unix(1395066363, 0)},
   156  		{value: 2, labels: []string{"1", "7", "15"}, timestamp: time.Unix(1395066363, 0)},
   157  	}
   158  	assertMetricValues(t, expectedMetricVals, metricVals, "Unexpected information about CPU threads")
   159  }
   160  
   161  func TestGetNodeMemory(t *testing.T) {
   162  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
   163  	assert.Nil(t, err)
   164  
   165  	metricVals := getNodeMemory(machineInfo)
   166  
   167  	assert.Equal(t, 2, len(metricVals))
   168  	expectedMetricVals := []metricValue{
   169  		{value: 33604804608, labels: []string{"0"}, timestamp: time.Unix(1395066363, 0)},
   170  		{value: 33604804606, labels: []string{"1"}, timestamp: time.Unix(1395066363, 0)},
   171  	}
   172  	assertMetricValues(t, expectedMetricVals, metricVals, "Unexpected information about Node memory")
   173  }
   174  
   175  func TestGetHugePagesCount(t *testing.T) {
   176  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
   177  	assert.Nil(t, err)
   178  
   179  	metricVals := getHugePagesCount(machineInfo)
   180  
   181  	assert.Equal(t, 4, len(metricVals))
   182  	expectedMetricVals := []metricValue{
   183  		{value: 0, labels: []string{"0", "1048576"}, timestamp: time.Unix(1395066363, 0)},
   184  		{value: 0, labels: []string{"0", "2048"}, timestamp: time.Unix(1395066363, 0)},
   185  		{value: 2, labels: []string{"1", "1048576"}, timestamp: time.Unix(1395066363, 0)},
   186  		{value: 4, labels: []string{"1", "2048"}, timestamp: time.Unix(1395066363, 0)},
   187  	}
   188  	assertMetricValues(t, expectedMetricVals, metricVals, "Unexpected information about Node memory")
   189  }
   190  
   191  func TestGetDistance(t *testing.T) {
   192  	machineInfo, err := testSubcontainersInfoProvider{}.GetMachineInfo()
   193  	assert.Nil(t, err)
   194  
   195  	metricVals := getDistance(machineInfo)
   196  
   197  	assert.Equal(t, 4, len(metricVals))
   198  	expectedMetricVals := []metricValue{
   199  		{value: 10, labels: []string{"0", "0"}, timestamp: time.Unix(1395066363, 0)},
   200  		{value: 12, labels: []string{"0", "1"}, timestamp: time.Unix(1395066363, 0)},
   201  		{value: 12, labels: []string{"1", "0"}, timestamp: time.Unix(1395066363, 0)},
   202  		{value: 10, labels: []string{"1", "1"}, timestamp: time.Unix(1395066363, 0)},
   203  	}
   204  	assertMetricValues(t, expectedMetricVals, metricVals, "Unexpected information about Node memory")
   205  }
   206  
   207  func assertMetricValues(t *testing.T, expected metricValues, actual metricValues, message string) {
   208  	for i := range actual {
   209  		assert.Truef(t, reflect.DeepEqual(expected[i], actual[i]),
   210  			"%s expected %#v but found %#v\n", message, expected[i], actual[i])
   211  	}
   212  }