@@ -22,6 +22,7 @@ import (
2222 "github.com/google/go-cmp/cmp/cmpopts"
2323 "github.com/metal-stack/metal-image-cache-sync/pkg/api"
2424 "github.com/spf13/afero"
25+ "github.com/stretchr/testify/assert"
2526 "github.com/stretchr/testify/require"
2627 "go.uber.org/zap/zaptest"
2728)
@@ -94,14 +95,18 @@ func Test_currentFileIndex(t *testing.T) {
9495}
9596
9697func createTestFile (t * testing.T , fs afero.Fs , p string ) {
97- require . Nil (t , fs . MkdirAll ( path .Base (p ), 0755 ))
98+ createTestDir (t , fs , path .Base (p ))
9899 f , err := fs .Create (p )
99100 require .Nil (t , err )
100101 defer f .Close ()
101102 _ , err = f .WriteString ("Test" )
102103 require .Nil (t , err )
103104}
104105
106+ func createTestDir (t * testing.T , fs afero.Fs , p string ) {
107+ require .Nil (t , fs .MkdirAll (p , 0755 ))
108+ }
109+
105110func dlLoggingSvc (data []byte ) (* s3.S3 , * []string , * []string ) {
106111 var m sync.Mutex
107112 names := []string {}
@@ -346,3 +351,130 @@ func TestSyncer_defineImageDiff(t *testing.T) {
346351func strPtr (s string ) * string {
347352 return & s
348353}
354+
355+ func Test_cleanEmptyDirs (t * testing.T ) {
356+ tests := []struct {
357+ name string
358+ fsModFunc func (t * testing.T , fs afero.Fs )
359+ fsCheckFunc func (t * testing.T , fs afero.Fs )
360+ wantErr error
361+ }{
362+ {
363+ name : "no directory contents, nothing happens" ,
364+ fsModFunc : nil ,
365+ wantErr : nil ,
366+ },
367+ {
368+ name : "flat deletion" ,
369+ fsModFunc : func (t * testing.T , fs afero.Fs ) {
370+ createTestDir (t , fs , cacheRoot + "/ubuntu" )
371+ },
372+ fsCheckFunc : func (t * testing.T , fs afero.Fs ) {
373+ exists , err := afero .Exists (fs , cacheRoot + "/ubuntu" )
374+ assert .NoError (t , err )
375+ assert .False (t , exists , "dir still exists" )
376+ },
377+ wantErr : nil ,
378+ },
379+ {
380+ name : "recursive deletion 1" ,
381+ fsModFunc : func (t * testing.T , fs afero.Fs ) {
382+ createTestDir (t , fs , cacheRoot + "/ubuntu/20.10/20201027" )
383+ },
384+ fsCheckFunc : func (t * testing.T , fs afero.Fs ) {
385+ exists , err := afero .Exists (fs , cacheRoot + "/ubuntu/20.10/20201027" )
386+ assert .NoError (t , err )
387+ assert .False (t , exists , "dir still exists" )
388+
389+ exists , err = afero .Exists (fs , cacheRoot + "/ubuntu/20.10" )
390+ assert .NoError (t , err )
391+ assert .False (t , exists , "dir still exists" )
392+
393+ exists , err = afero .Exists (fs , cacheRoot + "/ubuntu" )
394+ assert .NoError (t , err )
395+ assert .False (t , exists , "dir still exists" )
396+ },
397+ wantErr : nil ,
398+ },
399+ {
400+ name : "recursive deletion 2" ,
401+ fsModFunc : func (t * testing.T , fs afero.Fs ) {
402+ createTestFile (t , fs , cacheRoot + "/ubuntu/20.04/20201028/img.tar.lz4" )
403+ createTestDir (t , fs , cacheRoot + "/ubuntu/20.10/20201027" )
404+ },
405+ fsCheckFunc : func (t * testing.T , fs afero.Fs ) {
406+ exists , err := afero .Exists (fs , cacheRoot + "/ubuntu/20.10/20201027" )
407+ assert .NoError (t , err )
408+ assert .False (t , exists , "dir still exists" )
409+
410+ exists , err = afero .Exists (fs , cacheRoot + "/ubuntu/20.10" )
411+ assert .NoError (t , err )
412+ assert .False (t , exists , "dir still exists" )
413+
414+ exists , err = afero .Exists (fs , cacheRoot + "/ubuntu" )
415+ assert .NoError (t , err )
416+ assert .True (t , exists , "dir was deleted" )
417+ },
418+ wantErr : nil ,
419+ },
420+ {
421+ name : "kind of realistic scenario" ,
422+
423+ fsModFunc : func (t * testing.T , fs afero.Fs ) {
424+ createTestDir (t , fs , cacheRoot + "/boot/metal-hammer/releases/download/v0.8.0" )
425+ createTestFile (t , fs , cacheRoot + "/boot/metal-hammer/pull-requests/pr-title/metal-hammer-initrd.img.lz4" )
426+ createTestFile (t , fs , cacheRoot + "/boot/metal-hammer/pull-requests/pr-title/metal-hammer-initrd.img.lz4.md5" )
427+ createTestFile (t , fs , cacheRoot + "/ubuntu/20.10/20201026/img.tar.lz4" )
428+ createTestFile (t , fs , cacheRoot + "/ubuntu/20.10/20201026/img.tar.lz4.md5" )
429+ createTestDir (t , fs , cacheRoot + "/firewall/2.0/20210131" )
430+ createTestDir (t , fs , cacheRoot + "/firewall/2.0/20210207" )
431+ createTestFile (t , fs , cacheRoot + "/firewall/2.0/20210304/img.tar.lz4" )
432+ createTestFile (t , fs , cacheRoot + "/firewall/2.0/20210304/img.tar.lz4.md5" )
433+ },
434+ fsCheckFunc : func (t * testing.T , fs afero.Fs ) {
435+ for _ , subPath := range []string {
436+ "/boot/metal-hammer/releases" ,
437+ "/firewall/2.0.20210131" ,
438+ "/firewall/2.0.20210207" ,
439+ } {
440+ exists , err := afero .Exists (fs , cacheRoot + subPath )
441+ assert .NoError (t , err )
442+ assert .False (t , exists , "dir still exists" )
443+ }
444+
445+ for _ , subPath := range []string {
446+ "/boot/metal-hammer/pull-requests/pr-title/metal-hammer-initrd.img.lz4" ,
447+ "/boot/metal-hammer/pull-requests/pr-title/metal-hammer-initrd.img.lz4.md5" ,
448+ "/ubuntu/20.10/20201026/img.tar.lz4" ,
449+ "/ubuntu/20.10/20201026/img.tar.lz4.md5" ,
450+ "/firewall/2.0/20210304/img.tar.lz4" ,
451+ "/firewall/2.0/20210304/img.tar.lz4.md5" ,
452+ } {
453+ exists , err := afero .Exists (fs , cacheRoot + subPath )
454+ assert .NoError (t , err )
455+ assert .True (t , exists , "dir was deleted" )
456+ }
457+ },
458+ wantErr : nil ,
459+ },
460+ }
461+ for _ , tt := range tests {
462+ tt := tt
463+ t .Run (tt .name , func (t * testing.T ) {
464+ fs := afero .NewMemMapFs ()
465+ require .Nil (t , fs .MkdirAll (cacheRoot , 0755 ))
466+ if tt .fsModFunc != nil {
467+ tt .fsModFunc (t , fs )
468+ }
469+
470+ err := cleanEmptyDirs (fs , cacheRoot )
471+ if diff := cmp .Diff (err , tt .wantErr ); diff != "" {
472+ t .Errorf ("cleanEmptyDirs() diff = %v" , diff )
473+ }
474+
475+ if tt .fsCheckFunc != nil {
476+ tt .fsCheckFunc (t , fs )
477+ }
478+ })
479+ }
480+ }
0 commit comments