|
| 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 | +// TestControllerPropertiesEmptyOnFreshCluster: GET on a brand-new |
| 29 | +// controller returns 200 with an empty props map. |
| 30 | +func TestControllerPropertiesEmptyOnFreshCluster(t *testing.T) { |
| 31 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 32 | + defer stop() |
| 33 | + |
| 34 | + resp := httpGet(t, base+"/v1/controller/properties") |
| 35 | + defer func() { _ = resp.Body.Close() }() |
| 36 | + |
| 37 | + if resp.StatusCode != http.StatusOK { |
| 38 | + t.Fatalf("status: got %d, want 200", resp.StatusCode) |
| 39 | + } |
| 40 | + |
| 41 | + var got map[string]string |
| 42 | + |
| 43 | + err := json.NewDecoder(resp.Body).Decode(&got) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("decode: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + if len(got) != 0 { |
| 49 | + t.Errorf("expected empty props; got %v", got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// TestControllerPropertiesSetAndGet: PUT writes, GET reads back. |
| 54 | +func TestControllerPropertiesSetAndGet(t *testing.T) { |
| 55 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 56 | + defer stop() |
| 57 | + |
| 58 | + body, _ := json.Marshal(apiv1.GenericPropsModify{ |
| 59 | + OverrideProps: map[string]string{"DefaultDebugSslConnector": "DebugSslConnector"}, |
| 60 | + }) |
| 61 | + |
| 62 | + resp := httpPost(t, base+"/v1/controller/properties", body) |
| 63 | + _ = resp.Body.Close() |
| 64 | + |
| 65 | + if resp.StatusCode != http.StatusOK { |
| 66 | + t.Fatalf("PUT status: got %d, want 200", resp.StatusCode) |
| 67 | + } |
| 68 | + |
| 69 | + getResp := httpGet(t, base+"/v1/controller/properties") |
| 70 | + defer func() { _ = getResp.Body.Close() }() |
| 71 | + |
| 72 | + if getResp.StatusCode != http.StatusOK { |
| 73 | + t.Fatalf("GET status: got %d", getResp.StatusCode) |
| 74 | + } |
| 75 | + |
| 76 | + var got map[string]string |
| 77 | + |
| 78 | + err := json.NewDecoder(getResp.Body).Decode(&got) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("decode: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if got["DefaultDebugSslConnector"] != "DebugSslConnector" { |
| 84 | + t.Errorf("expected DefaultDebugSslConnector=DebugSslConnector; got %v", got) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestControllerPropertiesDelete: PUT with delete_props removes a key. |
| 89 | +func TestControllerPropertiesDelete(t *testing.T) { |
| 90 | + base, stop := startServerWithStore(t, store.NewInMemory()) |
| 91 | + defer stop() |
| 92 | + |
| 93 | + // Seed two keys. |
| 94 | + body, _ := json.Marshal(apiv1.GenericPropsModify{ |
| 95 | + OverrideProps: map[string]string{"KeepMe": "1", "RemoveMe": "2"}, |
| 96 | + }) |
| 97 | + resp := httpPost(t, base+"/v1/controller/properties", body) |
| 98 | + _ = resp.Body.Close() |
| 99 | + |
| 100 | + // Delete one. |
| 101 | + delBody, _ := json.Marshal(apiv1.GenericPropsModify{ |
| 102 | + DeleteProps: []string{"RemoveMe"}, |
| 103 | + }) |
| 104 | + |
| 105 | + delResp := httpPost(t, base+"/v1/controller/properties", delBody) |
| 106 | + _ = delResp.Body.Close() |
| 107 | + |
| 108 | + if delResp.StatusCode != http.StatusOK { |
| 109 | + t.Fatalf("delete status: got %d", delResp.StatusCode) |
| 110 | + } |
| 111 | + |
| 112 | + getResp := httpGet(t, base+"/v1/controller/properties") |
| 113 | + defer func() { _ = getResp.Body.Close() }() |
| 114 | + |
| 115 | + var got map[string]string |
| 116 | + _ = json.NewDecoder(getResp.Body).Decode(&got) |
| 117 | + |
| 118 | + if _, present := got["RemoveMe"]; present { |
| 119 | + t.Errorf("RemoveMe still in props: %v", got) |
| 120 | + } |
| 121 | + |
| 122 | + if got["KeepMe"] != "1" { |
| 123 | + t.Errorf("KeepMe lost: got %v", got) |
| 124 | + } |
| 125 | +} |
0 commit comments