This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
318 lines (264 loc) · 10 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"context"
"errors"
"fmt"
"log"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/cloudevents/sdk-go/pkg/cloudevents"
"github.com/cloudevents/sdk-go/pkg/cloudevents/client"
cloudeventshttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http"
"github.com/cloudevents/sdk-go/pkg/cloudevents/types"
"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"
keptn "github.com/keptn/go-utils/pkg/lib"
)
const eventbroker = "EVENTBROKER"
type envConfig struct {
// Port on which to listen for cloudevents
Port int `envconfig:"RCV_PORT" default:"8080"`
Path string `envconfig:"RCV_PATH" default:"/"`
}
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Fatalf("Failed to process env var: %s", err)
}
os.Exit(_main(os.Args[1:], env))
}
func gotEvent(ctx context.Context, event cloudevents.Event) error {
var shkeptncontext string
event.Context.ExtensionAs("shkeptncontext", &shkeptncontext)
logger := keptn.NewLogger(shkeptncontext, event.Context.GetID(), "jmeter-service")
data := &keptn.DeploymentFinishedEventData{}
if err := event.DataAs(data); err != nil {
logger.Error(fmt.Sprintf("Got Data Error: %s", err.Error()))
return err
}
if event.Type() != keptn.DeploymentFinishedEventType {
const errorMsg = "Received unexpected keptn event"
logger.Error(errorMsg)
return errors.New(errorMsg)
}
if data.TestStrategy == TestStrategy_RealUser {
logger.Info("Received '" + TestStrategy_RealUser + "' test strategy, hence no tests are triggered")
return nil
}
go runTests(event, shkeptncontext, *data, logger)
return nil
}
//
// This method executes the correct tests based on the passed testStrategy in the deployment finished event
// The method will always try to execute a health check workload first, then execute the workload based on the passed testStrategy
//
func runTests(event cloudevents.Event, shkeptncontext string, data keptn.DeploymentFinishedEventData, logger *keptn.Logger) {
testInfo := getTestInfo(data)
id := uuid.New().String()
startedAt := time.Now()
// load the workloads from JMeterConf
var err error
var jmeterconf *JMeterConf
jmeterconf, err = getJMeterConf(testInfo.Project, testInfo.Stage, testInfo.Service, logger)
// get the service endpoint we need to run the test against
var serviceUrl *url.URL
serviceUrl, err = getServiceURL(data)
if err != nil {
logger.Error(fmt.Sprintf("Error getting service url to run test against: %s", err.Error()))
if err := sendTestsFinishedEvent(shkeptncontext, event, startedAt, "fail", logger); err != nil {
logger.Error(fmt.Sprintf("Error sending test finished event: %s", err.Error()) + ". " + testInfo.ToString())
}
return
}
// first we run a health check workload. If that fails we stop the rest
var healthCheckWorkload *Workload
var res bool
healthCheckWorkload, err = getWorkload(jmeterconf, TestStrategy_HealthCheck)
if healthCheckWorkload != nil {
res, err = runWorkload(serviceUrl, testInfo, id, healthCheckWorkload, logger)
if err != nil {
logger.Error(err.Error())
return
}
if !res {
if err := sendTestsFinishedEvent(shkeptncontext, event, startedAt, "fail", logger); err != nil {
logger.Error(fmt.Sprintf("Error sending test finished event: %s", err.Error()) + ". " + testInfo.ToString())
}
return
}
logger.Info("Health Check test passed = " + strconv.FormatBool(res) + ". " + testInfo.ToString())
} else {
logger.Info("No Health Check test workload configuration found. Skipping Health Check")
}
// now lets execute the workload based on the passed testStrategy
res = false
var testStrategy = strings.ToLower(data.TestStrategy)
if testStrategy == "" {
// no testStrategy passed at all -> we just send a successful test finished event!
logger.Info("No testStrategy specified therefore skipping further test execution and sending back success")
res = true
} else {
// lets get the workload configuration for the test strategy
var teststrategyWorkload *Workload
teststrategyWorkload, err = getWorkload(jmeterconf, testStrategy)
if teststrategyWorkload != nil {
res, err = runWorkload(serviceUrl, testInfo, id, teststrategyWorkload, logger)
if err != nil {
logger.Error(err.Error())
res = false
} else {
logger.Info(fmt.Sprintf("Tests for %s with status = %s.%s", testStrategy, strconv.FormatBool(res), testInfo.ToString()))
}
} else {
// no workload for that test strategy!!
res = false
logger.Error(fmt.Sprintf("No workload definition found for testStrategy %s", testStrategy))
}
}
// now lets send the test finished event
if !res {
if err := sendTestsFinishedEvent(shkeptncontext, event, startedAt, "fail", logger); err != nil {
logger.Error(fmt.Sprintf("Error sending test finished event: %s", err.Error()) + ". " + testInfo.ToString())
}
return
}
if err := sendTestsFinishedEvent(shkeptncontext, event, startedAt, "pass", logger); err != nil {
logger.Error(fmt.Sprintf("Error sending test finished event: %s", err.Error()) + ". " + testInfo.ToString())
}
}
//
// Extracts relevant information from the data object
//
func getTestInfo(data keptn.DeploymentFinishedEventData) *TestInfo {
return &TestInfo{
Project: data.Project,
Service: data.Service,
Stage: data.Stage,
TestStrategy: data.TestStrategy,
}
}
//
// returns the service URL that is either passed via the DeploymentURI* parameters or constructs one based on keptn naming structure
//
func getServiceURL(data keptn.DeploymentFinishedEventData) (*url.URL, error) {
if data.DeploymentURILocal != "" {
return url.Parse(data.DeploymentURILocal)
} else if data.DeploymentURIPublic != "" {
return url.Parse(data.DeploymentURIPublic)
}
// Use educated guess of the service url based on stage, service name, deployment type
serviceURL := data.Service + "." + data.Project + "-" + data.Stage
if data.DeploymentStrategy == "blue_green_service" {
serviceURL = data.Service + "-canary" + "." + data.Project + "-" + data.Stage
}
serviceURL = "http://" + serviceURL + "/health"
return url.Parse(serviceURL)
}
//
// executes the actual JMEter tests based on the workload configuration
//
func runWorkload(serviceUrl *url.URL, testInfo *TestInfo, id string, workload *Workload, logger *keptn.Logger) (bool, error) {
// for testStrategy functional we enforce a 0% error policy!
breakOnFunctionalIssues := workload.TestStrategy == TestStrategy_Functional
logger.Info(
fmt.Sprintf("Running workload testStrategy=%s, vuser=%d, loopcount=%d, thinktime=%d, funcvalidation=%t, acceptederrors=%f, avgrtvalidation=%d, script=%s",
workload.TestStrategy, workload.VUser, workload.LoopCount, workload.ThinkTime, breakOnFunctionalIssues, workload.AcceptedErrorRate, workload.AvgRtValidation, workload.Script))
if runlocal {
logger.Info("LOCALLY: not executiong actual tests!")
return true, nil
}
os.RemoveAll(workload.TestStrategy + "_" + testInfo.Service)
os.RemoveAll(workload.TestStrategy + "_" + testInfo.Service + "_result.tlf")
os.RemoveAll("output.txt")
return executeJMeter(testInfo, workload, workload.TestStrategy+"_"+testInfo.Service, serviceUrl, workload.TestStrategy+""+id,
breakOnFunctionalIssues, logger)
}
func sendTestsFinishedEvent(shkeptncontext string, incomingEvent cloudevents.Event, startedAt time.Time, result string, logger *keptn.Logger) error {
source, _ := url.Parse("jmeter-service")
contentType := "application/json"
testFinishedData := keptn.TestsFinishedEventData{}
// fill in data from incoming event (e.g., project, service, stage, teststrategy, deploymentstrategy)
if err := incomingEvent.DataAs(&testFinishedData); err != nil {
logger.Error(fmt.Sprintf("Got Data Error: %s", err.Error()))
return err
}
// fill in timestamps
testFinishedData.Start = startedAt.Format(time.RFC3339)
testFinishedData.End = time.Now().Format(time.RFC3339)
// set test result
testFinishedData.Result = result
event := cloudevents.Event{
Context: cloudevents.EventContextV02{
ID: uuid.New().String(),
Time: &types.Timestamp{Time: time.Now()},
Type: keptn.TestsFinishedEventType,
Source: types.URLRef{URL: *source},
ContentType: &contentType,
Extensions: map[string]interface{}{"shkeptncontext": shkeptncontext},
}.AsV02(),
Data: testFinishedData,
}
return sendEvent(event)
}
func _main(args []string, env envConfig) int {
if runlocal {
log.Println("Running LOCALLY: env=runlocal")
}
ctx := context.Background()
t, err := cloudeventshttp.New(
cloudeventshttp.WithPort(env.Port),
cloudeventshttp.WithPath(env.Path),
)
if err != nil {
log.Fatalf("failed to create transport, %v", err)
}
c, err := client.New(t)
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Println("Started jmeter-extended-service on ", env.Path, env.Port)
log.Fatalf("failed to start receiver: %s", c.StartReceiver(ctx, gotEvent))
return 0
}
func sendEvent(event cloudevents.Event) error {
if runlocal {
log.Println("LOCALLY: Sending Event")
return nil
}
endPoint, err := getServiceEndpoint(eventbroker)
if err != nil {
return errors.New("Failed to retrieve endpoint of eventbroker. %s" + err.Error())
}
if endPoint.Host == "" {
return errors.New("Host of eventbroker not set")
}
transport, err := cloudeventshttp.New(
cloudeventshttp.WithTarget(endPoint.String()),
cloudeventshttp.WithEncoding(cloudeventshttp.StructuredV02),
)
if err != nil {
return errors.New("Failed to create transport:" + err.Error())
}
c, err := client.New(transport)
if err != nil {
return errors.New("Failed to create HTTP client:" + err.Error())
}
if _, _, err := c.Send(context.Background(), event); err != nil {
return errors.New("Failed to send cloudevent:, " + err.Error())
}
return nil
}
// getServiceEndpoint gets an endpoint stored in an environment variable and sets http as default scheme
func getServiceEndpoint(service string) (url.URL, error) {
url, err := url.Parse(os.Getenv(service))
if err != nil {
return *url, fmt.Errorf("Failed to retrieve value from ENVIRONMENT_VARIABLE: %s", service)
}
if url.Scheme == "" {
url.Scheme = "http"
}
return *url, nil
}