github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/test/utils/utils.go (about) 1 package testutils 2 3 import ( 4 "context" 5 "fmt" 6 "regexp" 7 "testing" 8 "time" 9 10 "github.com/filecoin-project/bacalhau/pkg/model" 11 "github.com/filecoin-project/bacalhau/pkg/node" 12 "github.com/filecoin-project/bacalhau/pkg/requester/publicapi" 13 "github.com/filecoin-project/bacalhau/pkg/system" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func GetJobFromTestOutput(ctx context.Context, t *testing.T, c *publicapi.RequesterAPIClient, out string) model.Job { 18 jobID := system.FindJobIDInTestOutput(out) 19 uuidRegex := regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`) 20 require.Regexp(t, uuidRegex, jobID, "Job ID should be a UUID") 21 22 j, _, err := c.Get(ctx, jobID) 23 require.NoError(t, err) 24 require.NotNil(t, j, "Failed to get job with ID: %s", out) 25 return j.Job 26 } 27 28 func FirstFatalError(_ *testing.T, output string) (model.TestFatalErrorHandlerContents, error) { 29 linesInOutput := system.SplitLines(output) 30 fakeFatalError := &model.TestFatalErrorHandlerContents{} 31 for _, line := range linesInOutput { 32 err := model.JSONUnmarshalWithMax([]byte(line), fakeFatalError) 33 if err != nil { 34 return model.TestFatalErrorHandlerContents{}, err 35 } else { 36 return *fakeFatalError, nil 37 } 38 } 39 return model.TestFatalErrorHandlerContents{}, fmt.Errorf("no fatal error found in output") 40 } 41 42 func MakeGenericJob() *model.Job { 43 return MakeJob(model.EngineDocker, model.VerifierNoop, model.PublisherNoop, []string{ 44 "echo", 45 "$(date +%s)", 46 }) 47 } 48 49 func MakeNoopJob() *model.Job { 50 return MakeJob(model.EngineNoop, model.VerifierNoop, model.PublisherNoop, []string{ 51 "echo", 52 "$(date +%s)", 53 }) 54 } 55 56 func MakeJob( 57 engineType model.Engine, 58 verifierType model.Verifier, 59 publisherType model.Publisher, 60 entrypointArray []string) *model.Job { 61 j := model.NewJob() 62 63 j.Spec = model.Spec{ 64 Engine: engineType, 65 Verifier: verifierType, 66 Publisher: publisherType, 67 Docker: model.JobSpecDocker{ 68 Image: "ubuntu:latest", 69 Entrypoint: entrypointArray, 70 }, 71 } 72 73 j.Spec.Deal = model.Deal{ 74 Concurrency: 1, 75 } 76 77 return j 78 } 79 80 // WaitForNodeDiscovery for the requester node to pick up the nodeInfo messages 81 func WaitForNodeDiscovery(t *testing.T, requesterNode *node.Node, expectedNodeCount int) { 82 ctx := context.Background() 83 waitDuration := 10 * time.Second 84 waitGaps := 20 * time.Millisecond 85 waitUntil := time.Now().Add(waitDuration) 86 87 for time.Now().Before(waitUntil) { 88 nodeInfos, err := requesterNode.NodeInfoStore.List(ctx) 89 require.NoError(t, err) 90 if len(nodeInfos) == expectedNodeCount { 91 break 92 } 93 time.Sleep(waitGaps) 94 } 95 nodeInfos, err := requesterNode.NodeInfoStore.List(ctx) 96 require.NoError(t, err) 97 if len(nodeInfos) != expectedNodeCount { 98 require.FailNowf(t, fmt.Sprintf("requester node didn't read all node infos even after waiting for %s", waitDuration), 99 "expected 4 node infos, got %d. %+v", len(nodeInfos), nodeInfos) 100 } 101 }