github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/storage/local_directory/storage_test.go (about) 1 //go:build unit || !integration 2 3 package localdirectory 4 5 import ( 6 "context" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/filecoin-project/bacalhau/pkg/logger" 12 _ "github.com/filecoin-project/bacalhau/pkg/logger" 13 "github.com/filecoin-project/bacalhau/pkg/model" 14 "github.com/filecoin-project/bacalhau/pkg/system" 15 "github.com/stretchr/testify/require" 16 "github.com/stretchr/testify/suite" 17 ) 18 19 var ctx context.Context 20 var tempDir string 21 var driver *StorageProvider 22 var cm *system.CleanupManager 23 24 type LocalDirectorySuite struct { 25 suite.Suite 26 } 27 28 func (suite *LocalDirectorySuite) prepareStorageSpec(sourcePath string) model.StorageSpec { 29 folderPath := filepath.Join(tempDir, sourcePath) 30 err := os.MkdirAll(folderPath, os.ModePerm) 31 require.NoError(suite.T(), err) 32 return model.StorageSpec{ 33 // source path is some kind of sub-path 34 // inside our local folder 35 SourcePath: sourcePath, 36 Path: "/path/inside/the/container", 37 } 38 } 39 40 // In order for 'go test' to run this suite, we need to create 41 // a normal test function and pass our suite to suite.Run 42 func TestLocalDirectorySuite(t *testing.T) { 43 suite.Run(t, new(LocalDirectorySuite)) 44 } 45 46 // Before each test 47 func (suite *LocalDirectorySuite) SetupTest() { 48 var setupErr error 49 logger.ConfigureTestLogging(suite.T()) 50 cm = system.NewCleanupManager() 51 ctx = context.Background() 52 tempDir = suite.T().TempDir() 53 driver, setupErr = NewStorage(cm, tempDir) 54 require.NoError(suite.T(), setupErr) 55 } 56 57 func (suite *LocalDirectorySuite) TestIsInstalled() { 58 installed, err := driver.IsInstalled(ctx) 59 require.NoError(suite.T(), err) 60 require.True(suite.T(), installed) 61 } 62 63 func (suite *LocalDirectorySuite) TestHasStorageLocally() { 64 subpath := "apples/oranges" 65 spec := suite.prepareStorageSpec(subpath) 66 hasStorageTrue, err := driver.HasStorageLocally(ctx, spec) 67 require.NoError(suite.T(), err) 68 require.True(suite.T(), hasStorageTrue, "file that exists should return true for HasStorageLocally") 69 spec.SourcePath = "apples/pears" 70 hasStorageFalse, err := driver.HasStorageLocally(ctx, spec) 71 require.NoError(suite.T(), err) 72 require.False(suite.T(), hasStorageFalse, "file that does not exist should return false for HasStorageLocally") 73 } 74 75 func (suite *LocalDirectorySuite) TestGetVolumeSize() { 76 subpath := "apples/oranges" 77 folderPath := filepath.Join(tempDir, subpath) 78 fileContents := "hello world" 79 spec := suite.prepareStorageSpec(subpath) 80 filePath := filepath.Join(folderPath, "file") 81 err := os.WriteFile(filePath, []byte(fileContents), 0644) 82 require.NoError(suite.T(), err) 83 volumeSize, err := driver.GetVolumeSize(ctx, spec) 84 require.NoError(suite.T(), err) 85 require.Equal(suite.T(), uint64(len(fileContents)), volumeSize, "the volume size should be the size of the file") 86 } 87 88 func (suite *LocalDirectorySuite) TestPrepareStorage() { 89 subpath := "apples/oranges" 90 spec := suite.prepareStorageSpec(subpath) 91 volume, err := driver.PrepareStorage(ctx, spec) 92 require.NoError(suite.T(), err) 93 require.Equal(suite.T(), volume.Source, filepath.Join(tempDir, subpath), "the volume source is correct") 94 } 95 96 func (suite *LocalDirectorySuite) TestExplode() { 97 subpath := "apples/oranges" 98 spec := suite.prepareStorageSpec(subpath) 99 exploded, err := driver.Explode(ctx, spec) 100 require.NoError(suite.T(), err) 101 require.Equal(suite.T(), len(exploded), 1, "the exploded list should be 1 item long") 102 require.Equal(suite.T(), exploded[0].SourcePath, subpath, "the subpath is correct") 103 }