@@ -17,6 +17,7 @@ import (
1717 "context"
1818 "testing"
1919
20+ "github.com/containerd/containerd/snapshots"
2021 "github.com/hashicorp/go-multierror"
2122 "github.com/pkg/errors"
2223
@@ -55,13 +56,53 @@ func getCachedSnapshotter(uut *SnapshotterCache) error {
5556 return nil
5657}
5758
58- func getSnapshotterPropogatesErrors (uut * SnapshotterCache ) error {
59+ func getSnapshotterPropagatesErrors (uut * SnapshotterCache ) error {
5960 if _ , err := uut .Get (context .Background (), "SnapshotterKey" , getSnapshotterErrorFunction ); err == nil {
6061 return errors .New ("Get function did not propagate errors from snapshotter generator function" )
6162 }
6263 return nil
6364}
6465
66+ func successfulWalk (ctx context.Context , info snapshots.Info ) error {
67+ return nil
68+ }
69+
70+ func applyWalkFunctionOnEmptyCache (uut * SnapshotterCache ) error {
71+ if err := uut .WalkAll (context .Background (), successfulWalk ); err != nil {
72+ return errors .New ("WalkAll on empty cache incorrectly resulted in error" )
73+ }
74+ return nil
75+ }
76+
77+ func applyWalkFunctionToAllCachedSnapshotters (uut * SnapshotterCache ) error {
78+ if _ , err := uut .Get (context .Background (), "Snapshotter-A" , getSnapshotterOkFunction ); err != nil {
79+ return errors .Wrap (err , "Adding snapshotter A to empty cache incorrectly resulted in error" )
80+ }
81+ if _ , err := uut .Get (context .Background (), "Snapshotter-B" , getSnapshotterOkFunction ); err != nil {
82+ return errors .Wrap (err , "Adding snapshotter B to empty cache incorrectly resulted in error" )
83+ }
84+ if err := uut .WalkAll (context .Background (), successfulWalk ); err != nil {
85+ return errors .New ("WalkAll on populated cache incorrectly resulted in error" )
86+ }
87+ return nil
88+ }
89+
90+ func applyWalkFunctionPropagatesErrors (uut * SnapshotterCache ) error {
91+ if _ , err := uut .Get (context .Background (), "Snapshotter-A" , getFailingSnapshotterOkFunction ); err != nil {
92+ return errors .Wrap (err , "Adding snapshotter A to empty cache incorrectly resulted in error" )
93+ }
94+ // The failing snapshotter mock will fail all Walk calls before applying
95+ // the snapshots.WalkFunc, but for the purposes of this test that is fine.
96+ // In which case, any function will do.
97+ walkFunc := func (ctx context.Context , info snapshots.Info ) error {
98+ return nil
99+ }
100+ if err := uut .WalkAll (context .Background (), walkFunc ); err == nil {
101+ return errors .New ("WalkAll did not propagate errors from walk function" )
102+ }
103+ return nil
104+ }
105+
65106func evictSnapshotterFromEmptyCache (uut * SnapshotterCache ) error {
66107 if err := uut .Evict ("SnapshotterKey" ); err == nil {
67108 return errors .New ("Evict function did not return error on call on empty cache" )
@@ -80,7 +121,7 @@ func evictSnapshotterFromCache(uut *SnapshotterCache) error {
80121 return nil
81122}
82123
83- func evictSnapshotterFromCachePropogatesCloseError (uut * SnapshotterCache ) error {
124+ func evictSnapshotterFromCachePropagatesCloseError (uut * SnapshotterCache ) error {
84125 if _ , err := uut .Get (context .Background (), "SnapshotterKey" , getFailingSnapshotterOkFunction ); err != nil {
85126 return errors .Wrap (err , "Adding snapshotter to empty cache incorrectly resulted in error" )
86127 }
@@ -156,14 +197,36 @@ func TestGetSnapshotterFromCache(t *testing.T) {
156197 }{
157198 {"AddSnapshotterToCache" , getSnapshotterFromEmptyCache },
158199 {"GetCachedSnapshotter" , getCachedSnapshotter },
159- {"PropogateFetchSnapshotterErrors" , getSnapshotterPropogatesErrors },
200+ {"PropogateFetchSnapshotterErrors" , getSnapshotterPropagatesErrors },
201+ }
202+
203+ for _ , test := range tests {
204+ t .Run (test .name , func (t * testing.T ) {
205+ uut := NewSnapshotterCache ()
206+ if err := test .run (uut ); err != nil {
207+ t .Fatalf ("%s: %s" , test .name , err .Error ())
208+ }
209+ })
210+ }
211+ }
212+
213+ func TestWalkAllFunctionOnCache (t * testing.T ) {
214+ t .Parallel ()
215+
216+ tests := []struct {
217+ name string
218+ run func (* SnapshotterCache ) error
219+ }{
220+ {"ApplyWalkFunctionOnEmptyCache" , applyWalkFunctionOnEmptyCache },
221+ {"ApplyWalkFunctionToAllCachedSnapshotters" , applyWalkFunctionToAllCachedSnapshotters },
222+ {"ApplyWalkFunctionPropogatesErrors" , applyWalkFunctionPropagatesErrors },
160223 }
161224
162225 for _ , test := range tests {
163226 t .Run (test .name , func (t * testing.T ) {
164227 uut := NewSnapshotterCache ()
165228 if err := test .run (uut ); err != nil {
166- t .Fatal ( test . name + ": " + err .Error ())
229+ t .Fatalf ( "%s: %s" , test . name , err .Error ())
167230 }
168231 })
169232 }
@@ -178,14 +241,14 @@ func TestEvictSnapshotterFromCache(t *testing.T) {
178241 }{
179242 {"EvictSnapshotterFromEmptyCache" , evictSnapshotterFromEmptyCache },
180243 {"EvictSnapshotterFromCache" , evictSnapshotterFromCache },
181- {"PropogateEvictSnapshotterCloseErrors" , evictSnapshotterFromCachePropogatesCloseError },
244+ {"PropogateEvictSnapshotterCloseErrors" , evictSnapshotterFromCachePropagatesCloseError },
182245 }
183246
184247 for _ , test := range tests {
185248 t .Run (test .name , func (t * testing.T ) {
186249 uut := NewSnapshotterCache ()
187250 if err := test .run (uut ); err != nil {
188- t .Fatal ( test . name + ": " + err .Error ())
251+ t .Fatalf ( "%s: %s" , test . name , err .Error ())
189252 }
190253 })
191254 }
@@ -207,7 +270,7 @@ func TestCloseCache(t *testing.T) {
207270 t .Run (test .name , func (t * testing.T ) {
208271 uut := NewSnapshotterCache ()
209272 if err := test .run (uut ); err != nil {
210- t .Fatal ( test . name + ": " + err .Error ())
273+ t .Fatalf ( "%s: %s" , test . name , err .Error ())
211274 }
212275 })
213276 }
@@ -228,7 +291,7 @@ func TestListSnapshotters(t *testing.T) {
228291 t .Run (test .name , func (t * testing.T ) {
229292 uut := NewSnapshotterCache ()
230293 if err := test .run (uut ); err != nil {
231- t .Fatal ( test . name + ": " + err .Error ())
294+ t .Fatalf ( "%s: %s" , test . name , err .Error ())
232295 }
233296 })
234297 }
0 commit comments