generated from keptn-sandbox/keptn-service-template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler_test.go
179 lines (149 loc) · 5.19 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"encoding/json"
"fmt"
"github.com/keptn/go-utils/pkg/lib/v0_2_0/fake"
"io/ioutil"
"testing"
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
myKeptn, err := keptnv2.NewKeptn(incomingEvent, keptnOptions)
return myKeptn, incomingEvent, err
}
// 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 := &keptnv2.ActionTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleActionTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
gotEvents := len(myKeptn.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.ActionTaskName) != myKeptn.EventSender.(*fake.EventSender).SentEvents[0].Type() {
t.Errorf("Expected a action.started event type")
}
// 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")
}
}
// 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 := &keptnv2.DeploymentTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleDeploymentTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}
// 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 := &keptnv2.EvaluationTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleEvaluationTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}
// 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 := &keptnv2.GetSLITriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleGetSliTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
gotEvents := len(myKeptn.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) != myKeptn.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) != myKeptn.EventSender.(*fake.EventSender).SentEvents[1].Type() {
t.Errorf("Expected a get-sli.finished event type")
}
}
// 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 := &keptnv2.ReleaseTriggeredEventData{}
err = incomingEvent.DataAs(specificEvent)
if err != nil {
t.Errorf("Error getting keptn event data")
}
err = HandleReleaseTriggeredEvent(myKeptn, *incomingEvent, specificEvent)
if err != nil {
t.Errorf("Error: " + err.Error())
}
}