75 lines
4.1 KiB
Go
75 lines
4.1 KiB
Go
|
|
package pipeline
|
||
|
|
|
||
|
|
import (
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestLayoutPipelineOrdersAndDetachesFlatProfile(t *testing.T) {
|
||
|
|
profile := PipelineProfile{ID: " demo ", References: map[string]ReferenceSource{"top": {Path: "top.txt"}}, Artifacts: map[string]ArtifactLaneProfile{
|
||
|
|
" z ": {Extract: ModuleBinding{Module: " producer ", Options: map[string]any{"items": []string{"original"}}}},
|
||
|
|
" a ": {References: map[string]ReferenceSource{"source": {Artifact: &ArtifactReference{Step: " prior ", Lane: " lane "}}}},
|
||
|
|
}}
|
||
|
|
layout, err := LayoutPipeline(profile)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if layout.ID != "demo" || layout.ExplicitSteps || len(layout.Steps) != 1 || layout.Steps[0].ID != "default" || !reflect.DeepEqual(layout.Steps[0].LaneIDs, []string{"a", "z"}) || !reflect.DeepEqual(layout.ConfiguredLaneIDs, []string{"a", "z"}) {
|
||
|
|
t.Fatalf("layout = %#v", layout)
|
||
|
|
}
|
||
|
|
lane := layout.Steps[0].Artifacts["a"]
|
||
|
|
lane.References["source"].Artifact.Step = "changed"
|
||
|
|
layout.Steps[0].Artifacts["z"].Extract.Options["items"].([]string)[0] = "changed"
|
||
|
|
if profile.ID != " demo " || profile.Artifacts[" a "].References["source"].Artifact.Step != " prior " || profile.Artifacts[" z "].Extract.Options["items"].([]string)[0] != "original" {
|
||
|
|
t.Fatal("layout mutated profile")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLayoutPipelinePreservesExplicitOrderAndReferences(t *testing.T) {
|
||
|
|
profile := PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{
|
||
|
|
{ID: " first ", References: map[string]ReferenceSource{"rules": {Path: " rules.txt "}}, Artifacts: map[string]ArtifactLaneProfile{" z ": {}, " a ": {}}},
|
||
|
|
{ID: " second ", References: map[string]ReferenceSource{"generated": {Artifact: &ArtifactReference{Step: "first", Lane: "a"}}}, Artifacts: map[string]ArtifactLaneProfile{" b ": {}}},
|
||
|
|
}}
|
||
|
|
layout, err := LayoutPipeline(profile)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !layout.ExplicitSteps || len(layout.Steps) != 2 || layout.Steps[0].ID != "first" || layout.Steps[1].ID != "second" || !reflect.DeepEqual(layout.Steps[0].LaneIDs, []string{"a", "z"}) || !reflect.DeepEqual(layout.ConfiguredLaneIDs, []string{"a", "b", "z"}) || layout.Steps[0].References["rules"].Path != " rules.txt " || layout.Steps[1].References["generated"].Artifact.Lane != "a" {
|
||
|
|
t.Fatalf("layout = %#v", layout)
|
||
|
|
}
|
||
|
|
layout.Steps[1].References["generated"].Artifact.Lane = "changed"
|
||
|
|
if profile.Steps[1].References["generated"].Artifact.Lane != "a" {
|
||
|
|
t.Fatal("layout references alias profile")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLayoutPipelineRejectsInvalidIdentitiesAndShapes(t *testing.T) {
|
||
|
|
for _, test := range []struct {
|
||
|
|
name string
|
||
|
|
profile PipelineProfile
|
||
|
|
want string
|
||
|
|
}{
|
||
|
|
{"missing identity", PipelineProfile{}, "pipeline id"},
|
||
|
|
{"mixed", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{"a": {}}, Steps: []PipelineStepProfile{{ID: "one"}}}, "both artifacts and steps"},
|
||
|
|
{"empty explicit", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{}}, "at least one ordered step"},
|
||
|
|
{"blank step", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: " "}}}, "step[0] id"},
|
||
|
|
{"duplicate steps", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: "a"}, {ID: " a "}}}, "step id"},
|
||
|
|
{"blank lane", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{" ": {}}}, "lane id"},
|
||
|
|
{"duplicate lane", PipelineProfile{ID: "demo", Artifacts: map[string]ArtifactLaneProfile{"a": {}, " a ": {}}}, "duplicated after trimming"},
|
||
|
|
{"duplicate across steps", PipelineProfile{ID: "demo", Steps: []PipelineStepProfile{{ID: "one", Artifacts: map[string]ArtifactLaneProfile{"a": {}}}, {ID: "two", Artifacts: map[string]ArtifactLaneProfile{" a ": {}}}}}, "duplicated across steps"},
|
||
|
|
} {
|
||
|
|
t.Run(test.name, func(t *testing.T) {
|
||
|
|
if _, err := LayoutPipeline(test.profile); err == nil || !strings.Contains(err.Error(), test.want) {
|
||
|
|
t.Fatalf("LayoutPipeline() error = %v, want %q", err, test.want)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
for _, profile := range []PipelineProfile{{ID: "demo"}, {ID: "demo", Steps: []PipelineStepProfile{{ID: "empty"}}}} {
|
||
|
|
layout, err := LayoutPipeline(profile)
|
||
|
|
if err != nil || len(layout.Steps) != 1 || len(layout.Steps[0].LaneIDs) != 0 {
|
||
|
|
t.Fatalf("empty composable layout = %#v, %v", layout, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|