@@ -12,15 +12,31 @@ import (
12
12
var (
13
13
TestNamespace1 = inventory.Namespace {
14
14
Name : "ns1" ,
15
+ Labels : map [string ]string {
16
+ "anchore.io/account" : "account1" ,
17
+ },
15
18
}
16
19
TestNamespace2 = inventory.Namespace {
17
20
Name : "ns2" ,
21
+ Labels : map [string ]string {
22
+ "anchore.io/account" : "account2" ,
23
+ },
18
24
}
19
25
TestNamespace3 = inventory.Namespace {
20
26
Name : "ns3" ,
27
+ Labels : map [string ]string {
28
+ "anchore.io/account" : "account3" ,
29
+ },
21
30
}
22
31
TestNamespace4 = inventory.Namespace {
23
32
Name : "ns4" ,
33
+ Labels : map [string ]string {
34
+ "anchore.io/account" : "account4" ,
35
+ },
36
+ }
37
+ TestNamespace5 = inventory.Namespace {
38
+ Name : "ns5-no-label" ,
39
+ Labels : map [string ]string {},
24
40
}
25
41
TestNamespaces = []inventory.Namespace {
26
42
TestNamespace1 ,
32
48
33
49
func TestGetAccountRoutedNamespaces (t * testing.T ) {
34
50
type args struct {
35
- defaultAccount string
36
- namespaces []inventory.Namespace
37
- accountRoutes config.AccountRoutes
51
+ defaultAccount string
52
+ namespaces []inventory.Namespace
53
+ accountRoutes config.AccountRoutes
54
+ namespaceLabelRouting config.AccountRouteByNamespaceLabel
38
55
}
39
56
tests := []struct {
40
57
name string
@@ -44,9 +61,10 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
44
61
{
45
62
name : "no account routes all to default" ,
46
63
args : args {
47
- defaultAccount : "admin" ,
48
- namespaces : TestNamespaces ,
49
- accountRoutes : config.AccountRoutes {},
64
+ defaultAccount : "admin" ,
65
+ namespaces : TestNamespaces ,
66
+ accountRoutes : config.AccountRoutes {},
67
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {},
50
68
},
51
69
want : map [string ][]inventory.Namespace {
52
70
"admin" : TestNamespaces ,
@@ -71,6 +89,7 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
71
89
Namespaces : []string {"ns4" },
72
90
},
73
91
},
92
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {},
74
93
},
75
94
want : map [string ][]inventory.Namespace {
76
95
"account1" : {TestNamespace1 },
@@ -89,15 +108,117 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
89
108
Namespaces : []string {"ns.*" },
90
109
},
91
110
},
111
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {},
92
112
},
93
113
want : map [string ][]inventory.Namespace {
94
114
"account1" : TestNamespaces ,
95
115
},
96
116
},
117
+ {
118
+ name : "namespaces to accounts that match a label only" ,
119
+ args : args {
120
+ defaultAccount : "admin" ,
121
+ namespaces : TestNamespaces ,
122
+ accountRoutes : config.AccountRoutes {},
123
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {
124
+ LabelKey : "anchore.io/account" ,
125
+ DefaultAccount : "default" ,
126
+ IgnoreMissingLabel : false ,
127
+ },
128
+ },
129
+ want : map [string ][]inventory.Namespace {
130
+ "account1" : {TestNamespace1 },
131
+ "account2" : {TestNamespace2 },
132
+ "account3" : {TestNamespace3 },
133
+ "account4" : {TestNamespace4 },
134
+ },
135
+ },
136
+ {
137
+ name : "namespaces to accounts that match a label only with namespace missing label (default account not set)" ,
138
+ args : args {
139
+ defaultAccount : "admin" ,
140
+ namespaces : append (TestNamespaces , TestNamespace5 ),
141
+ accountRoutes : config.AccountRoutes {},
142
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {
143
+ LabelKey : "anchore.io/account" ,
144
+ DefaultAccount : "" ,
145
+ IgnoreMissingLabel : false ,
146
+ },
147
+ },
148
+ want : map [string ][]inventory.Namespace {
149
+ "account1" : {TestNamespace1 },
150
+ "account2" : {TestNamespace2 },
151
+ "account3" : {TestNamespace3 },
152
+ "account4" : {TestNamespace4 },
153
+ "admin" : {TestNamespace5 },
154
+ },
155
+ },
156
+ {
157
+ name : "namespaces to accounts that match a label only with namespace missing label (default account set)" ,
158
+ args : args {
159
+ defaultAccount : "admin" ,
160
+ namespaces : append (TestNamespaces , TestNamespace5 ),
161
+ accountRoutes : config.AccountRoutes {},
162
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {
163
+ LabelKey : "anchore.io/account" ,
164
+ DefaultAccount : "defaultoverride" ,
165
+ IgnoreMissingLabel : false ,
166
+ },
167
+ },
168
+ want : map [string ][]inventory.Namespace {
169
+ "account1" : {TestNamespace1 },
170
+ "account2" : {TestNamespace2 },
171
+ "account3" : {TestNamespace3 },
172
+ "account4" : {TestNamespace4 },
173
+ "defaultoverride" : {TestNamespace5 },
174
+ },
175
+ },
176
+ {
177
+ name : "namespaces to accounts that match a label only with namespace missing label set to ignore" ,
178
+ args : args {
179
+ defaultAccount : "admin" ,
180
+ namespaces : append (TestNamespaces , TestNamespace5 ),
181
+ accountRoutes : config.AccountRoutes {},
182
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {
183
+ LabelKey : "anchore.io/account" ,
184
+ DefaultAccount : "" ,
185
+ IgnoreMissingLabel : true ,
186
+ },
187
+ },
188
+ want : map [string ][]inventory.Namespace {
189
+ "account1" : {TestNamespace1 },
190
+ "account2" : {TestNamespace2 },
191
+ "account3" : {TestNamespace3 },
192
+ "account4" : {TestNamespace4 },
193
+ },
194
+ },
195
+ {
196
+ name : "mix of account routes and label routing" ,
197
+ args : args {
198
+ defaultAccount : "admin" ,
199
+ namespaces : TestNamespaces ,
200
+ accountRoutes : config.AccountRoutes {
201
+ "explicitaccount1" : config.AccountRouteDetails {
202
+ Namespaces : []string {"ns1" },
203
+ },
204
+ },
205
+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {
206
+ LabelKey : "anchore.io/account" ,
207
+ DefaultAccount : "default" ,
208
+ IgnoreMissingLabel : false ,
209
+ },
210
+ },
211
+ want : map [string ][]inventory.Namespace {
212
+ "explicitaccount1" : {TestNamespace1 },
213
+ "account2" : {TestNamespace2 },
214
+ "account3" : {TestNamespace3 },
215
+ "account4" : {TestNamespace4 },
216
+ },
217
+ },
97
218
}
98
219
for _ , tt := range tests {
99
220
t .Run (tt .name , func (t * testing.T ) {
100
- got := GetAccountRoutedNamespaces (tt .args .defaultAccount , tt .args .namespaces , tt .args .accountRoutes )
221
+ got := GetAccountRoutedNamespaces (tt .args .defaultAccount , tt .args .namespaces , tt .args .accountRoutes , tt . args . namespaceLabelRouting )
101
222
assert .Equal (t , tt .want , got )
102
223
})
103
224
}
0 commit comments