Skip to content

Commit c134757

Browse files
zhujian7claude
andcommitted
Fix error assertion logic in registration tests and improve error handling
- Fix inverted error assertion logic in TestTemplateCSRConfigurationsFunc and TestTemplateCSRSignFunc - Change tests to properly check if expectedErr is empty vs non-empty - When no error expected, assert err == nil; when error expected, assert err != nil and contains substring - Fix strings.Contains argument order to check if actual error contains expected substring - Add nil template checks with proper error messages in CSRSign and PermissionConfig functions - Improve logging consistency with clusterName/addonName format across CSR functions - Guard against nil pointer access by checking err == nil before calling err.Error() 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Signed-off-by: zhujian <[email protected]>
1 parent 545b060 commit c134757

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

pkg/addon/templateagent/registration.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (a *CRDTemplateAgentAddon) TemplateCSRConfigurationsFunc() agent.CSRConfigu
116116
configs, err := agent.KubeClientSignerConfigurations(a.addonName, a.agentName)(cluster, addon)
117117
if err != nil {
118118
return nil, fmt.Errorf("failed to get kube signer config for %s/%s: %v",
119-
a.addonName, a.agentName, err)
119+
cluster.Name, a.addonName, err)
120120
}
121121
registrationConfigs = append(registrationConfigs, configs...)
122122
}
@@ -136,8 +136,8 @@ func (a *CRDTemplateAgentAddon) TemplateCSRConfigurationsFunc() agent.CSRConfigu
136136
}
137137

138138
default:
139-
a.logger.Info("CSRConfigurations unsupported registration type", "cluster", cluster.Name, "addon", a.addonName,
140-
"type", registration.Type)
139+
a.logger.Info("CSRConfigurations unsupported registration type",
140+
"clusterName", cluster.Name, "addonName", a.addonName, "type", registration.Type)
141141
}
142142

143143
}
@@ -178,10 +178,13 @@ func (a *CRDTemplateAgentAddon) TemplateCSRApproveCheckFunc() agent.CSRApproveFu
178178

179179
template, err := a.GetDesiredAddOnTemplate(addon, cluster.Name, a.addonName)
180180
if err != nil {
181-
a.logger.Info("CSRApproveCheck failed to get addon template", "addonName", a.addonName, "error", err)
181+
a.logger.Info("CSRApproveCheck failed to get addon template",
182+
"clusterName", cluster.Name, "addonName", a.addonName, "error", err)
182183
return false
183184
}
184185
if template == nil {
186+
a.logger.Info("CSRApproveCheck failed to get addon template, template is nil",
187+
"clusterName", cluster.Name, "addonName", a.addonName)
185188
return false
186189
}
187190

@@ -202,8 +205,8 @@ func (a *CRDTemplateAgentAddon) TemplateCSRApproveCheckFunc() agent.CSRApproveFu
202205
}
203206

204207
default:
205-
a.logger.Info("CSRApproveCheck unsupported registration type", "cluster", cluster.Name, "addon", a.addonName,
206-
"type", registration.Type)
208+
a.logger.Info("CSRApproveCheck unsupported registration type",
209+
"clusterName", cluster.Name, "addonName", a.addonName, "type", registration.Type)
207210
}
208211

