|
| 1 | +/* |
| 2 | +Copyright 2026 Cozystack contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "slices" |
| 23 | + "testing" |
| 24 | + |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 29 | + |
| 30 | + blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1" |
| 31 | + controllerpkg "github.com/cozystack/blockstor/internal/controller" |
| 32 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 33 | + "github.com/cozystack/blockstor/pkg/dispatcher" |
| 34 | + satellitepb "github.com/cozystack/blockstor/pkg/satellite/proto" |
| 35 | + "github.com/cozystack/blockstor/pkg/store" |
| 36 | +) |
| 37 | + |
| 38 | +// noopDialer always fails the dial. The Reconcile path's |
| 39 | +// dispatchApply step then returns RequeueAfter without crashing — |
| 40 | +// fine for tests that only assert the pre-dispatch CRD mutations. |
| 41 | +type noopDialer struct{} |
| 42 | + |
| 43 | +func (noopDialer) Dial(_ context.Context, _ string) (satellitepb.SatelliteClient, func() error, error) { |
| 44 | + return nil, nil, errNoopDial |
| 45 | +} |
| 46 | + |
| 47 | +var errNoopDial = errors.New("auto_diskful_test: noop dialer") |
| 48 | + |
| 49 | +// TestAutoDisklessPromoted: a DISKLESS replica that becomes InUse on |
| 50 | +// a node with a viable storage pool gets promoted to diskful — the |
| 51 | +// reconciler removes the DISKLESS flag and stamps StorPoolName on |
| 52 | +// Spec.Props. The satellite reconciler picks up the change on its |
| 53 | +// next reconcile and creates the LV / runs drbdadm attach. |
| 54 | +func TestAutoDisklessPromoted(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + ctx := context.Background() |
| 58 | + scheme := newScheme(t) |
| 59 | + st := store.NewInMemory() |
| 60 | + |
| 61 | + _ = st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 62 | + StoragePoolName: "stand", |
| 63 | + NodeName: "n1", |
| 64 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 65 | + }) |
| 66 | + |
| 67 | + resCRD := &blockstoriov1alpha1.Resource{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{ |
| 69 | + Name: "pvc-promote.n1", |
| 70 | + Finalizers: []string{"blockstor.io.blockstor.io/resource"}, |
| 71 | + }, |
| 72 | + Spec: blockstoriov1alpha1.ResourceSpec{ |
| 73 | + ResourceDefinitionName: "pvc-promote", |
| 74 | + NodeName: "n1", |
| 75 | + Flags: []string{apiv1.ResourceFlagDiskless}, |
| 76 | + }, |
| 77 | + Status: blockstoriov1alpha1.ResourceStatus{InUse: true}, |
| 78 | + } |
| 79 | + |
| 80 | + cli := fake.NewClientBuilder().WithScheme(scheme). |
| 81 | + WithStatusSubresource(&blockstoriov1alpha1.Resource{}). |
| 82 | + WithObjects(resCRD). |
| 83 | + Build() |
| 84 | + |
| 85 | + rec := &controllerpkg.ResourceReconciler{ |
| 86 | + Client: cli, |
| 87 | + Scheme: scheme, |
| 88 | + Dispatcher: dispatcher.New(noopDialer{}), |
| 89 | + Store: st, |
| 90 | + } |
| 91 | + |
| 92 | + // Reconcile drives several converging passes (DRBD-id allocation |
| 93 | + // → Status update → requeue → auto-diskful → Spec update → |
| 94 | + // requeue → dispatchApply). Run until convergence so the final |
| 95 | + // state is what we assert on. |
| 96 | + for range 8 { |
| 97 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "pvc-promote.n1"}}) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Reconcile: %v", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + got := &blockstoriov1alpha1.Resource{} |
| 104 | + if err := cli.Get(ctx, types.NamespacedName{Name: "pvc-promote.n1"}, got); err != nil { |
| 105 | + t.Fatalf("get: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + if slices.Contains(got.Spec.Flags, apiv1.ResourceFlagDiskless) { |
| 109 | + t.Errorf("DISKLESS flag still present: %v", got.Spec.Flags) |
| 110 | + } |
| 111 | + |
| 112 | + if got.Spec.Props["StorPoolName"] != "stand" { |
| 113 | + t.Errorf("StorPoolName: got %q, want stand", got.Spec.Props["StorPoolName"]) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestAutoDisklessSkipsTiebreaker: a TIE_BREAKER witness must NEVER |
| 118 | +// be auto-promoted — its whole point is the network-only quorum |
| 119 | +// presence. Promoting would defeat the purpose and waste storage. |
| 120 | +func TestAutoDisklessSkipsTiebreaker(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + |
| 123 | + ctx := context.Background() |
| 124 | + scheme := newScheme(t) |
| 125 | + st := store.NewInMemory() |
| 126 | + |
| 127 | + _ = st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 128 | + StoragePoolName: "stand", |
| 129 | + NodeName: "n1", |
| 130 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 131 | + }) |
| 132 | + |
| 133 | + resCRD := &blockstoriov1alpha1.Resource{ |
| 134 | + ObjectMeta: metav1.ObjectMeta{ |
| 135 | + Name: "pvc-tb.n1", |
| 136 | + Finalizers: []string{"blockstor.io.blockstor.io/resource"}, |
| 137 | + }, |
| 138 | + Spec: blockstoriov1alpha1.ResourceSpec{ |
| 139 | + ResourceDefinitionName: "pvc-tb", |
| 140 | + NodeName: "n1", |
| 141 | + Flags: []string{apiv1.ResourceFlagDiskless, apiv1.ResourceFlagTieBreaker}, |
| 142 | + }, |
| 143 | + Status: blockstoriov1alpha1.ResourceStatus{InUse: true}, |
| 144 | + } |
| 145 | + |
| 146 | + cli := fake.NewClientBuilder().WithScheme(scheme). |
| 147 | + WithStatusSubresource(&blockstoriov1alpha1.Resource{}). |
| 148 | + WithObjects(resCRD). |
| 149 | + Build() |
| 150 | + |
| 151 | + rec := &controllerpkg.ResourceReconciler{ |
| 152 | + Client: cli, |
| 153 | + Scheme: scheme, |
| 154 | + Dispatcher: dispatcher.New(noopDialer{}), |
| 155 | + Store: st, |
| 156 | + } |
| 157 | + |
| 158 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "pvc-tb.n1"}}) |
| 159 | + if err != nil { |
| 160 | + t.Fatalf("Reconcile: %v", err) |
| 161 | + } |
| 162 | + |
| 163 | + got := &blockstoriov1alpha1.Resource{} |
| 164 | + _ = cli.Get(ctx, types.NamespacedName{Name: "pvc-tb.n1"}, got) |
| 165 | + |
| 166 | + if !slices.Contains(got.Spec.Flags, apiv1.ResourceFlagDiskless) { |
| 167 | + t.Errorf("DISKLESS dropped from a TIE_BREAKER replica: %v", got.Spec.Flags) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// TestAutoDisklessSkipsWhenNoPool: no storage pool on the hosting |
| 172 | +// node → leave the diskless replica alone (a viable promotion isn't |
| 173 | +// possible). |
| 174 | +func TestAutoDisklessSkipsWhenNoPool(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + |
| 177 | + ctx := context.Background() |
| 178 | + scheme := newScheme(t) |
| 179 | + st := store.NewInMemory() |
| 180 | + // No StoragePool created — the node has no local storage. |
| 181 | + |
| 182 | + resCRD := &blockstoriov1alpha1.Resource{ |
| 183 | + ObjectMeta: metav1.ObjectMeta{ |
| 184 | + Name: "pvc-nopool.n1", |
| 185 | + Finalizers: []string{"blockstor.io.blockstor.io/resource"}, |
| 186 | + }, |
| 187 | + Spec: blockstoriov1alpha1.ResourceSpec{ |
| 188 | + ResourceDefinitionName: "pvc-nopool", |
| 189 | + NodeName: "n1", |
| 190 | + Flags: []string{apiv1.ResourceFlagDiskless}, |
| 191 | + }, |
| 192 | + Status: blockstoriov1alpha1.ResourceStatus{InUse: true}, |
| 193 | + } |
| 194 | + |
| 195 | + cli := fake.NewClientBuilder().WithScheme(scheme). |
| 196 | + WithStatusSubresource(&blockstoriov1alpha1.Resource{}). |
| 197 | + WithObjects(resCRD). |
| 198 | + Build() |
| 199 | + |
| 200 | + rec := &controllerpkg.ResourceReconciler{ |
| 201 | + Client: cli, |
| 202 | + Scheme: scheme, |
| 203 | + Dispatcher: dispatcher.New(noopDialer{}), |
| 204 | + Store: st, |
| 205 | + } |
| 206 | + |
| 207 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "pvc-nopool.n1"}}) |
| 208 | + if err != nil { |
| 209 | + t.Fatalf("Reconcile: %v", err) |
| 210 | + } |
| 211 | + |
| 212 | + got := &blockstoriov1alpha1.Resource{} |
| 213 | + _ = cli.Get(ctx, types.NamespacedName{Name: "pvc-nopool.n1"}, got) |
| 214 | + |
| 215 | + if !slices.Contains(got.Spec.Flags, apiv1.ResourceFlagDiskless) { |
| 216 | + t.Errorf("DISKLESS dropped despite no available pool: %v", got.Spec.Flags) |
| 217 | + } |
| 218 | + |
| 219 | + if got.Spec.Props["StorPoolName"] != "" { |
| 220 | + t.Errorf("StorPoolName set without a pool: %v", got.Spec.Props) |
| 221 | + } |
| 222 | +} |
0 commit comments