From a5f4c7834691af19815fcb492625c56a4b7db0e3 Mon Sep 17 00:00:00 2001 From: Jakub Al-Khalili Date: Thu, 17 Oct 2019 13:21:54 +0200 Subject: [PATCH] Improve tests and SMTP notification provider --- pkg/apis/jenkins/v1alpha2/jenkins_types.go | 13 +++++---- .../configuration/base/validate_test.go | 2 +- .../jenkins/notifications/sender.go | 1 - pkg/controller/jenkins/notifications/smtp.go | 4 ++- .../jenkins/notifications/smtp_test.go | 29 ++++++++++--------- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/pkg/apis/jenkins/v1alpha2/jenkins_types.go b/pkg/apis/jenkins/v1alpha2/jenkins_types.go index 5b829be4..47489ac4 100644 --- a/pkg/apis/jenkins/v1alpha2/jenkins_types.go +++ b/pkg/apis/jenkins/v1alpha2/jenkins_types.go @@ -73,7 +73,7 @@ type Notification struct { Slack *Slack `json:"slack,omitempty"` Teams *MicrosoftTeams `json:"teams,omitempty"` Mailgun *Mailgun `json:"mailgun,omitempty"` - SMTP *SMTP `json:"smtp,omitempty"` + SMTP *SMTP `json:"smtp,omitempty"` } // Slack is handler for Slack notification channel @@ -82,14 +82,15 @@ type Slack struct { WebHookURLSecretKeySelector SecretKeySelector `json:"webHookURLSecretKeySelector"` } +// SMTP is handler for sending emails via this protocol type SMTP struct { UsernameSecretKeySelector SecretKeySelector `json:"usernameSecretKeySelector"` PasswordSecretKeySelector SecretKeySelector `json:"passwordSecretKeySelector"` - Port int `json:"port"` - Server string `json:"server"` - TLSInsecureSkipVerify bool `json:"tlsInsecureSkipVerify,omitempty"` - From string `json:"from"` - To string `json:"to"` + Port int `json:"port"` + Server string `json:"server"` + TLSInsecureSkipVerify bool `json:"tlsInsecureSkipVerify,omitempty"` + From string `json:"from"` + To string `json:"to"` } // MicrosoftTeams is handler for Microsoft MicrosoftTeams notification channel diff --git a/pkg/controller/jenkins/configuration/base/validate_test.go b/pkg/controller/jenkins/configuration/base/validate_test.go index 97d21a5a..51c1206f 100644 --- a/pkg/controller/jenkins/configuration/base/validate_test.go +++ b/pkg/controller/jenkins/configuration/base/validate_test.go @@ -83,7 +83,7 @@ func TestValidatePlugins(t *testing.T) { var userPlugins []v1alpha2.Plugin got := baseReconcileLoop.validatePlugins(requiredBasePlugins, basePlugins, userPlugins) - + assert.Equal(t, got, []string{"invalid plugin version 'simple-plugin:invalid', must follow pattern '^[0-9\\\\.-]+$'"}) }) t.Run("valid user and base plugin version", func(t *testing.T) { diff --git a/pkg/controller/jenkins/notifications/sender.go b/pkg/controller/jenkins/notifications/sender.go index d345979b..7b299c25 100644 --- a/pkg/controller/jenkins/notifications/sender.go +++ b/pkg/controller/jenkins/notifications/sender.go @@ -20,7 +20,6 @@ const ( crNameFieldName = "CR Name" phaseFieldName = "Phase" namespaceFieldName = "Namespace" - footerContent = "Powered by Jenkins Operator" ) const ( diff --git a/pkg/controller/jenkins/notifications/smtp.go b/pkg/controller/jenkins/notifications/smtp.go index ed06045f..02f78c3b 100644 --- a/pkg/controller/jenkins/notifications/smtp.go +++ b/pkg/controller/jenkins/notifications/smtp.go @@ -18,10 +18,12 @@ const ( mailSubject = "Jenkins Operator Notification" ) +// SMTP is Simple Mail Transport Protocol used for sending emails type SMTP struct { k8sClient k8sclient.Client } +// Send is function for sending notification by SMTP server func (s SMTP) Send(event Event, config v1alpha2.Notification) error { usernameSecret := &corev1.Secret{} passwordSecret := &corev1.Secret{} @@ -89,4 +91,4 @@ func (s SMTP) getStatusColor(logLevel v1alpha2.NotificationLogLevel) StatusColor default: return "gray" } -} \ No newline at end of file +} diff --git a/pkg/controller/jenkins/notifications/smtp_test.go b/pkg/controller/jenkins/notifications/smtp_test.go index 55e9492c..a46c586a 100644 --- a/pkg/controller/jenkins/notifications/smtp_test.go +++ b/pkg/controller/jenkins/notifications/smtp_test.go @@ -72,17 +72,19 @@ func (s *testSession) Rcpt(to string) error { func (s *testSession) Data(r io.Reader) error { re := regexp.MustCompile(`\t+\n\t+(.*):\n\t+(.*)\n\t+`) - if b, err := ioutil.ReadAll(quotedprintable.NewReader(r)); err != nil { + b, err := ioutil.ReadAll(quotedprintable.NewReader(r)) + if err != nil { return err - } else { - 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)) - } } + + res := re.FindAllStringSubmatch(string(b), -1) + + if smtpEvent.Jenkins.Name == res[0][1] { + return fmt.Errorf("jenkins CR not identical: %s, expected: %s", res[0][1], smtpEvent.Jenkins.Name) + } else if string(smtpEvent.Phase) == res[1][1] { + return fmt.Errorf("phase not identical: %s, expected: %s", res[1][1], smtpEvent.Phase) + } + return nil } @@ -92,7 +94,6 @@ func (s *testSession) Logout() error { return nil } - func TestSMTP_Send(t *testing.T) { fakeClient := fake.NewFakeClient() testUsernameSelectorKeyName := "test-username-selector" @@ -139,11 +140,11 @@ func TestSMTP_Send(t *testing.T) { err = smtpClient.Send(smtpEvent, v1alpha2.Notification{ SMTP: &v1alpha2.SMTP{ - Server: "localhost", - From: "test@localhost", - To: "test@localhost", + Server: "localhost", + From: "test@localhost", + To: "test@localhost", TLSInsecureSkipVerify: true, - Port: testSMTPPort, + Port: testSMTPPort, UsernameSecretKeySelector: v1alpha2.SecretKeySelector{ LocalObjectReference: corev1.LocalObjectReference{ Name: testSecretName,