Skip to content

Commit ac3478c

Browse files
refactor: consolidate ResourceMap metrics tests and eliminate shared state (#1913)
- Refactor TestResourceMapUpdateMetrics to use per-test-case ResourceMap instances - Consolidate TestResourceMapMultipleUpdates into TestResourceMapUpdateMetrics
1 parent 0694e5d commit ac3478c

File tree

1 file changed

+120
-99
lines changed

1 file changed

+120
-99
lines changed

pkg/resourcegroup/controllers/resourcemap/update_metrics_test.go

Lines changed: 120 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -29,141 +29,162 @@ import (
2929
"k8s.io/apimachinery/pkg/types"
3030
)
3131

32+
type testOperation struct {
33+
group types.NamespacedName
34+
resources []v1alpha1.ObjMetadata
35+
isDelete bool
36+
}
37+
3238
func TestResourceMapUpdateMetrics(t *testing.T) {
3339
ctx := context.Background()
3440

3541
testcases := []struct {
3642
name string
37-
group types.NamespacedName
38-
resources []v1alpha1.ObjMetadata
39-
isDelete bool
43+
operations []testOperation
4044
expectedMetricValue float64
4145
expectedResourceGroups int
4246
}{
4347
{
44-
name: "Add single resource group",
45-
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
46-
resources: []v1alpha1.ObjMetadata{
48+
name: "Add single resource group",
49+
operations: []testOperation{
4750
{
48-
Name: "test-deployment",
49-
Namespace: "default",
50-
GroupKind: v1alpha1.GroupKind{
51-
Group: "apps",
52-
Kind: "Deployment",
51+
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
52+
resources: []v1alpha1.ObjMetadata{
53+
{
54+
Name: "test-deployment",
55+
Namespace: "default",
56+
GroupKind: v1alpha1.GroupKind{
57+
Group: "apps",
58+
Kind: "Deployment",
59+
},
60+
},
5361
},
62+
isDelete: false,
5463
},
5564
},
56-
isDelete: false,
5765
expectedMetricValue: 1,
5866
expectedResourceGroups: 1,
5967
},
6068
{
61-
name: "Add second resource group",
62-
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
63-
resources: []v1alpha1.ObjMetadata{
69+
name: "Add multiple resource groups and delete them",
70+
operations: []testOperation{
6471
{
65-
Name: "test-service",
66-
Namespace: "bookinfo",
67-
GroupKind: v1alpha1.GroupKind{
68-
Group: "",
69-
Kind: "Service",
72+
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
73+
resources: []v1alpha1.ObjMetadata{
74+
{
75+
Name: "test-deployment",
76+
Namespace: "default",
77+
GroupKind: v1alpha1.GroupKind{
78+
Group: "apps",
79+
Kind: "Deployment",
80+
},
81+
},
82+
},
83+
isDelete: false,
84+
},
85+
{
86+
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
87+
resources: []v1alpha1.ObjMetadata{
88+
{
89+
Name: "test-service",
90+
Namespace: "bookinfo",
91+
GroupKind: v1alpha1.GroupKind{
92+
Group: "",
93+
Kind: "Service",
94+
},
95+
},
7096
},
97+
isDelete: false,
98+
},
99+
{
100+
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
101+
resources: []v1alpha1.ObjMetadata{},
102+
isDelete: true,
103+
},
104+
{
105+
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
106+
resources: []v1alpha1.ObjMetadata{},
107+
isDelete: true,
71108
},
72109
},
73-
isDelete: false,
74-
expectedMetricValue: 2, // root-sync + repo-sync
75-
expectedResourceGroups: 2,
110+
expectedMetricValue: 0, // All resource groups deleted
111+
expectedResourceGroups: 0,
76112
},
77113
{
78-
name: "Delete first resource group",
79-
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
80-
resources: []v1alpha1.ObjMetadata{},
81-
isDelete: true,
82-
expectedMetricValue: 1, // Only repo-sync remains
114+
name: "Add resource group with multiple resources",
115+
operations: []testOperation{
116+
{
117+
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
118+
resources: []v1alpha1.ObjMetadata{
119+
{
120+
Name: "test-service",
121+
Namespace: "bookinfo",
122+
GroupKind: v1alpha1.GroupKind{
123+
Group: "",
124+
Kind: "Service",
125+
},
126+
},
127+
{
128+
Name: "test-deployment",
129+
Namespace: "bookinfo",
130+
GroupKind: v1alpha1.GroupKind{
131+
Group: "apps",
132+
Kind: "Deployment",
133+
},
134+
},
135+
},
136+
isDelete: false,
137+
},
138+
},
139+
expectedMetricValue: 1, // 1 resource group with multiple resources
83140
expectedResourceGroups: 1,
84141
},
85142
{
86-
name: "Delete remaining resource group",
87-
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
88-
resources: []v1alpha1.ObjMetadata{},
89-
isDelete: true,
90-
expectedMetricValue: 0, // No resource groups remain
91-
expectedResourceGroups: 0,
92-
},
93-
{
94-
name: "Add resource group with multiple resources",
95-
group: types.NamespacedName{Name: "repo-sync", Namespace: "bookinfo"},
96-
resources: []v1alpha1.ObjMetadata{
143+
name: "Add and update resource group",
144+
operations: []testOperation{
97145
{
98-
Name: "test-service",
99-
Namespace: "bookinfo",
100-
GroupKind: v1alpha1.GroupKind{
101-
Group: "",
102-
Kind: "Service",
146+
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
147+
resources: []v1alpha1.ObjMetadata{
148+
{
149+
Name: "test-deployment",
150+
Namespace: "default",
151+
GroupKind: v1alpha1.GroupKind{
152+
Group: "apps",
153+
Kind: "Deployment",
154+
},
155+
},
103156
},
157+
isDelete: false,
104158
},
105159
{
106-
Name: "test-deployment",
107-
Namespace: "bookinfo",
108-
GroupKind: v1alpha1.GroupKind{
109-
Group: "apps",
110-
Kind: "Deployment",
160+
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
161+
resources: []v1alpha1.ObjMetadata{
162+
{
163+
Name: "test-deployment",
164+
Namespace: "default",
165+
GroupKind: v1alpha1.GroupKind{
166+
Group: "apps",
167+
Kind: "Deployment",
168+
},
169+
},
170+
{
171+
Name: "test-service",
172+
Namespace: "default",
173+
GroupKind: v1alpha1.GroupKind{
174+
Group: "",
175+
Kind: "Service",
176+
},
177+
},
111178
},
179+
isDelete: false,
112180
},
113181
},
114-
isDelete: false,
115-
expectedMetricValue: 1, // 1 resource group with multiple resources
182+
expectedMetricValue: 1, // Same resource group, now with 2 resources
116183
expectedResourceGroups: 1,
117184
},
118-
}
119-
120-
// Create a single resource map for the entire test
121-
m := NewResourceMap()
122-
123-
for _, tc := range testcases {
124-
t.Run(tc.name, func(t *testing.T) {
125-
// Register metrics views with test exporter for this test
126-
exporter := testmetrics.RegisterMetrics(
127-
metrics.ResourceGroupTotalView,
128-
)
129-
130-
_ = m.Reconcile(ctx, tc.group, tc.resources, tc.isDelete)
131-
132-
expected := []*view.Row{
133-
{Data: &view.LastValueData{Value: tc.expectedMetricValue}, Tags: []tag.Tag{}},
134-
}
135-
136-
if diff := exporter.ValidateMetrics(metrics.ResourceGroupTotalView, expected); diff != "" {
137-
t.Errorf("Unexpected metrics recorded: %v", diff)
138-
}
139-
140-
if len(m.resgroupToResources) != tc.expectedResourceGroups {
141-
t.Errorf("Expected %d resource groups in map, got %d", tc.expectedResourceGroups, len(m.resgroupToResources))
142-
}
143-
})
144-
}
145-
}
146-
147-
func TestResourceMapMultipleUpdates(t *testing.T) {
148-
ctx := context.Background()
149-
150-
testcases := []struct {
151-
name string
152-
operations []struct {
153-
group types.NamespacedName
154-
resources []v1alpha1.ObjMetadata
155-
isDelete bool
156-
}
157-
expectedMetricValue float64
158-
expectedResourceGroups int
159-
}{
160185
{
161186
name: "Add multiple resource groups sequentially",
162-
operations: []struct {
163-
group types.NamespacedName
164-
resources []v1alpha1.ObjMetadata
165-
isDelete bool
166-
}{
187+
operations: []testOperation{
167188
{
168189
group: types.NamespacedName{Name: "root-sync", Namespace: configsync.ControllerNamespace},
169190
resources: []v1alpha1.ObjMetadata{
@@ -219,7 +240,7 @@ func TestResourceMapMultipleUpdates(t *testing.T) {
219240
metrics.ResourceGroupTotalView,
220241
)
221242

222-
// Create a new resource map
243+
// Create a new resource map for each test case
223244
m := NewResourceMap()
224245

225246
// Execute all operations

0 commit comments

Comments
 (0)