Resolve pipeline resources from their source locations
This commit is contained in:
127
internal/config/resource_base_test.go
Normal file
127
internal/config/resource_base_test.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestPipelineResourcePathsFollowEffectiveSources(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := writePipelineFixture(t, dir, "pipeline.yml", `config:
|
||||
imports:
|
||||
- path: fragments/shared.yml
|
||||
workspace: {root: root-work}
|
||||
`)
|
||||
writePipelineFixture(t, dir, "fragments/shared.yml", `campaigns: {root: campaigns}
|
||||
spool: {root: /absolute/spool}
|
||||
cache: {root: cache}
|
||||
audita: {config_path: config/tool.yml}
|
||||
scriptorium: {extends: audita}
|
||||
notarius:
|
||||
extends: audita
|
||||
enabled: true
|
||||
pipeline_id: dnd-session
|
||||
`)
|
||||
writePipelineFixture(t, dir, "overlays/testing.yml", "audita:\n config_path: custom.yml\n")
|
||||
base, err := LoadPipeline(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantConfig := filepath.Join(dir, "fragments", "config", "tool.yml")
|
||||
if base.Audita.ConfigPath != wantConfig || base.Scriptorium.ConfigPath != wantConfig || base.Notarius.ConfigPath != wantConfig {
|
||||
t.Fatalf("resource paths: audita=%q scriptorium=%q notarius=%q", base.Audita.ConfigPath, base.Scriptorium.ConfigPath, base.Notarius.ConfigPath)
|
||||
}
|
||||
if base.Notarius.WorkingDirectory != filepath.Dir(wantConfig) || base.Workspace.Root != filepath.Join(dir, "root-work") || base.Campaigns.Root != filepath.Join(dir, "fragments", "campaigns") || base.Cache.Root != filepath.Join(dir, "fragments", "cache") || base.Spool.Root != "/absolute/spool" {
|
||||
t.Fatalf("resolved paths: %#v", base)
|
||||
}
|
||||
overlay := "testing"
|
||||
updated, err := LoadPipelineWithOptions(root, PipelineLoadOptions{Overlay: &overlay})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantOverlay := filepath.Join(dir, "overlays", "custom.yml")
|
||||
if updated.Audita.ConfigPath != wantOverlay || updated.Scriptorium.ConfigPath != wantOverlay || updated.Notarius.ConfigPath != wantOverlay {
|
||||
t.Fatalf("overlay resource paths: audita=%q scriptorium=%q notarius=%q", updated.Audita.ConfigPath, updated.Scriptorium.ConfigPath, updated.Notarius.ConfigPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceBaseUsesInvocationDirectoryForContextlessAndCLIValues(t *testing.T) {
|
||||
invocation := t.TempDir()
|
||||
type document struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
for _, overrides := range [][]configkit.Override{nil, {{Path: configkit.Keys("path"), Value: "cli.txt", Flag: "--path"}}} {
|
||||
result, err := configkit.Load(configkit.Bytes("remote snapshot", []byte("path: input.txt\n")), configkit.Options[document]{
|
||||
Overrides: overrides,
|
||||
Definition: configkit.Definition[document]{Resolve: func(doc *document, resolution *configkit.Resolution) error {
|
||||
resolveHostResource(resolution, configkit.Keys("path"), &doc.Path, invocation)
|
||||
return nil
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name := "input.txt"
|
||||
if len(overrides) > 0 {
|
||||
name = "cli.txt"
|
||||
}
|
||||
if got, want := result.Config.Path, filepath.Join(invocation, name); got != want {
|
||||
t.Fatalf("resolved %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEquivalentPipelineResourceLayoutsHaveEqualDigests(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := writePipelineFixture(t, dir, "first.yml", "audita: {config_path: shared/tool.yml}\n")
|
||||
second := writePipelineFixture(t, dir, "second.yml", "config:\n imports:\n - path: shared/settings.yml\n")
|
||||
writePipelineFixture(t, dir, "shared/settings.yml", "audita: {config_path: tool.yml}\n")
|
||||
a, err := LoadPipeline(first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := LoadPipeline(second)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if a.Audita.ConfigPath != b.Audita.ConfigPath || EffectivePipelineDigest(a) != EffectivePipelineDigest(b) {
|
||||
t.Fatalf("equivalent resources differ: %q / %q", a.Audita.ConfigPath, b.Audita.ConfigPath)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "shared", "tool.yml")); !os.IsNotExist(err) {
|
||||
t.Fatalf("fixture unexpectedly exists: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceBaseFallsBackToMainForCompositeAndDefaultOrigins(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
root := writePipelineFixture(t, dir, "pipeline.yml", "config:\n imports:\n - path: fragment/part.yml\nvalues: {first: one}\n")
|
||||
writePipelineFixture(t, dir, "fragment/part.yml", "values: {second: two}\n")
|
||||
type document struct {
|
||||
Values map[string]string `yaml:"values"`
|
||||
Default string `yaml:"default"`
|
||||
}
|
||||
var compositeBase, defaultBase string
|
||||
var contributors int
|
||||
_, err := configkit.Load(configkit.File(root), configkit.Options[document]{
|
||||
Imports: true,
|
||||
Definition: configkit.Definition[document]{
|
||||
Defaults: func() document { return document{Default: "default.txt"} },
|
||||
Resolve: func(_ *document, resolution *configkit.Resolution) error {
|
||||
info, _ := resolution.EntryInfo(configkit.Keys("values"))
|
||||
contributors = len(info.Origin.Locations)
|
||||
compositeBase = resourceBase(resolution, configkit.Keys("values"), t.TempDir())
|
||||
defaultBase = resourceBase(resolution, configkit.Keys("default"), t.TempDir())
|
||||
return nil
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if contributors < 2 || compositeBase != dir || defaultBase != dir {
|
||||
t.Fatalf("fallback bases = %q, %q; want %q", compositeBase, defaultBase, dir)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user