| 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 |
) |
| 46 |
|
| 47 |
var ( |
| 48 |
Node1 = client.Node{ |
| 49 |
Name: "node1", |
| 50 |
Props: map[string]string{ |
| 51 |
"Aux/key1": "val1", |
| 52 |
"Aux/key2": "val2", |
| 53 |
}, |
| 54 |
Type: linstor.ValNodeTypeStlt, |
| 55 |
} |
| 56 |
Node2 = client.Node{ |
| 57 |
Name: "node2", |
| 58 |
Props: map[string]string{ |
| 59 |
"Aux/key2": "", |
| 60 |
"Aux/key3": "val3", |
| 61 |
}, |
| 62 |
Type: linstor.ValNodeTypeCmbd, |
| 63 |
} |
| 64 |
AllNodes = []client.Node{Node1, Node2} |
| 65 |
) |
| 66 |
|
| 67 |
func TestNodeCache(t *testing.T) { |
| 68 |
testSrv := TestServer{ |
| 69 |
counter: make(map[string]int), |
| 70 |
responses: map[string]TestResponse{ |
| 71 |
NodesRequest: {Code: http.StatusOK, Body: AllNodes}, |
| 72 |
ReconnectRequest: {Code: http.StatusOK}, |
| 73 |
}, |
| 74 |
} |
| 75 |
srv := httptest.NewServer(&testSrv) |
| 76 |
defer srv.Close() |
| 77 |
|
| 78 |
u, err := url.Parse(srv.URL) |
| 79 |
assert.NoError(t, err) |
| 80 |
|
| 81 |
cl, err := client.NewClient( |
| 82 |
client.HTTPClient(srv.Client()), |
| 83 |
client.BaseURL(u), |
| 84 |
cache.WithCaches(&cache.NodeCache{Timeout: 1 * time.Second}), |
| 85 |
) |
| 86 |
assert.NoError(t, err) |
| 87 |
|
| 88 |
nodes, err := cl.Nodes.GetAll(context.Background()) |
| 89 |
assert.NoError(t, err) |
| 90 |
assert.Equal(t, AllNodes, nodes) |
| 91 |
|
| 92 |
node1, err := cl.Nodes.Get(context.Background(), "node1") |
| 93 |
assert.NoError(t, err) |
| 94 |
assert.Equal(t, Node1, node1) |
| 95 |
|
| 96 |
_, err = cl.Nodes.Get(context.Background(), "node3") |
| 97 |
assert.Equal(t, client.NotFoundError, err) |
| 98 |
|
| 99 |
// Assert that the request was only sent once |
| 100 |
assert.Equal(t, 1, testSrv.counter[NodesRequest]) |
| 101 |
|
| 102 |
// Invalidate cache |
| 103 |
err = cl.Nodes.Reconnect(context.Background(), "node1") |
| 104 |
assert.NoError(t, err) |
| 105 |
|
| 106 |
node1, err = cl.Nodes.Get(context.Background(), "node1") |
| 107 |
assert.NoError(t, err) |
| 108 |
assert.Equal(t, Node1, node1) |
| 109 |
assert.Equal(t, 2, testSrv.counter[NodesRequest]) |
| 110 |
|
| 111 |
// Wait for cache time out to be reached |
| 112 |
time.Sleep(1*time.Second + 100*time.Millisecond) |
| 113 |
node1, err = cl.Nodes.Get(context.Background(), "node1") |
| 114 |
assert.NoError(t, err) |
| 115 |
assert.Equal(t, Node1, node1) |
| 116 |
assert.Equal(t, 3, testSrv.counter[NodesRequest]) |
| 117 |
} |
| 118 |
|
| 119 |
func TestNodeCachePropsFiltering(t *testing.T) { |
| 120 |
testSrv := TestServer{ |
| 121 |
counter: make(map[string]int), |
| 122 |
responses: map[string]TestResponse{ |
| 123 |
NodesRequest: {Code: http.StatusOK, Body: AllNodes}, |
| 124 |
ReconnectRequest: {Code: http.StatusOK}, |
| 125 |
}, |
| 126 |
} |
| 127 |
srv := httptest.NewServer(&testSrv) |
| 128 |
defer srv.Close() |
| 129 |
|
| 130 |
u, err := url.Parse(srv.URL) |
| 131 |
assert.NoError(t, err) |
| 132 |
|
| 133 |
cl, err := client.NewClient( |
| 134 |
client.HTTPClient(srv.Client()), |
| 135 |
client.BaseURL(u), |
| 136 |
cache.WithCaches(&cache.NodeCache{Timeout: 1 * time.Second}), |
| 137 |
) |
| 138 |
assert.NoError(t, err) |
| 139 |
|
| 140 |
// Filtering by presence of key |
| 141 |
nodes, err := cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2"}}) |
| 142 |
assert.NoError(t, err) |
| 143 |
assert.Equal(t, AllNodes, nodes) |
| 144 |
|
| 145 |
// Filtering by presence of key only on one node |
| 146 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key3"}}) |
| 147 |
assert.NoError(t, err) |
| 148 |
assert.Equal(t, []client.Node{Node2}, nodes) |
| 149 |
|
| 150 |
// Filtering by presence of key on no node |
| 151 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/other"}}) |
| 152 |
assert.NoError(t, err) |
| 153 |
assert.Empty(t, nodes) |
| 154 |
|
| 155 |
// Filtering by presence of specific key=value |
| 156 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2=val2"}}) |
| 157 |
assert.NoError(t, err) |
| 158 |
assert.Equal(t, []client.Node{Node1}, nodes) |
| 159 |
|
| 160 |
// Filtering by presence of specific key="" |
| 161 |
nodes, err = cl.Nodes.GetAll(context.Background(), &client.ListOpts{Prop: []string{"Aux/key2="}}) |
| 162 |
assert.NoError(t, err) |
| 163 |
assert.Equal(t, []client.Node{Node2}, nodes) |
| 164 |
} |