github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publisher/combo/combo_test.go (about)

     1  package combo
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/filecoin-project/bacalhau/pkg/model"
    10  	"github.com/filecoin-project/bacalhau/pkg/publisher"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  type mockPublisher struct {
    15  	isInstalled           bool
    16  	isInstalledErr        error
    17  	publishShardResult    model.StorageSpec
    18  	publishShardResultErr error
    19  	sleepTime             time.Duration
    20  }
    21  
    22  // IsInstalled implements publisher.Publisher
    23  func (m *mockPublisher) IsInstalled(context.Context) (bool, error) {
    24  	time.Sleep(m.sleepTime)
    25  	return m.isInstalled, m.isInstalledErr
    26  }
    27  
    28  // PublishShardResult implements publisher.Publisher
    29  func (m *mockPublisher) PublishShardResult(context.Context, model.JobShard, string, string) (model.StorageSpec, error) {
    30  	time.Sleep(m.sleepTime)
    31  	return m.publishShardResult, m.publishShardResultErr
    32  }
    33  
    34  var _ publisher.Publisher = (*mockPublisher)(nil)
    35  
    36  var healthyPublisher = mockPublisher{
    37  	isInstalled:        true,
    38  	publishShardResult: model.StorageSpec{Name: "test output"},
    39  }
    40  
    41  var errorPublisher = mockPublisher{
    42  	isInstalledErr:        fmt.Errorf("test error"),
    43  	publishShardResultErr: fmt.Errorf("test error"),
    44  }
    45  
    46  type comboTestCase struct {
    47  	publisher       publisher.Publisher
    48  	expectPublisher mockPublisher
    49  }
    50  
    51  func runTestCase(t *testing.T, name string, testCase comboTestCase) {
    52  	t.Run(name+"/IsInstalled", func(t *testing.T) {
    53  		result, err := testCase.publisher.IsInstalled(context.Background())
    54  		require.Equal(t, testCase.expectPublisher.isInstalledErr == nil, err == nil, err)
    55  		require.Equal(t, testCase.expectPublisher.isInstalled, result)
    56  	})
    57  	t.Run(name+"/PublishShardResult", func(t *testing.T) {
    58  		result, err := testCase.publisher.PublishShardResult(context.Background(), model.JobShard{}, "", "")
    59  		require.Equal(t, testCase.expectPublisher.publishShardResultErr == nil, err == nil, err)
    60  		require.Equal(t, testCase.expectPublisher.publishShardResult, result)
    61  	})
    62  }
    63  
    64  func runTestCases(t *testing.T, testCases map[string]comboTestCase) {
    65  	for name, testCase := range testCases {
    66  		runTestCase(t, name, testCase)
    67  	}
    68  }