forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_routes_test.go
108 lines (96 loc) · 3.06 KB
/
group_routes_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
package v1
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/core"
"go.k6.io/k6/core/local"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/minirunner"
"go.k6.io/k6/metrics"
)
func getTestPreInitState(tb testing.TB) *lib.TestPreInitState {
reg := metrics.NewRegistry()
return &lib.TestPreInitState{
Logger: testutils.NewLogger(tb),
RuntimeOptions: lib.RuntimeOptions{},
Registry: reg,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(reg),
}
}
func getTestRunState(tb testing.TB, options lib.Options, runner lib.Runner) *lib.TestRunState {
require.NoError(tb, runner.SetOptions(runner.GetOptions().Apply(options)))
return &lib.TestRunState{
TestPreInitState: getTestPreInitState(tb),
Options: options,
Runner: runner,
}
}
func TestGetGroups(t *testing.T) {
g0, err := lib.NewGroup("", nil)
assert.NoError(t, err)
g1, err := g0.Group("group 1")
assert.NoError(t, err)
g2, err := g1.Group("group 2")
assert.NoError(t, err)
testState := getTestRunState(t, lib.Options{}, &minirunner.MiniRunner{Group: g0})
execScheduler, err := local.NewExecutionScheduler(testState)
require.NoError(t, err)
engine, err := core.NewEngine(testState, execScheduler, nil)
require.NoError(t, err)
t.Run("list", func(t *testing.T) {
rw := httptest.NewRecorder()
NewHandler().ServeHTTP(rw, newRequestWithEngine(engine, "GET", "/v1/groups", nil))
res := rw.Result()
body := rw.Body.Bytes()
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.NotEmpty(t, body)
t.Run("document", func(t *testing.T) {
var doc groupsJSONAPI
assert.NoError(t, json.Unmarshal(body, &doc))
if assert.NotEmpty(t, doc.Data) {
assert.Equal(t, "groups", doc.Data[0].Type)
}
})
t.Run("groups", func(t *testing.T) {
var envelop groupsJSONAPI
require.NoError(t, json.Unmarshal(body, &envelop))
require.Len(t, envelop.Data, 3)
for _, data := range envelop.Data {
current := data.Attributes
switch current.ID {
case g0.ID:
assert.Equal(t, "", current.Name)
assert.Nil(t, current.Parent)
assert.Equal(t, "", current.ParentID)
assert.Len(t, current.GroupIDs, 1)
assert.EqualValues(t, []string{g1.ID}, current.GroupIDs)
case g1.ID:
assert.Equal(t, "group 1", current.Name)
assert.Nil(t, current.Parent)
assert.Equal(t, g0.ID, current.ParentID)
assert.EqualValues(t, []string{g2.ID}, current.GroupIDs)
case g2.ID:
assert.Equal(t, "group 2", current.Name)
assert.Nil(t, current.Parent)
assert.Equal(t, g1.ID, current.ParentID)
assert.EqualValues(t, []string{}, current.GroupIDs)
default:
assert.Fail(t, "Unknown ID: "+current.ID)
}
}
})
})
for _, gp := range []*lib.Group{g0, g1, g2} {
t.Run(gp.Name, func(t *testing.T) {
rw := httptest.NewRecorder()
NewHandler().ServeHTTP(rw, newRequestWithEngine(engine, "GET", "/v1/groups/"+gp.ID, nil))
res := rw.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
})
}
}