@@ -28,6 +28,7 @@ import (
2828 "time"
2929
3030 "github.com/go-git/go-git/v5/plumbing/format/gitignore"
31+ . "github.com/onsi/gomega"
3132
3233 sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
3334)
@@ -293,10 +294,96 @@ func TestStorageRemoveAllButCurrent(t *testing.T) {
293294 t .Fatalf ("Valid path did not successfully return: %v" , err )
294295 }
295296
296- if err := s .RemoveAllButCurrent (sourcev1.Artifact {Path : path .Join (dir , "really" , "nonexistent" )}); err == nil {
297+ if _ , err := s .RemoveAllButCurrent (sourcev1.Artifact {Path : path .Join (dir , "really" , "nonexistent" )}); err == nil {
297298 t .Fatal ("Did not error while pruning non-existent path" )
298299 }
299300 })
301+
302+ t .Run ("collect names of deleted items" , func (t * testing.T ) {
303+ g := NewWithT (t )
304+ dir , err := os .MkdirTemp ("" , "" )
305+ g .Expect (err ).ToNot (HaveOccurred ())
306+ t .Cleanup (func () { os .RemoveAll (dir ) })
307+
308+ s , err := NewStorage (dir , "hostname" , time .Minute )
309+ g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
310+
311+ artifact := sourcev1.Artifact {
312+ Path : path .Join ("foo" , "bar" , "artifact1.tar.gz" ),
313+ }
314+
315+ // Create artifact dir and artifacts.
316+ artifactDir := path .Join (dir , "foo" , "bar" )
317+ g .Expect (os .MkdirAll (artifactDir , 0755 )).NotTo (HaveOccurred ())
318+ current := []string {
319+ path .Join (artifactDir , "artifact1.tar.gz" ),
320+ }
321+ wantDeleted := []string {
322+ path .Join (artifactDir , "file1.txt" ),
323+ path .Join (artifactDir , "file2.txt" ),
324+ }
325+ createFile := func (files []string ) {
326+ for _ , c := range files {
327+ f , err := os .Create (c )
328+ g .Expect (err ).ToNot (HaveOccurred ())
329+ g .Expect (f .Close ()).ToNot (HaveOccurred ())
330+ }
331+ }
332+ createFile (current )
333+ createFile (wantDeleted )
334+ _ , err = s .Symlink (artifact , "latest.tar.gz" )
335+ g .Expect (err ).ToNot (HaveOccurred (), "failed to create symlink" )
336+
337+ deleted , err := s .RemoveAllButCurrent (artifact )
338+ g .Expect (err ).ToNot (HaveOccurred (), "failed to remove all but current" )
339+ g .Expect (deleted ).To (Equal (wantDeleted ))
340+ })
341+ }
342+
343+ func TestStorageRemoveAll (t * testing.T ) {
344+ tests := []struct {
345+ name string
346+ artifactPath string
347+ createArtifactPath bool
348+ wantDeleted string
349+ }{
350+ {
351+ name : "delete non-existent path" ,
352+ artifactPath : path .Join ("foo" , "bar" , "artifact1.tar.gz" ),
353+ createArtifactPath : false ,
354+ wantDeleted : "" ,
355+ },
356+ {
357+ name : "delete existing path" ,
358+ artifactPath : path .Join ("foo" , "bar" , "artifact1.tar.gz" ),
359+ createArtifactPath : true ,
360+ wantDeleted : path .Join ("foo" , "bar" ),
361+ },
362+ }
363+
364+ for _ , tt := range tests {
365+ t .Run (tt .name , func (t * testing.T ) {
366+ g := NewWithT (t )
367+ dir , err := os .MkdirTemp ("" , "" )
368+ g .Expect (err ).ToNot (HaveOccurred ())
369+ t .Cleanup (func () { os .RemoveAll (dir ) })
370+
371+ s , err := NewStorage (dir , "hostname" , time .Minute )
372+ g .Expect (err ).ToNot (HaveOccurred (), "failed to create new storage" )
373+
374+ artifact := sourcev1.Artifact {
375+ Path : tt .artifactPath ,
376+ }
377+
378+ if tt .createArtifactPath {
379+ g .Expect (os .MkdirAll (path .Join (dir , tt .artifactPath ), 0755 )).ToNot (HaveOccurred ())
380+ }
381+
382+ deleted , err := s .RemoveAll (artifact )
383+ g .Expect (err ).ToNot (HaveOccurred ())
384+ g .Expect (deleted ).To (ContainSubstring (tt .wantDeleted ), "unexpected deleted path" )
385+ })
386+ }
300387}
301388
302389func TestStorageCopyFromPath (t * testing.T ) {
0 commit comments