-
Notifications
You must be signed in to change notification settings - Fork 55
/
spec_test.go
217 lines (184 loc) · 5.48 KB
/
spec_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
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
//go:build norace
// +build norace
package unleash
import (
"encoding/json"
"os"
"path/filepath"
"sync"
"testing"
"github.com/Unleash/unleash-client-go/v4/api"
"github.com/Unleash/unleash-client-go/v4/context"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
const mockHost = "http://unleash-apu"
const specFolder = "./testdata/client-specification/specifications"
var specIndex = filepath.Join(specFolder, "index.json")
var specNotImplemented = []string{""}
type TestState struct {
Version int `json:"version"`
Features []api.Feature `json:"features"`
Segments []api.Segment `json:"segments"`
}
type TestCase struct {
Description string `json:"description"`
Context context.Context `json:"context"`
ToggleName string `json:"toggleName"`
ExpectedResult bool `json:"expectedResult"`
}
type expectedVariantResult struct {
api.Variant
// SpecFeatureEnabled represents the spec's feature_enabled field which has a
// different JSON field name than api.Variant
SpecFeatureEnabled bool `json:"feature_enabled"`
}
type VariantTestCase struct {
Description string `json:"description"`
Context context.Context `json:"context"`
ToggleName string `json:"toggleName"`
ExpectedResult *expectedVariantResult `json:"expectedResult"`
}
type Runner interface {
GetDescription() string
RunWithClient(*Client) func(*testing.T)
}
func (tc TestCase) GetDescription() string {
return tc.Description
}
func (tc TestCase) RunWithClient(client *Client) func(*testing.T) {
return func(t *testing.T) {
client.WaitForReady()
var wg sync.WaitGroup
wg.Add(1)
go func() {
// Call IsEnabled concurrently with itself to catch
// potential data races with go test -race.
client.IsEnabled(tc.ToggleName, WithContext(tc.Context))
wg.Done()
}()
result := client.IsEnabled(tc.ToggleName, WithContext(tc.Context))
wg.Wait()
assert.Equal(t, tc.ExpectedResult, result)
}
}
func (vtc VariantTestCase) GetDescription() string {
return vtc.Description
}
func (vtc VariantTestCase) RunWithClient(client *Client) func(*testing.T) {
client.staticContext = &vtc.Context
return func(t *testing.T) {
client.WaitForReady()
var wg sync.WaitGroup
wg.Add(1)
go func() {
// Call IsEnabled concurrently with itself to catch
// potential data races with go test -race.
client.GetVariant(vtc.ToggleName, WithVariantContext(vtc.Context))
wg.Done()
}()
result := client.GetVariant(vtc.ToggleName, WithVariantContext(vtc.Context))
wg.Wait()
assert.Equal(t, vtc.ExpectedResult.Enabled, result.Enabled)
// copy over the FeatureEnabled field with different JSON tag
vtc.ExpectedResult.FeatureEnabled = vtc.ExpectedResult.SpecFeatureEnabled
assert.Equal(t, &vtc.ExpectedResult.Variant, result)
}
}
type TestDefinition struct {
Name string `json:"name"`
State TestState `json:"state"`
Tests []TestCase `json:"tests"`
VariantTests []VariantTestCase `json:"variantTests"`
}
func (td TestDefinition) Mock(listener interface{}) (*Client, error) {
gock.New(mockHost).
Post("/client/register").
Reply(200)
gock.New(mockHost).
Get("/client/features").
Reply(200).
JSON(api.FeatureResponse{
Response: api.Response{
Version: td.State.Version,
},
Features: td.State.Features,
Segments: td.State.Segments,
})
return NewClient(
WithUrl(mockHost),
WithAppName("clientSpecificationTest"),
WithListener(listener),
)
}
func (td TestDefinition) Unmock() {
gock.OffAll()
}
func (td TestDefinition) Run(t *testing.T) {
runTest := func(test Runner) {
listener := &MockedListener{}
listener.On("OnReady").Return()
listener.On("OnRegistered", mock.AnythingOfType("ClientData")).Return()
listener.On("OnCount", mock.AnythingOfType("string"), mock.AnythingOfType("bool")).Return()
listener.On("OnError", mock.AnythingOfType("*errors.errorString")).Return()
client, err := td.Mock(listener)
assert.NoError(t, err)
t.Run(test.GetDescription(), test.RunWithClient(client))
client.Close()
listener.AssertCalled(t, "OnReady")
listener.AssertCalled(t, "OnRegistered", mock.AnythingOfType("ClientData"))
td.Unmock()
}
for _, test := range td.Tests {
runTest(test)
}
for _, test := range td.VariantTests {
runTest(test)
}
}
func (td TestDefinition) IsImplemented() bool {
for _, name := range specNotImplemented {
if name == td.Name {
return false
}
}
return true
}
type ClientSpecificationSuite struct {
suite.Suite
definitions []TestDefinition
}
func (s ClientSpecificationSuite) loadTestDefinition(testFile string) TestDefinition {
test, err := os.Open(filepath.Join(specFolder, testFile))
s.NoError(err)
defer test.Close()
var testDef TestDefinition
dec := json.NewDecoder(test)
err = dec.Decode(&testDef)
s.NoError(err)
return testDef
}
func (s *ClientSpecificationSuite) SetupTest() {
index, err := os.Open(specIndex)
s.NoError(err)
defer index.Close()
var testFiles []string
dec := json.NewDecoder(index)
err = dec.Decode(&testFiles)
s.NoError(err)
for _, testFile := range testFiles {
s.definitions = append(s.definitions, s.loadTestDefinition(testFile))
}
}
func (s ClientSpecificationSuite) TestClientSpecification() {
for _, td := range s.definitions {
if td.IsImplemented() {
s.T().Run(td.Name, td.Run)
}
}
}
func TestClientSpecificationSuite(t *testing.T) {
suite.Run(t, new(ClientSpecificationSuite))
}