Skip to content

Commit f41a500

Browse files
committed
test(controller): cover invalid bucket class label in reconcileBucketClaimForMonitor
A BucketClassName longer than 63 chars (valid as a Kubernetes object name, up to 253 chars) is not a valid label value, so the bucket-class label is skipped with an Info log rather than failing the Workload write. This can happen with SeaweedFS pool class names (<namespace>-<pool>-lock) when the tenant and pool names are both long -- a missing label in that case does not mean no class was set. Signed-off-by: harshitrwt <[email protected]>
1 parent 882929f commit f41a500

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

‎internal/controller/workloadmonitor_controller_test.go‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,73 @@ func TestReconcileBucketClaim_BucketClassLabelPropagated(t *testing.T) {
529529
}
530530
}
531531

532+
func TestReconcileBucketClaim_InvalidBucketClassSkipsLabel(t *testing.T) {
533+
s := newTestScheme()
534+
535+
monitor := &cozyv1alpha1.WorkloadMonitor{
536+
ObjectMeta: metav1.ObjectMeta{
537+
Name: "my-bucket",
538+
Namespace: "tenant-demo",
539+
},
540+
Spec: cozyv1alpha1.WorkloadMonitorSpec{
541+
Kind: "bucket",
542+
Type: "s3",
543+
Selector: map[string]string{
544+
"app.kubernetes.io/instance": "my-bucket",
545+
},
546+
},
547+
}
548+
549+
overlong := strings.Repeat("a", 64)
550+
bc := &cosiv1alpha1.BucketClaim{
551+
ObjectMeta: metav1.ObjectMeta{
552+
Name: "my-bucket",
553+
Namespace: "tenant-demo",
554+
Labels: map[string]string{
555+
"app.kubernetes.io/instance": "my-bucket",
556+
},
557+
},
558+
Spec: cosiv1alpha1.BucketClaimSpec{
559+
BucketClassName: overlong,
560+
Protocols: []cosiv1alpha1.Protocol{cosiv1alpha1.ProtocolS3},
561+
},
562+
Status: cosiv1alpha1.BucketClaimStatus{
563+
BucketReady: true,
564+
BucketName: "cosi-abc123",
565+
},
566+
}
567+
568+
fakeClient := fake.NewClientBuilder().
569+
WithScheme(s).
570+
WithObjects(monitor, bc).
571+
WithStatusSubresource(monitor).
572+
Build()
573+
574+
reconciler := &WorkloadMonitorReconciler{Client: fakeClient, Scheme: s}
575+
req := reconcile.Request{NamespacedName: types.NamespacedName{
576+
Name: "my-bucket",
577+
Namespace: "tenant-demo",
578+
}}
579+
580+
_, err := reconciler.Reconcile(context.TODO(), req)
581+
if err != nil {
582+
t.Fatalf("Reconcile returned error: %v", err)
583+
}
584+
585+
workload := &cozyv1alpha1.Workload{}
586+
err = fakeClient.Get(context.TODO(), types.NamespacedName{
587+
Name: "bucket-my-bucket",
588+
Namespace: "tenant-demo",
589+
}, workload)
590+
if err != nil {
591+
t.Fatalf("expected Workload to be created despite invalid bucket class, got error: %v", err)
592+
}
593+
594+
if v, ok := workload.Labels["workloads.cozystack.io/bucket-class"]; ok {
595+
t.Errorf("expected bucket-class label to be skipped for an invalid label value, got %q", v)
596+
}
597+
}
598+
532599
func TestReconcileBucketClaimNotReady(t *testing.T) {
533600
s := newTestScheme()
534601

0 commit comments

Comments
 (0)