diff --git a/modules/mesh-delivery-builder/internal/build/steps.go b/modules/mesh-delivery-builder/internal/build/steps.go new file mode 100644 index 0000000..3a84936 --- /dev/null +++ b/modules/mesh-delivery-builder/internal/build/steps.go @@ -0,0 +1,67 @@ +package build + +import ( + "context" + "encoding/json" + "fmt" + "time" + + "git.novox.be/novox/mesh-sdk/go/delivery" +) + +// Caller calls a tool of a module on one named machine (mesh-sdk process.Conn.CallOn; ADR 0469 clause 3). +type Caller interface { + CallOn(node, key string, body any) (json.RawMessage, error) +} + +// Steps waits for the end of a build module's long step: the value of its key `.` in the module's steps state +// (ADR 0469 clause 2) once its state is not running. It is a watch of that key, not a poll; it returns when the step +// ends or ctx does. +type Steps interface { + Wait(ctx context.Context, module, key string) (delivery.Step, error) +} + +// Runner runs a build module's long steps: the call answers at once with the step's key, and the result is read from that +// key when the step ends (ADR 0469 clause 2). +type Runner struct { + Caller Caller + Steps Steps + // Bound is how long a step may run before the builder stops waiting for it. + Bound time.Duration +} + +// Run calls tool of the build module on node with args, waits for the step's end and answers its result. A step that +// failed or was refused is an error naming the node, the state and the reason. +func (r Runner) Run(ctx context.Context, module, node, tool string, args map[string]any) (json.RawMessage, error) { + raw, err := r.Caller.CallOn(node, module+"."+tool, args) + if err != nil { + return nil, fmt.Errorf("%s on %s: %w", tool, node, err) + } + var answer struct { + Step string `json:"step"` + Key string `json:"key"` + State string `json:"state"` + } + if err := json.Unmarshal(raw, &answer); err != nil { + return nil, fmt.Errorf("%s on %s answered what is not a step: %w", tool, node, err) + } + if answer.Key == "" { + return nil, fmt.Errorf("%s on %s answered no step key: %s", tool, node, raw) + } + if n, _, err := delivery.ParseStepKey(answer.Key); err != nil || n != node { + return nil, fmt.Errorf("%s on %s answered the step key %q, which is not a key of that node", tool, node, answer.Key) + } + wait, cancel := context.WithTimeoutCause(ctx, r.Bound, fmt.Errorf("the step %s did not end within %s", answer.Key, r.Bound)) + defer cancel() + step, err := r.Steps.Wait(wait, module, answer.Key) + if err != nil { + if cause := context.Cause(wait); cause != nil && ctx.Err() == nil { + err = cause + } + return nil, fmt.Errorf("%s on %s: %w", tool, node, err) + } + if step.State != delivery.StepPassed { + return nil, fmt.Errorf("%s on %s: the step %s %s: %s", tool, node, answer.Key, step.State, step.Reason) + } + return step.Result, nil +} diff --git a/modules/mesh-delivery-builder/internal/build/steps_test.go b/modules/mesh-delivery-builder/internal/build/steps_test.go new file mode 100644 index 0000000..f6963ac --- /dev/null +++ b/modules/mesh-delivery-builder/internal/build/steps_test.go @@ -0,0 +1,103 @@ +package build + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + "time" + + "git.novox.be/novox/mesh-sdk/go/delivery" +) + +type call struct { + node, key string + body any +} + +// fakeCaller answers a long tool as the build modules do: at once, with the step and its key. +type fakeCaller struct { + calls []call + answer string + err error +} + +func (f *fakeCaller) CallOn(node, key string, body any) (json.RawMessage, error) { + f.calls = append(f.calls, call{node, key, body}) + if f.err != nil { + return nil, f.err + } + return json.RawMessage(f.answer), nil +} + +// fakeSteps holds the steps values by `/`; Wait on a key it does not hold blocks until the context ends. +type fakeSteps struct{ steps map[string]delivery.Step } + +func (f *fakeSteps) Wait(ctx context.Context, module, key string) (delivery.Step, error) { + if s, ok := f.steps[module+"/"+key]; ok { + return s, nil + } + <-ctx.Done() + return delivery.Step{}, context.Cause(ctx) +} + +func stepAt(node, id, state, reason string, result string) delivery.Step { + s := delivery.Step{ID: id, Tool: "build_go_fetch", Node: node, State: state, Reason: reason} + if result != "" { + s.Result = json.RawMessage(result) + } + return s +} + +func TestRunCallsTheToolOnTheNodeAndReadsTheStepsKey(t *testing.T) { // 1607/AC5 + caller := &fakeCaller{answer: `{"step":"abc1","key":"ace.abc1","state":"running"}`} + steps := &fakeSteps{steps: map[string]delivery.Step{"mesh-build-go/ace.abc1": stepAt("ace", "abc1", delivery.StepPassed, "", `{"workspace":"w1","commit":"c"}`)}} + res, err := Runner{Caller: caller, Steps: steps, Bound: time.Second}.Run(context.Background(), "mesh-build-go", "ace", "build_go_fetch", map[string]any{"repository": "novox/r", "commit": "c"}) + if err != nil { + t.Fatal(err) + } + if len(caller.calls) != 1 || caller.calls[0].node != "ace" || caller.calls[0].key != "mesh-build-go.build_go_fetch" { + t.Errorf("calls = %+v, want one on ace for mesh-build-go.build_go_fetch", caller.calls) + } + var fr struct{ Workspace string } + if err := json.Unmarshal(res, &fr); err != nil || fr.Workspace != "w1" { + t.Errorf("result = %s (%v), want the step's result", res, err) + } +} + +func TestRunFailsOnAFailedOrRefusedStepNamingItsReason(t *testing.T) { // 1607/AC5 + for _, state := range []string{delivery.StepFailed, delivery.StepRefused} { + caller := &fakeCaller{answer: `{"step":"abc1","key":"ace.abc1","state":"running"}`} + steps := &fakeSteps{steps: map[string]delivery.Step{"mesh-build-go/ace.abc1": stepAt("ace", "abc1", state, "go is go1.26.0, the recipe builds with go1.27.1", "")}} + _, err := Runner{Caller: caller, Steps: steps, Bound: time.Second}.Run(context.Background(), "mesh-build-go", "ace", "build_go_build", nil) + if err == nil || !strings.Contains(err.Error(), "go1.27.1") || !strings.Contains(err.Error(), state) || !strings.Contains(err.Error(), "ace") { + t.Errorf("%s: error = %v, want the node, the state and the reason", state, err) + } + } +} + +func TestRunStopsWaitingAtTheBound(t *testing.T) { // 1607/AC5 + caller := &fakeCaller{answer: `{"step":"abc1","key":"ace.abc1","state":"running"}`} + _, err := Runner{Caller: caller, Steps: &fakeSteps{}, Bound: 20 * time.Millisecond}.Run(context.Background(), "mesh-build-go", "ace", "build_go_build", nil) + if err == nil || !strings.Contains(err.Error(), "ace.abc1") || !strings.Contains(err.Error(), "did not end") { + t.Errorf("error = %v, want the step key and that it did not end within its bound", err) + } +} + +func TestRunRefusesAnAnswerWithNoKeyAndAKeyOfAnotherNode(t *testing.T) { // 1607/AC5 + for _, answer := range []string{`{"state":"running"}`, `{"step":"abc1","key":"novox.abc1","state":"running"}`, `not json`} { + _, err := Runner{Caller: &fakeCaller{answer: answer}, Steps: &fakeSteps{}, Bound: time.Second}.Run(context.Background(), "mesh-build-go", "ace", "build_go_fetch", nil) + if err == nil { + t.Errorf("answer %s: no error", answer) + } + } +} + +func TestRunReturnsTheCallsOwnError(t *testing.T) { // 1607/AC5 + boom := errors.New("no responders") + _, err := Runner{Caller: &fakeCaller{err: boom}, Steps: &fakeSteps{}, Bound: time.Second}.Run(context.Background(), "mesh-build-go", "ace", "build_go_fetch", nil) + if !errors.Is(err, boom) || !strings.Contains(err.Error(), "ace") { + t.Errorf("error = %v, want the call's error naming the node", err) + } +}