|
| 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 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 26 | + |
| 27 | + controllerpkg "github.com/cozystack/blockstor/internal/controller" |
| 28 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 29 | + "github.com/cozystack/blockstor/pkg/store" |
| 30 | +) |
| 31 | + |
| 32 | +// TestRGPlaceCountBumpFillsGap: bumping place_count from 2 to 3 on |
| 33 | +// the parent RG triggers the placer to backfill the missing replica |
| 34 | +// for every spawned RD on the next RG reconcile. Existing replicas |
| 35 | +// keep their nodes — placer.Place treats them as already-placed. |
| 36 | +func TestRGPlaceCountBumpFillsGap(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + ctx := context.Background() |
| 40 | + scheme := newScheme(t) |
| 41 | + cli := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 42 | + st := store.NewInMemory() |
| 43 | + |
| 44 | + for _, n := range []string{"n1", "n2", "n3"} { |
| 45 | + _ = st.Nodes().Create(ctx, &apiv1.Node{Name: n, Type: apiv1.NodeTypeSatellite}) |
| 46 | + _ = st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 47 | + StoragePoolName: "pool", |
| 48 | + NodeName: n, |
| 49 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + if err := st.ResourceGroups().Create(ctx, &apiv1.ResourceGroup{ |
| 54 | + Name: "rg", |
| 55 | + SelectFilter: apiv1.AutoSelectFilter{ |
| 56 | + PlaceCount: 2, |
| 57 | + StoragePool: "pool", |
| 58 | + }, |
| 59 | + }); err != nil { |
| 60 | + t.Fatalf("seed rg: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{ |
| 64 | + Name: "pvc-prop", |
| 65 | + ResourceGroupName: "rg", |
| 66 | + }); err != nil { |
| 67 | + t.Fatalf("seed rd: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + for _, n := range []string{"n1", "n2"} { |
| 71 | + _ = st.Resources().Create(ctx, &apiv1.Resource{Name: "pvc-prop", NodeName: n}) |
| 72 | + } |
| 73 | + |
| 74 | + // Bump place_count on the RG. |
| 75 | + updated := apiv1.ResourceGroup{ |
| 76 | + Name: "rg", |
| 77 | + SelectFilter: apiv1.AutoSelectFilter{ |
| 78 | + PlaceCount: 3, |
| 79 | + StoragePool: "pool", |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + if err := st.ResourceGroups().Update(ctx, &updated); err != nil { |
| 84 | + t.Fatalf("update rg: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + rec := &controllerpkg.ResourceGroupReconciler{Client: cli, Scheme: scheme, Store: st} |
| 88 | + |
| 89 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "rg"}}) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Reconcile: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + got, err := st.Resources().ListByDefinition(ctx, "pvc-prop") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("list: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + if len(got) != 3 { |
| 100 | + t.Fatalf("replica count after bump: got %d, want 3; entries=%v", len(got), got) |
| 101 | + } |
| 102 | + |
| 103 | + nodes := map[string]bool{} |
| 104 | + for _, r := range got { |
| 105 | + nodes[r.NodeName] = true |
| 106 | + } |
| 107 | + |
| 108 | + if !nodes["n1"] || !nodes["n2"] || !nodes["n3"] { |
| 109 | + t.Errorf("expected replicas on n1+n2+n3 after bump; got %v", got) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// TestRGUpdateNoChangeNoOp: re-running the reconciler on an unchanged |
| 114 | +// RG doesn't churn anything. Idempotency for the periodic resync. |
| 115 | +func TestRGUpdateNoChangeNoOp(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + ctx := context.Background() |
| 119 | + scheme := newScheme(t) |
| 120 | + cli := fake.NewClientBuilder().WithScheme(scheme).Build() |
| 121 | + st := store.NewInMemory() |
| 122 | + |
| 123 | + for _, n := range []string{"n1", "n2"} { |
| 124 | + _ = st.Nodes().Create(ctx, &apiv1.Node{Name: n, Type: apiv1.NodeTypeSatellite}) |
| 125 | + _ = st.StoragePools().Create(ctx, &apiv1.StoragePool{ |
| 126 | + StoragePoolName: "pool", |
| 127 | + NodeName: n, |
| 128 | + ProviderKind: apiv1.StoragePoolKindLVMThin, |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + _ = st.ResourceGroups().Create(ctx, &apiv1.ResourceGroup{ |
| 133 | + Name: "rg", |
| 134 | + SelectFilter: apiv1.AutoSelectFilter{PlaceCount: 2, StoragePool: "pool"}, |
| 135 | + }) |
| 136 | + _ = st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{ |
| 137 | + Name: "pvc-stable", |
| 138 | + ResourceGroupName: "rg", |
| 139 | + }) |
| 140 | + |
| 141 | + for _, n := range []string{"n1", "n2"} { |
| 142 | + _ = st.Resources().Create(ctx, &apiv1.Resource{Name: "pvc-stable", NodeName: n}) |
| 143 | + } |
| 144 | + |
| 145 | + rec := &controllerpkg.ResourceGroupReconciler{Client: cli, Scheme: scheme, Store: st} |
| 146 | + |
| 147 | + for range 3 { |
| 148 | + _, err := rec.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: "rg"}}) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("Reconcile: %v", err) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + got, _ := st.Resources().ListByDefinition(ctx, "pvc-stable") |
| 155 | + if len(got) != 2 { |
| 156 | + t.Errorf("replica count drifted: got %d, want 2; entries=%v", len(got), got) |
| 157 | + } |
| 158 | +} |
0 commit comments