Verify configured pipeline secrets startup

This commit is contained in:
2026-11-23 14:28:02 +00:00
parent 457b5a3976
commit 716869d553
2 changed files with 114 additions and 0 deletions

View File

@@ -1,10 +1,16 @@
package app
import (
"bytes"
"context"
"errors"
"os"
"path/filepath"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
configkit "go.maximumdirect.net/configkit"
)
func TestNotariusCredentialLoadsFromConfiguredBootstrap(t *testing.T) {
@@ -26,3 +32,103 @@ func TestNotariusCredentialLoadsFromConfiguredBootstrap(t *testing.T) {
t.Fatal("configured bootstrap did not retain its report")
}
}
func TestConfiguredPipelineAcquisitionDiscoveryAndReuse(t *testing.T) {
const name = "NARRATIO_DISCOVERED_TEST_KEY"
restoreEnvAfterTest(t, name)
root := t.TempDir()
pipeline := filepath.Join(root, "narratio.yml")
mustWriteSecretFile(t, filepath.Join(root, "secrets", name), "discovered-value")
mustWriteTestFile(t, pipeline, "config: {secrets_dir: secrets}\nwhisperx: {transcribe_url: https://example.test}\nnotarius: {enabled: false, credentials: ["+name+"]}\n")
search := configkit.SearchPaths(pipeline)
t.Chdir(t.TempDir())
acquisition := newConfigurationAcquisition(configurationSelection{}, commandDependencies{pipelineSearch: &search})
first, err := acquisition.Pipeline(context.Background())
if err != nil {
t.Fatal(err)
}
if first.Notarius.Credentials[0].Value() != "discovered-value" || config.EffectivePipelineRootPath(first) != pipeline || first.ConfigKitResult() == nil {
t.Fatal("discovered configuration lost credential or provenance")
}
if err := os.Remove(pipeline); err != nil {
t.Fatal(err)
}
second, err := acquisition.Pipeline(context.Background())
if err != nil || first != second {
t.Fatalf("acquisition did not reuse result: %v", err)
}
}
func TestConfiguredBootstrapEnvironmentSelection(t *testing.T) {
const name = "NARRATIO_ENV_SELECTION_TEST"
root := t.TempDir()
pipeline := filepath.Join(root, "pipeline.yml")
mustWriteSecretFile(t, filepath.Join(root, "secrets", name), "file-value")
for _, test := range []struct{ name, control, initial, want string }{
{"environment only", "", "environment-value", "environment-value"},
{"preserve", "config: {secrets_dir: secrets}\n", "environment-value", "environment-value"},
{"overwrite", "config: {secrets_dir: secrets, secrets_overwrite: true}\n", "environment-value", "file-value"},
} {
t.Run(test.name, func(t *testing.T) {
t.Setenv(name, test.initial)
mustWriteTestFile(t, pipeline, test.control+"whisperx: {transcribe_url: https://example.test}\nnotarius: {enabled: false, credentials: ["+name+"]}\n")
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline)}, commandDependencies{})
cfg, err := acquired.Pipeline(context.Background())
if err != nil {
t.Fatal(err)
}
if cfg.Notarius.Credentials[0].Value() != test.want {
t.Fatal("wrong resolved credential")
}
})
}
t.Setenv(name, "")
mustWriteTestFile(t, pipeline, "config: {secrets_dir: secrets}\nwhisperx: {transcribe_url: https://example.test}\n")
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline)}, commandDependencies{})
if _, err := acquired.Pipeline(context.Background()); err != nil {
t.Fatal(err)
}
if os.Getenv(name) != "" || !acquired.bootstrapReport.Entries()[0].ExistingEmpty {
t.Fatal("existing empty environment value was not preserved")
}
}
func TestConfiguredStartupFailureStopsBeforeExecution(t *testing.T) {
root := t.TempDir()
pipeline := filepath.Join(root, "pipeline.yml")
mustWriteTestFile(t, pipeline, "config: {secrets_dir: absent}\nwhisperx: {transcribe_url: https://example.test}\n")
called := false
application, err := newApplication(commandDependencies{}, func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
called = true
return nil, nil
})
if err != nil {
t.Fatal(err)
}
var output, diagnostics bytes.Buffer
result := application.Execute(context.Background(), []string{"run", "session", "--config", pipeline}, commandkit.Streams{Out: &output, Err: &diagnostics})
var native *configkit.SecretsError
if called || !errors.As(result.Cause, &native) || result.Status != 1 {
t.Fatalf("result=%#v executed=%t diagnostics=%s", result, called, diagnostics.String())
}
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline)}, commandDependencies{})
if _, err := acquired.Pipeline(context.Background()); !errors.As(err, &native) || acquired.bootstrapReport == nil {
t.Fatalf("lost report or cause: %v", err)
}
}
func TestAdjacentSecretsAreNotSelectedImplicitly(t *testing.T) {
const name = "NARRATIO_ADJACENT_UNUSED_TEST"
restoreEnvAfterTest(t, name)
root := t.TempDir()
mustWriteSecretFile(t, filepath.Join(root, "secrets", name), "unexpected")
pipeline := filepath.Join(root, "pipeline.yml")
mustWriteTestFile(t, pipeline, "whisperx: {transcribe_url: https://example.test}\n")
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline)}, commandDependencies{})
if _, err := acquired.Pipeline(context.Background()); err != nil {
t.Fatal(err)
}
if _, ok := os.LookupEnv(name); ok {
t.Fatal("adjacent secrets were loaded without configuration")
}
}