github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publisher/filecoin_lotus/publisher_test.go (about) 1 package filecoinlotus 2 3 import ( 4 "context" 5 "github.com/filecoin-project/bacalhau/pkg/model" 6 "github.com/filecoin-project/bacalhau/pkg/publisher/filecoin_lotus/api" 7 "github.com/filecoin-project/bacalhau/pkg/publisher/filecoin_lotus/api/storagemarket" 8 "github.com/filecoin-project/go-address" 9 abi2 "github.com/filecoin-project/go-state-types/abi" 10 big2 "github.com/filecoin-project/go-state-types/big" 11 "github.com/golang/mock/gomock" 12 "github.com/ipfs/go-cid" 13 "github.com/libp2p/go-libp2p/core/peer" 14 "github.com/stretchr/testify/suite" 15 "os" 16 "path/filepath" 17 "testing" 18 "time" 19 ) 20 21 type PublisherTestSuite struct { 22 suite.Suite 23 executor *Publisher 24 client *MockClient 25 } 26 27 // This test aims to ensure the publisher is covered by _some_ tests, as the tests in `pkg/test/devstack/lotus_test.go` 28 // are flaking in CI so are currently skipped. 29 func TestPublisherTestSuite(t *testing.T) { 30 suite.Run(t, new(PublisherTestSuite)) 31 } 32 33 func (s *PublisherTestSuite) SetupTest() { 34 ctrl := gomock.NewController(s.T()) 35 s.T().Cleanup(ctrl.Finish) 36 s.client = NewMockClient(ctrl) 37 s.executor = newPublisher(PublisherConfig{ 38 StorageDuration: 1 * time.Hour, 39 MaximumPing: 1 * time.Second, 40 }, s.client) 41 } 42 43 func (s *PublisherTestSuite) TestIsInstalled() { 44 s.client.EXPECT().Version(gomock.Any()).Return(api.APIVersion{Version: "hello"}, nil) 45 actual, err := s.executor.IsInstalled(context.Background()) 46 s.NoError(err) 47 s.True(actual) 48 } 49 50 func (s *PublisherTestSuite) TestPublishToLotus() { 51 contentCid := cid.MustParse("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi") 52 add, err := address.NewIDAddress(1234) 53 s.Require().NoError(err) 54 55 gomock.InOrder( 56 s.client.EXPECT(). 57 ClientImport(gomock.Any(), gomock.Any()). 58 DoAndReturn(func(ctx context.Context, ref api.FileRef) (*api.ImportRes, error) { 59 s.True(ref.IsCAR) 60 return &api.ImportRes{ 61 Root: contentCid, 62 ImportID: 0, 63 }, nil 64 }), 65 s.client.EXPECT().ClientDealPieceCID(gomock.Any(), contentCid).Return(api.DataCIDSize{ 66 PieceSize: 512, 67 PieceCID: contentCid, 68 }, nil), 69 s.client.EXPECT().StateGetNetworkParams(gomock.Any()).Return(&api.NetworkParams{BlockDelaySecs: 30}, nil), 70 s.client.EXPECT().WalletDefaultAddress(gomock.Any()).Return(add, nil), 71 s.client.EXPECT().StateListMiners(gomock.Any(), gomock.Any()).Return([]address.Address{add}, nil), 72 s.client.EXPECT().StateMinerInfo(gomock.Any(), add, gomock.Any()).Return(api.MinerInfo{ 73 PeerId: pointer[peer.ID]("4321"), 74 }, nil), 75 s.client.EXPECT().StateMinerPower(gomock.Any(), add, gomock.Any()).Return(&api.MinerPower{HasMinPower: true}, nil), 76 s.client.EXPECT(). 77 ClientQueryAsk(gomock.Any(), peer.ID("4321"), add). 78 Return(&api.StorageAsk{Response: &storagemarket.StorageAsk{ 79 Price: big2.NewInt(512), 80 MinPieceSize: 128, 81 MaxPieceSize: 1024, 82 }}, nil), 83 s.client.EXPECT(). 84 ClientStartDeal(gomock.Any(), gomock.Any()). 85 DoAndReturn(func(_ context.Context, deal *api.StartDealParams) (*cid.Cid, error) { 86 s.Equal("graphsync", deal.Data.TransferType) 87 s.Equal(contentCid, deal.Data.Root) 88 s.Equal(contentCid, *deal.Data.PieceCid) 89 s.Equal(abi2.UnpaddedPieceSize(508), deal.Data.PieceSize) 90 s.Equal(add, deal.Wallet) 91 s.Equal(add, deal.Miner) 92 s.Equal(big2.NewInt(0), deal.EpochPrice) 93 s.Equal(uint64(120), deal.MinBlocksDuration) 94 return &contentCid, nil 95 }), 96 s.client.EXPECT().ClientGetDealUpdates(gomock.Any()).DoAndReturn(func(context.Context) (<-chan api.DealInfo, error) { 97 c := make(chan api.DealInfo, 2) 98 c <- api.DealInfo{ 99 ProposalCid: contentCid, 100 State: storagemarket.StorageDealAcceptWait, 101 } 102 c <- api.DealInfo{ 103 ProposalCid: contentCid, 104 State: storagemarket.StorageDealCheckForAcceptance, 105 } 106 return c, nil 107 }), 108 ) 109 110 resultsDir := s.T().TempDir() 111 s.Require().NoError(os.WriteFile(filepath.Join(resultsDir, "hello.txt"), []byte("world"), 0644)) 112 113 spec, err := s.executor.PublishShardResult(context.Background(), model.JobShard{ 114 Job: &model.Job{Metadata: model.Metadata{ID: "foo"}}, 115 Index: 0, 116 }, "1234", resultsDir) 117 s.Require().NoError(err) 118 119 s.Equal(contentCid.String(), spec.CID) 120 } 121 122 func pointer[T any](t T) *T { 123 return &t 124 } 125 126 //go:generate go run github.com/golang/mock/mockgen -destination mock_test.go -package filecoinlotus -write_package_comment=false -source ./api/api.go Client