forked from opendatahub-io/models-as-a-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper_test.go
More file actions
210 lines (190 loc) · 6.54 KB
/
mapper_test.go
File metadata and controls
210 lines (190 loc) · 6.54 KB
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
package tier_test
import (
"testing"
"github.com/opendatahub-io/maas-billing/maas-api/internal/constant"
"github.com/opendatahub-io/maas-billing/maas-api/internal/tier"
"github.com/opendatahub-io/maas-billing/maas-api/test/fixtures"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
)
// Use unified test constants from fixtures
const (
testNamespace = fixtures.TestNamespace
testTenant = fixtures.TestTenant
)
func TestMapper_GetTierForGroups(t *testing.T) {
ctx := t.Context()
configMap := fixtures.CreateTierConfigMap(testNamespace)
clientset := fake.NewClientset([]runtime.Object{configMap}...)
mapper := tier.NewMapper(clientset, testTenant, testNamespace)
tests := []struct {
name string
groups []string
expectedTier string
expectedError bool
description string
}{
{
name: "single group - free tier",
groups: []string{"system:authenticated"},
expectedTier: "free",
description: "User belongs to only free tier group",
},
{
name: "inferred SA group - free tier",
groups: []string{"system:serviceaccounts:test-tenant-tier-free"},
expectedTier: "free",
description: "User belongs to only free tier group",
},
{
name: "inferred SA group - premium tier",
groups: []string{"system:serviceaccounts:test-tenant-tier-premium"},
expectedTier: "premium",
description: "User belongs to only premium tier group",
},
{
name: "single group - premium tier",
groups: []string{"premium-users"},
expectedTier: "premium",
description: "User belongs to only premium tier group",
},
{
name: "single group - enterprise tier",
groups: []string{"enterprise-users"},
expectedTier: "enterprise",
description: "User belongs to only enterprise tier group",
},
{
name: "multiple groups - enterprise wins over free",
groups: []string{"system:authenticated", "enterprise-users"},
expectedTier: "enterprise",
description: "User belongs to both free and enterprise - enterprise has higher level (20 > 1)",
},
{
name: "multiple groups - premium wins over free",
groups: []string{"free-users", "premium-users"},
expectedTier: "premium",
description: "User belongs to both free and premium - premium has higher level (10 > 1)",
},
{
name: "multiple groups - enterprise wins over premium",
groups: []string{"premium-users", "enterprise-users"},
expectedTier: "enterprise",
description: "User belongs to both premium and enterprise - enterprise has higher level (20 > 10)",
},
{
name: "multiple groups - enterprise wins over developer",
groups: []string{"developer-users", "enterprise-users"},
expectedTier: "enterprise",
description: "User belongs to both developer and enterprise - enterprise has higher level (20 > 15)",
},
{
name: "multiple groups - developer wins over premium",
groups: []string{"premium-users", "developer-users"},
expectedTier: "developer",
description: "User belongs to both premium and developer - developer has higher level (15 > 10)",
},
{
name: "multiple groups - service account groups",
groups: []string{"system:serviceaccounts", "system:serviceaccounts:test-tenant-tier-premium", "system:authenticated"},
expectedTier: "premium",
description: "User belongs to both premium and developer - developer has higher level (15 > 10)",
},
{
name: "three groups - enterprise wins",
groups: []string{"free-users", "premium-users", "enterprise-users"},
expectedTier: "enterprise",
description: "User belongs to free, premium, and enterprise - enterprise has highest level (20)",
},
{
name: "all groups - enterprise wins",
groups: []string{"system:authenticated", "premium-users", "developer-users", "admin-users"},
expectedTier: "enterprise",
description: "User belongs to groups across all tiers - enterprise has highest level (20)",
},
{
name: "no groups provided",
groups: []string{},
expectedError: true,
description: "Empty groups array should return error",
},
{
name: "unknown groups",
groups: []string{"unknown-group-1", "unknown-group-2"},
expectedError: true,
description: "Groups not found in any tier should return error",
},
{
name: "mix of known and unknown groups",
groups: []string{"premium-users", "unknown-group"},
expectedTier: "premium",
description: "Should find tier for known group and ignore unknown ones",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mappedTiers, err := mapper.GetTierForGroups(ctx, tt.groups...)
if tt.expectedError && err == nil {
t.Errorf("expected error but got none")
return
}
if !tt.expectedError && err != nil {
t.Errorf("unexpected error: %v", err)
return
}
if mappedTiers != tt.expectedTier {
t.Errorf("expected mappedTiers %s, got %s", tt.expectedTier, mappedTiers)
}
})
}
}
func TestMapper_GetTierForGroups_MissingConfigMap(t *testing.T) {
ctx := t.Context()
clientset := fake.NewClientset()
mapper := tier.NewMapper(clientset, testTenant, testNamespace)
// Should default to free mappedTier when ConfigMap is missing
mappedTier, err := mapper.GetTierForGroups(ctx, "any-group", "another-group")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if mappedTier != "free" {
t.Errorf("expected default mappedTier 'free', got %s", mappedTier)
}
}
func TestMapper_GetTierForGroups_SameLevels(t *testing.T) {
ctx := t.Context()
// Test case where two tiers have the same level
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: constant.TierMappingConfigMap,
Namespace: testNamespace,
},
Data: map[string]string{
"tiers": `
- name: tier-a
description: Tier A
level: 10
groups:
- group-a
- name: tier-b
description: Tier B
level: 10
groups:
- group-b
`,
},
}
clientset := fake.NewClientset([]runtime.Object{configMap}...)
mapper := tier.NewMapper(clientset, testTenant, testNamespace)
// When levels are equal, first tier found should win
mappedTier, err := mapper.GetTierForGroups(ctx, "group-a", "group-b")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
// Should return tier-a since it appears first in the config and has same level
if mappedTier != "tier-a" {
t.Errorf("expected mappedTier 'tier-a', got %s", mappedTier)
}
}