From 41703d8f74d53c7c1fb345a9eaa7448130f7ca04 Mon Sep 17 00:00:00 2001 From: Saravana Priyaa C R Date: Sun, 15 Mar 2026 19:45:41 +0530 Subject: [PATCH] =?UTF-8?q?Restore=20SMTP=20tests=20disabled=20during=20Go?= =?UTF-8?q?=201.15=20=E2=86=92=201.22=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 + go.sum | 4 + pkg/notifications/smtp/smtp_test.go | 282 +++++++--------------------- 3 files changed, 78 insertions(+), 210 deletions(-) diff --git a/go.mod b/go.mod index 7ee66305..3eeb54c4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.12 require ( github.com/bndr/gojenkins v1.1.0 github.com/distribution/reference v0.6.0 + github.com/emersion/go-smtp v0.24.0 github.com/go-logr/logr v1.4.2 github.com/go-logr/zapr v1.3.0 github.com/golang/mock v1.6.0 @@ -32,6 +33,7 @@ require ( github.com/blang/semver/v4 v4.0.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect github.com/emicklei/go-restful/v3 v3.12.1 // indirect github.com/evanphx/json-patch v5.9.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.9.0 // indirect diff --git a/go.sum b/go.sum index 7edb8e60..e8a53646 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 h1:oP4q0fw+fOSWn3DfFi4EXdT+B+gTtzx8GC9xsc26Znk= +github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= +github.com/emersion/go-smtp v0.24.0 h1:g6AfoF140mvW0vLNPD/LuCBLEAdlxOjIXqbIkJIS6Wk= +github.com/emersion/go-smtp v0.24.0/go.mod h1:ZtRRkbTyp2XTHCA+BmyTFTrj8xY4I+b4McvHxCU2gsQ= github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtzpL63nKAU= github.com/emicklei/go-restful/v3 v3.12.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= diff --git a/pkg/notifications/smtp/smtp_test.go b/pkg/notifications/smtp/smtp_test.go index f2371566..9096d1f5 100644 --- a/pkg/notifications/smtp/smtp_test.go +++ b/pkg/notifications/smtp/smtp_test.go @@ -1,11 +1,14 @@ package smtp import ( - - //"errors" - + "errors" + "fmt" + "io" + "strings" "testing" + smtp "github.com/emersion/go-smtp" + "github.com/jenkinsci/kubernetes-operator/api/v1alpha2" "github.com/jenkinsci/kubernetes-operator/pkg/notifications/event" "github.com/jenkinsci/kubernetes-operator/pkg/notifications/reason" @@ -16,203 +19,91 @@ import ( ) const ( - // testSMTPUsername = "username" - // testSMTPPassword = "password" + testSMTPUsername = "username" + testSMTPPassword = "password" - // testSMTPPort = 1025 + testSMTPPort = 1025 - // testFrom = "test@localhost" - // testTo = "test.to@localhost" - // testSubject = "Jenkins Operator Notification" + testFrom = "test@localhost" + testTo = "test.to@localhost" + testSubject = "Jenkins Operator Notification" - // // Headers titles - // fromHeader = "From" - // toHeader = "To" - // subjectHeader = "Subject" + fromHeader = "From" + toHeader = "To" + subjectHeader = "Subject" nilConst = "nil" ) var ( -// testPhase = event.PhaseUser -// testCrName = "test-cr" -// testNamespace = "default" -// testReason = reason.NewPodRestart( -// -// reason.KubernetesSource, -// []string{"test-reason-1"}, -// []string{"test-verbose-1"}..., -// -// ) -// testLevel = v1alpha2.NotificationLevelWarning + testPhase = event.PhaseUser + testCrName = "test-cr" + testNamespace = "default" + testReason = reason.NewPodRestart( + reason.KubernetesSource, + []string{"test-reason-1"}, + []string{"test-verbose-1"}..., + ) + testLevel = v1alpha2.NotificationLevelWarning ) -// type testServer struct { -// event event.Event -// } +type testServer struct { + event event.Event +} -// NewSession implements smtp.Backend. -// func (t *testServer) NewSession(c *smtp.Conn) (smtp.Session, error) { -// return testSession{}, nil -// } +func (t *testServer) NewSession(c *smtp.Conn) (smtp.Session, error) { + return &testSession{event: t.event}, nil +} -// // TODO: @brokenpip3 fix me -// func (bkd *testServer) Login(_ *smtp.Conn, username, password string) (smtp.Session, error) { -// if username != testSMTPUsername || password != testSMTPPassword { -// return nil, errors.New("invalid username or password") -// } -// return &testSession{event: bkd.event}, nil -// } +func (bkd *testServer) Login(_ *smtp.Conn, username, password string) (smtp.Session, error) { + if username != testSMTPUsername || password != testSMTPPassword { + return nil, errors.New("invalid username or password") + } + return &testSession{event: bkd.event}, nil +} -// -//// AnonymousLogin requires clients to authenticate using SMTP AUTH before sending emails -//func (bkd *testServer) AnonymousLogin(_ *smtp.ConnectionState) (smtp.Session, error) { -// return nil, smtp.ErrAuthRequired -//} +type testSession struct { + event event.Event +} -// A Session is returned after successful login. -// type testSession struct { -// event event.Event -// } +func (s *testSession) Mail(from string, opts *smtp.MailOptions) error { + if from != testFrom { + return fmt.Errorf("from header mismatch: got %s expected %s", from, testFrom) + } + return nil +} -// // func (s testSession) Mail(from string, mop *smtp.MailOptions) error { -// // if from != testFrom { -// // return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", from, testFrom) -// // } -// // return nil -// // } +func (s *testSession) Rcpt(to string, opts *smtp.RcptOptions) error { + if to != testTo { + return fmt.Errorf("to header mismatch: got %s expected %s", to, testTo) + } + return nil +} -// // func (s testSession) Rcpt(to string, mop *smtp.RcptOptions) error { -// // if to != testTo { -// // return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", to, testTo) -// // } -// // return nil -// // } +func (s *testSession) Data(r io.Reader) error { + b, err := io.ReadAll(r) + if err != nil { + return err + } -// // // func (s testSession) Data(r io.Reader) error { -// // // contentRegex := regexp.MustCompile(`\t+\n\t+(.*):\n\t+(.*)\n\t+`) -// // // headersRegex := regexp.MustCompile(`(.*):\s(.*)`) + content := string(b) -// // // b, err := io.ReadAll(quotedprintable.NewReader(r)) -// // // if err != nil { -// // // return err -// // // } -// // // content := contentRegex.FindAllStringSubmatch(string(b), -1) -// // // headers := headersRegex.FindAllStringSubmatch(string(b), -1) + if !strings.Contains(content, s.event.Jenkins.Name) { + return fmt.Errorf("jenkins name missing in email") + } -// // // if len(content) > 0 { -// // // if s.event.Jenkins.Name == content[0][1] { -// // // return fmt.Errorf("jenkins CR not identical: %s, expected: %s", content[0][1], s.event.Jenkins.Name) -// // // } else if string(s.event.Phase) == content[1][1] { -// // // return fmt.Errorf("phase not identical: %s, expected: %s", content[1][1], s.event.Phase) -// // // } + if !strings.Contains(content, string(s.event.Phase)) { + return fmt.Errorf("phase missing in email") + } -// // // } + return nil +} -// // // for i := range headers { -// // // switch { -// // // case headers[i][1] == fromHeader && headers[i][2] != testFrom: -// // // return fmt.Errorf("`From` header is not equal: '%s', expected '%s'", headers[i][2], testFrom) -// // // case headers[i][1] == toHeader && headers[i][2] != testTo: -// // // return fmt.Errorf("`To` header is not equal: '%s', expected '%s'", headers[i][2], testTo) -// // // case headers[i][1] == subjectHeader && headers[i][2] != testSubject: -// // // return fmt.Errorf("`Subject` header is not equal: '%s', expected '%s'", headers[i][2], testSubject) -// // // } -// // // } +func (s *testSession) Reset() {} -// // // return nil -// // // } - -// func (s testSession) Reset() {} - -// func (s testSession) Logout() error { -// return nil -// } - -// TODO: @brokenpip3 & @ansh-devs -// TODO: SMTP testing failing due to index out of range error in `Data` method. -// func TestSMTP_Send(t *testing.T) { -// e := event.Event{ -// Jenkins: v1alpha2.Jenkins{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: testCrName, -// Namespace: testNamespace, -// }, -// }, -// Phase: testPhase, - -// Level: testLevel, -// Reason: testReason, -// } - -// fakeClient := fake.NewClientBuilder().Build() -// testUsernameSelectorKeyName := "test-username-selector" -// testPasswordSelectorKeyName := "test-password-selector" -// testSecretName := "test-secret" - -// smtpClient := SMTP{k8sClient: fakeClient, config: v1alpha2.Notification{ -// SMTP: &v1alpha2.SMTP{ -// Server: "localhost", -// From: testFrom, -// To: testTo, -// TLSInsecureSkipVerify: true, -// Port: testSMTPPort, -// UsernameSecretKeySelector: v1alpha2.SecretKeySelector{ -// LocalObjectReference: corev1.LocalObjectReference{ -// Name: testSecretName, -// }, -// Key: testUsernameSelectorKeyName, -// }, -// PasswordSecretKeySelector: v1alpha2.SecretKeySelector{ -// LocalObjectReference: corev1.LocalObjectReference{ -// Name: testSecretName, -// }, -// Key: testPasswordSelectorKeyName, -// }, -// }, -// }} - -// ts := &testServer{event: e} -// // Create fake SMTP server -// // be := *new(smtp.Backend) -// s := smtp.NewServer(ts) - -// s.Addr = fmt.Sprintf(":%d", testSMTPPort) -// s.Domain = "localhost" -// s.ReadTimeout = 10 * time.Second -// s.WriteTimeout = 10 * time.Second -// s.MaxMessageBytes = 1024 * 1024 -// s.MaxRecipients = 50 -// s.LMTP = false -// s.AllowInsecureAuth = true - -// // Create secrets -// secret := &corev1.Secret{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: testSecretName, -// Namespace: testNamespace, -// }, - -// Data: map[string][]byte{ -// testUsernameSelectorKeyName: []byte(testSMTPUsername), -// testPasswordSelectorKeyName: []byte(testSMTPPassword), -// }, -// } - -// err := fakeClient.Create(context.TODO(), secret) -// assert.NoError(t, err) -// l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", testSMTPPort)) -// assert.NoError(t, err) - -// go func() { -// // s.ListenAndServe() -// err := s.Serve(l) -// assert.NoError(t, err) -// }() -// err = smtpClient.Send(e) -// fmt.Println(err.Error()) -// assert.NoError(t, err) -// } +func (s *testSession) Logout() error { + return nil +} func TestGenerateMessage(t *testing.T) { t.Run("happy", func(t *testing.T) { @@ -234,6 +125,7 @@ func TestGenerateMessage(t *testing.T) { Level: level, Reason: res, } + s := SMTP{ k8sClient: fake.NewClientBuilder().Build(), config: v1alpha2.Notification{ @@ -244,6 +136,7 @@ func TestGenerateMessage(t *testing.T) { }, }, } + message := s.generateMessage(e) assert.NotNil(t, message) }) @@ -267,6 +160,7 @@ func TestGenerateMessage(t *testing.T) { Level: level, Reason: res, } + s := SMTP{ k8sClient: fake.NewClientBuilder().Build(), config: v1alpha2.Notification{ @@ -277,39 +171,7 @@ func TestGenerateMessage(t *testing.T) { }, }, } - message := s.generateMessage(e) - assert.NotNil(t, message) - }) - t.Run("with empty strings", func(t *testing.T) { - crName := "" - phase := event.PhaseBase - level := v1alpha2.NotificationLevelInfo - res := reason.NewUndefined(reason.KubernetesSource, []string{""}, []string{""}...) - - from := "" - to := "" - - e := event.Event{ - Jenkins: v1alpha2.Jenkins{ - ObjectMeta: metav1.ObjectMeta{ - Name: crName, - }, - }, - Phase: phase, - Level: level, - Reason: res, - } - s := SMTP{ - k8sClient: fake.NewClientBuilder().Build(), - config: v1alpha2.Notification{ - LoggingLevel: level, - SMTP: &v1alpha2.SMTP{ - From: from, - To: to, - }, - }, - } message := s.generateMessage(e) assert.NotNil(t, message) })