github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/downloader/estuary/downloader_test.go (about)

     1  //go:build integration || !unit
     2  
     3  package estuary
     4  
     5  import (
     6  	"context"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/filecoin-project/bacalhau/pkg/system"
    13  
    14  	"github.com/filecoin-project/bacalhau/pkg/model"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  const testCID = "bafkreihhfsv64fxhjix43i66vue6ezcwews3eg6tacxar7mnkqrg5vn6pe"
    19  const testURL = "https://api.estuary.tech/gw/ipfs/bafkreihhfsv64fxhjix43i66vue6ezcwews3eg6tacxar7mnkqrg5vn6pe"
    20  
    21  func TestFetchResult(t *testing.T) {
    22  	// create a new Estuary downloader
    23  	settings := &model.DownloaderSettings{
    24  		Timeout: time.Second * 60,
    25  	}
    26  	cm := system.NewCleanupManager()
    27  	downloader := NewEstuaryDownloader(cm, settings)
    28  
    29  	tests := []struct {
    30  		CID  string
    31  		Name string
    32  		URL  string
    33  	}{
    34  		{
    35  			CID: testCID, Name: testCID, URL: testURL,
    36  		},
    37  		{
    38  			CID: testCID, Name: "", URL: "",
    39  		},
    40  	}
    41  
    42  	for _, ts := range tests {
    43  		// create a temp directory for the downloaded file
    44  		downloadDir, err := os.MkdirTemp("", "estuary-download-testData-*")
    45  		require.NoError(t, err)
    46  		downloadPath := filepath.Join(downloadDir, testCID)
    47  
    48  		// create a PublishedResult with the test data
    49  		result := model.PublishedResult{
    50  			Data: model.StorageSpec{
    51  				StorageSource: model.StorageSourceEstuary,
    52  				Name:          ts.Name,
    53  				CID:           ts.CID,
    54  				URL:           ts.URL,
    55  			},
    56  		}
    57  
    58  		// call FetchResult to download the file
    59  		err = downloader.FetchResult(context.Background(), result, downloadPath)
    60  		require.NoError(t, err)
    61  
    62  		// check that the file was downloaded to the correct location
    63  		if _, err := os.Stat(downloadPath); os.IsNotExist(err) {
    64  			t.Errorf("Expected file %s to be downloaded, but it does not exist", downloadPath)
    65  		}
    66  
    67  		// check the content of the downloaded file
    68  		data, err := os.ReadFile(downloadPath)
    69  		require.NoError(t, err)
    70  
    71  		require.Equal(t, "Hello From Bacalhau\n", string(data))
    72  
    73  		err = os.RemoveAll(downloadDir)
    74  		require.NoError(t, err)
    75  	}
    76  }