github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/test/ipfs/ipfs_host_storage_test.go (about) 1 //go:build unit || !integration 2 3 package ipfs 4 5 import ( 6 "context" 7 "fmt" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/filecoin-project/bacalhau/pkg/ipfs" 13 "github.com/filecoin-project/bacalhau/pkg/logger" 14 _ "github.com/filecoin-project/bacalhau/pkg/logger" 15 "github.com/filecoin-project/bacalhau/pkg/model" 16 "github.com/filecoin-project/bacalhau/pkg/storage" 17 ipfs_storage "github.com/filecoin-project/bacalhau/pkg/storage/ipfs" 18 "github.com/filecoin-project/bacalhau/pkg/system" 19 "github.com/spf13/cobra" 20 "github.com/stretchr/testify/require" 21 "github.com/stretchr/testify/suite" 22 ) 23 24 type IPFSHostStorageSuite struct { 25 suite.Suite 26 rootCmd *cobra.Command 27 } 28 29 // In order for 'go test' to run this suite, we need to create 30 // a normal test function and pass our suite to suite.Run 31 func TestIPFSHostStorageSuite(t *testing.T) { 32 suite.Run(t, new(IPFSHostStorageSuite)) 33 } 34 35 // Before each test 36 func (suite *IPFSHostStorageSuite) SetupTest() { 37 logger.ConfigureTestLogging(suite.T()) 38 err := system.InitConfigForTesting(suite.T()) 39 require.NoError(suite.T(), err) 40 } 41 42 type getStorageFunc func(ctx context.Context, cm *system.CleanupManager, api ipfs.Client) ( 43 storage.Storage, error) 44 45 func (suite *IPFSHostStorageSuite) TestIpfsApiCopyFile() { 46 runFileTest( 47 suite.T(), 48 model.StorageSourceIPFS, 49 func(ctx context.Context, cm *system.CleanupManager, api ipfs.Client) ( 50 storage.Storage, error) { 51 52 return ipfs_storage.NewStorage(cm, api) 53 }, 54 ) 55 } 56 57 func (suite *IPFSHostStorageSuite) TestIPFSAPICopyFolder() { 58 runFolderTest( 59 suite.T(), 60 model.StorageSourceIPFS, 61 func(ctx context.Context, cm *system.CleanupManager, api ipfs.Client) ( 62 storage.Storage, error) { 63 64 return ipfs_storage.NewStorage(cm, api) 65 }, 66 ) 67 } 68 69 func runFileTest(t *testing.T, engine model.StorageSourceType, getStorageDriver getStorageFunc) { 70 ctx := context.Background() 71 // get a single IPFS server 72 stack, cm := SetupTest(ctx, t, 1) 73 defer TeardownTest(cm) 74 75 // add this file to the server 76 EXAMPLE_TEXT := `hello world` 77 fileCid, err := ipfs.AddTextToNodes(ctx, []byte(EXAMPLE_TEXT), stack.IPFSClients[0]) 78 require.NoError(t, err) 79 80 // construct an ipfs docker storage client 81 storageDriver, err := getStorageDriver(ctx, cm, stack.IPFSClients[0]) 82 require.NoError(t, err) 83 84 // the storage spec for the cid we added 85 storage := model.StorageSpec{ 86 StorageSource: engine, 87 CID: fileCid, 88 Path: "/data/file.txt", 89 } 90 91 // does the storage client think we have the cid locally? 92 hasCid, err := storageDriver.HasStorageLocally(ctx, storage) 93 require.NoError(t, err) 94 require.True(t, hasCid) 95 96 volume, err := storageDriver.PrepareStorage(ctx, storage) 97 require.NoError(t, err) 98 99 // we should now be able to read our file content 100 // from the file on the host via fuse 101 r, err := os.ReadFile(volume.Source) 102 require.NoError(t, err) 103 require.Equal(t, string(r), EXAMPLE_TEXT) 104 105 err = storageDriver.CleanupStorage(ctx, storage, volume) 106 require.NoError(t, err) 107 } 108 109 func runFolderTest(t *testing.T, engine model.StorageSourceType, getStorageDriver getStorageFunc) { 110 ctx := context.Background() 111 // get a single IPFS server 112 stack, cm := SetupTest(ctx, t, 1) 113 defer TeardownTest(cm) 114 115 dir := t.TempDir() 116 117 EXAMPLE_TEXT := `hello world` 118 err := os.WriteFile(fmt.Sprintf("%s/file.txt", dir), []byte(EXAMPLE_TEXT), 0644) 119 require.NoError(t, err) 120 121 // add this file to the server 122 folderCid, err := ipfs.AddFileToNodes(ctx, dir, stack.IPFSClients[0]) 123 require.NoError(t, err) 124 125 // construct an ipfs docker storage client 126 storageDriver, err := getStorageDriver(ctx, cm, stack.IPFSClients[0]) 127 require.NoError(t, err) 128 129 // the storage spec for the cid we added 130 storage := model.StorageSpec{ 131 StorageSource: engine, 132 CID: folderCid, 133 Path: "/data/folder", 134 } 135 136 // does the storage client think we have the cid locally? 137 hasCid, err := storageDriver.HasStorageLocally(ctx, storage) 138 require.NoError(t, err) 139 require.True(t, hasCid) 140 141 volume, err := storageDriver.PrepareStorage(ctx, storage) 142 require.NoError(t, err) 143 144 // we should now be able to read our file content 145 // from the file on the host via fuse 146 147 r, err := os.ReadFile(filepath.Join(volume.Source, "file.txt")) 148 require.NoError(t, err) 149 require.Equal(t, string(r), EXAMPLE_TEXT) 150 151 err = storageDriver.CleanupStorage(ctx, storage, volume) 152 require.NoError(t, err) 153 }