Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
197 lines (157 loc) · 4.92 KB
/
Copy pathcache_test.go
File metadata and controls
197 lines (157 loc) · 4.92 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package templates
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestCache(t *testing.T) {
templates := NewCache()
testErr := errors.New("test error")
data, _, err := templates.Has("test")
require.Nil(t, err, "invalid value for err")
require.Nil(t, data, "invalid value for data")
item := &Template{}
templates.Store("test", item, nil, testErr)
data, _, err = templates.Has("test")
require.Equal(t, testErr, err, "invalid value for err")
require.Equal(t, item, data, "invalid value for data")
}
func TestCacheFileBased(t *testing.T) {
tempDir, err := os.MkdirTemp("", "cache_test")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
cache := NewCache()
template := &Template{}
// Create a test file
testFile := filepath.Join(tempDir, "test.yaml")
err = os.WriteFile(testFile, []byte("test content"), 0644)
require.NoError(t, err)
// Store template with file
cache.Store(testFile, template, []byte("raw content"), nil)
// Should be able to retrieve it
retrieved, raw, err := cache.Has(testFile)
require.NoError(t, err)
require.Equal(t, template, retrieved)
require.Equal(t, []byte("raw content"), raw)
// Modify file content (should invalidate cache)
time.Sleep(10 * time.Millisecond) // Ensure mod time difference
err = os.WriteFile(testFile, []byte("modified content"), 0644)
require.NoError(t, err)
// Cache should be invalidated
retrieved, raw, err = cache.Has(testFile)
require.NoError(t, err)
require.Nil(t, retrieved)
require.Nil(t, raw)
}
func TestCacheFileDeletion(t *testing.T) {
tempDir, err := os.MkdirTemp("", "cache_test")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
cache := NewCache()
template := &Template{}
// Create a test file
testFile := filepath.Join(tempDir, "test.yaml")
err = os.WriteFile(testFile, []byte("test content"), 0644)
require.NoError(t, err)
// Store template with file
cache.Store(testFile, template, []byte("raw content"), nil)
// Should be able to retrieve it
retrieved, raw, err := cache.Has(testFile)
require.NoError(t, err)
require.Equal(t, template, retrieved)
require.Equal(t, []byte("raw content"), raw)
// Delete the file
err = os.Remove(testFile)
require.NoError(t, err)
// Cache should be invalidated
retrieved, raw, err = cache.Has(testFile)
require.NoError(t, err)
require.Nil(t, retrieved)
require.Nil(t, raw)
}
func TestCacheStoreWithoutRaw(t *testing.T) {
cache := NewCache()
template := &Template{}
testErr := errors.New("test error")
// Store without raw data
cache.StoreWithoutRaw("test", template, testErr)
// Should be able to retrieve template but not raw data
retrieved, raw, err := cache.Has("test")
require.Equal(t, testErr, err)
require.Equal(t, template, retrieved)
require.Empty(t, raw)
}
func TestCacheGet(t *testing.T) {
cache := NewCache()
template := &Template{}
testErr := errors.New("test error")
// Test cache miss
retrieved, err := cache.Get("nonexistent")
require.NoError(t, err)
require.Nil(t, retrieved)
// Store template
cache.Store("test", template, []byte("raw"), testErr)
// Should be able to get template
retrieved, err = cache.Get("test")
require.Equal(t, testErr, err)
require.Equal(t, template, retrieved)
}
func TestCachePurge(t *testing.T) {
cache := NewCache()
template := &Template{}
// Store multiple templates
cache.Store("test1", template, []byte("raw1"), nil)
cache.Store("test2", template, []byte("raw2"), nil)
// Verify they exist
retrieved1, _, _ := cache.Has("test1")
require.Equal(t, template, retrieved1)
retrieved2, _, _ := cache.Has("test2")
require.Equal(t, template, retrieved2)
// Purge cache
cache.Purge()
// Should be empty now
retrieved1, _, _ = cache.Has("test1")
require.Nil(t, retrieved1)
retrieved2, _, _ = cache.Has("test2")
require.Nil(t, retrieved2)
}
func TestCacheNonFileTemplates(t *testing.T) {
cache := NewCache()
template := &Template{}
testErr := errors.New("test error")
// Store non-file template (like the original test)
cache.Store("nonfile", template, []byte("raw"), testErr)
// Should work normally
retrieved, raw, err := cache.Has("nonfile")
require.Equal(t, testErr, err)
require.Equal(t, template, retrieved)
require.Equal(t, []byte("raw"), raw)
}
func TestCacheFileBasedStoreWithoutRaw(t *testing.T) {
// Create a temporary directory for test files
tempDir, err := os.MkdirTemp("", "cache_test")
require.NoError(t, err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
cache := NewCache()
template := &Template{}
// Create a test file
testFile := filepath.Join(tempDir, "test.yaml")
err = os.WriteFile(testFile, []byte("test content"), 0644)
require.NoError(t, err)
// Store template without raw data
cache.StoreWithoutRaw(testFile, template, nil)
// Should be able to retrieve template but not raw data
retrieved, raw, err := cache.Has(testFile)
require.NoError(t, err)
require.Equal(t, template, retrieved)
require.Empty(t, raw)
}