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