|
| 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 | + "testing" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 27 | + |
| 28 | + blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1" |
| 29 | + controllerpkg "github.com/cozystack/blockstor/internal/controller" |
| 30 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 31 | + "github.com/cozystack/blockstor/pkg/store" |
| 32 | +) |
| 33 | + |
| 34 | +// TestNodeReconciler_EvictedTriggersMigration: when a Node is flagged |
| 35 | +// EVICTED, every Resource on it gets a replacement created on a |
| 36 | +// non-evicted node. Existing peer count + new replica must match the |
| 37 | +// RG's place_count. |
| 38 | +func TestNodeReconciler_EvictedTriggersMigration(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + |
| 41 | + ctx := context.Background() |
| 42 | + scheme := newScheme(t) |
| 43 | + cli := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 44 | + |
| 45 | + st := store.NewInMemory() |
| 46 | + |
| 47 | + // Two storage pools on three nodes; n1 is the evicted one. |
| 48 | + for _, name := range []string{"n1", "n2", "n3"} { |
| 49 | + if err := st.Nodes().Create(ctx, &apiv1.Node{Name: name, Type: apiv1.NodeTypeSatellite}); err != nil { |
| 50 | + t.Fatalf("seed node: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 54 | + StoragePoolName: "pool", |
| 55 | + NodeName: name, |
| 56 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 57 | + }); err != nil { |
| 58 | + t.Fatalf("seed pool: %v", err) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // RG with place_count=2. |
| 63 | + if err := st.ResourceGroups().Create(ctx, &apiv1.ResourceGroup{ |
| 64 | + Name: "rg", |
| 65 | + SelectFilter: apiv1.AutoSelectFilter{ |
| 66 | + PlaceCount: 2, |
| 67 | + StoragePool: "pool", |
| 68 | + }, |
| 69 | + }); err != nil { |
| 70 | + t.Fatalf("seed RG: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{ |
| 74 | + Name: "pvc-1", |
| 75 | + ResourceGroupName: "rg", |
| 76 | + }); err != nil { |
| 77 | + t.Fatalf("seed RD: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Two replicas exist: n1 + n2. We're going to evict n1. |
| 81 | + for _, node := range []string{"n1", "n2"} { |
| 82 | + if err := st.Resources().Create(ctx, &apiv1.Resource{Name: "pvc-1", NodeName: node}); err != nil { |
| 83 | + t.Fatalf("seed resource: %v", err) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Mark n1 evicted via the store + Node CRD on the fake client. |
| 88 | + n1 := &apiv1.Node{ |
| 89 | + Name: "n1", |
| 90 | + Type: apiv1.NodeTypeSatellite, |
| 91 | + Flags: []string{apiv1.NodeFlagEvicted}, |
| 92 | + } |
| 93 | + |
| 94 | + if err := st.Nodes().Update(ctx, n1); err != nil { |
| 95 | + t.Fatalf("flag n1 evicted: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + if err := cli.Create(ctx, &blockstoriov1alpha1.Node{ |
| 99 | + ObjectMeta: metav1.ObjectMeta{Name: "n1"}, |
| 100 | + Spec: blockstoriov1alpha1.NodeSpec{ |
| 101 | + Type: apiv1.NodeTypeSatellite, |
| 102 | + Flags: []string{apiv1.NodeFlagEvicted}, |
| 103 | + }, |
| 104 | + }); err != nil { |
| 105 | + t.Fatalf("create n1 CRD: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + rec := &controllerpkg.NodeReconciler{Client: cli, Scheme: scheme, Store: st} |
| 109 | + |
| 110 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "n1"}}) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Reconcile: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + got, err := st.Resources().ListByDefinition(ctx, "pvc-1") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("list: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + // EVICTED is the soft "drain me" hint: the migration adds a |
| 121 | + // replacement on a healthy node but leaves the source replica |
| 122 | + // in place — the operator decides when to actually remove it |
| 123 | + // (typically once the new replica is UpToDate). For LOST, the |
| 124 | + // source is deleted in the same reconcile pass. |
| 125 | + // |
| 126 | + // So after one EVICTED reconcile we expect 3 replicas: original |
| 127 | + // n1 + n2 + the freshly placed replacement on n3. |
| 128 | + if len(got) != 3 { |
| 129 | + t.Fatalf("replica count: got %d, want 3; entries=%v", len(got), got) |
| 130 | + } |
| 131 | + |
| 132 | + gotNodes := map[string]bool{} |
| 133 | + for _, r := range got { |
| 134 | + gotNodes[r.NodeName] = true |
| 135 | + } |
| 136 | + |
| 137 | + for _, want := range []string{"n1", "n2", "n3"} { |
| 138 | + if !gotNodes[want] { |
| 139 | + t.Errorf("expected replica on %s after migration; got %v", want, got) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// TestNodeReconciler_LostDeletesSourceResource: when a Node is flagged |
| 145 | +// LOST, the Resource on it is deleted via the K8s API in addition to |
| 146 | +// the migration trigger. |
| 147 | +func TestNodeReconciler_LostDeletesSourceResource(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + |
| 150 | + ctx := context.Background() |
| 151 | + scheme := newScheme(t) |
| 152 | + |
| 153 | + resCRD := &blockstoriov1alpha1.Resource{ |
| 154 | + ObjectMeta: metav1.ObjectMeta{Name: "pvc-1.n1"}, |
| 155 | + Spec: blockstoriov1alpha1.ResourceSpec{ |
| 156 | + ResourceDefinitionName: "pvc-1", |
| 157 | + NodeName: "n1", |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + cli := fake.NewClientBuilder().WithScheme(scheme). |
| 162 | + WithObjects( |
| 163 | + &blockstoriov1alpha1.Node{ |
| 164 | + ObjectMeta: metav1.ObjectMeta{Name: "n1"}, |
| 165 | + Spec: blockstoriov1alpha1.NodeSpec{ |
| 166 | + Type: apiv1.NodeTypeSatellite, |
| 167 | + Flags: []string{apiv1.NodeFlagEvicted, apiv1.NodeFlagLost}, |
| 168 | + }, |
| 169 | + }, |
| 170 | + resCRD, |
| 171 | + ). |
| 172 | + Build() |
| 173 | + |
| 174 | + st := store.NewInMemory() |
| 175 | + |
| 176 | + for _, name := range []string{"n1", "n2"} { |
| 177 | + _ = st.Nodes().Create(ctx, &apiv1.Node{Name: name, Type: apiv1.NodeTypeSatellite}) |
| 178 | + _ = st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 179 | + StoragePoolName: "pool", |
| 180 | + NodeName: name, |
| 181 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 182 | + }) |
| 183 | + } |
| 184 | + |
| 185 | + _ = st.Nodes().Update(ctx, &apiv1.Node{ |
| 186 | + Name: "n1", |
| 187 | + Type: apiv1.NodeTypeSatellite, |
| 188 | + Flags: []string{apiv1.NodeFlagEvicted, apiv1.NodeFlagLost}, |
| 189 | + }) |
| 190 | + _ = st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{Name: "pvc-1"}) |
| 191 | + _ = st.Resources().Create(ctx, &apiv1.Resource{Name: "pvc-1", NodeName: "n1"}) |
| 192 | + |
| 193 | + rec := &controllerpkg.NodeReconciler{Client: cli, Scheme: scheme, Store: st} |
| 194 | + |
| 195 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "n1"}}) |
| 196 | + if err != nil { |
| 197 | + t.Fatalf("Reconcile: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + // The Resource CRD on the lost node must be gone (or have |
| 201 | + // DeletionTimestamp set if it had a finalizer). |
| 202 | + got := &blockstoriov1alpha1.Resource{} |
| 203 | + |
| 204 | + err = cli.Get(ctx, types.NamespacedName{Name: "pvc-1.n1"}, got) |
| 205 | + if err == nil && got.DeletionTimestamp.IsZero() { |
| 206 | + t.Errorf("expected Resource on LOST node to be deleted; still present: %v", got) |
| 207 | + } |
| 208 | +} |
0 commit comments