Improve tests and SMTP notification provider
This commit is contained in:
parent
4f5391d708
commit
a5f4c78346
|
|
@ -73,7 +73,7 @@ type Notification struct {
|
||||||
Slack *Slack `json:"slack,omitempty"`
|
Slack *Slack `json:"slack,omitempty"`
|
||||||
Teams *MicrosoftTeams `json:"teams,omitempty"`
|
Teams *MicrosoftTeams `json:"teams,omitempty"`
|
||||||
Mailgun *Mailgun `json:"mailgun,omitempty"`
|
Mailgun *Mailgun `json:"mailgun,omitempty"`
|
||||||
SMTP *SMTP `json:"smtp,omitempty"`
|
SMTP *SMTP `json:"smtp,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slack is handler for Slack notification channel
|
// Slack is handler for Slack notification channel
|
||||||
|
|
@ -82,14 +82,15 @@ type Slack struct {
|
||||||
WebHookURLSecretKeySelector SecretKeySelector `json:"webHookURLSecretKeySelector"`
|
WebHookURLSecretKeySelector SecretKeySelector `json:"webHookURLSecretKeySelector"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SMTP is handler for sending emails via this protocol
|
||||||
type SMTP struct {
|
type SMTP struct {
|
||||||
UsernameSecretKeySelector SecretKeySelector `json:"usernameSecretKeySelector"`
|
UsernameSecretKeySelector SecretKeySelector `json:"usernameSecretKeySelector"`
|
||||||
PasswordSecretKeySelector SecretKeySelector `json:"passwordSecretKeySelector"`
|
PasswordSecretKeySelector SecretKeySelector `json:"passwordSecretKeySelector"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
Server string `json:"server"`
|
Server string `json:"server"`
|
||||||
TLSInsecureSkipVerify bool `json:"tlsInsecureSkipVerify,omitempty"`
|
TLSInsecureSkipVerify bool `json:"tlsInsecureSkipVerify,omitempty"`
|
||||||
From string `json:"from"`
|
From string `json:"from"`
|
||||||
To string `json:"to"`
|
To string `json:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MicrosoftTeams is handler for Microsoft MicrosoftTeams notification channel
|
// MicrosoftTeams is handler for Microsoft MicrosoftTeams notification channel
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ const (
|
||||||
crNameFieldName = "CR Name"
|
crNameFieldName = "CR Name"
|
||||||
phaseFieldName = "Phase"
|
phaseFieldName = "Phase"
|
||||||
namespaceFieldName = "Namespace"
|
namespaceFieldName = "Namespace"
|
||||||
footerContent = "Powered by Jenkins Operator"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,12 @@ const (
|
||||||
mailSubject = "Jenkins Operator Notification"
|
mailSubject = "Jenkins Operator Notification"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SMTP is Simple Mail Transport Protocol used for sending emails
|
||||||
type SMTP struct {
|
type SMTP struct {
|
||||||
k8sClient k8sclient.Client
|
k8sClient k8sclient.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send is function for sending notification by SMTP server
|
||||||
func (s SMTP) Send(event Event, config v1alpha2.Notification) error {
|
func (s SMTP) Send(event Event, config v1alpha2.Notification) error {
|
||||||
usernameSecret := &corev1.Secret{}
|
usernameSecret := &corev1.Secret{}
|
||||||
passwordSecret := &corev1.Secret{}
|
passwordSecret := &corev1.Secret{}
|
||||||
|
|
|
||||||
|
|
@ -72,17 +72,19 @@ func (s *testSession) Rcpt(to string) error {
|
||||||
func (s *testSession) Data(r io.Reader) error {
|
func (s *testSession) Data(r io.Reader) error {
|
||||||
re := regexp.MustCompile(`\t+<tr>\n\t+<td><b>(.*):</b></td>\n\t+<td>(.*)</td>\n\t+</tr>`)
|
re := regexp.MustCompile(`\t+<tr>\n\t+<td><b>(.*):</b></td>\n\t+<td>(.*)</td>\n\t+</tr>`)
|
||||||
|
|
||||||
if b, err := ioutil.ReadAll(quotedprintable.NewReader(r)); err != nil {
|
b, err := ioutil.ReadAll(quotedprintable.NewReader(r))
|
||||||
|
if err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -92,7 +94,6 @@ func (s *testSession) Logout() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestSMTP_Send(t *testing.T) {
|
func TestSMTP_Send(t *testing.T) {
|
||||||
fakeClient := fake.NewFakeClient()
|
fakeClient := fake.NewFakeClient()
|
||||||
testUsernameSelectorKeyName := "test-username-selector"
|
testUsernameSelectorKeyName := "test-username-selector"
|
||||||
|
|
@ -139,11 +140,11 @@ func TestSMTP_Send(t *testing.T) {
|
||||||
|
|
||||||
err = smtpClient.Send(smtpEvent, v1alpha2.Notification{
|
err = smtpClient.Send(smtpEvent, v1alpha2.Notification{
|
||||||
SMTP: &v1alpha2.SMTP{
|
SMTP: &v1alpha2.SMTP{
|
||||||
Server: "localhost",
|
Server: "localhost",
|
||||||
From: "test@localhost",
|
From: "test@localhost",
|
||||||
To: "test@localhost",
|
To: "test@localhost",
|
||||||
TLSInsecureSkipVerify: true,
|
TLSInsecureSkipVerify: true,
|
||||||
Port: testSMTPPort,
|
Port: testSMTPPort,
|
||||||
UsernameSecretKeySelector: v1alpha2.SecretKeySelector{
|
UsernameSecretKeySelector: v1alpha2.SecretKeySelector{
|
||||||
LocalObjectReference: corev1.LocalObjectReference{
|
LocalObjectReference: corev1.LocalObjectReference{
|
||||||
Name: testSecretName,
|
Name: testSecretName,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue