|
| 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 | + "encoding/json" |
| 21 | + "net/http" |
| 22 | + "testing" |
| 23 | + |
| 24 | + apiv1 "github.com/cozystack/blockstor/pkg/api/v1" |
| 25 | + "github.com/cozystack/blockstor/pkg/store" |
| 26 | +) |
| 27 | + |
| 28 | +// TestSnapshotRestoreCreatesNewRD: POST .../snapshot-restore-resource |
| 29 | +// builds a brand-new ResourceDefinition from a snapshot. Mirrors what |
| 30 | +// `linstor snapshot resource restore` does upstream. |
| 31 | +func TestSnapshotRestoreCreatesNewRD(t *testing.T) { |
| 32 | + st := store.NewInMemory() |
| 33 | + ctx := t.Context() |
| 34 | + |
| 35 | + if err := st.ResourceDefinitions().Create(ctx, &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 36 | + t.Fatalf("seed RD: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + if err := st.Snapshots().Create(ctx, &apiv1.Snapshot{ |
| 40 | + Name: "snap-1", |
| 41 | + ResourceName: "pvc-1", |
| 42 | + Nodes: []string{"n1", "n2"}, |
| 43 | + VolumeDefinitions: []apiv1.SnapshotVolumeDef{ |
| 44 | + {VolumeNumber: 0, SizeKib: 1024 * 1024}, |
| 45 | + }, |
| 46 | + }); err != nil { |
| 47 | + t.Fatalf("seed snap: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + base, stop := startServerWithStore(t, st) |
| 51 | + defer stop() |
| 52 | + |
| 53 | + body, _ := json.Marshal(map[string]string{ |
| 54 | + "to_resource": "pvc-2", |
| 55 | + "from_snapshot": "snap-1", |
| 56 | + }) |
| 57 | + |
| 58 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/snapshot-restore-resource", body) |
| 59 | + _ = resp.Body.Close() |
| 60 | + |
| 61 | + if resp.StatusCode != http.StatusCreated { |
| 62 | + t.Fatalf("status: got %d, want 201", resp.StatusCode) |
| 63 | + } |
| 64 | + |
| 65 | + got, err := st.ResourceDefinitions().Get(ctx, "pvc-2") |
| 66 | + if err != nil { |
| 67 | + t.Fatalf("expected pvc-2 to exist: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + if got.Name != "pvc-2" { |
| 71 | + t.Errorf("Name: got %q", got.Name) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestSnapshotRestoreUnknownSnapshot: 404 if the snapshot doesn't exist. |
| 76 | +func TestSnapshotRestoreUnknownSnapshot(t *testing.T) { |
| 77 | + st := store.NewInMemory() |
| 78 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 79 | + t.Fatalf("seed: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + base, stop := startServerWithStore(t, st) |
| 83 | + defer stop() |
| 84 | + |
| 85 | + body, _ := json.Marshal(map[string]string{ |
| 86 | + "to_resource": "pvc-2", |
| 87 | + "from_snapshot": "ghost", |
| 88 | + }) |
| 89 | + |
| 90 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/snapshot-restore-resource", body) |
| 91 | + _ = resp.Body.Close() |
| 92 | + |
| 93 | + if resp.StatusCode != http.StatusNotFound { |
| 94 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// TestSnapshotRestoreMissingFields: empty `to_resource` → 400. |
| 99 | +func TestSnapshotRestoreMissingFields(t *testing.T) { |
| 100 | + st := store.NewInMemory() |
| 101 | + if err := st.ResourceDefinitions().Create(t.Context(), &apiv1.ResourceDefinition{Name: "pvc-1"}); err != nil { |
| 102 | + t.Fatalf("seed: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + base, stop := startServerWithStore(t, st) |
| 106 | + defer stop() |
| 107 | + |
| 108 | + body, _ := json.Marshal(map[string]string{ |
| 109 | + "from_snapshot": "snap-1", |
| 110 | + }) |
| 111 | + |
| 112 | + resp := httpPost(t, base+"/v1/resource-definitions/pvc-1/snapshot-restore-resource", body) |
| 113 | + _ = resp.Body.Close() |
| 114 | + |
| 115 | + if resp.StatusCode != http.StatusBadRequest { |
| 116 | + t.Errorf("status: got %d, want 400", resp.StatusCode) |
| 117 | + } |
| 118 | +} |
0 commit comments