262 lines
10 KiB
Go
262 lines
10 KiB
Go
package publish
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.maximumdirect.net/eric/distributor/internal/bundle"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/config"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/state"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/storage/fake"
|
|
"gitea.maximumdirect.net/eric/distributor/internal/testutil"
|
|
)
|
|
|
|
func TestBuildSharedRootTreatsAbsentOwnerAsPublishable(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
writeFakeSharedRootState(t, destinationBackend, "bundle", sharedRootStateWithOwners(t, sourceBundle.Manifest, false))
|
|
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeReplace)
|
|
plan, err := Build(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if plan.Action != ActionPublishNew {
|
|
t.Fatalf("plan action = %s, want publish_new", plan.Action)
|
|
}
|
|
if plan.OwnerScope != state.CurrentOwnerScope("reports", "archive") {
|
|
t.Fatalf("owner scope = %#v, want reports/archive", plan.OwnerScope)
|
|
}
|
|
if got, want := len(plan.OtherOwnerOutputs), 1; got != want {
|
|
t.Fatalf("other owner output count = %d, want %d", got, want)
|
|
}
|
|
if got, want := len(plan.OwnerOutputsToWrite), 1; got != want {
|
|
t.Fatalf("owner output write count = %d, want %d", got, want)
|
|
}
|
|
if len(plan.OwnerOutputsToDelete) != 0 || len(plan.RetainedOwnerOutputs) != 0 {
|
|
t.Fatalf("delete=%#v retained=%#v, want none", plan.OwnerOutputsToDelete, plan.RetainedOwnerOutputs)
|
|
}
|
|
}
|
|
|
|
func TestBuildSharedRootReplaceDeletesOnlyCurrentOwnerOmittedOutputs(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\nNew.\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
writeFakeSharedRootState(t, destinationBackend, "bundle", sharedRootStateWithOwners(t, sourceBundle.Manifest, true))
|
|
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeReplace)
|
|
plan, err := Build(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if plan.Action != ActionReplaceOlder {
|
|
t.Fatalf("plan action = %s, want replace_older", plan.Action)
|
|
}
|
|
if got, want := sharedRootOutputPathList(plan.OwnerOutputsToDelete), "old.md"; got != want {
|
|
t.Fatalf("owner outputs to delete = %q, want %q", got, want)
|
|
}
|
|
if got, want := sharedRootOutputPathList(plan.OtherOwnerOutputs), "other/report.md"; got != want {
|
|
t.Fatalf("other owner outputs = %q, want %q", got, want)
|
|
}
|
|
if len(plan.RetainedOwnerOutputs) != 0 {
|
|
t.Fatalf("retained owner outputs = %#v, want none", plan.RetainedOwnerOutputs)
|
|
}
|
|
}
|
|
|
|
func TestBuildSharedRootMergeRetainsCurrentOwnerOmittedOutputs(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\nNew.\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
writeFakeSharedRootState(t, destinationBackend, "bundle", sharedRootStateWithOwners(t, sourceBundle.Manifest, true))
|
|
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeMerge)
|
|
plan, err := Build(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if got, want := sharedRootOutputPathList(plan.RetainedOwnerOutputs), "old.md"; got != want {
|
|
t.Fatalf("retained owner outputs = %q, want %q", got, want)
|
|
}
|
|
if len(plan.OwnerOutputsToDelete) != 0 {
|
|
t.Fatalf("owner outputs to delete = %#v, want none", plan.OwnerOutputsToDelete)
|
|
}
|
|
}
|
|
|
|
func TestBuildSharedRootRejectsOtherOwnerPathConflict(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
sharedRoot := sharedRootStateWithOwners(t, sourceBundle.Manifest, false)
|
|
sharedRoot.Outputs[0].Path = "report.md"
|
|
writeFakeSharedRootState(t, destinationBackend, "bundle", sharedRoot)
|
|
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeReplace)
|
|
plan, err := Build(context.Background(), req)
|
|
if err == nil || !strings.Contains(err.Error(), "fail_conflict") {
|
|
t.Fatalf("Build() error = %v, want fail_conflict", err)
|
|
}
|
|
if plan.Action != ActionFailConflict {
|
|
t.Fatalf("plan action = %s, want fail_conflict", plan.Action)
|
|
}
|
|
}
|
|
|
|
func TestBuildSharedRootRejectsUnmanagedPathCollision(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
writeFakeSharedRootState(t, destinationBackend, "bundle", sharedRootStateWithOwners(t, sourceBundle.Manifest, false))
|
|
testutil.WriteFakeFile(t, destinationBackend, "bundle/report.md", "unmanaged")
|
|
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeReplace)
|
|
plan, err := Build(context.Background(), req)
|
|
if err == nil || !strings.Contains(err.Error(), "fail_unmanaged") {
|
|
t.Fatalf("Build() error = %v, want fail_unmanaged", err)
|
|
}
|
|
if plan.Action != ActionFailUnmanaged {
|
|
t.Fatalf("plan action = %s, want fail_unmanaged", plan.Action)
|
|
}
|
|
}
|
|
|
|
func TestExecuteSharedRootWriteActionsAreDisabled(t *testing.T) {
|
|
sourceBackend := fake.New()
|
|
sourceBundle := testutil.WriteFakeSourceBundle(t, sourceBackend, "bundle", testutil.BundleOptions{
|
|
Files: []testutil.SourceFile{{Path: "report.md", Data: "# Report\n"}},
|
|
})
|
|
destinationBackend := fake.New()
|
|
req := sharedRootRequest(sourceBackend, destinationBackend, sourceBundle, config.ReconciliationModeReplace)
|
|
plan, err := Build(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("Build() error = %v", err)
|
|
}
|
|
if err := Execute(context.Background(), req, plan); err == nil || !strings.Contains(err.Error(), "shared-root publish execution is not implemented") {
|
|
t.Fatalf("Execute() error = %v, want shared-root disabled error", err)
|
|
}
|
|
}
|
|
|
|
func sharedRootRequest(sourceBackend, destinationBackend *fake.Backend, sourceBundle bundle.Bundle, reconciliationMode string) Request {
|
|
return Request{
|
|
PipelineID: "reports",
|
|
DestinationID: "archive",
|
|
SourceBundle: sourceBundle,
|
|
SourceBackend: sourceBackend,
|
|
DestinationBackend: destinationBackend,
|
|
DestinationBundlePath: sourceBundle.RootRelativePath,
|
|
Publish: config.PublishPolicy{Source: true},
|
|
State: config.StatePolicy{Mode: config.StateModeSharedRoot},
|
|
Reconciliation: config.ReconciliationPolicy{Mode: reconciliationMode},
|
|
Transfer: defaultTransfer(),
|
|
DistributorVersion: "test",
|
|
}
|
|
}
|
|
|
|
func writeFakeSharedRootState(t *testing.T, backend *fake.Backend, relative string, sharedRoot state.SharedRootState) {
|
|
t.Helper()
|
|
data, err := json.MarshalIndent(sharedRoot, "", " ")
|
|
if err != nil {
|
|
t.Fatalf("marshal shared-root state: %v", err)
|
|
}
|
|
statePath, err := storage.StatePath(relative)
|
|
if err != nil {
|
|
t.Fatalf("state path: %v", err)
|
|
}
|
|
testutil.WriteFakeFile(t, backend, statePath, string(append(data, '\n')))
|
|
for _, output := range sharedRoot.Outputs {
|
|
path, err := storage.Join(relative, output.Path)
|
|
if err != nil {
|
|
t.Fatalf("join output path: %v", err)
|
|
}
|
|
testutil.WriteFakeFile(t, backend, path, "old")
|
|
}
|
|
}
|
|
|
|
func sharedRootStateWithOwners(t *testing.T, current bundle.Manifest, includeCurrent bool) state.SharedRootState {
|
|
t.Helper()
|
|
createdAt := time.Date(2026, 5, 30, 11, 12, 0, 0, time.UTC)
|
|
otherManifest := testutil.ValidManifest(testutil.BundleOptions{
|
|
ID: "other.source",
|
|
Files: []testutil.SourceFile{{Path: "other/report.md", Data: "# Other\n"}},
|
|
})
|
|
sharedRoot := state.SharedRootState{
|
|
SchemaVersion: state.SharedRootSchemaVersion,
|
|
DistributorVersion: "test",
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: createdAt,
|
|
State: state.StatePolicy{Mode: state.StateModeSharedRoot},
|
|
Owners: []state.OwnerRecord{{
|
|
Scope: state.CurrentOwnerScope("other", "archive"),
|
|
Reconciliation: state.ReconciliationPolicy{Mode: config.ReconciliationModeReplace},
|
|
Source: state.SourceState{Manifest: otherManifest},
|
|
}},
|
|
Outputs: []state.SharedRootOutputFile{{
|
|
Path: "other/report.md",
|
|
Kind: state.OutputKindSource,
|
|
SourcePath: "other/report.md",
|
|
SHA256: otherManifest.Files[0].SHA256,
|
|
Size: otherManifest.Files[0].Size,
|
|
Owner: state.CurrentOwnerScope("other", "archive"),
|
|
SourceID: otherManifest.ID,
|
|
SourceDigest: otherManifest.Digest,
|
|
SourceCreated: otherManifest.Created,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: createdAt,
|
|
}},
|
|
}
|
|
if !includeCurrent {
|
|
return sharedRoot
|
|
}
|
|
older := current
|
|
older.Created = older.Created.Add(-time.Hour)
|
|
older.Files = append([]bundle.ManifestFile(nil), current.Files...)
|
|
older.Files = append(older.Files, bundle.ManifestFile{
|
|
Path: "old.md",
|
|
SHA256: bundle.FileDigest([]byte("old\n")),
|
|
Size: int64(len("old\n")),
|
|
})
|
|
older.Digest = bundle.BundleDigest(older.Files)
|
|
scope := state.CurrentOwnerScope("reports", "archive")
|
|
sharedRoot.Owners = append(sharedRoot.Owners, state.OwnerRecord{
|
|
Scope: scope,
|
|
Reconciliation: state.ReconciliationPolicy{Mode: config.ReconciliationModeReplace},
|
|
Source: state.SourceState{Manifest: older},
|
|
})
|
|
for _, file := range older.Files {
|
|
sharedRoot.Outputs = append(sharedRoot.Outputs, state.SharedRootOutputFile{
|
|
Path: file.Path,
|
|
Kind: state.OutputKindSource,
|
|
SourcePath: file.Path,
|
|
SHA256: file.SHA256,
|
|
Size: file.Size,
|
|
Owner: scope,
|
|
SourceID: older.ID,
|
|
SourceDigest: older.Digest,
|
|
SourceCreated: older.Created,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: createdAt,
|
|
})
|
|
}
|
|
return sharedRoot
|
|
}
|
|
|
|
func sharedRootOutputPathList(outputs []state.SharedRootOutputFile) string {
|
|
paths := make([]string, 0, len(outputs))
|
|
for _, output := range outputs {
|
|
paths = append(paths, output.Path)
|
|
}
|
|
return strings.Join(paths, ",")
|
|
}
|