From 4845ee13e9f6a798dc370cdd4b21d2087be0ff55 Mon Sep 17 00:00:00 2001 From: Ivan Poleshchuk Date: Thu, 22 Sep 2022 17:47:25 +0500 Subject: [PATCH] Add IPFS test --- .../catalyst-uploader_test.go | 59 +++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/cmd/catalyst-uploader/catalyst-uploader_test.go b/cmd/catalyst-uploader/catalyst-uploader_test.go index e21b6d4..457f609 100644 --- a/cmd/catalyst-uploader/catalyst-uploader_test.go +++ b/cmd/catalyst-uploader/catalyst-uploader_test.go @@ -9,6 +9,7 @@ import ( "github.com/google/uuid" "github.com/livepeer/go-tools/drivers" "github.com/stretchr/testify/assert" + "net/http" "net/url" "os" "os/exec" @@ -54,25 +55,35 @@ func testE2E(assert *assert.Assertions, fullUriStr string) { // load object and compare contents outUrl, _ := url.Parse(outJson.Uri) - fullUri, _ := outUrl.Parse(fullUriStr) - bucket := splitNonEmpty(fullUri.Path, '/')[0] - if !strings.Contains(outUrl.Host, bucket) { - // if bucket is not included in domain name of output URI, then it's already in the path - bucket = "" + if strings.Contains(fullUriStr, "ipfs://") { + cid := outUrl.Path + resp, err := http.Get("https://gateway.pinata.cloud/ipfs/" + cid) + assert.NoError(err) + defer resp.Body.Close() + ipfsData := new(bytes.Buffer) + ipfsData.ReadFrom(resp.Body) + assert.Equal(rndData, ipfsData.Bytes()) + } else { + fullUri, _ := outUrl.Parse(fullUriStr) + bucket := splitNonEmpty(fullUri.Path, '/')[0] + if !strings.Contains(outUrl.Host, bucket) { + // if bucket is not included in domain name of output URI, then it's already in the path + bucket = "" + } + // compare key after leading slash + assert.Equal(fullUri.Path, path.Clean("/"+bucket+"/"+outUrl.Path)) + os, err := drivers.ParseOSURL(fullUriStr, true) + assert.NoError(err) + session := os.NewSession("") + // second argument is object key and passed to API unmodified + data, err := session.ReadData(context.Background(), "") + assert.NoError(err) + assert.Equal(*data.Size, int64(len(rndData))) + osBuf := new(bytes.Buffer) + osBuf.ReadFrom(data.Body) + osData := osBuf.Bytes() + assert.Equal(rndData, osData) } - // compare key after leading slash - assert.Equal(fullUri.Path, path.Clean("/"+bucket+"/"+outUrl.Path)) - os, err := drivers.ParseOSURL(fullUriStr, true) - assert.NoError(err) - session := os.NewSession("") - // second argument is object key and passed to API unmodified - data, err := session.ReadData(context.Background(), "") - assert.NoError(err) - assert.Equal(*data.Size, int64(len(rndData))) - osBuf := new(bytes.Buffer) - osBuf.ReadFrom(data.Body) - osData := osBuf.Bytes() - assert.Equal(rndData, osData) } func TestFsHandlerE2E(t *testing.T) { @@ -127,6 +138,18 @@ func TestS3HandlerE2E(t *testing.T) { } } +func TestIpfsHandlerE2E(t *testing.T) { + assert := assert.New(t) + key := os.Getenv("PINATA_KEY") + secret := os.Getenv("PINATA_SECRET") + if secret != "" { + uri := fmt.Sprintf("ipfs://%s:%s@%s/", key, secret, "pinata.cloud") + testE2E(assert, uri) + } else { + fmt.Println("No IPFS provider credentials, test skipped") + } +} + func TestMinioHandlerE2E(t *testing.T) { assert := assert.New(t) s3key := os.Getenv("MINIO_S3_KEY")