Skip to content

Commit

Permalink
Merge pull request #24 from keptn-sandbox/feature/20/cloud-events-080
Browse files Browse the repository at this point in the history
Make service-template compatible with Keptn 0.8.0-alpha
  • Loading branch information
christian-kreuzberger-dtx authored Feb 19, 2021
2 parents f18b66a + 2f08aa5 commit 54086a1
Show file tree
Hide file tree
Showing 28 changed files with 949 additions and 988 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ When writing code, it is recommended to follow the coding style suggested by the
If you don't care about the details, your first entrypoint is [eventhandlers.go](eventhandlers.go). Within this file
you can add implementation for pre-defined Keptn Cloud events.

To better understand Keptn CloudEvents, please look at the [Keptn Spec](https://github.com/keptn/spec).
To better understand all variants of Keptn CloudEvents, please look at the [Keptn Spec](https://github.com/keptn/spec).

If you want to get more insights, please look into [main.go](main.go), [deploy/service.yaml](deploy/service.yaml),
If you want to get more insights into processing those CloudEvents or even defining your own CloudEvents in code, please
look into [main.go](main.go) (specifically `processKeptnCloudEvent`), [deploy/service.yaml](deploy/service.yaml),
consult the [Keptn docs](https://keptn.sh/docs/) as well as existing [Keptn Core](https://github.com/keptn/keptn) and
[Keptn Contrib](https://github.com/keptn-contrib/) services.

Expand Down
10 changes: 4 additions & 6 deletions deploy/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ spec:
ports:
- containerPort: 8080
env:
- name: EVENTBROKER
value: 'http://event-broker/keptn'
- name: CONFIGURATION_SERVICE
value: 'http://configuration-service:8080'
- name: distributor
image: keptn/distributor:0.7.1
image: keptn/distributor:0.8.0-alpha
livenessProbe:
httpGet:
path: /health
Expand All @@ -38,11 +36,11 @@ spec:
- containerPort: 8080
resources:
requests:
memory: "32Mi"
cpu: "50m"
memory: "16Mi"
cpu: "25m"
limits:
memory: "128Mi"
cpu: "500m"
cpu: "250m"
env:
- name: PUBSUB_URL
value: 'nats://keptn-nats-cluster'
Expand Down
177 changes: 59 additions & 118 deletions eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package main
import (
"encoding/json"
"fmt"
"github.com/keptn/go-utils/pkg/lib/v0_2_0/fake"
"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"

"github.com/cloudevents/sdk-go/pkg/cloudevents"
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 @@ -28,210 +29,150 @@ func initializeTestObjects(eventFileName string) (*keptnlib.Keptn, *cloudevents.
return nil, nil, fmt.Errorf("Error parsing: %s", err.Error())
}

var keptnOptions = keptn.KeptnOpts{}
// Add a Fake EventSender to KeptnOptions
var keptnOptions = keptn.KeptnOpts{
EventSender: &fake.EventSender{},
}
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")
// 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.ConfigureMonitoringEventData{}
specificEvent := &keptnv2.ActionTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleConfigureMonitoringEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleActionTriggeredEvent(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())
}
}
gotEvents := len(myKeptn.EventSender.(*fake.EventSender).SentEvents)

//
// 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
// Verify that HandleGetSliTriggeredEvent has sent 2 cloudevents
if gotEvents != 2 {
t.Errorf("Expected two events to be sent, but got %v", gotEvents)
}

specificEvent := &keptnlib.DeploymentFinishedEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
// Verify that the first CE sent is a .started event
if keptnv2.GetStartedEventType(keptnv2.ActionTaskName) != myKeptn.EventSender.(*fake.EventSender).SentEvents[0].Type() {
t.Errorf("Expected a action.started event type")
}

err = HandleDeploymentFinishedEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
// Verify that the second CE sent is a .finished event
if keptnv2.GetFinishedEventType(keptnv2.ActionTaskName) != myKeptn.EventSender.(*fake.EventSender).SentEvents[1].Type() {
t.Errorf("Expected a action.finished event type")
}
}

//
// 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")
// 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.TestsFinishedEventData{}
specificEvent := &keptnv2.DeploymentTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleTestsFinishedEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleDeploymentTriggeredEvent(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 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.StartEvaluationEventData{}
specificEvent := &keptnv2.EvaluationTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleStartEvaluationEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleEvaluationTriggeredEvent(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 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.EvaluationDoneEventData{}
specificEvent := &keptnv2.GetSLITriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}

err = HandleEvaluationDoneEvent(myKeptn, *incomingEvent, specificEvent)
err = HandleGetSliTriggeredEvent(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")
if err != nil {
t.Error(err)
return
}
gotEvents := len(myKeptn.EventSender.(*fake.EventSender).SentEvents)

specificEvent := &keptnlib.InternalGetSLIEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
// Verify that HandleGetSliTriggeredEvent has sent 2 cloudevents
if gotEvents != 2 {
t.Errorf("Expected two events to be sent, but got %v", gotEvents)
}

err = HandleInternalGetSLIEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
// Verify that the first CE sent is a .started event
if keptnv2.GetStartedEventType(keptnv2.GetSLITaskName) != myKeptn.EventSender.(*fake.EventSender).SentEvents[0].Type() {
t.Errorf("Expected a get-sli.started event type")
}
}

//
// 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")
if err != nil {
t.Error(err)
return
}

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

err = HandleProblemEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
// Verify that the second CE sent is a .finished event
if keptnv2.GetFinishedEventType(keptnv2.GetSLITaskName) != myKeptn.EventSender.(*fake.EventSender).SentEvents[1].Type() {
t.Errorf("Expected a get-sli.finished event type")
}
}

//
// 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
Loading

0 comments on commit 54086a1

Please sign in to comment.