@@ -26,14 +26,17 @@ import (
26
26
"github.com/stretchr/testify/require"
27
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
28
29
+ "github.com/dapr/dapr/pkg/apis/common"
29
30
compapi "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
30
31
"github.com/dapr/kit/ptr"
31
32
)
32
33
33
34
func TestLoad (t * testing.T ) {
34
35
t .Run ("valid yaml content" , func (t * testing.T ) {
35
36
tmp := t .TempDir ()
36
- request := NewComponents (tmp )
37
+ request := NewComponents (Options {
38
+ Paths : []string {tmp },
39
+ })
37
40
filename := "test-component-valid.yaml"
38
41
yaml := `
39
42
apiVersion: dapr.io/v1alpha1
56
59
57
60
t .Run ("invalid yaml head" , func (t * testing.T ) {
58
61
tmp := t .TempDir ()
59
- request := NewComponents (tmp )
62
+ request := NewComponents (Options {
63
+ Paths : []string {tmp },
64
+ })
60
65
61
66
filename := "test-component-invalid.yaml"
62
67
yaml := `
@@ -72,23 +77,29 @@ name: statestore`
72
77
})
73
78
74
79
t .Run ("load components file not exist" , func (t * testing.T ) {
75
- request := NewComponents ("test-path-no-exists" )
80
+ request := NewComponents (Options {
81
+ Paths : []string {"test-path-no-exists" },
82
+ })
76
83
77
84
components , err := request .Load (context .Background ())
78
85
require .Error (t , err )
79
86
assert .Empty (t , components )
80
87
})
81
88
82
89
t .Run ("error and namespace" , func (t * testing.T ) {
83
- buildComp := func (name string , namespace * string ) string {
90
+ buildComp := func (name string , namespace * string , scopes ... string ) string {
84
91
var ns string
85
92
if namespace != nil {
86
93
ns = fmt .Sprintf ("\n namespace: %s\n " , * namespace )
87
94
}
95
+ var scopeS string
96
+ if len (scopes ) > 0 {
97
+ scopeS = fmt .Sprintf ("\n scopes:\n - %s" , strings .Join (scopes , "\n - " ))
98
+ }
88
99
return fmt .Sprintf (`apiVersion: dapr.io/v1alpha1
89
100
kind: Component
90
101
metadata:
91
- name: %s%s` , name , ns )
102
+ name: %s%s%s ` , name , ns , scopeS )
92
103
}
93
104
94
105
tests := map [string ]struct {
@@ -183,6 +194,26 @@ metadata:
183
194
},
184
195
expErr : false ,
185
196
},
197
+ "only return manifests in scope" : {
198
+ comps : []string {
199
+ buildComp ("comp1" , nil , "myappid" ),
200
+ buildComp ("comp2" , nil , "myappid" , "anotherappid" ),
201
+ buildComp ("comp3" , nil , "anotherappid" ),
202
+ },
203
+ namespace : ptr .Of ("foo" ),
204
+ expComps : []compapi.Component {
205
+ {
206
+ TypeMeta : metav1.TypeMeta {APIVersion : "dapr.io/v1alpha1" , Kind : "Component" },
207
+ ObjectMeta : metav1.ObjectMeta {Name : "comp1" , Namespace : "" },
208
+ Scoped : common.Scoped {Scopes : []string {"myappid" }},
209
+ },
210
+ {
211
+ TypeMeta : metav1.TypeMeta {APIVersion : "dapr.io/v1alpha1" , Kind : "Component" },
212
+ ObjectMeta : metav1.ObjectMeta {Name : "comp2" , Namespace : "" },
213
+ Scoped : common.Scoped {Scopes : []string {"myappid" , "anotherappid" }},
214
+ },
215
+ },
216
+ },
186
217
}
187
218
188
219
for name , test := range tests {
@@ -195,7 +226,10 @@ metadata:
195
226
if test .namespace != nil {
196
227
t .Setenv ("NAMESPACE" , * test .namespace )
197
228
}
198
- loader := NewComponents (tmp )
229
+ loader := NewComponents (Options {
230
+ Paths : []string {tmp },
231
+ AppID : "myappid" ,
232
+ })
199
233
components , err := loader .Load (context .Background ())
200
234
assert .Equal (t , test .expErr , err != nil , "%v" , err )
201
235
assert .Equal (t , test .expComps , components )
@@ -207,7 +241,7 @@ metadata:
207
241
func Test_loadWithOrder (t * testing.T ) {
208
242
t .Run ("no file should return empty set" , func (t * testing.T ) {
209
243
tmp := t .TempDir ()
210
- d := NewComponents (tmp ).(* disk [compapi.Component ])
244
+ d := NewComponents (Options { Paths : [] string { tmp }} ).(* disk [compapi.Component ])
211
245
set , err := d .loadWithOrder ()
212
246
require .NoError (t , err )
213
247
assert .Empty (t , set .order )
@@ -225,7 +259,7 @@ spec:
225
259
type: state.couchbase
226
260
` ), fs .FileMode (0o600 )))
227
261
228
- d := NewComponents (tmp ).(* disk [compapi.Component ])
262
+ d := NewComponents (Options { Paths : [] string { tmp }} ).(* disk [compapi.Component ])
229
263
set , err := d .loadWithOrder ()
230
264
require .NoError (t , err )
231
265
assert .Equal (t , []manifestOrder {
@@ -259,7 +293,7 @@ spec:
259
293
type: state.couchbase
260
294
` ), fs .FileMode (0o600 )))
261
295
262
- d := NewComponents (tmp ).(* disk [compapi.Component ])
296
+ d := NewComponents (Options { Paths : [] string { tmp }} ).(* disk [compapi.Component ])
263
297
set , err := d .loadWithOrder ()
264
298
require .NoError (t , err )
265
299
assert .Equal (t , []manifestOrder {
@@ -300,7 +334,9 @@ spec:
300
334
}
301
335
}
302
336
303
- d := NewComponents (tmp1 , tmp2 , tmp3 ).(* disk [compapi.Component ])
337
+ d := NewComponents (Options {
338
+ Paths : []string {tmp1 , tmp2 , tmp3 },
339
+ }).(* disk [compapi.Component ])
304
340
set , err := d .loadWithOrder ()
305
341
require .NoError (t , err )
306
342
assert .Equal (t , []manifestOrder {
0 commit comments