github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/storage/filecoin_unsealed/storage_test.go (about)

     1  //go:build unit || !integration
     2  
     3  package filecoinunsealed
     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 FilecoinUnsealedSuite struct {
    25  	suite.Suite
    26  }
    27  
    28  func (suite *FilecoinUnsealedSuite) prepareCid(cid string) model.StorageSpec {
    29  	folderPath := filepath.Join(tempDir, cid)
    30  	err := os.MkdirAll(folderPath, os.ModePerm)
    31  	require.NoError(suite.T(), err)
    32  	return model.StorageSpec{
    33  		StorageSource: model.StorageSourceFilecoinUnsealed,
    34  		CID:           cid,
    35  		Path:          folderPath,
    36  	}
    37  }
    38  
    39  // In order for 'go test' to run this suite, we need to create
    40  // a normal test function and pass our suite to suite.Run
    41  func TestFilecoinUnsealedSuite(t *testing.T) {
    42  	suite.Run(t, new(FilecoinUnsealedSuite))
    43  }
    44  
    45  // Before each test
    46  func (suite *FilecoinUnsealedSuite) SetupTest() {
    47  	var setupErr error
    48  	logger.ConfigureTestLogging(suite.T())
    49  	cm = system.NewCleanupManager()
    50  	ctx = context.Background()
    51  	tempDir = suite.T().TempDir()
    52  	driver, setupErr = NewStorage(cm, filepath.Join(tempDir, "{{.CID}}"))
    53  	require.NoError(suite.T(), setupErr)
    54  }
    55  
    56  func (suite *FilecoinUnsealedSuite) TestIsInstalled() {
    57  	installed, err := driver.IsInstalled(ctx)
    58  	require.NoError(suite.T(), err)
    59  	require.True(suite.T(), installed)
    60  }
    61  
    62  func (suite *FilecoinUnsealedSuite) TestHasStorageLocally() {
    63  	cid := "123"
    64  	spec := suite.prepareCid(cid)
    65  	hasStorageTrue, err := driver.HasStorageLocally(ctx, spec)
    66  	require.NoError(suite.T(), err)
    67  	require.True(suite.T(), hasStorageTrue, "file that exists should return true for HasStorageLocally")
    68  	spec.CID = "apples"
    69  	hasStorageFalse, err := driver.HasStorageLocally(ctx, spec)
    70  	require.NoError(suite.T(), err)
    71  	require.False(suite.T(), hasStorageFalse, "file that does not exist should return false for HasStorageLocally")
    72  }
    73  
    74  func (suite *FilecoinUnsealedSuite) TestGetVolumeSize() {
    75  	cid := "123"
    76  	fileContents := "hello world"
    77  	spec := suite.prepareCid(cid)
    78  	filePath := filepath.Join(spec.Path, "file")
    79  	err := os.WriteFile(filePath, []byte(fileContents), 0644)
    80  	require.NoError(suite.T(), err)
    81  	volumeSize, err := driver.GetVolumeSize(ctx, spec)
    82  	require.NoError(suite.T(), err)
    83  	require.Equal(suite.T(), uint64(len(fileContents)), volumeSize, "the volume size should be the size of the file")
    84  }
    85  
    86  func (suite *FilecoinUnsealedSuite) TestPrepareStorage() {
    87  	cid := "123"
    88  	spec := suite.prepareCid(cid)
    89  	volume, err := driver.PrepareStorage(ctx, spec)
    90  	require.NoError(suite.T(), err)
    91  	require.Equal(suite.T(), spec.Path, volume.Source, "the volume source should be the same as the spec path")
    92  }
    93  
    94  func (suite *FilecoinUnsealedSuite) TestExplode() {
    95  	cid := "123"
    96  	spec := suite.prepareCid(cid)
    97  	exploded, err := driver.Explode(ctx, spec)
    98  	require.NoError(suite.T(), err)
    99  	require.Equal(suite.T(), len(exploded), 1, "the exploded list should be 1 item long")
   100  	require.Equal(suite.T(), exploded[0].CID, cid, "the cid is correct")
   101  }