-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstate_test.go
More file actions
275 lines (250 loc) · 8.08 KB
/
state_test.go
File metadata and controls
275 lines (250 loc) · 8.08 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
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
// Copyright SAP SE
// SPDX-License-Identifier: Apache-2.0
package commitments
import (
"testing"
"github.com/cobaltcore-dev/cortex/api/v1alpha1"
"github.com/cobaltcore-dev/cortex/internal/knowledge/extractor/plugins/compute"
hv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Test helper: creates a minimal flavor group for testing
func testFlavorGroup() compute.FlavorGroupFeature {
return compute.FlavorGroupFeature{
Name: "test-group",
Flavors: []compute.FlavorInGroup{
{Name: "large", VCPUs: 16, MemoryMB: 32768, DiskGB: 100},
{Name: "medium", VCPUs: 8, MemoryMB: 16384, DiskGB: 50},
{Name: "small", VCPUs: 4, MemoryMB: 8192, DiskGB: 25},
},
SmallestFlavor: compute.FlavorInGroup{
Name: "small", VCPUs: 4, MemoryMB: 8192, DiskGB: 25,
},
LargestFlavor: compute.FlavorInGroup{
Name: "large", VCPUs: 16, MemoryMB: 32768, DiskGB: 100,
},
}
}
func TestFromCommitment_CalculatesMemoryCorrectly(t *testing.T) {
flavorGroup := testFlavorGroup()
commitment := Commitment{
UUID: "test-uuid",
ProjectID: "project-1",
ResourceName: "hw_version_test-group_ram",
Amount: 5, // 5 multiples of smallest flavor
}
state, err := FromCommitment(commitment, flavorGroup)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify basic fields
if state.CommitmentUUID != "test-uuid" {
t.Errorf("expected UUID test-uuid, got %s", state.CommitmentUUID)
}
if state.ProjectID != "project-1" {
t.Errorf("expected ProjectID project-1, got %s", state.ProjectID)
}
if state.FlavorGroupName != "test-group" {
t.Errorf("expected FlavorGroupName test-group, got %s", state.FlavorGroupName)
}
// Verify memory calculation: 5 * 8192 MB = 40960 MB = 42949672960 bytes
expectedMemory := int64(5 * 8192 * 1024 * 1024)
if state.TotalMemoryBytes != expectedMemory {
t.Errorf("expected memory %d, got %d", expectedMemory, state.TotalMemoryBytes)
}
}
func TestFromCommitment_InvalidResourceName(t *testing.T) {
flavorGroup := testFlavorGroup()
commitment := Commitment{
UUID: "test-uuid",
ProjectID: "project-1",
ResourceName: "invalid_resource_name", // missing "ram_" prefix
Amount: 1,
}
_, err := FromCommitment(commitment, flavorGroup)
if err == nil {
t.Fatal("expected error for invalid resource name, got nil")
}
}
func TestFromReservations_SumsMemoryCorrectly(t *testing.T) {
reservations := []v1alpha1.Reservation{
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-0",
},
Spec: v1alpha1.ReservationSpec{
Resources: map[hv1.ResourceName]resource.Quantity{
hv1.ResourceMemory: *resource.NewQuantity(8*1024*1024*1024, resource.BinarySI), // 8 GiB
},
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "test-group",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-1",
},
Spec: v1alpha1.ReservationSpec{
Resources: map[hv1.ResourceName]resource.Quantity{
hv1.ResourceMemory: *resource.NewQuantity(16*1024*1024*1024, resource.BinarySI), // 16 GiB
},
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "test-group",
},
},
},
}
state, err := FromReservations(reservations)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify fields extracted from first reservation
if state.CommitmentUUID != "abc123" {
t.Errorf("expected UUID abc123, got %s", state.CommitmentUUID)
}
if state.ProjectID != "project-1" {
t.Errorf("expected ProjectID project-1, got %s", state.ProjectID)
}
if state.FlavorGroupName != "test-group" {
t.Errorf("expected FlavorGroupName test-group, got %s", state.FlavorGroupName)
}
// Verify memory is summed correctly: 8 GiB + 16 GiB = 24 GiB
expectedMemory := int64(24 * 1024 * 1024 * 1024)
if state.TotalMemoryBytes != expectedMemory {
t.Errorf("expected memory %d, got %d", expectedMemory, state.TotalMemoryBytes)
}
}
func TestFromReservations_EmptyList(t *testing.T) {
_, err := FromReservations([]v1alpha1.Reservation{})
if err == nil {
t.Fatal("expected error for empty reservation list, got nil")
}
}
func TestFromReservations_SkipsInconsistentFlavorGroup(t *testing.T) {
reservations := []v1alpha1.Reservation{
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-0",
},
Spec: v1alpha1.ReservationSpec{
Resources: map[hv1.ResourceName]resource.Quantity{
hv1.ResourceMemory: *resource.NewQuantity(8*1024*1024*1024, resource.BinarySI),
},
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "test-group",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-1",
},
Spec: v1alpha1.ReservationSpec{
Resources: map[hv1.ResourceName]resource.Quantity{
hv1.ResourceMemory: *resource.NewQuantity(16*1024*1024*1024, resource.BinarySI),
},
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "wrong-group", // Different flavor group
},
},
},
}
state, err := FromReservations(reservations)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Should only count first reservation with matching flavor group
expectedMemory := int64(8 * 1024 * 1024 * 1024)
if state.TotalMemoryBytes != expectedMemory {
t.Errorf("expected memory %d (ignoring inconsistent reservation), got %d",
expectedMemory, state.TotalMemoryBytes)
}
}
func TestFromReservations_MixedCommitmentUUIDs(t *testing.T) {
reservations := []v1alpha1.Reservation{
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-0",
},
Spec: v1alpha1.ReservationSpec{
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "test-group",
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-xyz789-0", // Different commitment UUID
},
Spec: v1alpha1.ReservationSpec{
CommittedResourceReservation: &v1alpha1.CommittedResourceReservationSpec{
ProjectID: "project-1",
ResourceGroup: "test-group",
},
},
},
}
_, err := FromReservations(reservations)
if err == nil {
t.Fatal("expected error for mixed commitment UUIDs, got nil")
}
}
func TestFromReservations_NonCommittedResourceType(t *testing.T) {
reservations := []v1alpha1.Reservation{
{
ObjectMeta: metav1.ObjectMeta{
Name: "commitment-abc123-0",
},
Spec: v1alpha1.ReservationSpec{
Type: v1alpha1.ReservationTypeFailover, // Wrong type
},
},
}
_, err := FromReservations(reservations)
if err == nil {
t.Fatal("expected error for non-CR reservation type, got nil")
}
}
func TestGetFlavorGroupNameFromResource_Valid(t *testing.T) {
// Test valid resource names with underscores in flavor group
name, err := getFlavorGroupNameFromResource("hw_version_hana_medium_v2_ram")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if name != "hana_medium_v2" {
t.Errorf("expected hana_medium_v2, got %s", name)
}
}
func TestGetFlavorGroupNameFromResource_Invalid(t *testing.T) {
invalidCases := []string{
"ram_2101", // old format
"invalid", // completely wrong
"hw_version__ram", // empty group name
"hw_version_2101", // missing suffix
}
for _, input := range invalidCases {
if _, err := getFlavorGroupNameFromResource(input); err == nil {
t.Errorf("expected error for %q, got nil", input)
}
}
}
func TestResourceNameRoundTrip(t *testing.T) {
// Test that ResourceNameFromFlavorGroup and getFlavorGroupNameFromResource are inverses
for _, groupName := range []string{"2101", "hana_1", "hana_medium_v2"} {
resourceName := ResourceNameFromFlavorGroup(groupName)
recovered, err := getFlavorGroupNameFromResource(resourceName)
if err != nil {
t.Fatalf("round-trip failed for %q: %v", groupName, err)
}
if recovered != groupName {
t.Errorf("round-trip mismatch: %q -> %q -> %q", groupName, resourceName, recovered)
}
}
}