Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

test: ipfs unit test #102

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions integration/suite/suite_ipfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package suite

import (
"fmt"
"time"

shell "github.com/ipfs/go-ipfs-api"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)

type TestSuiteIpfs struct {
suite.Suite

dktPool *dockertest.Pool
dktResource *dockertest.Resource
}

func (suite *TestSuiteIpfs) SetupSuite() {
dktPool, err := dockertest.NewPool("")
if err != nil {
log.Panicf("Could not connect to docker: %v", err)
}
suite.dktPool = dktPool
}

func (suite *TestSuiteIpfs) SetupTest() {
var err error

suite.dktResource, err = suite.dktPool.RunWithOptions(
&dockertest.RunOptions{
Name: "ipfs_host",
Repository: "ipfs/kubo",
Tag: "latest",
},
func(config *docker.HostConfig) {
config.AutoRemove = true // so that stopped containers are removed automatically
config.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
},
)
if err != nil {
log.Panicf("Could not start resource: %v", err)
}

suite.dktPool.MaxWait = 10 * time.Second
if err := suite.dktPool.Retry(func() error {
sh := shell.NewShell(suite.IpfsEndPoint(5001))
if !sh.IsUp() {
return fmt.Errorf("not ready")
}
return nil
}); err != nil {
log.Panicf("Could not connect to ipfs: %s", err)
}
}

func (suite *TestSuiteIpfs) TearDownTest() {
if err := suite.dktPool.Purge(suite.dktResource); err != nil {
log.Printf("Could not purge resource: %s", err)
}
}

func (suite *TestSuiteIpfs) IpfsEndPoint(port int) string {
return fmt.Sprintf(
"localhost:%s",
suite.dktResource.GetPort(fmt.Sprintf("%d/tcp", port)),
)
}
1 change: 0 additions & 1 deletion ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func NewIpfs(url string) *Ipfs {
newShell := shell.NewShell(url)

log.Info("successfully connect to IPFS node")

return &Ipfs{
sh: newShell,
}
Expand Down
108 changes: 64 additions & 44 deletions ipfs/ipfs_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,66 @@
package ipfs_test

//type testdata struct {
// Name string `json:"name"`
// Description string `json:"description"`
//}
//
//const (
// ipfsNodeAddr = "ipfs.io/ipfs"
//)
//
//func TestIpfsAdd(t *testing.T) {
// testData := &testdata{
// Name: "panacea",
// Description: "medibloc mainnet",
// }
//
// newIpfs := store.NewIpfs(ipfsNodeAddr)
//
// testDataBz, err := json.Marshal(testData)
// require.NoError(t, err)
//
// _, err = newIpfs.Add(testDataBz)
// require.NoError(t, err)
//
//}
//
//func TestIpfsGet(t *testing.T) {
//
// newIpfs := store.NewIpfs(ipfsNodeAddr)
//
// file, err := ioutil.ReadFile("./testdata/test_deal.json")
// require.NoError(t, err)
//
// cid, err := newIpfs.Add(file)
// require.NoError(t, err)
//
// getStrings, err := newIpfs.Get(cid)
// require.NoError(t, err)
//
// var deal types.Deal
// err = json.Unmarshal(file, &deal)
// require.NoError(t, err)
//
// require.Equal(t, deal.DataSchema, getStrings)
//}
import (
"encoding/json"
"math/rand"
"os"
"testing"

"github.com/medibloc/panacea-core/v2/x/datadeal/types"
"github.com/medibloc/panacea-doracle/integration/suite"
"github.com/medibloc/panacea-doracle/ipfs"
"github.com/stretchr/testify/require"
)

type ipfsTestSuite struct {
suite.TestSuiteIpfs
}

func TestIpfs(t *testing.T) {
suite.Run(t, new(ipfsTestSuite))
}

type testdata struct {
Name string `json:"name"`
Description string `json:"description"`
Id uint64 `json:"id"`
}

func (suite *ipfsTestSuite) TestIpfsAdd() {
newIpfs := ipfs.NewIpfs(suite.IpfsEndPoint(5001))

testData := &testdata{
inchori marked this conversation as resolved.
Show resolved Hide resolved
Name: "panacea",
Description: "medibloc mainnet",
Id: rand.Uint64(),
}

testDataBz, err := json.Marshal(testData)
require.NoError(suite.T(), err)

_, err = newIpfs.Add(testDataBz)
require.NoError(suite.T(), err)
}

func (suite *ipfsTestSuite) TestIpfsGet() {
newIpfs := ipfs.NewIpfs(suite.IpfsEndPoint(5001))

file, err := os.ReadFile("testdata/test_deal.json")
require.NoError(suite.T(), err)

cid, err := newIpfs.Add(file)
require.NoError(suite.T(), err)

getStrings, err := newIpfs.Get(cid)
require.NoError(suite.T(), err)

var deal types.Deal
err = json.Unmarshal(file, &deal)
require.NoError(suite.T(), err)

var deal2 types.Deal
err = json.Unmarshal(getStrings, &deal2)
require.NoError(suite.T(), err)

require.Equal(suite.T(), deal, deal2)
}
11 changes: 11 additions & 0 deletions ipfs/testdata/test_deal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data_schema": [
"https://json.schemastore.org/github-issue-forms.json"
],
"budget": {
"denom": "umed",
"amount": "1000000"
},
"max_num_data": 10000,
"buyer_address": "panacea1m0vxn07gpdnz76pd8qm9nhv80q9t8ljt8jj8mt"
}