Improved notification tests, refactor ConfigurationType to Phase

This commit is contained in:
Jakub Al-Khalili
2019-10-03 15:53:39 +02:00
parent ca3508ef49
commit 4e8b0fa72e
13 changed files with 222 additions and 207 deletions
@@ -28,7 +28,7 @@ const content = `
<td>%s</td>
</tr>
<tr>
<td><b>Configuration type:</b></td>
<td><b>Phase:</b></td>
<td>%s</td>
</tr>
</table>
@@ -83,7 +83,7 @@ func (m MailGun) Send(event Event, config v1alpha2.Notification) error {
statusMessage = event.Message
}
htmlMessage := fmt.Sprintf(content, m.getStatusColor(event.LogLevel), notificationTitle(event), statusMessage, event.Jenkins.Name, event.ConfigurationType)
htmlMessage := fmt.Sprintf(content, m.getStatusColor(event.LogLevel), notificationTitle(event), statusMessage, event.Jenkins.Name, event.Phase)
msg := mg.NewMessage(fmt.Sprintf("Jenkins Operator Notifier <%s>", config.Mailgun.From), notificationTitle(event), "", config.Mailgun.Recipient)
msg.SetHtml(htmlMessage)
@@ -102,10 +102,10 @@ func (t Teams) Send(event Event, config v1alpha2.Notification) error {
tm.Summary = message
}
if event.ConfigurationType != ConfigurationTypeUnknown {
if event.Phase != PhaseUnknown {
tm.Sections[0].Facts = append(tm.Sections[0].Facts, TeamsFact{
Name: configurationTypeFieldName,
Value: string(event.ConfigurationType),
Name: phaseFieldName,
Value: string(event.Phase),
})
}
@@ -27,10 +27,10 @@ func TestTeams_Send(t *testing.T) {
Namespace: testNamespace,
},
},
ConfigurationType: testConfigurationType,
Message: testMessage,
MessagesVerbose: testMessageVerbose,
LogLevel: testLoggingLevel,
Phase: testPhase,
Message: testMessage,
MessagesVerbose: testMessageVerbose,
LogLevel: testLoggingLevel,
}
teams := Teams{k8sClient: fakeClient}
@@ -51,8 +51,8 @@ func TestTeams_Send(t *testing.T) {
for _, fact := range mainSection.Facts {
switch fact.Name {
case configurationTypeFieldName:
assert.Equal(t, fact.Value, string(event.ConfigurationType))
case phaseFieldName:
assert.Equal(t, fact.Value, string(event.Phase))
case crNameFieldName:
assert.Equal(t, fact.Value, event.Jenkins.Name)
case messageFieldName:
+29 -29
View File
@@ -2,50 +2,50 @@ package notifications
import (
"fmt"
"net/http"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/event"
"github.com/jenkinsci/kubernetes-operator/pkg/log"
"net/http"
"github.com/pkg/errors"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
infoTitleText = "Jenkins Operator reconciliation info"
warnTitleText = "Jenkins Operator reconciliation warning"
messageFieldName = "Message"
loggingLevelFieldName = "Logging Level"
crNameFieldName = "CR Name"
configurationTypeFieldName = "Phase"
namespaceFieldName = "Namespace"
footerContent = "Powered by Jenkins Operator"
infoTitleText = "Jenkins Operator reconciliation info"
warnTitleText = "Jenkins Operator reconciliation warning"
messageFieldName = "Message"
loggingLevelFieldName = "Logging Level"
crNameFieldName = "CR Name"
phaseFieldName = "Phase"
namespaceFieldName = "Namespace"
footerContent = "Powered by Jenkins Operator"
)
const (
// ConfigurationTypeBase is core configuration of Jenkins provided by the Operator
ConfigurationTypeBase ConfigurationType = "base"
// PhaseBase is core configuration of Jenkins provided by the Operator
PhaseBase Phase = "base"
// ConfigurationTypeUser is user-defined configuration of Jenkins
ConfigurationTypeUser ConfigurationType = "user"
// PhaseUser is user-defined configuration of Jenkins
PhaseUser Phase = "user"
// ConfigurationTypeUnknown is untraceable type of configuration
ConfigurationTypeUnknown ConfigurationType = "unknown"
// PhaseUnknown is untraceable type of configuration
PhaseUnknown Phase = "unknown"
)
var (
testConfigurationType = ConfigurationTypeUser
testCrName = "test-cr"
testNamespace = "default"
testMessage = "test-message"
testMessageVerbose = []string{"detail-test-message"}
testLoggingLevel = v1alpha2.NotificationLogLevelWarning
testPhase = PhaseUser
testCrName = "test-cr"
testNamespace = "default"
testMessage = "test-message"
testMessageVerbose = []string{"detail-test-message"}
testLoggingLevel = v1alpha2.NotificationLogLevelWarning
client = http.Client{}
)
// ConfigurationType defines the type of configuration
type ConfigurationType string
// Phase defines the type of configuration
type Phase string
// StatusColor is useful for better UX
type StatusColor string
@@ -55,11 +55,11 @@ type LoggingLevel string
// Event contains event details which will be sent as a notification
type Event struct {
Jenkins v1alpha2.Jenkins
ConfigurationType ConfigurationType
LogLevel v1alpha2.NotificationLogLevel
Message string
MessagesVerbose []string
Jenkins v1alpha2.Jenkins
Phase Phase
LogLevel v1alpha2.NotificationLogLevel
Message string
MessagesVerbose []string
}
type service interface {
@@ -104,10 +104,10 @@ func (s Slack) Send(event Event, config v1alpha2.Notification) error {
mainAttachment.Fields[0].Value = message
}
if event.ConfigurationType != ConfigurationTypeUnknown {
if event.Phase != PhaseUnknown {
mainAttachment.Fields = append(mainAttachment.Fields, SlackField{
Title: configurationTypeFieldName,
Value: string(event.ConfigurationType),
Title: phaseFieldName,
Value: string(event.Phase),
Short: true,
})
}
@@ -27,10 +27,10 @@ func TestSlack_Send(t *testing.T) {
Namespace: testNamespace,
},
},
ConfigurationType: testConfigurationType,
Message: testMessage,
MessagesVerbose: testMessageVerbose,
LogLevel: testLoggingLevel,
Phase: testPhase,
Message: testMessage,
MessagesVerbose: testMessageVerbose,
LogLevel: testLoggingLevel,
}
slack := Slack{k8sClient: fakeClient}
@@ -48,8 +48,8 @@ func TestSlack_Send(t *testing.T) {
assert.Equal(t, mainAttachment.Title, notificationTitle(event))
for _, field := range mainAttachment.Fields {
switch field.Title {
case configurationTypeFieldName:
assert.Equal(t, field.Value, string(event.ConfigurationType))
case phaseFieldName:
assert.Equal(t, field.Value, string(event.Phase))
case crNameFieldName:
assert.Equal(t, field.Value, event.Jenkins.Name)
case messageFieldName: