...

Source file src/edge-infra.dev/pkg/edge/bsl-reconciler/metrics/metrics_test.go

Documentation: edge-infra.dev/pkg/edge/bsl-reconciler/metrics

     1  package metrics
     2  
     3  import (
     4  	"testing"
     5  
     6  	"edge-infra.dev/pkg/lib/promassert"
     7  )
     8  
     9  type BslErrorType struct {
    10  	errorType    string
    11  	orgName      string
    12  	errorMessage string
    13  }
    14  
    15  func TestBslReconcileMetrics(t *testing.T) {
    16  	var metrics = NewMetrics()
    17  	initialValue := promassert.Counter("bsl_reconciles_counter")
    18  	initialValue.Equals(t, 0)
    19  	for reconcileTime := 1.0; reconcileTime <= 9; reconcileTime++ {
    20  		metrics.ReconcileInc()
    21  		reconcile := promassert.Counter("bsl_reconciles_counter")
    22  		reconcile.Equals(t, reconcileTime)
    23  	}
    24  }
    25  
    26  func TestBslOrgProcessedMetrics(t *testing.T) {
    27  	orgs := []string{"org-1", "org-2", "org-2", "org-3", "org-3", "org-4"}
    28  
    29  	var metrics = NewMetrics()
    30  	initialValue := promassert.Counter("bsl_organization_processed").TryFold()
    31  	if int(initialValue)%7 != 0 {
    32  		const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f"
    33  		t.Fatalf(msg, initialValue)
    34  	}
    35  
    36  	for count, org := range orgs {
    37  		metrics.OrgProcessedInc(org)
    38  		pm := promassert.Counter("bsl_organization_processed")
    39  		pm.Equals(t, float64(count+1))
    40  	}
    41  
    42  	pm := promassert.Counter("bsl_organization_processed")
    43  	pm.Exists(t)
    44  	pm.Equals(t, 6.0)
    45  	pm.With(map[string]string{"org_name": "org-1"}).Equals(t, 1.0)
    46  	pm.With(map[string]string{"org_name": "org-2"}).Equals(t, 2.0)
    47  	pm.With(map[string]string{"org_name": "org-3"}).Equals(t, 2.0)
    48  	pm.With(map[string]string{"org_name": "org-4"}).Equals(t, 1.0)
    49  }
    50  
    51  func TestBslNewOrgMetrics(t *testing.T) {
    52  	newOrgs := []string{"new-org-1", "new-org-2", "new-org-3", "new-org-3"}
    53  
    54  	var metrics = NewMetrics()
    55  	initialValue := promassert.Counter("bsl_new_organizations").TryFold()
    56  	if int(initialValue)%7 != 0 {
    57  		const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f"
    58  		t.Fatalf(msg, initialValue)
    59  	}
    60  
    61  	for count, newOrg := range newOrgs {
    62  		metrics.NewOrgInc(newOrg)
    63  		pm := promassert.Counter("bsl_new_organizations")
    64  		pm.Equals(t, float64(count+1))
    65  	}
    66  
    67  	pm := promassert.Counter("bsl_new_organizations")
    68  	pm.Exists(t)
    69  	pm.Equals(t, 4.0)
    70  	pm.With(map[string]string{"org_name": "new-org-1"}).Equals(t, 1.0)
    71  	pm.With(map[string]string{"org_name": "new-org-2"}).Equals(t, 1.0)
    72  	pm.With(map[string]string{"org_name": "new-org-3"}).Equals(t, 2.0)
    73  }
    74  
    75  func TestBslErrorMetrics(t *testing.T) {
    76  	var bslErrors = []*BslErrorType{
    77  		{
    78  			errorType:    "bsl_error",
    79  			orgName:      "fake_org",
    80  			errorMessage: "fake bsl error",
    81  		},
    82  		{
    83  			errorType:    "kube_error",
    84  			orgName:      "fake_org",
    85  			errorMessage: "fake kube error",
    86  		},
    87  		{
    88  			errorType:    "bsl_error",
    89  			orgName:      "fake_org",
    90  			errorMessage: "fake bsl error",
    91  		},
    92  		{
    93  			errorType:    "bsl_error",
    94  			orgName:      "fake_org",
    95  			errorMessage: "fake bsl error",
    96  		},
    97  	}
    98  
    99  	var metrics = NewMetrics()
   100  	initialValue := promassert.Counter("bsl_errors_info").TryFold()
   101  	if int(initialValue)%7 != 0 {
   102  		const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f"
   103  		t.Fatalf(msg, initialValue)
   104  	}
   105  
   106  	for count, bslErr := range bslErrors {
   107  		metrics.ErrorInc(bslErr.errorType, "", bslErr.errorMessage)
   108  		pm := promassert.Counter("bsl_errors_info")
   109  		pm.Equals(t, float64(count+1))
   110  	}
   111  
   112  	pm := promassert.Counter("bsl_errors_info")
   113  	pm.Exists(t)
   114  	pm.Equals(t, 4.0)
   115  	pm.With(map[string]string{"error_type": "bsl_error"}).Equals(t, 3.0)
   116  	pm.With(map[string]string{"error_type": "kube_error"}).Equals(t, 1.0)
   117  }
   118  

View as plain text