-
Notifications
You must be signed in to change notification settings - Fork 479
/
common.go
422 lines (368 loc) · 12.9 KB
/
common.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation and Dapr Contributors.
// Licensed under the MIT License.
// ------------------------------------------------------------
package conformance
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"testing"
"fortio.org/fortio/log"
"github.com/dapr/components-contrib/bindings"
b_azure_blobstorage "github.com/dapr/components-contrib/bindings/azure/blobstorage"
b_azure_eventgrid "github.com/dapr/components-contrib/bindings/azure/eventgrid"
b_azure_servicebusqueues "github.com/dapr/components-contrib/bindings/azure/servicebusqueues"
b_azure_storagequeues "github.com/dapr/components-contrib/bindings/azure/storagequeues"
b_http "github.com/dapr/components-contrib/bindings/http"
b_kafka "github.com/dapr/components-contrib/bindings/kafka"
b_redis "github.com/dapr/components-contrib/bindings/redis"
"github.com/dapr/components-contrib/pubsub"
p_servicebus "github.com/dapr/components-contrib/pubsub/azure/servicebus"
p_hazelcast "github.com/dapr/components-contrib/pubsub/hazelcast"
p_kafka "github.com/dapr/components-contrib/pubsub/kafka"
p_mqtt "github.com/dapr/components-contrib/pubsub/mqtt"
p_natsstreaming "github.com/dapr/components-contrib/pubsub/natsstreaming"
p_pulsar "github.com/dapr/components-contrib/pubsub/pulsar"
p_rabbitmq "github.com/dapr/components-contrib/pubsub/rabbitmq"
p_redis "github.com/dapr/components-contrib/pubsub/redis"
"github.com/dapr/components-contrib/secretstores"
ss_azure "github.com/dapr/components-contrib/secretstores/azure/keyvault"
ss_kubernetes "github.com/dapr/components-contrib/secretstores/kubernetes"
ss_local_env "github.com/dapr/components-contrib/secretstores/local/env"
ss_local_file "github.com/dapr/components-contrib/secretstores/local/file"
"github.com/dapr/components-contrib/state"
s_cosmosdb "github.com/dapr/components-contrib/state/azure/cosmosdb"
s_mongodb "github.com/dapr/components-contrib/state/mongodb"
s_redis "github.com/dapr/components-contrib/state/redis"
conf_bindings "github.com/dapr/components-contrib/tests/conformance/bindings"
conf_pubsub "github.com/dapr/components-contrib/tests/conformance/pubsub"
conf_secret "github.com/dapr/components-contrib/tests/conformance/secretstores"
conf_state "github.com/dapr/components-contrib/tests/conformance/state"
"github.com/dapr/dapr/pkg/apis/components/v1alpha1"
"github.com/dapr/dapr/pkg/components"
config "github.com/dapr/dapr/pkg/config/modes"
"github.com/google/uuid"
"github.com/dapr/dapr/pkg/logger"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
)
const (
redis = "redis"
kafka = "kafka"
generateUUID = "$((uuid))"
)
// nolint:gochecknoglobals
var testLogger = logger.NewLogger("testLogger")
type TestConfiguration struct {
ComponentType string `yaml:"componentType,omitempty"`
Components []TestComponent `yaml:"components,omitempty"`
}
type TestComponent struct {
Component string `yaml:"component,omitempty"`
Profile string `yaml:"profile,omitempty"`
AllOperations bool `yaml:"allOperations,omitempty"`
Operations []string `yaml:"operations,omitempty"`
Config map[string]interface{} `yaml:"config,omitempty"`
}
// NewTestConfiguration reads the tests.yml and loads the TestConfiguration
func NewTestConfiguration(configFilepath string) (*TestConfiguration, error) {
if isYaml(configFilepath) {
b, err := readTestConfiguration(configFilepath)
if err != nil {
log.Warnf("error reading file %s : %s", configFilepath, err)
return nil, err
}
tc, err := decodeYaml(b)
return &tc, err
}
return nil, errors.New("no test configuration file tests.yml found")
}
func LoadComponents(componentPath string) ([]v1alpha1.Component, error) {
cfg := config.StandaloneConfig{
ComponentsPath: componentPath,
}
standaloneComps := components.NewStandaloneComponents(cfg)
components, err := standaloneComps.LoadComponents()
if err != nil {
return nil, err
}
return components, nil
}
func LookUpEnv(key string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return ""
}
func ParseConfigurationMap(t *testing.T, configMap map[string]interface{}) {
for k, v := range configMap {
switch val := v.(type) {
case string:
if strings.EqualFold(val, generateUUID) {
// check if generate uuid is specified
val = uuid.New().String()
t.Logf("Generated UUID %s", val)
configMap[k] = val
}
case map[string]interface{}:
ParseConfigurationMap(t, val)
case map[interface{}]interface{}:
parseConfigurationInterfaceMap(t, val)
}
}
}
func parseConfigurationInterfaceMap(t *testing.T, configMap map[interface{}]interface{}) {
for k, v := range configMap {
switch val := v.(type) {
case string:
if strings.EqualFold(val, generateUUID) {
// check if generate uuid is specified
val = uuid.New().String()
t.Logf("Generated UUID %s", val)
configMap[k] = val
}
case map[string]interface{}:
ParseConfigurationMap(t, val)
case map[interface{}]interface{}:
parseConfigurationInterfaceMap(t, val)
}
}
}
func ConvertMetadataToProperties(items []v1alpha1.MetadataItem) (map[string]string, error) {
properties := map[string]string{}
for _, c := range items {
val := c.Value.String()
if strings.HasPrefix(c.Value.String(), "${{") {
// look up env var with that name. remove ${{}} and space
k := strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(val, "${{"), "}}"))
v := LookUpEnv(k)
if v == "" {
return map[string]string{}, fmt.Errorf("required env var is not set %s", k)
}
val = v
}
properties[c.Name] = val
}
return properties, nil
}
// isYaml checks whether the file is yaml or not
func isYaml(fileName string) bool {
extension := strings.ToLower(filepath.Ext(fileName))
if extension == ".yaml" || extension == ".yml" {
return true
}
return false
}
func readTestConfiguration(filePath string) ([]byte, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading file %s", filePath)
}
return b, nil
}
func decodeYaml(b []byte) (TestConfiguration, error) {
var testConfig TestConfiguration
err := yaml.Unmarshal(b, &testConfig)
if err != nil {
log.Warnf("error parsing string as yaml %s", err)
return TestConfiguration{}, err
}
return testConfig, nil
}
func (tc *TestConfiguration) loadComponentsAndProperties(t *testing.T, filepath string) (map[string]string, error) {
comps, err := LoadComponents(filepath)
assert.Nil(t, err)
assert.Equal(t, 1, len(comps)) // We only expect a single component per file
c := comps[0]
props, err := ConvertMetadataToProperties(c.Spec.Metadata)
return props, err
}
func convertComponentNameToPath(componentName, componentProfile string) string {
pathName := componentName
if strings.Contains(componentName, ".") {
pathName = strings.Join(strings.Split(componentName, "."), "/")
}
if componentProfile != "" {
pathName = path.Join(pathName, componentProfile)
}
return pathName
}
func (tc *TestConfiguration) Run(t *testing.T) {
// Increase verbosity of tests to allow troubleshooting of runs.
testLogger.SetOutputLevel(logger.DebugLevel)
// For each component in the tests file run the conformance test
for _, comp := range tc.Components {
testName := comp.Component
if comp.Profile != "" {
testName += "-" + comp.Profile
}
t.Run(testName, func(t *testing.T) {
// Parse and generate any keys
ParseConfigurationMap(t, comp.Config)
componentConfigPath := convertComponentNameToPath(comp.Component, comp.Profile)
switch tc.ComponentType {
case "state":
filepath := fmt.Sprintf("../config/state/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
store := loadStateStore(comp)
assert.NotNil(t, store)
storeConfig := conf_state.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations, comp.Config)
conf_state.ConformanceTests(t, props, store, storeConfig)
case "secretstores":
filepath := fmt.Sprintf("../config/secretstores/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
store := loadSecretStore(comp)
assert.NotNil(t, store)
storeConfig := conf_secret.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations)
conf_secret.ConformanceTests(t, props, store, storeConfig)
case "pubsub":
filepath := fmt.Sprintf("../config/pubsub/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
pubsub := loadPubSub(comp)
assert.NotNil(t, pubsub)
pubsubConfig, err := conf_pubsub.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations, comp.Config)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
conf_pubsub.ConformanceTests(t, props, pubsub, pubsubConfig)
case "bindings":
filepath := fmt.Sprintf("../config/bindings/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
inputBinding := loadInputBindings(comp)
outputBinding := loadOutputBindings(comp)
atLeastOne(t, func(item interface{}) bool {
return item != nil
}, inputBinding, outputBinding)
bindingsConfig, err := conf_bindings.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations, comp.Config)
if err != nil {
t.Errorf("error running conformance test for %s: %s", comp.Component, err)
break
}
conf_bindings.ConformanceTests(t, props, inputBinding, outputBinding, bindingsConfig)
default:
t.Errorf("unknown component type %s", tc.ComponentType)
}
})
}
}
func loadPubSub(tc TestComponent) pubsub.PubSub {
var pubsub pubsub.PubSub
switch tc.Component {
case redis:
pubsub = p_redis.NewRedisStreams(testLogger)
case "azure.servicebus":
pubsub = p_servicebus.NewAzureServiceBus(testLogger)
case "natsstreaming":
pubsub = p_natsstreaming.NewNATSStreamingPubSub(testLogger)
case kafka:
pubsub = p_kafka.NewKafka(testLogger)
case "pulsar":
pubsub = p_pulsar.NewPulsar(testLogger)
case "mqtt":
pubsub = p_mqtt.NewMQTTPubSub(testLogger)
case "hazelcast":
pubsub = p_hazelcast.NewHazelcastPubSub(testLogger)
case "rabbitmq":
pubsub = p_rabbitmq.NewRabbitMQ(testLogger)
default:
return nil
}
return pubsub
}
func loadSecretStore(tc TestComponent) secretstores.SecretStore {
var store secretstores.SecretStore
switch tc.Component {
case "azure.keyvault":
store = ss_azure.NewAzureKeyvaultSecretStore(testLogger)
case "kubernetes":
store = ss_kubernetes.NewKubernetesSecretStore(testLogger)
case "localenv":
store = ss_local_env.NewEnvSecretStore(testLogger)
case "localfile":
store = ss_local_file.NewLocalSecretStore(testLogger)
default:
return nil
}
return store
}
func loadStateStore(tc TestComponent) state.Store {
var store state.Store
switch tc.Component {
case redis:
store = s_redis.NewRedisStateStore(testLogger)
case "cosmosdb":
store = s_cosmosdb.NewCosmosDBStateStore(testLogger)
case "mongodb":
store = s_mongodb.NewMongoDB(testLogger)
default:
return nil
}
return store
}
func loadOutputBindings(tc TestComponent) bindings.OutputBinding {
var binding bindings.OutputBinding
switch tc.Component {
case redis:
binding = b_redis.NewRedis(testLogger)
case "azure.blobstorage":
binding = b_azure_blobstorage.NewAzureBlobStorage(testLogger)
case "azure.storagequeues":
binding = b_azure_storagequeues.NewAzureStorageQueues(testLogger)
case "azure.servicebusqueues":
binding = b_azure_servicebusqueues.NewAzureServiceBusQueues(testLogger)
case "azure.eventgrid":
binding = b_azure_eventgrid.NewAzureEventGrid(testLogger)
case kafka:
binding = b_kafka.NewKafka(testLogger)
case "http":
binding = b_http.NewHTTP(testLogger)
default:
return nil
}
return binding
}
func loadInputBindings(tc TestComponent) bindings.InputBinding {
var binding bindings.InputBinding
switch tc.Component {
case "azure.servicebusqueues":
binding = b_azure_servicebusqueues.NewAzureServiceBusQueues(testLogger)
case "azure.storagequeues":
binding = b_azure_storagequeues.NewAzureStorageQueues(testLogger)
case "azure.eventgrid":
binding = b_azure_eventgrid.NewAzureEventGrid(testLogger)
case kafka:
binding = b_kafka.NewKafka(testLogger)
default:
return nil
}
return binding
}
func atLeastOne(t *testing.T, predicate func(interface{}) bool, items ...interface{}) {
met := false
for _, item := range items {
met = met || predicate(item)
}
assert.True(t, met)
}