60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
package build
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/buildstep"
|
|
"git.novox.be/novox/mesh-sdk/go/delivery"
|
|
)
|
|
|
|
// result reads a captured step value of mesh-build-image (testdata/captured/README) and returns its result.
|
|
func captured(t *testing.T, name string) json.RawMessage {
|
|
t.Helper()
|
|
raw, err := os.ReadFile("testdata/captured/" + name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var step delivery.Step
|
|
if err := json.Unmarshal(raw, &step); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
if step.State != delivery.StepPassed || len(step.Result) == 0 {
|
|
t.Fatalf("%s: state %q, result %s, want a passed step with a result", name, step.State, step.Result)
|
|
}
|
|
return step.Result
|
|
}
|
|
|
|
// The captured image steps, run as a pulled ask runs them (buildstep.RunAsk), answer what the builder reads: the trunk's
|
|
// build its digest and reference, a candidate's its digest and no reference (issue 1887 A4a).
|
|
func TestCapturedImageStepsDecodeWithTheBuildersOwnTypes(t *testing.T) { // review r2, mesh-catalog 2; ADR 0487 clause 1
|
|
for _, tc := range []struct {
|
|
build string
|
|
publish bool
|
|
ref bool
|
|
}{{"build-trunk.json", true, true}, {"build-candidate.json", false, false}} {
|
|
steps := buildstep.AskSteps{
|
|
Fetch: func(context.Context, map[string]any) (any, error) {
|
|
return json.RawMessage(captured(t, "fetch.json")), nil
|
|
},
|
|
Build: func(context.Context, map[string]any) (any, error) { return json.RawMessage(captured(t, tc.build)), nil },
|
|
}
|
|
ask := delivery.StepAsk{ID: buildstep.NewID(), Kind: delivery.AskDelivery, Language: delivery.AskImage, Asker: delivery.BuilderModule,
|
|
At: time.Now(), Build: &delivery.BuildJob{Module: "m", Path: "modules/m", Publish: tc.publish,
|
|
Fetch: map[string]any{"repository": "novox/mesh-catalog", "commit": strings.Repeat("a", 40)},
|
|
Artifacts: []delivery.AskArtifact{{Artifact: "server"}}}}
|
|
out, err := buildstep.RunAsk(context.Background(), steps, ask)
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", tc.build, err)
|
|
}
|
|
a := out.(delivery.BuildAnswer)
|
|
if len(a.Artifacts) != 1 || a.Artifacts[0].Digest == "" || (a.Artifacts[0].Reference != "") != tc.ref {
|
|
t.Errorf("%s answered %+v, want a digest and a reference only for the trunk", tc.build, a)
|
|
}
|
|
}
|
|
}
|