github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/test/requester/publicapi/server_test.go (about) 1 //go:build unit || !integration 2 3 package publicapi 4 5 import ( 6 "context" 7 "testing" 8 9 "github.com/filecoin-project/bacalhau/pkg/logger" 10 "github.com/filecoin-project/bacalhau/pkg/model" 11 "github.com/filecoin-project/bacalhau/pkg/node" 12 requester_publicapi "github.com/filecoin-project/bacalhau/pkg/requester/publicapi" 13 testutils "github.com/filecoin-project/bacalhau/pkg/test/utils" 14 "github.com/google/uuid" 15 "github.com/stretchr/testify/require" 16 "github.com/stretchr/testify/suite" 17 ) 18 19 // Define the suite, and absorb the built-in basic suite 20 // functionality from testify - including a T() method which 21 // returns the current testing context 22 type ServerSuite struct { 23 suite.Suite 24 node *node.Node 25 client *requester_publicapi.RequesterAPIClient 26 } 27 28 // In order for 'go test' to run this suite, we need to create 29 // a normal test function and pass our suite to suite.Run 30 func TestServerSuite(t *testing.T) { 31 suite.Run(t, new(ServerSuite)) 32 } 33 34 // Before each test 35 func (s *ServerSuite) SetupTest() { 36 logger.ConfigureTestLogging(s.T()) 37 n, client := setupNodeForTest(s.T()) 38 s.node = n 39 s.client = client 40 } 41 42 // After each test 43 func (s *ServerSuite) TearDownTest() { 44 s.node.CleanupManager.Cleanup(context.Background()) 45 } 46 47 func (s *ServerSuite) TestList() { 48 ctx := context.Background() 49 50 // Should have no jobs initially: 51 jobs, err := s.client.List(ctx, "", model.IncludeAny, model.ExcludeNone, 10, true, "created_at", true) 52 require.NoError(s.T(), err) 53 require.Empty(s.T(), jobs) 54 55 // Submit a random job to the node: 56 j := testutils.MakeNoopJob() 57 58 _, err = s.client.Submit(ctx, j) 59 require.NoError(s.T(), err) 60 61 // Should now have one job: 62 jobs, err = s.client.List(ctx, "", model.IncludeAny, model.ExcludeNone, 10, true, "created_at", true) 63 require.NoError(s.T(), err) 64 require.Len(s.T(), jobs, 1) 65 } 66 67 func (s *ServerSuite) TestSubmitRejectsJobWithSigilHeader() { 68 j := testutils.MakeNoopJob() 69 jobID, err := uuid.NewRandom() 70 require.NoError(s.T(), err) 71 72 s.client.DefaultHeaders["X-Bacalhau-Job-ID"] = jobID.String() 73 _, err = s.client.Submit(context.Background(), j) 74 require.Error(s.T(), err) 75 }