github.com/Azure/aad-pod-identity@v1.8.17/pkg/metrics/metrics_test.go (about)

     1  package metrics
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"go.opencensus.io/stats"
     8  	"go.opencensus.io/stats/view"
     9  )
    10  
    11  // TestBasicAndDurationReport test for basic count,duration metrics and related tags
    12  func TestBasicAndDurationReport(t *testing.T) {
    13  	reporter, err := initTest()
    14  	if err != nil {
    15  		t.Errorf("Failed to initialize Test:%v", err)
    16  	}
    17  
    18  	testCounterMetric(t, reporter, AssignedIdentityAdditionCountM)
    19  	testCounterMetric(t, reporter, AssignedIdentityDeletionCountM)
    20  	testCounterMetric(t, reporter, AssignedIdentityUpdateCountM)
    21  	testCounterMetric(t, reporter, MICCycleCountM)
    22  	testCounterMetric(t, reporter, MICNewLeaderElectionCountM)
    23  	testCounterMetric(t, reporter, CloudProviderOperationsErrorsCountM)
    24  	testCounterMetric(t, reporter, KubernetesAPIOperationsErrorsCountM)
    25  	testOperationDurationMetric(t, reporter, CloudProviderOperationsDurationM)
    26  	testOperationDurationMetric(t, reporter, NMIOperationsDurationM)
    27  }
    28  
    29  // testOperationDurationMetric tests the duration metric and related tags
    30  func testOperationDurationMetric(t *testing.T, reporter *Reporter, m *stats.Float64Measure) {
    31  	minimumDuration := float64(2)
    32  	maximumDuration := float64(4)
    33  	testOperationKey := "test"
    34  	err := reporter.ReportOperation(testOperationKey, m.M(minimumDuration))
    35  	if err != nil {
    36  		t.Errorf("Error when reporting metrics: %v from %v", err, m.Name())
    37  	}
    38  	err = reporter.ReportOperation(testOperationKey, m.M(maximumDuration))
    39  	if err != nil {
    40  		t.Errorf("Error when reporting metrics: %v from %v", err, m.Name())
    41  	}
    42  
    43  	row, err := view.RetrieveData(m.Name())
    44  	if err != nil {
    45  		t.Errorf("Error when retrieving data: %v from %v", err, m.Name())
    46  	}
    47  
    48  	duration, ok := row[0].Data.(*view.DistributionData)
    49  	if !ok {
    50  		t.Error("DistributionData missing")
    51  	}
    52  
    53  	tag := row[0].Tags[0]
    54  	if tag.Key.Name() != operationTypeKey.Name() && tag.Value != testOperationKey {
    55  		t.Errorf("Tag does not match for %v", operationTypeKey.Name())
    56  	}
    57  	if duration.Min != minimumDuration {
    58  		t.Errorf("Metric: %v - Expected %v, got %v. ", m.Name(), duration.Min, minimumDuration)
    59  	}
    60  	if duration.Max != maximumDuration {
    61  		t.Errorf("Metric: %v - Expected %v, got %v. ", m.Name(), duration.Max, maximumDuration)
    62  	}
    63  }
    64  
    65  // testCounterMetric test the given measure count
    66  func testCounterMetric(t *testing.T, reporter *Reporter, m *stats.Int64Measure) {
    67  	totalNumberOfOperations := 2
    68  	reporter.Report(m.M(1))
    69  	reporter.Report(m.M(1))
    70  	row, err := view.RetrieveData(m.Name())
    71  	if err != nil {
    72  		t.Errorf("Error when retrieving data: %v from %v", err, m.Name())
    73  	}
    74  
    75  	count, ok := row[0].Data.(*view.CountData)
    76  	if !ok {
    77  		t.Error("ReportRequest should have aggregation Count()")
    78  	}
    79  	if count.Value != int64(totalNumberOfOperations) {
    80  		t.Errorf("Metric: %v - Expected %v, got %v. ", m.Name(), count.Value, totalNumberOfOperations)
    81  	}
    82  }
    83  
    84  // initTest initialize the view and reporter for the test
    85  func initTest() (*Reporter, error) {
    86  	// initilize the views
    87  	err := registerViews()
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	reporter, err := NewReporter()
    92  	if err != nil {
    93  		return nil, fmt.Errorf("failed to create reporter for metrics, error: %+v", err)
    94  	}
    95  	return reporter, nil
    96  }