-
Notifications
You must be signed in to change notification settings - Fork 465
/
common.go
306 lines (262 loc) · 9.05 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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
package conformance
import (
"errors"
"fmt"
"io/ioutil"
"os"
"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_storagequeues "github.com/dapr/components-contrib/bindings/azure/storagequeues"
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_redis "github.com/dapr/components-contrib/pubsub/redis"
"github.com/dapr/components-contrib/secretstores"
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_output_bindings "github.com/dapr/components-contrib/tests/conformance/bindings/output"
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"
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"`
AllOperations bool `yaml:"allOperations,omitempty"`
Operations []string `yaml:"operations,omitempty"`
Config map[string]string `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]string) {
for k, v := range configMap {
val := v
if strings.EqualFold(val, generateUUID) {
// check if generate uuid is specified
val = uuid.New().String()
t.Logf("Generated UUID %s", val)
}
configMap[k] = 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 string) string {
if strings.Contains(componentName, ".") {
return strings.Join(strings.Split(componentName, "."), "/")
}
return componentName
}
func (tc *TestConfiguration) Run(t *testing.T) {
// For each component in the tests file run the conformance test
for _, comp := range tc.Components {
t.Run(comp.Component, func(t *testing.T) {
// Parse and generate any keys
ParseConfigurationMap(t, comp.Config)
componentConfigPath := convertComponentNameToPath(comp.Component)
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 := conf_pubsub.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations, comp.Config)
conf_pubsub.ConformanceTests(t, props, pubsub, pubsubConfig)
case "output-binding":
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
}
binding := loadOutputBindings(comp)
assert.NotNil(t, binding)
bindingsConfig := conf_output_bindings.NewTestConfig(comp.Component, comp.AllOperations, comp.Operations, comp.Config)
conf_output_bindings.ConformanceTests(t, props, binding, 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)
default:
return nil
}
return pubsub
}
func loadSecretStore(tc TestComponent) secretstores.SecretStore {
var store secretstores.SecretStore
switch tc.Component {
case "localfile":
store = ss_local_file.NewLocalSecretStore(testLogger)
case "localenv":
store = ss_local_env.NewEnvSecretStore(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)
default:
return nil
}
return binding
}