github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/metrics/node_info_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"strconv"
     5  	"testing"
     6  
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/onflow/flow-go/utils/unittest"
    12  )
    13  
    14  // TestNodeInfoCollector_NodeInfo tests if node info collector reports desired metrics
    15  func TestNodeInfoCollector_NodeInfo(t *testing.T) {
    16  	reg := prometheus.NewRegistry()
    17  	prometheus.DefaultRegisterer = reg
    18  	collector := NewNodeInfoCollector()
    19  	version := "0.29"
    20  	commit := "63cec231136914941e2358de2054a6ef71ea3c99"
    21  	sporkID := unittest.IdentifierFixture().String()
    22  	protocolVersion := uint(10076)
    23  	collector.NodeInfo(version, commit, sporkID, protocolVersion)
    24  	metricsFamilies, err := reg.Gather()
    25  	require.NoError(t, err)
    26  
    27  	assertReported := func(value string) {
    28  		for _, metric := range metricsFamilies[0].Metric {
    29  			for _, label := range metric.GetLabel() {
    30  				if label.GetValue() == value {
    31  					return
    32  				}
    33  			}
    34  		}
    35  		assert.Failf(t, "metric not found", "except to find value %s", value)
    36  	}
    37  
    38  	protocolVersionAsString := strconv.FormatUint(uint64(protocolVersion), 10)
    39  	for _, value := range []string{version, commit, sporkID, protocolVersionAsString} {
    40  		assertReported(value)
    41  	}
    42  }