| 1 |
package cache_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"net/http" |
| 7 |
"net/http/httptest" |
| 8 |
"net/url" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
linstor "github.com/LINBIT/golinstor" |
| 15 |
"github.com/LINBIT/golinstor/cache" |
| 16 |
"github.com/LINBIT/golinstor/client" |
| 17 |
) |
| 18 |
|
| 19 |
type TestResponse struct { |
| 20 |
Code int |
| 21 |
Body any |
| 22 |
} |
| 23 |
|
| 24 |
type TestServer struct { |
| 25 |
counter map[string]int |
| 26 |
responses map[string]TestResponse |
| 27 |
} |
| 28 |
|
| 29 |
func (t *TestServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) { |
| 30 |
req := request.Method + " " + request.URL.String() |
| 31 |
t.counter[req]++ |
| 32 |
|
| 33 |
resp, ok := t.responses[req] |
| 34 |
if !ok { |
| 35 |
writer.WriteHeader(http.StatusInternalServerError) |
| 36 |
} else { |
| 37 |
writer.WriteHeader(resp.Code) |
| 38 |
_ = json.NewEncoder(writer).Encode(resp.Body) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
const ( |
| 43 |
NodesRequest = "GET /v1/nodes?cached=true&limit=0&offset=0" |
| 44 |
ReconnectRequest = "PUT /v1/nodes/node1/reconnect" |
| 45 |
StoragePoolViewRequest = "GET /v1/view/storage-pools?cached=true&limit=0&offset=0" |
| 46 |
CreateDevicePoolRequest = "POST /v1/physical-storage/node1" |
| 47 |
) |
| 48 |
|
| 49 |
var ( |
| 50 |
Node1 = client.Node{ |
| 51 |
Name: "node1", |
| 52 |
Props: map[string]string{ |
| 53 |
"Aux/key1": "val1", |
| 54 |
"Aux/key2": "val2", |
| 55 |
}, |
| 56 |
Type: linstor.ValNodeTypeStlt, |
| 57 |
} |
| 58 |
Node2 = client.Node{ |
| 59 |
Name: "node2", |
| 60 |
Props: map[string]string{ |
| 61 |
"Aux/key2": "", |
| 62 |
"Aux/key3": "val3", |
| 63 |
}, |
| 64 |
Type: linstor.ValNodeTypeCmbd, |
| 65 |
} |
| 66 |
AllNodes = []client.Node{Node1, Node2} |
| 67 |
) |
| 68 |
|
| 69 |
func TestNodeCache(t *testing.T) { |
| 70 |
testSrv := TestServer{ |
| 71 |
counter: make(map[string]int), |
| 72 |
responses: map[string]TestResponse{ |
| 73 |
NodesRequest: {Code: http.StatusOK, Body: AllNodes}, |
| 74 |
ReconnectRequest: {Code: http.StatusOK}, |
| 75 |
}, |
| 76 |
} |
| 77 |
srv := httptest.NewServer(&testSrv) |
| 78 |
defer srv.Close() |
| 79 |
|
| 80 |
u, err := url.Parse(srv.URL) |
| 81 |
assert.NoError(t, err) |
| 82 |
|
| 83 |
cl, err := client.NewClient( |
| 84 |
client.HTTPClient(srv.Client()), |
| 85 |
client.BaseURL(u), |
| 86 |
cache.WithCaches(&cache.NodeCache{Timeout: 1 * time.Second}), |
| 87 |
) |
| 88 |
assert.NoError(t, err) |
| 89 |
|
| 90 |
nodes, err := cl.Nodes.GetAll(context.Background()) |
| 91 |
assert.NoError(t, err) |
| 92 |
assert.Equal(t, AllNodes, nodes) |
| 93 |
|
| 94 |
node1, err := cl.Nodes.Get(context.Background(), "node1") |
| 95 |
assert.NoError(t, err) |
| 96 |
assert.Equal(t, Node1, node1) |
| 97 |
|
| 98 |
_, err = cl.Nodes.Get(context.Background(), "node3") |
| 99 |
assert.Equal(t, client.NotFoundError, err) |
| 100 |
|
| 101 |
// Assert that the request was only sent once |
| 102 |
assert.Equal(t, 1, testSrv.counter[NodesRequest]) |
| 103 |
|
| 104 |
// Invalidate cache |
| 105 |
err = cl.Nodes.Reconnect(context.Background(), "node1") |
| 106 |
assert.NoError(t, err) |
| 107 |
|
| 108 |
node1, err = cl.Nodes.Get(context.Background(), "node1") |
| 109 |
assert.NoError(t, err) |
| 110 |
assert.Equal(t, Node1, node1) |
| 111 |
assert.Equal(t, 2, testSrv.counter[NodesRequest]) |
| 112 |
|
| 113 |
// Wait for cache time out to be reached |
| 114 |
time.Sleep(1*time.Second + 100*time.Millisecond) |
| 115 |
node1, err = cl.Nodes.Get(context.Background(), "node1") |
| 116 |
assert.NoError(t, err) |
| 117 |
assert.Equal(t, Node1, node1) |
| 118 |
assert.Equal(t, 3, testSrv.counter[NodesRequest]) |
| 119 |
} |
| 120 |
|
| 121 |
func TestNodeCacheCreateDevicePool(t *testing.T) { |
| 122 |
pool1 := client.StoragePool{StoragePoolName: "pool1", NodeName: "node1"} |
| 123 |
|
| 124 |
testSrv := TestServer{ |
| 125 |
counter: make(map[string]int), |
| 126 |
responses: map[string]TestResponse{ |
| 127 |
StoragePoolViewRequest: {Code: http.StatusOK, Body: []client.StoragePool{pool1}}, |
| 128 |
CreateDevicePoolRequest: {Code: http.StatusOK}, |
| 129 |
}, |
| 130 |
} |
| 131 |
srv := httptest.NewServer(&testSrv) |
| 132 |
defer srv.Close() |
| 133 |
|
| 134 |
u, err := url.Parse(srv.URL) |
| 135 |
assert.NoError(t, err) |
| 136 |
|
| 137 |
cl, err := client.NewClient( |
| 138 |
client.HTTPClient(srv.Client()), |
| 139 |
client.BaseURL(u), |
| 140 |
cache.WithCaches(&cache.NodeCache{Timeout: 1 * time.Minute}), |
| 141 |
) |
| 142 |
assert.NoError(t, err) |
| 143 |
|
| 144 |
pools, err := cl.Nodes.GetStoragePoolView(context.Background()) |
| 145 |
assert.NoError(t, err) |
| 146 |
assert.Equal(t, []client.StoragePool{pool1}, pools) |
| 147 |
assert.Equal(t, 1, testSrv.counter[StoragePoolViewRequest]) |
| 148 |
|
| 149 |
// Creating a device pool invalidates the storage pool cache, as the |
| 150 |
// request may also create a storage pool |
| 151 |
err = cl.Nodes.CreateDevicePool(context.Background(), "node1", client.PhysicalStorageCreate{ |
| 152 |
ProviderKind: client.LVM, |
| 153 |
DevicePaths: []string{"/dev/sda"}, |
| 154 |
WithStoragePool: client.PhysicalStorageStoragePoolCreate{Name: "pool2"}, |
| 155 |
}) |
| 156 |
assert.NoError(t, err) |
| 157 |
|
| 158 |
_, err = cl.Nodes.GetStoragePoolView(context.Background()) |
| 159 |
assert.NoError(t, err) |
| 160 |
assert.Equal(t, 2, testSrv.counter[StoragePoolViewRequest]) |
| 161 |
} |
| 162 |
|
| 163 |
func TestNodeCachePropsFiltering(t *testing.T) { |
| 164 |
testSrv := TestServer{ |
| 165 |
counter: make(map[string]int), |
| 166 |
responses: map[string]TestResponse{ |
| 167 |
NodesRequest: {Code: http.StatusOK, Body: AllNodes}, |
| 168 |
ReconnectRequest: {Code: http.StatusOK}, |
| 169 |
}, |
| 170 |
} |
| 171 |
srv := httptest.NewServer(&testSrv) |
| 172 |
defer srv.Close() |
| 173 |
|
| 174 |
u, err := url.Parse(srv.URL) |
| 175 |
assert.NoError(t, err) |
| 176 |
|
| 177 |
cl, err := client.NewClient( |
| 178 |
client.HTTPClient(srv.Client()), |
| 179 |
client.BaseURL(u), |
| 180 |
cache.WithCaches(&cache.NodeCache{Timeout: 1 * time.Second}), |
| 181 |
) |
| 182 |
assert.NoError(t, err) |
| 183 |
|
| 184 |
// Filtering by presence of key |
| 185 |
nodes, err := cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2"}}) |
| 186 |
assert.NoError(t, err) |
| 187 |
assert.Equal(t, AllNodes, nodes) |
| 188 |
|
| 189 |
// Filtering by presence of key only on one node |
| 190 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key3"}}) |
| 191 |
assert.NoError(t, err) |
| 192 |
assert.Equal(t, []client.Node{Node2}, nodes) |
| 193 |
|
| 194 |
// Filtering by presence of key on no node |
| 195 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/other"}}) |
| 196 |
assert.NoError(t, err) |
| 197 |
assert.Empty(t, nodes) |
| 198 |
|
| 199 |
// Filtering by presence of specific key=value |
| 200 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2=val2"}}) |
| 201 |
assert.NoError(t, err) |
| 202 |
assert.Equal(t, []client.Node{Node1}, nodes) |
| 203 |
|
| 204 |
// Filtering by presence of specific key="" |
| 205 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2="}}) |
| 206 |
assert.NoError(t, err) |
| 207 |
assert.Equal(t, []client.Node{Node2}, nodes) |
| 208 |
} |