forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
176 lines (142 loc) · 4.64 KB
/
cache_test.go
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
165
166
167
168
169
170
171
172
173
174
175
176
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
"github.com/gorilla/mux"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
// TODO: Now that the plugin cache can be mocked, come back later and
// simplify these tests.
type mockCache struct {
mock.Mock
items map[string]interface{}
}
func newMockCache() *mockCache {
return &mockCache{items: make(map[string]interface{})}
}
func (m *mockCache) Get(cat, key string) (interface{}, error) {
return nil, nil
}
func (m *mockCache) GetOrUpdate(cat, key string, ttl time.Duration, resetTTLOnHit bool, generateValue func() (interface{}, error)) (interface{}, error) {
key = cat + "::" + key
if v, ok := m.items[key]; ok {
return v, nil
}
val, err := generateValue()
if err != nil {
return nil, err
}
m.items[key] = val
return val, nil
}
func (m *mockCache) Flush() {
m.items = make(map[string]interface{})
}
func (m *mockCache) Delete(matcher *regexp.Regexp) []string {
deleted := make([]string, 0, len(m.items))
for k := range m.items {
if matcher.MatchString(k) {
delete(m.items, k)
deleted = append(deleted, k)
}
}
return deleted
}
type CacheHandlerTestSuite struct {
suite.Suite
router *mux.Router
}
func (suite *CacheHandlerTestSuite) SetupSuite() {
plugin.SetTestCache(newMockCache())
suite.router = mux.NewRouter()
suite.router.Handle("/cache", cacheHandler).Methods(http.MethodDelete)
}
func (suite *CacheHandlerTestSuite) TearDownSuite() {
plugin.UnsetTestCache()
}
func (suite *CacheHandlerTestSuite) TestRejectsGet() {
req := httptest.NewRequest(http.MethodGet, "http://example.com/cache", nil)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(http.StatusMethodNotAllowed, w.Code)
}
func (suite *CacheHandlerTestSuite) TestClearCache() {
// Populate the cache with a mocked resource and plugin.cached*
parent := newMockedParent()
parent.SetTestID("/dir")
parent.On("List", mock.Anything).Return([]plugin.Entry{}, nil)
reqCtx := context.WithValue(context.Background(), mountpointKey, "/")
expectedChildren := make(map[string]plugin.Entry)
if children, err := plugin.List(reqCtx, parent); suite.Nil(err) {
suite.Equal(expectedChildren, children.Map())
}
// Test clearing a different cache
req := httptest.NewRequest(http.MethodDelete, "http://example.com/cache?path=/file", nil).WithContext(reqCtx)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(http.StatusOK, w.Code)
suite.Equal("[]\n", w.Body.String())
if children, err := plugin.List(context.Background(), parent); suite.Nil(err) {
suite.Equal(expectedChildren, children.Map())
}
// Test clearing the cache
req = httptest.NewRequest(http.MethodDelete, "http://example.com/cache?path=/dir", nil).WithContext(reqCtx)
w = httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(http.StatusOK, w.Code)
suite.Equal(`["List::/dir"]`, strings.TrimSpace(w.Body.String()))
if children, err := plugin.List(context.Background(), parent); suite.Nil(err) {
suite.Equal(expectedChildren, children.Map())
}
parent.AssertNumberOfCalls(suite.T(), "List", 2)
}
func (suite *CacheHandlerTestSuite) TestClearCacheErrors() {
reqCtx := context.WithValue(context.Background(), mountpointKey, "/mnt")
// Test clearing cache by a relative path
req := httptest.NewRequest(http.MethodDelete, "http://example.com/cache?path=mnt/file", nil).WithContext(reqCtx)
w := httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(http.StatusBadRequest, w.Code)
var errResp apitypes.ErrorObj
suite.NoError(json.Unmarshal(w.Body.Bytes(), &errResp))
suite.Equal(apitypes.RelativePath, errResp.Kind)
// Test clearing cache outside the mountpoint
req = httptest.NewRequest(http.MethodDelete, "http://example.com/cache?path=/a/file", nil).WithContext(reqCtx)
w = httptest.NewRecorder()
suite.router.ServeHTTP(w, req)
suite.Equal(http.StatusBadRequest, w.Code)
suite.NoError(json.Unmarshal(w.Body.Bytes(), &errResp))
suite.Equal(apitypes.NonWashPath, errResp.Kind)
}
func TestCacheHandler(t *testing.T) {
suite.Run(t, new(CacheHandlerTestSuite))
}
type mockedParent struct {
plugin.EntryBase
mock.Mock
}
func newMockedParent() *mockedParent {
p := &mockedParent{
EntryBase: plugin.NewEntry("mockParent"),
}
return p
}
func (p *mockedParent) List(ctx context.Context) ([]plugin.Entry, error) {
args := p.Called(ctx)
return args.Get(0).([]plugin.Entry), args.Error(1)
}
func (p *mockedParent) ChildSchemas() []*plugin.EntrySchema {
return nil
}
func (p *mockedParent) Schema() *plugin.EntrySchema {
return nil
}