Skip to content

Commit

Permalink
Add test for prometheus metrics in BillingEntity Email Cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
Aline Abler committed Apr 21, 2023
1 parent 55343ff commit d80c68d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
52 changes: 52 additions & 0 deletions controllers/billingentity_email_cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions controllers/email_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ 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",
})
}

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",
})
}
8 changes: 4 additions & 4 deletions controllers/invitation_email_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`),
Expand All @@ -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
`),
Expand Down

0 comments on commit d80c68d

Please sign in to comment.