-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
setup_teardown_routes.go
127 lines (105 loc) · 3.8 KB
/
setup_teardown_routes.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2018 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package v1
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/manyminds/api2go/jsonapi"
"github.com/loadimpact/k6/api/common"
)
// NullSetupData is wrapper around null to satisfy jsonapi
type NullSetupData struct {
SetupData
Data interface{} `json:"data,omitempty" yaml:"data"`
}
// SetupData is just a simple wrapper to satisfy jsonapi
type SetupData struct {
Data interface{} `json:"data" yaml:"data"`
}
// GetName is a dummy method so we can satisfy the jsonapi.EntityNamer interface
func (sd SetupData) GetName() string {
return "setupData"
}
// GetID is a dummy method so we can satisfy the jsonapi.MarshalIdentifier interface
func (sd SetupData) GetID() string {
return "default"
}
func handleSetupDataOutput(rw http.ResponseWriter, setupData json.RawMessage) {
rw.Header().Set("Content-Type", "application/json")
var err error
var data []byte
if setupData == nil {
data, err = jsonapi.Marshal(NullSetupData{Data: nil})
} else {
data, err = jsonapi.Marshal(SetupData{setupData})
}
if err != nil {
apiError(rw, "Encoding error", err.Error(), http.StatusInternalServerError)
return
}
_, _ = rw.Write(data)
}
// HandleGetSetupData just returns the current JSON-encoded setup data
func HandleGetSetupData(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
runner := common.GetEngine(r.Context()).ExecutionScheduler.GetRunner()
handleSetupDataOutput(rw, runner.GetSetupData())
}
// HandleSetSetupData just parses the JSON request body and sets the result as setup data for the runner
func HandleSetSetupData(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
apiError(rw, "Error reading request body", err.Error(), http.StatusBadRequest)
return
}
var data interface{}
if len(body) > 0 {
if err := json.Unmarshal(body, &data); err != nil {
apiError(rw, "Error parsing request body", err.Error(), http.StatusBadRequest)
return
}
}
runner := common.GetEngine(r.Context()).ExecutionScheduler.GetRunner()
if len(body) == 0 {
runner.SetSetupData(nil)
} else {
runner.SetSetupData(body)
}
handleSetupDataOutput(rw, runner.GetSetupData())
}
// HandleRunSetup executes the runner's Setup() method and returns the result
func HandleRunSetup(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
engine := common.GetEngine(r.Context())
runner := engine.ExecutionScheduler.GetRunner()
if err := runner.Setup(r.Context(), engine.Samples); err != nil {
apiError(rw, "Error executing setup", err.Error(), http.StatusInternalServerError)
return
}
handleSetupDataOutput(rw, runner.GetSetupData())
}
// HandleRunTeardown executes the runner's Teardown() method
func HandleRunTeardown(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
engine := common.GetEngine(r.Context())
runner := common.GetEngine(r.Context()).ExecutionScheduler.GetRunner()
if err := runner.Teardown(r.Context(), engine.Samples); err != nil {
apiError(rw, "Error executing teardown", err.Error(), http.StatusInternalServerError)
}
}