Skip to content

Commit

Permalink
#20 Fixed tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kreuzberger <[email protected]>
  • Loading branch information
christian-kreuzberger-dtx committed Feb 9, 2021
1 parent ef804f6 commit 9fd0bfe
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 661 deletions.
163 changes: 33 additions & 130 deletions eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"io/ioutil"
"testing"

keptnlib "github.com/keptn/go-utils/pkg/lib"
keptn "github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"

cloudevents "github.com/cloudevents/sdk-go/v2" // make sure to use v2 cloudevents here
)

/**
* loads a cloud event from the passed test json file and initializes a keptn object with it
*/
func initializeTestObjects(eventFileName string) (*keptnlib.Keptn, *cloudevents.Event, error) {
func initializeTestObjects(eventFileName string) (*keptnv2.Keptn, *cloudevents.Event, error) {
// load sample event
eventFile, err := ioutil.ReadFile(eventFileName)
if err != nil {
Expand All @@ -30,208 +30,111 @@ func initializeTestObjects(eventFileName string) (*keptnlib.Keptn, *cloudevents.

var keptnOptions = keptn.KeptnOpts{}
keptnOptions.UseLocalFileSystem = true
myKeptn, err := keptnlib.NewKeptn(incomingEvent, keptnOptions)
myKeptn, err := keptnv2.NewKeptn(incomingEvent, keptnOptions)

return myKeptn, incomingEvent, err
}

// Handles ConfigureMonitoringEventType = "sh.keptn.event.monitoring.configure"
func TestHandleConfigureMonitoringEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/configure-monitoring.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.ConfigureMonitoringEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleConfigureMonitoringEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles ConfigurationChangeEventType = "sh.keptn.event.configuration.change"
// TODO: add in your handler code
//
func TestHandleConfigurationChangeEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/configuration-change.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.ConfigurationChangeEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleConfigurationChangeEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles DeploymentFinishedEventType = "sh.keptn.events.deployment-finished"
// TODO: add in your handler code
//
func TestHandleDeploymentFinishedEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/deployment-finished.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.DeploymentFinishedEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleDeploymentFinishedEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles TestsFinishedEventType = "sh.keptn.events.tests-finished"
// TODO: add in your handler code
//
func TestHandleTestsFinishedEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/tests-finished.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.TestsFinishedEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleTestsFinishedEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles EvaluationDoneEventType = "sh.keptn.events.evaluation-done"
// TODO: add in your handler code
//
func TestHandleStartEvaluationEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/start-evaluation.json")
// Tests HandleActionTriggeredEvent
// TODO: Add your test-code
func TestHandleActionTriggeredEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/action.triggered.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.StartEvaluationEventData{}
specificEvent := &keptnv2.ActionTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleStartEvaluationEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleActionTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles DeploymentFinishedEventType = "sh.keptn.events.deployment-finished"
// TODO: add in your handler code
//
func TestHandleEvaluationDoneEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/evaluation-done.json")
// Tests HandleDeploymentTriggeredEvent
// TODO: Add your test-code
func TestHandleDeploymentTriggeredEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/evaluation.triggered.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.EvaluationDoneEventData{}
specificEvent := &keptnv2.DeploymentTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleEvaluationDoneEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleDeploymentTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

// Tests the InternalGetSLIEvent Handler
func TestHandleInternalGetSLIEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/get-sli.json")
// Tests HandleEvaluationTriggeredEvent
// TODO: Add your test-code
func TestHandleEvaluationTriggeredEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/evaluation.triggered.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.InternalGetSLIEventData{}
specificEvent := &keptnv2.EvaluationTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleInternalGetSLIEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleEvaluationTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles ProblemOpenEventType = "sh.keptn.event.problem.open"
// Handles ProblemEventType = "sh.keptn.events.problem"
// TODO: add in your handler code
//
func TestHandleProblemEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/problem.json")
// Tests the HandleGetSliTriggeredEvent Handler
// TODO: Add your test-code
func TestHandleGetSliTriggered(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/get-sli.triggered.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.ProblemEventData{}
specificEvent := &keptnv2.GetSLITriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleProblemEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleGetSliTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}

//
// Handles ActionTriggeredEventType = "sh.keptn.event.action.triggered"
// TODO: add in your handler code
//
func TestHandleActionTriggeredEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/action-triggered.json")
// Tests the HandleReleaseTriggeredEvent Handler
// TODO: Add your test-code
func TestHandleReleaseTriggeredEvent(t *testing.T) {
myKeptn, incomingEvent, err := initializeTestObjects("test-events/release.triggered.json")
if err != nil {
t.Error(err)
return
}

specificEvent := &keptnlib.ActionTriggeredEventData{}
specificEvent := &keptnv2.ReleaseTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleActionTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleReleaseTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
Expand Down
42 changes: 36 additions & 6 deletions eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func GenericLogKeptnCloudEventHandler(myKeptn *keptnv2.Keptn, incomingEvent clou
return nil
}

// OldHandleConfigureMonitoringTriggeredEvent handles old configure-monitoring events
// OldHandleConfigureMonitoringEvent handles old configure-monitoring events
// TODO: add in your handler code
func OldHandleConfigureMonitoringTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptn.ConfigureMonitoringEventData) error {
func OldHandleConfigureMonitoringEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptn.ConfigureMonitoringEventData) error {
log.Printf("Handling old configure-monitoring Event: %s", incomingEvent.Context.GetID())

return nil
Expand Down Expand Up @@ -55,6 +55,14 @@ func HandleTestTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.
return nil
}

// HandleApprovalTriggeredEvent handles approval.triggered events
// TODO: add in your handler code
func HandleApprovalTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.ApprovalTriggeredEventData) error {
log.Printf("Handling approval.triggered Event: %s", incomingEvent.Context.GetID())

return nil
}

// HandleEvaluationTriggeredEvent handles evaluation.triggered events
// TODO: add in your handler code
func HandleEvaluationTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.EvaluationTriggeredEventData) error {
Expand All @@ -63,7 +71,24 @@ func HandleEvaluationTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloude
return nil
}

// HandleReleaseTriggeredEvent handles release.triggered events
// TODO: add in your handler code
func HandleReleaseTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.ReleaseTriggeredEventData) error {
log.Printf("Handling release.triggered Event: %s", incomingEvent.Context.GetID())

return nil
}

