forked from keptn-sandbox/datadog-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler_test.go
79 lines (64 loc) · 2.31 KB
/
eventhandler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"github.com/keptn/go-utils/pkg/lib/v0_2_0/fake"
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) (*keptnv2.Keptn, *cloudevents.Event, error) {
// load sample event
eventFile, err := ioutil.ReadFile(eventFileName)
if err != nil {
return nil, nil, fmt.Errorf("Cant load %s: %s", eventFileName, err.Error())
}
incomingEvent := &cloudevents.Event{}
err = json.Unmarshal(eventFile, incomingEvent)
if err != nil {
return nil, nil, fmt.Errorf("Error parsing: %s", err.Error())
}
// Add a Fake EventSender to KeptnOptions
var keptnOptions = keptn.KeptnOpts{
EventSender: &fake.EventSender{},
}
keptnOptions.UseLocalFileSystem = true
ddKeptn, err := keptnv2.NewKeptn(incomingEvent, keptnOptions)
return ddKeptn, incomingEvent, err
}
// Tests the HandleGetSliTriggeredEvent Handler
// TODO: Add your test-code
func TestHandleGetSliTriggered(t *testing.T) {
ddKeptn, incomingEvent, err := initializeTestObjects("test/events/get-sli.triggered.json")
if err != nil {
t.Error(err)
return
}
specificEvent := &keptnv2.GetSLITriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleGetSliTriggeredEvent(ddKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
gotEvents := len(ddKeptn.EventSender.(*fake.EventSender).SentEvents)
// Verify that HandleGetSliTriggeredEvent has sent 2 cloudevents
if gotEvents != 2 {
t.Errorf("Expected two events to be sent, but got %v", gotEvents)
}
// Verify that the first CE sent is a .started event
if keptnv2.GetStartedEventType(keptnv2.GetSLITaskName) != ddKeptn.EventSender.(*fake.EventSender).SentEvents[0].Type() {
t.Errorf("Expected a get-sli.started event type")
}
// Verify that the second CE sent is a .finished event
if keptnv2.GetFinishedEventType(keptnv2.GetSLITaskName) != ddKeptn.EventSender.(*fake.EventSender).SentEvents[1].Type() {
t.Errorf("Expected a get-sli.finished event type")
}
}