|
| 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 rest |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + "slices" |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 25 | + "github.com/cozystack/blockstor/pkg/store" |
| 26 | +) |
| 27 | + |
| 28 | +// TestToggleDiskPromotesDiskless: PUT toggle-disk on a DISKLESS |
| 29 | +// replica drops the DISKLESS flag — the satellite picks the rest |
| 30 | +// of the work up from the auto-diskful path on its next reconcile. |
| 31 | +func TestToggleDiskPromotesDiskless(t *testing.T) { |
| 32 | + st := store.NewInMemory() |
| 33 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 34 | + t.Fatalf("seed RD: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + if err := st.Resources().Create(t.Context(), &apiv1.Resource{ |
| 38 | + Name: "pvc-1", |
| 39 | + NodeName: "n1", |
| 40 | + Flags: []string{apiv1.ResourceFlagDiskless}, |
| 41 | + }); err != nil { |
| 42 | + t.Fatalf("seed Resource: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + base, stop := startServerWithStore(t, st) |
| 46 | + defer stop() |
| 47 | + |
| 48 | + resp := httpPut(t, base+"/v1/resource-definitions/pvc-1/resources/n1/toggle-disk", nil) |
| 49 | + _ = resp.Body.Close() |
| 50 | + |
| 51 | + if resp.StatusCode != http.StatusOK { |
| 52 | + t.Errorf("status: got %d, want 200", resp.StatusCode) |
| 53 | + } |
| 54 | + |
| 55 | + got, err := st.Resources().Get(t.Context(), "pvc-1", "n1") |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("Get: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + if slices.Contains(got.Flags, apiv1.ResourceFlagDiskless) { |
| 61 | + t.Errorf("DISKLESS flag still present after toggle-disk: %v", got.Flags) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TestToggleDiskWithExplicitPool stamps the storage pool name on |
| 66 | +// the typed Spec.Props["StorPoolName"] when promoting via the |
| 67 | +// `/storage-pool/{pool}` path. Pins the upstream-LINSTOR-shaped |
| 68 | +// URL and verifies the controller's auto-diskful path won't have |
| 69 | +// to pick a pool. |
| 70 | +func TestToggleDiskWithExplicitPool(t *testing.T) { |
| 71 | + st := store.NewInMemory() |
| 72 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-pool"}); err != nil { |
| 73 | + t.Fatalf("seed RD: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if err := st.Resources().Create(t.Context(), &apiv1.Resource{ |
| 77 | + Name: "pvc-pool", |
| 78 | + NodeName: "n2", |
| 79 | + Flags: []string{apiv1.ResourceFlagDiskless}, |
| 80 | + }); err != nil { |
| 81 | + t.Fatalf("seed Resource: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + base, stop := startServerWithStore(t, st) |
| 85 | + defer stop() |
| 86 | + |
| 87 | + resp := httpPut(t, base+"/v1/resource-definitions/pvc-pool/resources/n2/toggle-disk/storage-pool/zfs-thin", nil) |
| 88 | + _ = resp.Body.Close() |
| 89 | + |
| 90 | + if resp.StatusCode != http.StatusOK { |
| 91 | + t.Errorf("status: got %d, want 200", resp.StatusCode) |
| 92 | + } |
| 93 | + |
| 94 | + got, err := st.Resources().Get(t.Context(), "pvc-pool", "n2") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Get: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + if got.Props["StorPoolName"] != "zfs-thin" { |
| 100 | + t.Errorf("Props[StorPoolName]: got %q, want zfs-thin", got.Props["StorPoolName"]) |
| 101 | + } |
| 102 | + |
| 103 | + if slices.Contains(got.Flags, apiv1.ResourceFlagDiskless) { |
| 104 | + t.Errorf("DISKLESS flag still present: %v", got.Flags) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// TestToggleDiskDemotesDiskful: PUT on a diskful replica adds the |
| 109 | +// DISKLESS flag — the satellite tears down the LV on its next |
| 110 | +// reconcile via the existing detach hook. |
| 111 | +func TestToggleDiskDemotesDiskful(t *testing.T) { |
| 112 | + st := store.NewInMemory() |
| 113 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-d"}); err != nil { |
| 114 | + t.Fatalf("seed RD: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + if err := st.Resources().Create(t.Context(), &apiv1.Resource{ |
| 118 | + Name: "pvc-d", |
| 119 | + NodeName: "n3", |
| 120 | + // no DISKLESS flag — currently diskful |
| 121 | + }); err != nil { |
| 122 | + t.Fatalf("seed Resource: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + base, stop := startServerWithStore(t, st) |
| 126 | + defer stop() |
| 127 | + |
| 128 | + resp := httpPut(t, base+"/v1/resource-definitions/pvc-d/resources/n3/toggle-disk", nil) |
| 129 | + _ = resp.Body.Close() |
| 130 | + |
| 131 | + if resp.StatusCode != http.StatusOK { |
| 132 | + t.Errorf("status: got %d, want 200", resp.StatusCode) |
| 133 | + } |
| 134 | + |
| 135 | + got, err := st.Resources().Get(t.Context(), "pvc-d", "n3") |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Get: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + if !slices.Contains(got.Flags, apiv1.ResourceFlagDiskless) { |
| 141 | + t.Errorf("DISKLESS flag missing after toggle-disk: %v", got.Flags) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestToggleDiskUnknownReplica: 404 on a missing (rd, node) pair. |
| 146 | +func TestToggleDiskUnknownReplica(t *testing.T) { |
| 147 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 148 | + defer stop() |
| 149 | + |
| 150 | + resp := httpPut(t, base+"/v1/resource-definitions/ghost/resources/n9/toggle-disk", nil) |
| 151 | + _ = resp.Body.Close() |
| 152 | + |
| 153 | + if resp.StatusCode != http.StatusNotFound { |
| 154 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 155 | + } |
| 156 | +} |
0 commit comments