123 lines
6.5 KiB
Go
123 lines
6.5 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func TestReferenceSourceMapsConflictPreservesTrimmedSourceKinds(t *testing.T) {
|
|
external := ExternalReference("rules.md")
|
|
generated := ReferenceSource{Artifact: &ArtifactReference{Step: "producer", Lane: "notes"}}
|
|
for _, test := range []struct {
|
|
name string
|
|
base, override map[string]ReferenceSource
|
|
want bool
|
|
}{
|
|
{name: "empty"},
|
|
{name: "empty base", override: map[string]ReferenceSource{"rules": generated}},
|
|
{name: "disjoint", base: map[string]ReferenceSource{"rules": external}, override: map[string]ReferenceSource{"notes": generated}},
|
|
{name: "external replacement", base: map[string]ReferenceSource{"rules": external}, override: map[string]ReferenceSource{"rules": ExternalReference("other.md")}},
|
|
{name: "generated replacement", base: map[string]ReferenceSource{"rules": generated}, override: map[string]ReferenceSource{"rules": {Artifact: &ArtifactReference{Step: "other", Lane: "notes"}}}},
|
|
{name: "opposite kinds", base: map[string]ReferenceSource{"rules": external}, override: map[string]ReferenceSource{"rules": generated}, want: true},
|
|
{name: "trimmed opposite kinds", base: map[string]ReferenceSource{" rules\t": external}, override: map[string]ReferenceSource{"\nrules ": generated}, want: true},
|
|
{name: "same-kind trimmed duplicates", base: map[string]ReferenceSource{"rules": external, " rules ": external}, override: map[string]ReferenceSource{"rules": external}},
|
|
{name: "mixed trimmed duplicates", base: map[string]ReferenceSource{"rules": external, " rules ": generated}, override: map[string]ReferenceSource{"rules": external}, want: true},
|
|
{name: "disjoint mixed duplicates", base: map[string]ReferenceSource{"rules": external, " rules ": generated}, override: map[string]ReferenceSource{"notes": external}},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if got := referenceSourceMapsConflict(test.base, test.override); got != test.want {
|
|
t.Fatalf("conflict = %t, want %t", got, test.want)
|
|
}
|
|
if got := referenceSourceMapsConflict(test.override, test.base); got != test.want {
|
|
t.Fatalf("reverse conflict = %t, want %t", got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReferenceCollectionResolutionAndCopy(t *testing.T) {
|
|
pipelineSource := ReferenceSource{Path: "base.yml", Collections: []string{"base"}}
|
|
localSource := ReferenceSource{Path: "local.yml", Collections: []string{}}
|
|
target := referenceResolutionTarget{
|
|
PipelineID: "demo", Stage: StageExtract, Module: "extractor",
|
|
Slots: []contracts.ReferenceSlot{{Name: "glossary"}},
|
|
PipelineReferences: map[string]ReferenceSource{"glossary": pipelineSource},
|
|
LocalReferences: map[string]ReferenceSource{"glossary": localSource},
|
|
}
|
|
bindings, err := resolveReferenceTargetBindings(target)
|
|
if err != nil || len(bindings) != 1 || bindings[0].Source != "local.yml" || bindings[0].Collections == nil || len(bindings[0].Collections) != 0 {
|
|
t.Fatalf("local binding = %#v, %v", bindings, err)
|
|
}
|
|
target.LocalReferences["glossary"] = ReferenceSource{Path: "other.yml"}
|
|
bindings, err = resolveReferenceTargetBindings(target)
|
|
if err != nil || bindings[0].Collections != nil {
|
|
t.Fatalf("replacement inherited collections: %#v, %v", bindings, err)
|
|
}
|
|
target.LocalReferences = nil
|
|
bindings, err = resolveReferenceTargetBindings(target)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bindings[0].Collections[0] = "changed"
|
|
if pipelineSource.Collections[0] != "base" {
|
|
t.Fatal("binding selection aliases input")
|
|
}
|
|
profile := ClonePipelineProfile(PipelineProfile{References: map[string]ReferenceSource{"glossary": pipelineSource, "empty": localSource}})
|
|
profile.References["glossary"].Collections[0] = "changed"
|
|
if pipelineSource.Collections[0] != "base" || profile.References["empty"].Collections == nil {
|
|
t.Fatal("profile clone lost selection isolation or explicit emptiness")
|
|
}
|
|
withEmpty, err := json.Marshal(localSource)
|
|
if err != nil || !strings.Contains(string(withEmpty), `"collections":[]`) {
|
|
t.Fatalf("encoded reference = %s, %v", withEmpty, err)
|
|
}
|
|
for _, test := range []struct {
|
|
selection []string
|
|
wantEmpty bool
|
|
}{{nil, false}, {[]string{}, true}} {
|
|
encoded, err := json.Marshal(ReferenceBinding{SlotName: "glossary", Collections: test.selection})
|
|
if err != nil || strings.Contains(string(encoded), `"collections":[]`) != test.wantEmpty {
|
|
t.Fatalf("encoded binding = %s, %v", encoded, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReferenceOverrideKeepsTargetCollectionSelection(t *testing.T) {
|
|
for _, selection := range [][]string{nil, {}, {"campaign"}} {
|
|
target := referenceResolutionTarget{
|
|
PipelineID: "demo", LaneID: "lane", Stage: StageExtract, Module: "extractor",
|
|
Slots: []contracts.ReferenceSlot{{Name: "glossary"}},
|
|
LocalReferences: map[string]ReferenceSource{"glossary": {Path: "old.yml", Collections: selection}},
|
|
Options: ResolveOptions{ReferenceOverrides: []ReferenceBinding{{Stage: StageExtract, LaneID: "lane", SlotName: "glossary", Source: "new.yml"}}},
|
|
}
|
|
bindings, err := resolveReferenceTargetBindings(target)
|
|
if err != nil || len(bindings) != 1 || bindings[0].Source != "new.yml" || (bindings[0].Collections == nil) != (selection == nil) || len(bindings[0].Collections) != len(selection) {
|
|
t.Fatalf("override with %#v = %#v, %v", selection, bindings, err)
|
|
}
|
|
if len(selection) > 0 && bindings[0].Collections[0] != selection[0] {
|
|
t.Fatalf("selection changed: %#v", bindings)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNormalizeTargetReferenceSourceOwnsAndValidatesSelector(t *testing.T) {
|
|
target := referenceResolutionTarget{PipelineID: "demo", StepID: "consumer", LaneID: "lane", Stage: StageExtract, Module: "extractor"}
|
|
input := ReferenceSource{Artifact: &ArtifactReference{Step: " producer ", Lane: " output "}}
|
|
slot, source, err := normalizeTargetReferenceSource(target, " rules ", input)
|
|
if err != nil || slot != "rules" || source.Artifact.Step != "producer" || source.Artifact.Lane != "output" {
|
|
t.Fatalf("normalized = %q %#v, %v", slot, source, err)
|
|
}
|
|
source.Artifact.Step = "changed"
|
|
if input.Artifact.Step != " producer " {
|
|
t.Fatal("normalization mutated input selector")
|
|
}
|
|
for _, invalid := range []ReferenceSource{{}, {Path: "file", Artifact: &ArtifactReference{Step: "one", Lane: "two"}}, {Artifact: &ArtifactReference{Step: " ", Lane: "two"}}} {
|
|
if _, _, err := normalizeTargetReferenceSource(target, "rules", invalid); err == nil || !strings.Contains(err.Error(), `step "consumer" lane "lane"`) {
|
|
t.Fatalf("invalid source error = %v", err)
|
|
}
|
|
}
|
|
}
|