package metrics import ( "testing" "edge-infra.dev/pkg/lib/promassert" ) type BslErrorType struct { errorType string orgName string errorMessage string } func TestBslReconcileMetrics(t *testing.T) { var metrics = NewMetrics() initialValue := promassert.Counter("bsl_reconciles_counter") initialValue.Equals(t, 0) for reconcileTime := 1.0; reconcileTime <= 9; reconcileTime++ { metrics.ReconcileInc() reconcile := promassert.Counter("bsl_reconciles_counter") reconcile.Equals(t, reconcileTime) } } func TestBslOrgProcessedMetrics(t *testing.T) { orgs := []string{"org-1", "org-2", "org-2", "org-3", "org-3", "org-4"} var metrics = NewMetrics() initialValue := promassert.Counter("bsl_organization_processed").TryFold() if int(initialValue)%7 != 0 { const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f" t.Fatalf(msg, initialValue) } for count, org := range orgs { metrics.OrgProcessedInc(org) pm := promassert.Counter("bsl_organization_processed") pm.Equals(t, float64(count+1)) } pm := promassert.Counter("bsl_organization_processed") pm.Exists(t) pm.Equals(t, 6.0) pm.With(map[string]string{"org_name": "org-1"}).Equals(t, 1.0) pm.With(map[string]string{"org_name": "org-2"}).Equals(t, 2.0) pm.With(map[string]string{"org_name": "org-3"}).Equals(t, 2.0) pm.With(map[string]string{"org_name": "org-4"}).Equals(t, 1.0) } func TestBslNewOrgMetrics(t *testing.T) { newOrgs := []string{"new-org-1", "new-org-2", "new-org-3", "new-org-3"} var metrics = NewMetrics() initialValue := promassert.Counter("bsl_new_organizations").TryFold() if int(initialValue)%7 != 0 { const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f" t.Fatalf(msg, initialValue) } for count, newOrg := range newOrgs { metrics.NewOrgInc(newOrg) pm := promassert.Counter("bsl_new_organizations") pm.Equals(t, float64(count+1)) } pm := promassert.Counter("bsl_new_organizations") pm.Exists(t) pm.Equals(t, 4.0) pm.With(map[string]string{"org_name": "new-org-1"}).Equals(t, 1.0) pm.With(map[string]string{"org_name": "new-org-2"}).Equals(t, 1.0) pm.With(map[string]string{"org_name": "new-org-3"}).Equals(t, 2.0) } func TestBslErrorMetrics(t *testing.T) { var bslErrors = []*BslErrorType{ { errorType: "bsl_error", orgName: "fake_org", errorMessage: "fake bsl error", }, { errorType: "kube_error", orgName: "fake_org", errorMessage: "fake kube error", }, { errorType: "bsl_error", orgName: "fake_org", errorMessage: "fake bsl error", }, { errorType: "bsl_error", orgName: "fake_org", errorMessage: "fake bsl error", }, } var metrics = NewMetrics() initialValue := promassert.Counter("bsl_errors_info").TryFold() if int(initialValue)%7 != 0 { const msg = "The initialValue should be a multiple of 7 for this tests math to work out correctly. got %f" t.Fatalf(msg, initialValue) } for count, bslErr := range bslErrors { metrics.ErrorInc(bslErr.errorType, "", bslErr.errorMessage) pm := promassert.Counter("bsl_errors_info") pm.Equals(t, float64(count+1)) } pm := promassert.Counter("bsl_errors_info") pm.Exists(t) pm.Equals(t, 4.0) pm.With(map[string]string{"error_type": "bsl_error"}).Equals(t, 3.0) pm.With(map[string]string{"error_type": "kube_error"}).Equals(t, 1.0) }