test: add some unit test cases for new Deployer

This commit is contained in:
Fu Diwei
2024-11-19 20:03:11 +08:00
parent 6367785b1b
commit 51fb9dca58
7 changed files with 307 additions and 38 deletions

View File

@@ -1,19 +1,25 @@
package email_test
import (
"context"
"os"
"strconv"
"testing"
notifierEmail "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/email"
npEmail "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/email"
)
const (
MockSubject = "test_subject"
MockMessage = "test_message"
)
/*
Shell command to run this test:
CERTIMATE_NOTIFIER_EMAIL_SMTPHOST="smtp.example.com" \
CERTIMATE_NOTIFIER_EMAIL_SMTPPORT=465 \
CERTIMATE_NOTIFIER_EMAIL_SMTPTLS=true \
CERTIMATE_NOTIFIER_EMAIL_SMTPHOST="smtp.example.com" \
CERTIMATE_NOTIFIER_EMAIL_USERNAME="your-username" \
CERTIMATE_NOTIFIER_EMAIL_PASSWORD="your-password" \
CERTIMATE_NOTIFIER_EMAIL_SENDERADDRESS="sender@example.com" \
@@ -21,31 +27,34 @@ Shell command to run this test:
go test -v -run TestNotify email_test.go
*/
func TestNotify(t *testing.T) {
smtpPort, err := strconv.ParseInt(os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPPORT"), 10, 32)
if err != nil {
t.Errorf("invalid envvar: %+v", err)
panic(err)
}
envPrefix := "CERTIMATE_NOTIFIER_EMAIL_"
tSmtpHost := os.Getenv(envPrefix + "SMTPHOST")
tSmtpPort, _ := strconv.ParseInt(os.Getenv(envPrefix+"SMTPPORT"), 10, 32)
tSmtpTLS, _ := strconv.ParseBool(os.Getenv(envPrefix + "SMTPTLS"))
tSmtpUsername := os.Getenv(envPrefix + "USERNAME")
tSmtpPassword := os.Getenv(envPrefix + "PASSWORD")
tSenderAddress := os.Getenv(envPrefix + "SENDERADDRESS")
tReceiverAddress := os.Getenv(envPrefix + "RECEIVERADDRESS")
smtpTLS, err := strconv.ParseBool(os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPTLS"))
if err != nil {
t.Errorf("invalid envvar: %+v", err)
panic(err)
}
res, err := notifierEmail.New(&notifierEmail.EmailNotifierConfig{
SmtpHost: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SMTPHOST"),
SmtpPort: int32(smtpPort),
SmtpTLS: smtpTLS,
Username: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_USERNAME"),
Password: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_PASSWORD"),
SenderAddress: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_SENDERADDRESS"),
ReceiverAddress: os.Getenv("CERTIMATE_NOTIFIER_EMAIL_RECEIVERADDRESS"),
notifier, err := npEmail.New(&npEmail.EmailNotifierConfig{
SmtpHost: tSmtpHost,
SmtpPort: int32(tSmtpPort),
SmtpTLS: tSmtpTLS,
Username: tSmtpUsername,
Password: tSmtpPassword,
SenderAddress: tSenderAddress,
ReceiverAddress: tReceiverAddress,
})
if err != nil {
t.Errorf("invalid envvar: %+v", err)
t.Errorf("err: %+v", err)
panic(err)
}
t.Logf("notify result: %v", res)
res, err := notifier.Notify(context.Background(), MockSubject, MockMessage)
if err != nil {
t.Errorf("err: %+v", err)
panic(err)
}
t.Logf("ok: %v", res)
}

View File

@@ -0,0 +1,41 @@
package webhook_test
import (
"context"
"os"
"testing"
npWebhook "github.com/usual2970/certimate/internal/pkg/core/notifier/providers/webhook"
)
const (
MockSubject = "test_subject"
MockMessage = "test_message"
)
/*
Shell command to run this test:
CERTIMATE_NOTIFIER_WEBHOOK_URL="https://example.com/your-webhook-url" \
go test -v -run TestNotify webhook_test.go
*/
func TestNotify(t *testing.T) {
envPrefix := "CERTIMATE_NOTIFIER_WEBHOOK_"
tUrl := os.Getenv(envPrefix + "URL")
notifier, err := npWebhook.New(&npWebhook.WebhookNotifierConfig{
Url: tUrl,
})
if err != nil {
t.Errorf("err: %+v", err)
panic(err)
}
res, err := notifier.Notify(context.Background(), MockSubject, MockMessage)
if err != nil {
t.Errorf("err: %+v", err)
panic(err)
}
t.Logf("ok: %v", res)
}