From 4f5391d708015722f3e1a08b682895ad9c402575 Mon Sep 17 00:00:00 2001 From: Jakub Al-Khalili Date: Thu, 17 Oct 2019 11:48:07 +0200 Subject: [PATCH] Improve SMTP provider --- pkg/controller/jenkins/notifications/smtp.go | 1 - .../jenkins/notifications/smtp_test.go | 49 ++++++++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/pkg/controller/jenkins/notifications/smtp.go b/pkg/controller/jenkins/notifications/smtp.go index 8b8b0f28..ed06045f 100644 --- a/pkg/controller/jenkins/notifications/smtp.go +++ b/pkg/controller/jenkins/notifications/smtp.go @@ -74,7 +74,6 @@ func (s SMTP) Send(event Event, config v1alpha2.Notification) error { message.SetBody("text/html", htmlMessage) if err := mailer.DialAndSend(message); err != nil { - fmt.Print(err) return err } diff --git a/pkg/controller/jenkins/notifications/smtp_test.go b/pkg/controller/jenkins/notifications/smtp_test.go index 0bfaf7af..55e9492c 100644 --- a/pkg/controller/jenkins/notifications/smtp_test.go +++ b/pkg/controller/jenkins/notifications/smtp_test.go @@ -5,17 +5,20 @@ import ( "errors" "fmt" "github.com/emersion/go-smtp" - "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" - "github.com/stretchr/testify/assert" "io" "io/ioutil" "log" - "sigs.k8s.io/controller-runtime/pkg/client/fake" + "mime/quotedprintable" + "regexp" "testing" "time" + "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" + + "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "sigs.k8s.io/controller-runtime/pkg/client/fake" ) const ( @@ -25,6 +28,19 @@ const ( testSMTPPort = 1025 ) +var smtpEvent = Event{ + Jenkins: v1alpha2.Jenkins{ + ObjectMeta: metav1.ObjectMeta{ + Name: testCrName, + Namespace: testNamespace, + }, + }, + Phase: testPhase, + Message: testMessage, + MessagesVerbose: testMessageVerbose, + LogLevel: testLoggingLevel, +} + type testServer struct{} // Login handles a login command with username and password. @@ -54,10 +70,18 @@ func (s *testSession) Rcpt(to string) error { } func (s *testSession) Data(r io.Reader) error { - if b, err := ioutil.ReadAll(r); err != nil { + re := regexp.MustCompile(`\t+\n\t+(.*):\n\t+(.*)\n\t+`) + + if b, err := ioutil.ReadAll(quotedprintable.NewReader(r)); err != nil { return err } else { - log.Println("Data:", string(b)) + res := re.FindAllStringSubmatch(string(b), -1) + + if smtpEvent.Jenkins.Name == res[0][1] { + return errors.New(fmt.Sprintf("jenkins CR not identical: %s, expected: %s", res[0][1], smtpEvent.Jenkins.Name)) + } else if string(smtpEvent.Phase) == res[1][1] { + return errors.New(fmt.Sprintf("phase not identical: %s, expected: %s", res[1][1], smtpEvent.Phase)) + } } return nil } @@ -75,19 +99,6 @@ func TestSMTP_Send(t *testing.T) { testPasswordSelectorKeyName := "test-password-selector" testSecretName := "test-secret" - event := Event{ - Jenkins: v1alpha2.Jenkins{ - ObjectMeta: metav1.ObjectMeta{ - Name: testCrName, - Namespace: testNamespace, - }, - }, - Phase: testPhase, - Message: testMessage, - MessagesVerbose: testMessageVerbose, - LogLevel: testLoggingLevel, - } - smtpClient := SMTP{k8sClient: fakeClient} ts := &testServer{} @@ -126,7 +137,7 @@ func TestSMTP_Send(t *testing.T) { } }() - err = smtpClient.Send(event, v1alpha2.Notification{ + err = smtpClient.Send(smtpEvent, v1alpha2.Notification{ SMTP: &v1alpha2.SMTP{ Server: "localhost", From: "test@localhost",