/
cucumber.go
278 lines (246 loc) · 7.15 KB
/
cucumber.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
// Package cucumber allows you to use cucumber to execute Gherkin based
// BDD test scenarios with some helpful API testing step implementations.
//
// Some steps allow you store variables or use those variables. The variables
// are scoped to the Scenario. The http response state is stored in the users
// session. Switching users will switch the session. Scenarios are executed
// concurrently. The same user can be logged into two scenarios, but each scenario
// has a different session.
//
// Note: be careful using the same user/organization across different scenarios since
// they will likely see unexpected API mutations done in the other scenarios.
//
// Using in a test
// func TestMain(m *testing.M) {
//
// ocmServer := mocks.NewMockConfigurableServerBuilder().Build()
// defer ocmServer.Close()
//
// h, _, teardown := test.RegisterIntegration(&testing.T{}, ocmServer)
// defer teardown()
//
// cucumber.TestMain(h)
//
//}
package cucumber
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/test"
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
"github.com/itchyny/gojq"
"github.com/pmezard/go-difflib/difflib"
)
func NewTestSuite(helper *test.Helper) *TestSuite {
return &TestSuite{
Helper: helper,
ApiURL: "http://localhost:8000",
users: map[string]*TestUser{},
nextOrgId: 20000000,
}
}
func DefaultOptions() godog.Options {
opts := godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress",
Paths: []string{"features"},
Randomize: time.Now().UTC().UnixNano(), // randomize TestScenario execution order
Concurrency: 10,
}
return opts
}
// TestSuite holds the state global to all the test scenarios.
// It is accessed concurrently from all test scenarios.
type TestSuite struct {
ApiURL string
Helper *test.Helper
Mu sync.Mutex
users map[string]*TestUser
nextOrgId uint32
}
// TestUser represents a user that can login to the system. The same users are shared by
// the different test scenarios.
type TestUser struct {
Name string
OrgId string
Token string
UserName string
Ctx context.Context
Mu sync.Mutex
}
// TestScenario holds that state of single scenario. It is not accessed
// concurrently.
type TestScenario struct {
Suite *TestSuite
CurrentUser string
PathPrefix string
sessions map[string]*TestSession
Variables map[string]interface{}
hasTestCaseLock bool
}
func (s *TestScenario) User() *TestUser {
s.Suite.Mu.Lock()
defer s.Suite.Mu.Unlock()
return s.Suite.users[s.CurrentUser]
}
func (s *TestScenario) Session() *TestSession {
result := s.sessions[s.CurrentUser]
if result == nil {
result = &TestSession{
TestUser: s.User(),
Client: &http.Client{},
Header: http.Header{},
}
s.sessions[s.CurrentUser] = result
}
return result
}
func (s *TestScenario) JsonMustMatch(actual, expected string, expand bool) error {
var actualParsed interface{}
err := json.Unmarshal([]byte(actual), &actualParsed)
if err != nil {
return fmt.Errorf("error parsing actual json: %v\njson was:\n%s", err, actual)
}
var expectedParsed interface{}
expanded := expected
if expand {
expanded, err = s.Expand(expected, []string{"defs", "ref"})
if err != nil {
return err
}
}
if err := json.Unmarshal([]byte(expanded), &expectedParsed); err != nil {
return fmt.Errorf("error parsing expected json: %v\njson was:\n%s", err, expanded)
}
if !reflect.DeepEqual(expectedParsed, actualParsed) {
expected, _ := json.MarshalIndent(expectedParsed, "", " ")
actual, _ := json.MarshalIndent(actualParsed, "", " ")
diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(string(expected)),
B: difflib.SplitLines(string(actual)),
FromFile: "Expected",
FromDate: "",
ToFile: "Actual",
ToDate: "",
Context: 1,
})
return fmt.Errorf("actual does not match expected, diff:\n%s", diff)
}
return nil
}
// Expand replaces ${var} or $var in the string based on saved Variables in the session/test scenario.
func (s *TestScenario) Expand(value string, skippedVars []string) (result string, rerr error) {
session := s.Session()
return os.Expand(value, func(name string) string {
if contains(skippedVars, name) {
return "$" + name
}
arrayResponse := strings.HasPrefix(name, "response[")
if strings.HasPrefix(name, "response.") || arrayResponse {
selector := "." + name
query, err := gojq.Parse(selector)
if err != nil {
rerr = err
return ""
}
j, err := session.RespJson()
if err != nil {
rerr = err
return ""
}
j = map[string]interface{}{
"response": j,
}
iter := query.Run(j)
if next, found := iter.Next(); found {
switch next := next.(type) {
case string:
return next
case float32:
case float64:
// handle int64 returned as float in json
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", next), "0"), ".")
case nil:
rerr = fmt.Errorf("field ${%s} not found in json response:\n%s", name, string(session.RespBytes))
return ""
case error:
rerr = fmt.Errorf("failed to evaluate selection: %s: %v", name, next)
return ""
default:
return fmt.Sprintf("%s", next)
}
} else {
rerr = fmt.Errorf("field ${%s} not found in json response:\n%s", name, string(session.RespBytes))
return ""
}
}
value, found := s.Variables[name]
if !found {
return ""
}
return fmt.Sprint(value)
}), rerr
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// TestSession holds the http context for a user kinda like a browser. Each scenario
// had a different session even if using the same user.
type TestSession struct {
TestUser *TestUser
Client *http.Client
Resp *http.Response
Ctx context.Context
RespBytes []byte
respJson interface{}
Header http.Header
EventStream bool
EventStreamEvents chan interface{}
Debug bool
}
// RespJson returns the last http response body as json
func (s *TestSession) RespJson() (interface{}, error) {
if s.respJson == nil {
if err := json.Unmarshal(s.RespBytes, &s.respJson); err != nil {
return nil, fmt.Errorf("error parsing json response: %v\nbody: %s", err, string(s.RespBytes))
}
if s.Debug {
fmt.Println("response json:")
e := json.NewEncoder(os.Stdout)
e.SetIndent("", " ")
_ = e.Encode(s.respJson)
fmt.Println("")
}
}
return s.respJson, nil
}
func (s *TestSession) SetRespBytes(bytes []byte) {
s.RespBytes = bytes
s.respJson = nil
}
// StepModules is the list of functions used to add steps to a godog.ScenarioContext, you can
// add more to this list if you need test TestSuite specific steps.
var StepModules []func(ctx *godog.ScenarioContext, s *TestScenario)
func (suite *TestSuite) InitializeScenario(ctx *godog.ScenarioContext) {
s := &TestScenario{
Suite: suite,
sessions: map[string]*TestSession{},
Variables: map[string]interface{}{},
}
for _, module := range StepModules {
module(ctx, s)
}
}