// HandleRemediationTriggeredEvent handles remediation.triggered events
// TODO: add in your handler code
func HandleRemediationTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.RemediationTriggeredEventData) error {
log.Printf("Handling remediation.triggered Event: %s", incomingEvent.Context.GetID())

return nil
}

// sendGetSliFinishedCloudEvent is a helper function to send a get-sli.finished event
// ToDo: This will need to be refactored once https://github.com/keptn/keptn/issues/2913 is ready
func sendGetSliFinishedCloudEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.GetSLITriggeredEventData,
status keptnv2.StatusType, result keptnv2.ResultType, message string) error {
log.Printf("Sending getSli Finished Cloud Event with status=%s and result=%s back to Keptn (%s)", status, result, message)
Expand All @@ -82,9 +107,10 @@ func sendGetSliFinishedCloudEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudeve
return SendEvent(myKeptn, finishedEvent, incomingEvent)
}

// HandleGetSliEvent handles get-sli events
// HandleGetSliTriggeredEvent handles get-sli.triggered events if SLIProvider == keptn-service-template-go
// This function acts as an example showing how to handle get-sli events by sending .started and .finished events
// TODO: adapt handler code to your needs
func HandleGetSliEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.GetSLITriggeredEventData) error {
func HandleGetSliTriggeredEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event, data *keptnv2.GetSLITriggeredEventData) error {
log.Printf("Handling get-sli.triggered Event: %s", incomingEvent.Context.GetID())

// Step 1 - Do we need to do something?
Expand All @@ -96,6 +122,7 @@ func HandleGetSliEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event,

// Step 2 - Send out a get-sli.started CloudEvent
// The get-sli.started cloud-event is new since Keptn 0.8.0 and is required for the task to start
// ToDo: This will need to be refactored once https://github.com/keptn/keptn/issues/2913 is ready
getSliStartedData := keptnv2.GetSLIStartedEventData{}

getSliStartedData.EventData = data.EventData
Expand Down Expand Up @@ -126,6 +153,8 @@ func HandleGetSliEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event,
sliFile := "keptn-service-template-go/sli.yaml"
sliConfigFileContent, err := myKeptn.GetKeptnResource(sliFile)

// FYI you do not need to "fail" if sli.yaml is missing, you can also assume smart defaults like we do
// in keptn-contrib/dynatrace-service and keptn-contrib/prometheus-service
if err != nil {
// failed to fetch sli config file
errMsg := fmt.Sprintf("Failed to fetch SLI file %s from config repo: %s", sliFile, err.Error())
Expand Down Expand Up @@ -154,6 +183,7 @@ func HandleGetSliEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event,
labels["Link to Data Source"] = "https://mydatasource/myquery?testRun=" + testRunID

// Step 8 - Build get-sli.finished event data
// ToDo: This will need to be refactored once https://github.com/keptn/keptn/issues/2913 is ready
getSliFinishedEventData := keptnv2.GetSLIFinishedEventData{
EventData: keptnv2.EventData{
Project: data.Project,
Expand All @@ -174,12 +204,12 @@ func HandleGetSliEvent(myKeptn *keptnv2.Keptn, incomingEvent cloudevents.Event,
},
}

// Step 8 - Convert To CloudEvent
// Step 9 - Convert To CloudEvent
finishedEvent := cloudevents.NewEvent()
finishedEvent.SetType(keptnv2.GetFinishedEventType(keptnv2.GetSLITaskName))
finishedEvent.SetData(cloudevents.ApplicationJSON, getSliFinishedEventData)

// Step 9 - send action.finished CloudEvent back to Keptn
// Step 10 - send action.finished CloudEvent back to Keptn
return SendEvent(myKeptn, finishedEvent, incomingEvent)
}

Expand Down
Loading

0 comments on commit 9fd0bfe

Please sign in to comment.