|
| 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 | +// TestKVSetThenGet: round-trip through REST. |
| 29 | +func TestKVSetThenGet(t *testing.T) { |
| 30 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 31 | + defer stop() |
| 32 | + |
| 33 | + body, err := json.Marshal(apiv1.GenericPropsModify{ |
| 34 | + OverrideProps: map[string]string{"k": "v"}, |
| 35 | + }) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("marshal: %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + resp := httpPost(t, base+"/v1/key-value-store/csi-volumes", body) |
| 41 | + _ = resp.Body.Close() |
| 42 | + |
| 43 | + if resp.StatusCode != http.StatusOK { |
| 44 | + t.Fatalf("set: got %d, want 200", resp.StatusCode) |
| 45 | + } |
| 46 | + |
| 47 | + getResp := httpGet(t, base+"/v1/key-value-store/csi-volumes") |
| 48 | + defer func() { _ = getResp.Body.Close() }() |
| 49 | + |
| 50 | + if getResp.StatusCode != http.StatusOK { |
| 51 | + t.Fatalf("get: got %d, want 200", getResp.StatusCode) |
| 52 | + } |
| 53 | + |
| 54 | + var kv apiv1.KV |
| 55 | + if jErr := json.NewDecoder(getResp.Body).Decode(&kv); jErr != nil { |
| 56 | + t.Fatalf("decode: %v", jErr) |
| 57 | + } |
| 58 | + |
| 59 | + if kv.Name != "csi-volumes" || kv.Props["k"] != "v" { |
| 60 | + t.Errorf("got %+v", kv) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// TestKVGetMissing: 404 on absent instance. |
| 65 | +func TestKVGetMissing(t *testing.T) { |
| 66 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 67 | + defer stop() |
| 68 | + |
| 69 | + resp := httpGet(t, base+"/v1/key-value-store/ghost") |
| 70 | + _ = resp.Body.Close() |
| 71 | + |
| 72 | + if resp.StatusCode != http.StatusNotFound { |
| 73 | + t.Errorf("status: got %d, want 404", resp.StatusCode) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// TestKVDeleteThenGet: deleted instance becomes 404. |
| 78 | +func TestKVDeleteThenGet(t *testing.T) { |
| 79 | + st := store.NewInMemory() |
| 80 | + if err := st.KeyValueStore().SetKeys(t.Context(), "x", apiv1.GenericPropsModify{ |
| 81 | + OverrideProps: map[string]string{"k": "v"}, |
| 82 | + }); err != nil { |
| 83 | + t.Fatalf("seed: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + base, stop := startServerWithStore(t, st) |
| 87 | + defer stop() |
| 88 | + |
| 89 | + delResp := httpDelete(t, base+"/v1/key-value-store/x") |
| 90 | + _ = delResp.Body.Close() |
| 91 | + |
| 92 | + if delResp.StatusCode != http.StatusNoContent { |
| 93 | + t.Fatalf("delete: got %d, want 204", delResp.StatusCode) |
| 94 | + } |
| 95 | + |
| 96 | + getResp := httpGet(t, base+"/v1/key-value-store/x") |
| 97 | + _ = getResp.Body.Close() |
| 98 | + |
| 99 | + if getResp.StatusCode != http.StatusNotFound { |
| 100 | + t.Errorf("post-delete: got %d, want 404", getResp.StatusCode) |
| 101 | + } |
| 102 | +} |
0 commit comments