209212
}
@@ -247,11 +250,12 @@ func (a *CRDTemplateAgentAddon) TemplateCSRSignFunc() agent.CSRSignerFunc {
247250
csr *certificatesv1.CertificateSigningRequest) ([]byte, error) {
248251
template, err := a.GetDesiredAddOnTemplate(addon, cluster.Name, a.addonName)
249252
if err != nil {
250-
return nil, fmt.Errorf("CSRSign failed to get template for addon %s in cluster %s: %v",
251-
a.addonName, cluster.Name, err)
253+
return nil, fmt.Errorf("CSRSign failed to get template for addon %s/%s: %v",
254+
cluster.Name, a.addonName, err)
252255
}
253256
if template == nil {
254-
return nil, nil
257+
return nil, fmt.Errorf("CSRSign failed to get addon template for addon %s/%s, template is nil",
258+
cluster.Name, a.addonName)
255259
}
256260

257261
for _, registration := range template.Spec.Registration {
@@ -268,8 +272,8 @@ func (a *CRDTemplateAgentAddon) TemplateCSRSignFunc() agent.CSRSignerFunc {
268272
}
269273

270274
default:
271-
a.logger.Info("CSRSign unsupported registration type", "cluster", cluster.Name, "addon", a.addonName,
272-
"type", registration.Type)
275+
a.logger.Info("CSRSign unsupported registration type",
276+
"clusterName", cluster.Name, "addonName", a.addonName, "type", registration.Type)
273277
}
274278

275279
}
@@ -286,7 +290,7 @@ func CustomSignerWithExpiry(
286290
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn,
287291
csr *certificatesv1.CertificateSigningRequest) ([]byte, error) {
288292
if customSignerConfig == nil {
289-
return nil, fmt.Errorf("custome signer config is nil")
293+
return nil, fmt.Errorf("custom signer config is nil")
290294
}
291295

292296
if csr.Spec.SignerName != customSignerConfig.SignerName {
@@ -300,13 +304,13 @@ func CustomSignerWithExpiry(
300304
caSecret, err := kubeclient.CoreV1().Secrets(secretNamespace).Get(
301305
context.TODO(), customSignerConfig.SigningCA.Name, metav1.GetOptions{})
302306
if err != nil {
303-
return nil, fmt.Errorf("get custome signer ca %s/%s failed, %v",
307+
return nil, fmt.Errorf("get custom signer ca %s/%s failed: %w",
304308
secretNamespace, customSignerConfig.SigningCA.Name, err)
305309
}
306310

307311
caData, caKey, err := extractCAdata(caSecret.Data[corev1.TLSCertKey], caSecret.Data[corev1.TLSPrivateKeyKey])
308312
if err != nil {
309-
return nil, fmt.Errorf("get ca %s/%s data failed, %v",
313+
return nil, fmt.Errorf("get ca %s/%s data failed: %w",
310314
secretNamespace, customSignerConfig.SigningCA.Name, err)
311315
}
312316
return utils.DefaultSignerWithExpiry(caKey, caData, duration)(cluster, addon, csr)
@@ -353,10 +357,12 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
353357
return func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) error {
354358
template, err := a.GetDesiredAddOnTemplate(addon, cluster.Name, a.addonName)
355359
if err != nil {
356-
return err
360+
return fmt.Errorf("PermissionConfig failed to get addon template for addon %s/%s: %v",
361+
cluster.Name, a.addonName, err)
357362
}
358363
if template == nil {
359-
return nil
364+
return fmt.Errorf("PermissionConfig failed to get addon template for addon %s/%s, template is nil",
365+
cluster.Name, a.addonName)
360366
}
361367

362368
for _, registration := range template.Spec.Registration {
@@ -376,8 +382,8 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
376382
continue
377383

378384
default:
379-
a.logger.Info("PermissionConfig unsupported registration type", "cluster", cluster.Name, "addon", a.addonName,
380-
"type", registration.Type)
385+
a.logger.Info("PermissionConfig unsupported registration type",
386+
"clusterName", cluster.Name, "addonName", a.addonName, "type", registration.Type)
381387
}
382388

383389
}

pkg/addon/templateagent/registration_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,16 @@ func TestTemplateCSRConfigurationsFunc(t *testing.T) {
141141
agent := NewCRDTemplateAgentAddon(ctx, c.addon.Name, nil, addonClient, addonInformerFactory, nil, nil)
142142
f := agent.TemplateCSRConfigurationsFunc()
143143
registrationConfigs, err := f(c.cluster, c.addon)
144-
if err != nil {
145-
if !strings.Contains(c.expectedErr, err.Error()) {
146-
t.Fatalf("case: %s, err: %v", c.name, err)
144+
if c.expectedErr == "" {
145+
if err != nil {
146+
t.Fatalf("case: %s, expected no error but got: %v", c.name, err)
147+
}
148+
} else {
149+
if err == nil {
150+
t.Fatalf("case: %s, expected error but got none", c.name)
151+
}
152+
if !strings.Contains(err.Error(), c.expectedErr) {
153+
t.Fatalf("case: %s, expected error containing %q but got: %v", c.name, c.expectedErr, err)
147154
}
148155
}
149156
if !equality.Semantic.DeepEqual(registrationConfigs, c.expectedConfigs) {
@@ -415,9 +422,16 @@ func TestTemplateCSRSignFunc(t *testing.T) {
415422
agent := NewCRDTemplateAgentAddon(ctx, c.addon.Name, hubKubeClient, addonClient, addonInformerFactory, nil, nil)
416423
f := agent.TemplateCSRSignFunc()
417424
cert, err := f(c.cluster, c.addon, c.csr)
418-
if err != nil {
419-
if !strings.Contains(c.expectedErr, err.Error()) {
420-
t.Fatalf("case: %s, err: %v", c.name, err)
425+
if c.expectedErr == "" {
426+
if err != nil {
427+
t.Fatalf("case: %s, expected no error but got: %v", c.name, err)
428+
}
429+
} else {
430+
if err == nil {
431+
t.Fatalf("case: %s, expected error but got none", c.name)
432+
}
433+
if !strings.Contains(err.Error(), c.expectedErr) {
434+
t.Fatalf("case: %s, expected error containing %q but got: %v", c.name, c.expectedErr, err)
421435
}
422436
}
423437
if !bytes.Equal(cert, c.expectedCert) {

0 commit comments

Comments
 (0)