@@ -20,17 +20,20 @@ import (
2020 "testing"
2121
2222 "github.com/stretchr/testify/assert"
23+ "github.com/stretchr/testify/require"
2324 "go.uber.org/mock/gomock"
2425 corev1 "k8s.io/api/core/v1"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/runtime"
28+ ctrl "sigs.k8s.io/controller-runtime"
2729 "sigs.k8s.io/controller-runtime/pkg/client"
28-
29- "github.com/banzaicloud/koperator/pkg/scale"
30+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3031
3132 banzaiv1alpha1 "github.com/banzaicloud/koperator/api/v1alpha1"
3233 "github.com/banzaicloud/koperator/api/v1beta1"
34+ scalemocks "github.com/banzaicloud/koperator/controllers/tests/mocks"
3335 "github.com/banzaicloud/koperator/pkg/resources/kafka/mocks"
36+ "github.com/banzaicloud/koperator/pkg/scale"
3437)
3538
3639func TestBrokersJBODSelector (t * testing.T ) {
@@ -426,3 +429,79 @@ func TestCreateCCOperation(t *testing.T) {
426429 testCase .parameterCheck (t , createdOperation .Status .CurrentTask .Parameters )
427430 }
428431}
432+
433+ func TestReconcileDiskRemovalMatchesExactLogDir (t * testing.T ) {
434+ const brokerID = "1"
435+ testCases := []struct {
436+ name string
437+ mountPath string
438+ onlineDirs []string
439+ wantTarget string
440+ }{
441+ {
442+ name : "sibling path reported first" ,
443+ mountPath : "/kafka-logs1" ,
444+ onlineDirs : []string {"/kafka-logs10/kafka" , "/kafka-logs1/kafka" },
445+ wantTarget : "1-/kafka-logs1/kafka" ,
446+ },
447+ {
448+ name : "Kafka normalizes a trailing separator" ,
449+ mountPath : "/kafka-logs1/" ,
450+ onlineDirs : []string {"/kafka-logs10/kafka" , "/kafka-logs1/kafka" },
451+ wantTarget : "1-/kafka-logs1/kafka" ,
452+ },
453+ {
454+ name : "requested path not online on this broker" ,
455+ mountPath : "/kafka-logs1" ,
456+ onlineDirs : []string {"/kafka-logs10/kafka" },
457+ },
458+ }
459+ for _ , tc := range testCases {
460+ t .Run (tc .name , func (t * testing.T ) {
461+ ctx := context .Background ()
462+ scheme := runtime .NewScheme ()
463+ require .NoError (t , v1beta1 .AddToScheme (scheme ))
464+ require .NoError (t , banzaiv1alpha1 .AddToScheme (scheme ))
465+ cluster := & v1beta1.KafkaCluster {
466+ ObjectMeta : metav1.ObjectMeta {Name : "kafka" , Namespace : "kafka" },
467+ Status : v1beta1.KafkaClusterStatus {BrokersState : map [string ]v1beta1.BrokerState {
468+ brokerID : {GracefulActionState : v1beta1.GracefulActionState {VolumeStates : map [string ]v1beta1.VolumeState {
469+ tc .mountPath : {CruiseControlVolumeState : v1beta1 .GracefulDiskRemovalRequired },
470+ }}},
471+ }},
472+ }
473+ kube := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (cluster ).
474+ WithStatusSubresource (cluster , & banzaiv1alpha1.CruiseControlOperation {}).Build ()
475+ scaler := scalemocks .NewMockCruiseControlScaler (gomock .NewController (t ))
476+ scaler .EXPECT ().LogDirsByBroker (ctx ).Return (map [string ]map [scale.LogDirState ][]string {
477+ brokerID : {scale .LogDirStateOnline : tc .onlineDirs },
478+ "2" : {scale .LogDirStateOnline : {"/kafka-logs1/kafka" }},
479+ }, nil )
480+ r := & CruiseControlTaskReconciler {
481+ Client : kube , DirectClient : kube , Scheme : scheme , ScaleFactory : scalemocks .NewMockScaleFactory (scaler ),
482+ }
483+
484+ key := client .ObjectKeyFromObject (cluster )
485+ _ , err := r .Reconcile (ctx , ctrl.Request {NamespacedName : key })
486+ operations := & banzaiv1alpha1.CruiseControlOperationList {}
487+ require .NoError (t , kube .List (ctx , operations , client .InNamespace (cluster .Namespace )))
488+ require .NoError (t , kube .Get (ctx , key , cluster ))
489+ state := cluster .Status .BrokersState [brokerID ].GracefulActionState .VolumeStates [tc .mountPath ]
490+ if tc .wantTarget == "" {
491+ assert .Error (t , err )
492+ assert .Empty (t , operations .Items )
493+ assert .Equal (t , v1beta1 .GracefulDiskRemovalRequired , state .CruiseControlVolumeState )
494+ assert .Nil (t , state .CruiseControlOperationReference )
495+ return
496+ }
497+ require .NoError (t , err )
498+ require .Len (t , operations .Items , 1 )
499+ operation := & operations .Items [0 ]
500+ assert .Equal (t , banzaiv1alpha1 .OperationRemoveDisks , operation .CurrentTaskOperation ())
501+ assert .Equal (t , tc .wantTarget , operation .CurrentTaskParameters ()[scale .ParamBrokerIDAndLogDirs ])
502+ assert .Equal (t , v1beta1 .GracefulDiskRemovalScheduled , state .CruiseControlVolumeState )
503+ require .NotNil (t , state .CruiseControlOperationReference )
504+ assert .Equal (t , operation .Name , state .CruiseControlOperationReference .Name )
505+ })
506+ }
507+ }
0 commit comments