@@ -370,6 +370,134 @@ var _ = Describe("Resources", func() {
370370
371371 })
372372
373+ Context ("PulsarTopic Compaction Threshold" , Ordered , func () {
374+ var (
375+ compactionTopic * v1alphav1.PulsarTopic
376+ compactionTopicName string = "test-compaction-topic"
377+ compactionThreshold int64 = 104857600 // 100MB in bytes
378+ )
379+
380+ BeforeEach (func () {
381+ compactionTopic = utils .MakePulsarTopicWithCompactionThreshold (
382+ namespaceName ,
383+ compactionTopicName ,
384+ "persistent://cloud/stage/compaction-test" ,
385+ pconnName ,
386+ compactionThreshold ,
387+ lifecyclePolicy ,
388+ )
389+ })
390+
391+ It ("should create topic with compaction threshold successfully" , func () {
392+ err := k8sClient .Create (ctx , compactionTopic )
393+ Expect (err == nil || apierrors .IsAlreadyExists (err )).Should (BeTrue ())
394+ })
395+
396+ It ("should be ready" , func () {
397+ Eventually (func () bool {
398+ t := & v1alphav1.PulsarTopic {}
399+ tns := types.NamespacedName {Namespace : namespaceName , Name : compactionTopicName }
400+ Expect (k8sClient .Get (ctx , tns , t )).Should (Succeed ())
401+ return v1alphav1 .IsPulsarResourceReady (t )
402+ }, "20s" , "100ms" ).Should (BeTrue ())
403+ })
404+
405+ It ("should have compaction threshold set in Pulsar" , func () {
406+ Eventually (func (g Gomega ) {
407+ podName := fmt .Sprintf ("%s-broker-0" , brokerName )
408+ containerName := fmt .Sprintf ("%s-broker" , brokerName )
409+ stdout , _ , err := utils .ExecInPod (k8sConfig , namespaceName , podName , containerName ,
410+ "./bin/pulsar-admin topics get-compaction-threshold " + compactionTopic .Spec .Name )
411+ g .Expect (err ).Should (Succeed ())
412+ g .Expect (stdout ).Should (Not (BeEmpty ()))
413+ // The output should contain the threshold value in bytes
414+ g .Expect (stdout ).Should (ContainSubstring ("104857600" ))
415+ }, "20s" , "100ms" ).Should (Succeed ())
416+ })
417+
418+ It ("should update compaction threshold successfully" , func () {
419+ newThreshold := int64 (209715200 ) // 200MB in bytes
420+
421+ topic := & v1alphav1.PulsarTopic {}
422+ tns := types.NamespacedName {Namespace : namespaceName , Name : compactionTopicName }
423+ Expect (k8sClient .Get (ctx , tns , topic )).Should (Succeed ())
424+
425+ topic .Spec .CompactionThreshold = & newThreshold
426+ err := k8sClient .Update (ctx , topic )
427+ Expect (err ).Should (Succeed ())
428+ })
429+
430+ It ("should be ready after update" , func () {
431+ Eventually (func () bool {
432+ t := & v1alphav1.PulsarTopic {}
433+ tns := types.NamespacedName {Namespace : namespaceName , Name : compactionTopicName }
434+ Expect (k8sClient .Get (ctx , tns , t )).Should (Succeed ())
435+ return v1alphav1 .IsPulsarResourceReady (t )
436+ }, "20s" , "100ms" ).Should (BeTrue ())
437+ })
438+
439+ It ("should have updated compaction threshold in Pulsar" , func () {
440+ Eventually (func (g Gomega ) {
441+ podName := fmt .Sprintf ("%s-broker-0" , brokerName )
442+ containerName := fmt .Sprintf ("%s-broker" , brokerName )
443+ stdout , _ , err := utils .ExecInPod (k8sConfig , namespaceName , podName , containerName ,
444+ "./bin/pulsar-admin topics get-compaction-threshold " + compactionTopic .Spec .Name )
445+ g .Expect (err ).Should (Succeed ())
446+ g .Expect (stdout ).Should (Not (BeEmpty ()))
447+ // The output should contain the new threshold value in bytes
448+ g .Expect (stdout ).Should (ContainSubstring ("209715200" ))
449+ }, "20s" , "100ms" ).Should (Succeed ())
450+ })
451+
452+ It ("should remove compaction threshold when set to nil" , func () {
453+ topic := & v1alphav1.PulsarTopic {}
454+ tns := types.NamespacedName {Namespace : namespaceName , Name : compactionTopicName }
455+ Expect (k8sClient .Get (ctx , tns , topic )).Should (Succeed ())
456+
457+ topic .Spec .CompactionThreshold = nil
458+ err := k8sClient .Update (ctx , topic )
459+ Expect (err ).Should (Succeed ())
460+ })
461+
462+ It ("should be ready after removing threshold" , func () {
463+ Eventually (func () bool {
464+ t := & v1alphav1.PulsarTopic {}
465+ tns := types.NamespacedName {Namespace : namespaceName , Name : compactionTopicName }
466+ Expect (k8sClient .Get (ctx , tns , t )).Should (Succeed ())
467+ return v1alphav1 .IsPulsarResourceReady (t )
468+ }, "20s" , "100ms" ).Should (BeTrue ())
469+ })
470+
471+ It ("should have no compaction threshold in Pulsar after removal" , func () {
472+ Eventually (func (g Gomega ) {
473+ podName := fmt .Sprintf ("%s-broker-0" , brokerName )
474+ containerName := fmt .Sprintf ("%s-broker" , brokerName )
475+ stdout , stderr , err := utils .ExecInPod (k8sConfig , namespaceName , podName , containerName ,
476+ "./bin/pulsar-admin topics get-compaction-threshold " + compactionTopic .Spec .Name )
477+ // When no compaction threshold is set, the command might fail or return empty/default value
478+ // We need to check both success and failure cases
479+ if err != nil {
480+ // Command failed, which might be expected when no threshold is set
481+ g .Expect (stderr ).Should (ContainSubstring ("not found" ))
482+ } else {
483+ // Command succeeded but should show no threshold or default value
484+ g .Expect (stdout ).Should (SatisfyAny (
485+ BeEmpty (),
486+ ContainSubstring ("0" ),
487+ ContainSubstring ("-1" ),
488+ ))
489+ }
490+ }, "20s" , "100ms" ).Should (Succeed ())
491+ })
492+
493+ AfterEach (func () {
494+ // Clean up the compaction test topic
495+ if compactionTopic != nil {
496+ k8sClient .Delete (ctx , compactionTopic )
497+ }
498+ })
499+ })
500+
373501 Context ("PulsarPermission operation" , Label ("Permissions" ), func () {
374502 It ("should grant the pulsarpermission successfully" , func () {
375503 err := k8sClient .Create (ctx , ppermission )
0 commit comments