github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/compute/bidstrategy/input_locality_strategy_test.go (about)

     1  //go:build unit || !integration
     2  
     3  package bidstrategy
     4  
     5  import (
     6  	"context"
     7  	"testing"
     8  
     9  	"github.com/filecoin-project/bacalhau/pkg/executor"
    10  	noop_executor "github.com/filecoin-project/bacalhau/pkg/executor/noop"
    11  	"github.com/filecoin-project/bacalhau/pkg/model"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type InputLocalityStrategySuite struct {
    16  	suite.Suite
    17  	statelessJob BidStrategyRequest
    18  	statefulJob  BidStrategyRequest
    19  }
    20  
    21  func (s *InputLocalityStrategySuite) SetupSuite() {
    22  	s.statelessJob = getBidStrategyRequest()
    23  	s.statefulJob = getBidStrategyRequestWithInput()
    24  }
    25  
    26  func (s *InputLocalityStrategySuite) TestInputLocality() {
    27  	testCases := []struct {
    28  		name              string
    29  		policy            model.JobSelectionDataLocality
    30  		hasStorageLocally bool
    31  		expectedShouldBid bool
    32  		request           BidStrategyRequest
    33  	}{
    34  		// we are local - we do have the file - we should accept
    35  		{
    36  			"local mode -> have file -> should accept",
    37  			model.Local,
    38  			true,
    39  			true,
    40  			s.statefulJob,
    41  		},
    42  
    43  		// we are local - we don't have the file - we should reject
    44  		{
    45  			"local mode -> don't have file -> should reject",
    46  			model.Local,
    47  			false,
    48  			false,
    49  			s.statefulJob,
    50  		},
    51  
    52  		// we are local - stateless job - we should accept
    53  		{
    54  			"local mode -> stateless job -> should accept",
    55  			model.Local,
    56  			false,
    57  			true,
    58  			s.statelessJob,
    59  		},
    60  
    61  		// we are anywhere - we do have the file - we should accept
    62  		{
    63  			"anywhere mode -> have file -> should accept",
    64  			model.Anywhere,
    65  			true,
    66  			true,
    67  			s.statefulJob,
    68  		},
    69  
    70  		// we are anywhere - we don't have the file - we should accept
    71  		{
    72  			"anywhere mode -> don't have file -> should accept",
    73  			model.Anywhere,
    74  			false,
    75  			true,
    76  			s.statefulJob,
    77  		},
    78  
    79  		// we are anywhere - stateless job - we should accept
    80  		{
    81  			"anywhere mode ->s tateless job -> should accept",
    82  			model.Anywhere,
    83  			false,
    84  			true,
    85  			s.statelessJob,
    86  		},
    87  	}
    88  
    89  	for _, test := range testCases {
    90  		s.Run(test.name, func() {
    91  			noop_executor := noop_executor.NewNoopExecutorWithConfig(noop_executor.ExecutorConfig{
    92  				ExternalHooks: noop_executor.ExecutorConfigExternalHooks{
    93  					HasStorageLocally: func(ctx context.Context, volume model.StorageSpec) (bool, error) {
    94  						return test.hasStorageLocally, nil
    95  					},
    96  				},
    97  			})
    98  			params := InputLocalityStrategyParams{
    99  				Locality:  test.policy,
   100  				Executors: model.NewNoopProvider[model.Engine, executor.Executor](noop_executor),
   101  			}
   102  			strategy := NewInputLocalityStrategy(params)
   103  			result, err := strategy.ShouldBid(context.Background(), test.request)
   104  			s.NoError(err)
   105  			s.Equal(test.expectedShouldBid, result.ShouldBid)
   106  		})
   107  	}
   108  }
   109  
   110  func TestInputLocalityStrategySuite(t *testing.T) {
   111  	suite.Run(t, new(InputLocalityStrategySuite))
   112  }