@@ -19,6 +19,7 @@ import (
19
19
"github.com/spf13/afero"
20
20
"github.com/stretchr/testify/mock"
21
21
"github.com/stretchr/testify/require"
22
+ "golang.org/x/exp/slices"
22
23
23
24
"github.com/grafana/pyroscope/pkg/phlaredb"
24
25
"github.com/grafana/pyroscope/pkg/phlaredb/shipper"
@@ -427,11 +428,16 @@ func TestFSBlockManager(t *testing.T) {
427
428
fs .markBlocksShippedForTenant (t , tenantID , uploadedBlockIDs ... )
428
429
}
429
430
431
+ // Create a lost+found directory.
432
+ fs .createDirectories (t , "lost+found" )
433
+
430
434
t .Run ("GetTenantIDs" , func (t * testing.T ) {
431
435
bm := newFSBlockManager (root , e , fs )
432
436
tenantIDs , err := bm .GetTenantIDs (context .Background ())
433
437
require .NoError (t , err )
434
438
require .Equal (t , []string {"1218" , "anonymous" }, tenantIDs )
439
+ // Explicitly check lost+found isn't in tenant id list.
440
+ require .NotContains (t , tenantIDs , "lost+found" )
435
441
})
436
442
437
443
t .Run ("GetBlocksForTenant" , func (t * testing.T ) {
@@ -461,6 +467,55 @@ func TestFSBlockManager(t *testing.T) {
461
467
})
462
468
}
463
469
470
+ func TestFSBlockManager_isTenantDir (t * testing.T ) {
471
+ const root = "/data"
472
+ dirPaths := []string {
473
+ // Skip, not tenant ids
474
+ "lost+found" ,
475
+ ".DS_Store" ,
476
+
477
+ // Skip, no local dir
478
+ "1234/head/01HKWWF79V1STKXBNYW7WCMDGM" ,
479
+ "1234/head/01HKWWF8939QM6E7BS69X0RASG" ,
480
+
481
+ // Tenant dirs
482
+ "anonymous/local/01HKWWF3CTFC5EJN6JJ96TY4W9" ,
483
+ "anonymous/local/01HKWWF4C298KVTEEQ3RW6TVHZ" ,
484
+ "1218/local/01HKWWF5BB2DJVDP0DTMT9MDMN" ,
485
+ "1218/local/01HKWWF6AKVZDCWQB12MHWG7FN" ,
486
+ "9876/local" ,
487
+ }
488
+ filePaths := []string {
489
+ // Skip all files
490
+ "somefile.txt" ,
491
+ }
492
+
493
+ fs := & mockFS {
494
+ Fs : afero .NewMemMapFs (),
495
+ Root : root ,
496
+ }
497
+ fs .createDirectories (t , dirPaths ... )
498
+ fs .createFiles (t , filePaths ... )
499
+
500
+ gotTenantIDs := []string {}
501
+ entries , err := fs .ReadDir (fs .Root )
502
+ require .NoError (t , err )
503
+
504
+ bm := & realFSBlockManager {
505
+ Root : fs .Root ,
506
+ FS : fs ,
507
+ }
508
+ for _ , entry := range entries {
509
+ if bm .isTenantDir (fs .Root , entry ) {
510
+ gotTenantIDs = append (gotTenantIDs , entry .Name ())
511
+ }
512
+ }
513
+ slices .Sort (gotTenantIDs )
514
+
515
+ wantTenantIDs := []string {"1218" , "9876" , "anonymous" }
516
+ require .Equal (t , wantTenantIDs , gotTenantIDs )
517
+ }
518
+
464
519
func TestSortBlocks (t * testing.T ) {
465
520
createAnonymousBlock := func (t * testing.T , blockID string , uploaded bool ) * tenantBlock {
466
521
t .Helper ()
@@ -591,6 +646,30 @@ func (mfs *mockFS) markBlocksShippedForTenant(t *testing.T, tenantID string, blo
591
646
}
592
647
}
593
648
649
+ func (mfs * mockFS ) createDirectories (t * testing.T , paths ... string ) {
650
+ t .Helper ()
651
+ for _ , path := range paths {
652
+ path = filepath .Join (mfs .Root , path )
653
+ err := mfs .MkdirAll (path , 0755 )
654
+ if err != nil {
655
+ t .Fatalf ("failed to create directory: %s: %v" , path , err )
656
+ return
657
+ }
658
+ }
659
+ }
660
+
661
+ func (mfs * mockFS ) createFiles (t * testing.T , paths ... string ) {
662
+ t .Helper ()
663
+ for _ , path := range paths {
664
+ path = filepath .Join (mfs .Root , path )
665
+ _ , err := mfs .Create (path )
666
+ if err != nil {
667
+ t .Fatalf ("failed to create file: %s: %v" , path , err )
668
+ return
669
+ }
670
+ }
671
+ }
672
+
594
673
type mockBlockManager struct {
595
674
mock.Mock
596
675
}
0 commit comments