From d80c68d60a0955bc3502aa49f66dcb50c34f7d47 Mon Sep 17 00:00:00 2001 From: Aline Abler Date: Fri, 21 Apr 2023 10:49:45 +0200 Subject: [PATCH] Add test for prometheus metrics in BillingEntity Email Cronjob --- Makefile | 2 +- controller.go | 2 +- .../billingentity_email_cronjob_test.go | 52 +++++++++++++++++++ controllers/email_metrics.go | 4 +- .../invitation_email_controller_test.go | 8 +-- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6600dbff..47fd4b37 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ run-api: build ## Starts control api apiserver against the current Kubernetes cl .PHONY: run-controller run-controller: build ## Starts control api controller against the current Kubernetes cluster (based on your local config) $(localenv_make) webhook-certs/tls.key - $(BIN_FILENAME) controller --username-prefix "appuio#" --webhook-cert-dir=./local-env/webhook-certs --webhook-port=9444 --zap-log-level debug + $(BIN_FILENAME) controller --username-prefix "appuio#" --webhook-cert-dir=./local-env/webhook-certs --webhook-port=9444 --zap-log-level debug --billingentity-email-cron-interval "@every 1m" .PHONY: local-env local-env-setup: ## Setup local kind-based dev environment diff --git a/controller.go b/controller.go index 2577922b..6041939d 100644 --- a/controller.go +++ b/controller.go @@ -100,7 +100,7 @@ func ControllerCommand() *cobra.Command { billingEntityEmailBodyTemplate := cmd.Flags().String("billingentity-email-body-template", defaultBillingEntityEmailTemplate, "Body for billing entity modification update mails") billingEntityEmailRecipient := cmd.Flags().String("billingentity-email-recipient", "", "Recipient e-mail address for billing entity modification update mails") billingEntityEmailSubject := cmd.Flags().String("billingentity-email-subject", "An APPUiO Billing Entity has been updated", "Subject for billing entity modification update mails") - billingEntityCronInterval := cmd.Flags().String("billingentity-email-cron-interval", "@every 1m", "Cron interval for how frequently billing entity update e-mails are sent") + billingEntityCronInterval := cmd.Flags().String("billingentity-email-cron-interval", "@every 1h", "Cron interval for how frequently billing entity update e-mails are sent") cmd.Run = func(*cobra.Command, []string) { scheme := runtime.NewScheme() diff --git a/controllers/billingentity_email_cronjob_test.go b/controllers/billingentity_email_cronjob_test.go index 53f887ad..e30815a5 100644 --- a/controllers/billingentity_email_cronjob_test.go +++ b/controllers/billingentity_email_cronjob_test.go @@ -3,8 +3,11 @@ package controllers_test import ( "context" "errors" + "strings" "testing" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -74,6 +77,55 @@ func billingEntityCronJob(c client.WithWatch) *BillingEntityEmailCronJob { ) return &r } +func Test_BillingEntityEmailCronJob_MetricsCorrect(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJob(c) + + err := j.Run(ctx) + require.NoError(t, err) + + reg := prometheus.NewRegistry() + reg.MustRegister(j.GetMetrics()) + require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` +# HELP control_api_billingentity_emails_sent_failed_total Total number of e-mails which failed to send +# TYPE control_api_billingentity_emails_sent_failed_total counter +control_api_billingentity_emails_sent_failed_total 0 +# HELP control_api_billingentity_emails_sent_success_total Total number of successfully sent e-mails +# TYPE control_api_billingentity_emails_sent_success_total counter +control_api_billingentity_emails_sent_success_total 1 +`), + )) +} + +func Test_BillingEntityEmailCronJob_WithSendingFailure_MetricsCorrect(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJobWithFailingSender(c) + + err := j.Run(ctx) + require.NoError(t, err) + + reg := prometheus.NewRegistry() + reg.MustRegister(j.GetMetrics()) + require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` +# HELP control_api_billingentity_emails_sent_failed_total Total number of e-mails which failed to send +# TYPE control_api_billingentity_emails_sent_failed_total counter +control_api_billingentity_emails_sent_failed_total 1 +# HELP control_api_billingentity_emails_sent_success_total Total number of successfully sent e-mails +# TYPE control_api_billingentity_emails_sent_success_total counter +control_api_billingentity_emails_sent_success_total 0 +`), + )) +} func billingEntityCronJobWithFailingSender(c client.WithWatch) *BillingEntityEmailCronJob { r := NewBillingEntityEmailCronJob( diff --git a/controllers/email_metrics.go b/controllers/email_metrics.go index 564e05b7..4f5d500c 100644 --- a/controllers/email_metrics.go +++ b/controllers/email_metrics.go @@ -8,7 +8,7 @@ func newSuccessCounter(subsystem string) prometheus.Counter { return prometheus.NewCounter(prometheus.CounterOpts{ Subsystem: subsystem, Name: "sent_success_total", - Help: "Total number of successfully sent invitation e-mails", + Help: "Total number of successfully sent e-mails", }) } @@ -16,6 +16,6 @@ func newFailureCounter(subsystem string) prometheus.Counter { return prometheus.NewCounter(prometheus.CounterOpts{ Subsystem: subsystem, Name: "sent_failed_total", - Help: "Total number of invitation e-mails which failed to send", + Help: "Total number of e-mails which failed to send", }) } diff --git a/controllers/invitation_email_controller_test.go b/controllers/invitation_email_controller_test.go index 3bbc68ec..3c2f744c 100644 --- a/controllers/invitation_email_controller_test.go +++ b/controllers/invitation_email_controller_test.go @@ -91,10 +91,10 @@ func Test_InvitationEmailReconciler_Reconcile_MetricsCorrect(t *testing.T) { reg := prometheus.NewRegistry() reg.MustRegister(r.GetMetrics()) require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` -# HELP control_api_invitation_emails_sent_failed_total Total number of invitation e-mails which failed to send +# HELP control_api_invitation_emails_sent_failed_total Total number of e-mails which failed to send # TYPE control_api_invitation_emails_sent_failed_total counter control_api_invitation_emails_sent_failed_total 0 -# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent invitation e-mails +# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent e-mails # TYPE control_api_invitation_emails_sent_success_total counter control_api_invitation_emails_sent_success_total 1 `), @@ -119,10 +119,10 @@ func Test_InvitationEmailReconciler_Reconcile_WithSendingFailure_MetricsCorrect( reg := prometheus.NewRegistry() reg.MustRegister(r.GetMetrics()) require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` -# HELP control_api_invitation_emails_sent_failed_total Total number of invitation e-mails which failed to send +# HELP control_api_invitation_emails_sent_failed_total Total number of e-mails which failed to send # TYPE control_api_invitation_emails_sent_failed_total counter control_api_invitation_emails_sent_failed_total 1 -# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent invitation e-mails +# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent e-mails # TYPE control_api_invitation_emails_sent_success_total counter control_api_invitation_emails_sent_success_total 0 `),