@@ -12,15 +12,31 @@ import (
1212var (
1313 TestNamespace1 = inventory.Namespace {
1414 Name : "ns1" ,
15+ Labels : map [string ]string {
16+ "anchore.io/account" : "account1" ,
17+ },
1518 }
1619 TestNamespace2 = inventory.Namespace {
1720 Name : "ns2" ,
21+ Labels : map [string ]string {
22+ "anchore.io/account" : "account2" ,
23+ },
1824 }
1925 TestNamespace3 = inventory.Namespace {
2026 Name : "ns3" ,
27+ Labels : map [string ]string {
28+ "anchore.io/account" : "account3" ,
29+ },
2130 }
2231 TestNamespace4 = inventory.Namespace {
2332 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 {},
2440 }
2541 TestNamespaces = []inventory.Namespace {
2642 TestNamespace1 ,
3248
3349func TestGetAccountRoutedNamespaces (t * testing.T ) {
3450 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
3855 }
3956 tests := []struct {
4057 name string
@@ -44,9 +61,10 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
4461 {
4562 name : "no account routes all to default" ,
4663 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 {},
5068 },
5169 want : map [string ][]inventory.Namespace {
5270 "admin" : TestNamespaces ,
@@ -71,6 +89,7 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
7189 Namespaces : []string {"ns4" },
7290 },
7391 },
92+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {},
7493 },
7594 want : map [string ][]inventory.Namespace {
7695 "account1" : {TestNamespace1 },
@@ -89,15 +108,117 @@ func TestGetAccountRoutedNamespaces(t *testing.T) {
89108 Namespaces : []string {"ns.*" },
90109 },
91110 },
111+ namespaceLabelRouting : config.AccountRouteByNamespaceLabel {},
92112 },
93113 want : map [string ][]inventory.Namespace {
94114 "account1" : TestNamespaces ,
95115 },
96116 },
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+ },
97218 }
98219 for _ , tt := range tests {
99220 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 )
101222 assert .Equal (t , tt .want , got )
102223 })
103224 }
0 commit comments