From 61ede046a17e5c6614b4e160b2366288ac77d222 Mon Sep 17 00:00:00 2001 From: Nathan Kester Date: Mon, 16 Nov 2026 13:01:53 -0700 Subject: [PATCH] AIC-3901 part 3: the first pull makes a variation code-owned Co-Authored-By: Claude Opus 5.5 --- internal/sync/bootstrap/bootstrap.go | 170 ++++++++- internal/sync/bootstrap/ownership_test.go | 441 ++++++++++++++++++++++ internal/sync/prompt/create_test.go | 13 +- internal/sync/prompt/runner.go | 4 + 4 files changed, 625 insertions(+), 3 deletions(-) create mode 100644 internal/sync/bootstrap/ownership_test.go diff --git a/internal/sync/bootstrap/bootstrap.go b/internal/sync/bootstrap/bootstrap.go index 8fc711e05..6a313b958 100644 --- a/internal/sync/bootstrap/bootstrap.go +++ b/internal/sync/bootstrap/bootstrap.go @@ -5,6 +5,7 @@ package bootstrap import ( "bytes" "cmp" + "context" "errors" "fmt" "io" @@ -34,6 +35,13 @@ type AttachmentReader interface { ReadAttachment(projectKey string, kind syncdomain.AttachmentKind, key string) (syncdomain.Attachment, error) } +// Settings reads and writes the LaunchDarkly sync settings that decide which +// side owns a variation. +type Settings interface { + ResolveOwnership(projectKey string, ids []syncdomain.ResourceID) (map[syncdomain.ResourceID]syncdomain.Ownership, error) + ClaimForCode(projectKey string, ids []syncdomain.ResourceID) error +} + // Options are the dependencies and the input of one add. type Options struct { // Kind is the kind of resource to add. The empty kind is a variation. @@ -55,6 +63,19 @@ type Options struct { // Selections are the resources to add. If it is empty, Run asks the user. Selections []syncdomain.ResourceID NoInput bool + // Settings makes code the owner of each variation that the add brings + // into the repo for the first time. + Settings Settings + // ErrorOutput receives warnings and ownership notices. + ErrorOutput io.Writer + // Yes continues without a confirmation prompt when the sync settings are + // unavailable. + Yes bool + // Context stops the settings-unavailable question when it ends. A nil + // Context never ends. + Context context.Context + // isTerminal reports whether the streams are a terminal. Tests replace it. + isTerminal func(io.Reader, io.Writer) bool } // Run writes the files of the selected resources and records their baseline. @@ -286,6 +307,19 @@ func finishSelection(options Options, files []synclocal.VariationFile) error { if err != nil { return err } + claimed, proceed, err := claimNewVariations(options, baseline.Lock, files) + if err != nil || !proceed { + return err + } + // The claim is already written, so a later failure says so. The add can + // run again, because the claim sets the same rules. + claimedNote := func(err error) error { + if len(claimed) == 0 { + return err + } + return fmt.Errorf("%w; the new variations in project %s were already claimed for code, and running add again is safe", + err, strings.Join(claimed, ", ")) + } var creation synclocal.Creation if options.Initial { @@ -294,20 +328,152 @@ func finishSelection(options Options, files []synclocal.VariationFile) error { creation, err = options.Store.Add(files) } if err != nil { - return err + return claimedNote(err) } next, err := recordCreatedVariations(baseline.Lock, options.Store, files) if err == nil { _, err = options.Baselines.Save(baseline, next) } if err != nil { - return errors.Join(err, options.Store.RollbackCreation(creation)) + return claimedNote(errors.Join(err, options.Store.RollbackCreation(creation))) } writeSummary(options.Output, options.Initial, len(creation.VariationPaths), "variation") return nil } +// claimNewVariations makes code the owner of each variation that enters the +// repo for the first time, before the add writes any file. It reads the +// ownership of every project first, and claims nothing until every read +// succeeded, so that a failed read or a canceled add leaves no claim behind. +// Then it makes at most one claim per project. A variation that an explicit +// LaunchDarkly rule owns is pulled but not claimed. When a project has no +// sync settings, it warns, and asks whether to add anyway unless Yes is set. +// It returns the projects that it claimed in. The bool result is false when +// the user declines. +func claimNewVariations(options Options, lock syncmanifest.Manifest, files []synclocal.VariationFile) ([]string, bool, error) { + if options.Settings == nil { + return nil, true, nil + } + byProject := make(map[string][]syncdomain.ResourceID) + var projectKeys []string + for _, file := range files { + id := syncdomain.VariationID(file.ProjectKey, file.ConfigKey, file.Variation.Key) + if lock.Has(id) { + continue + } + if _, seen := byProject[id.ProjectKey]; !seen { + projectKeys = append(projectKeys, id.ProjectKey) + } + byProject[id.ProjectKey] = append(byProject[id.ProjectKey], id) + } + slices.Sort(projectKeys) + + errorOutput := options.ErrorOutput + if errorOutput == nil { + errorOutput = io.Discard + } + decisions := make(map[string]ownershipDecision, len(projectKeys)) + var unavailable []string + for _, projectKey := range projectKeys { + decision, err := decideProject(options.Settings, projectKey, byProject[projectKey]) + switch { + case syncapi.IsNotFound(err): + unavailable = append(unavailable, projectKey) + continue + case errors.Is(err, syncapi.ErrSyncSettingsForbidden): + return nil, false, syncdomain.SyncSettingsForbiddenProblem(projectKey, "record code ownership", "nothing was added", err) + case err != nil: + return nil, false, fmt.Errorf("make code the owner of the new variations in project %s: %w", projectKey, err) + } + decisions[projectKey] = decision + } + if len(unavailable) != 0 { + ctx := options.Context + if ctx == nil { + ctx = context.Background() + } + proceed, err := syncinteractive.ConfirmWithoutSettings(syncinteractive.SettingsUnavailable{ + Context: ctx, + Input: options.Input, + Prompt: options.Output, + Output: options.Output, + ErrorOutput: errorOutput, + NoInput: options.NoInput, + Yes: options.Yes, + IsTerminal: options.isTerminal, + ProjectKeys: unavailable, + Unrecorded: "code ownership of the new variations", + Question: withoutOwnershipQuestion, + Verb: "Add", + }) + if err != nil || !proceed { + return nil, false, err + } + } + + console := syncconsole.New(errorOutput) + var claimed []string + for _, projectKey := range projectKeys { + decision, available := decisions[projectKey] + if !available { + continue + } + if len(decision.claims) != 0 { + err := options.Settings.ClaimForCode(projectKey, decision.claims) + switch { + case errors.Is(err, syncapi.ErrSyncSettingsForbidden): + return nil, false, syncdomain.SyncSettingsForbiddenProblem(projectKey, "record code ownership", addConsequence(claimed), err) + case err != nil: + return nil, false, fmt.Errorf("make code the owner of the new variations in project %s, so %s: %w", projectKey, addConsequence(claimed), err) + } + claimed = append(claimed, projectKey) + } + for _, id := range decision.managed { + _ = console.Line(syncdomain.ManagedInLaunchDarklyMessage(id)) + } + } + return claimed, true, nil +} + +// addConsequence says what a failed claim leaves behind: no file, and the +// claims of the projects that were already claimed. +func addConsequence(claimed []string) string { + if len(claimed) == 0 { + return "nothing was added" + } + return fmt.Sprintf("nothing was added, but variations in project %s were already claimed for code", strings.Join(claimed, ", ")) +} + +// withoutOwnershipQuestion asks whether to add variations whose ownership +// LaunchDarkly cannot record. +const withoutOwnershipQuestion = "Add without recording ownership? [y/N] " + +// ownershipDecision splits the new variations of one project into those to +// claim for code and those that LaunchDarkly manages. +type ownershipDecision struct { + claims, managed []syncdomain.ResourceID +} + +// decideProject reads the ownership of the new variations of one project +// and decides which ones to claim. It claims nothing. +func decideProject(settings Settings, projectKey string, ids []syncdomain.ResourceID) (ownershipDecision, error) { + ownership, err := settings.ResolveOwnership(projectKey, ids) + if err != nil { + return ownershipDecision{}, err + } + var decision ownershipDecision + for _, id := range ids { + switch syncdomain.DecideOwnership(id, ownership[id]) { + case syncdomain.OwnershipClaim: + decision.claims = append(decision.claims, id) + case syncdomain.OwnershipManagedInLaunchDarkly: + decision.managed = append(decision.managed, id) + } + } + return decision, nil +} + // recordCreatedVariations returns a copy of the manifest with a baseline for // each new variation. It reads the variations from disk, because Add keeps an // existing tool or skill file, and the baseline must match that file. diff --git a/internal/sync/bootstrap/ownership_test.go b/internal/sync/bootstrap/ownership_test.go new file mode 100644 index 000000000..0df3cbccb --- /dev/null +++ b/internal/sync/bootstrap/ownership_test.go @@ -0,0 +1,441 @@ +package bootstrap + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "slices" + "strings" + "testing" + "testing/iotest" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + syncdomain "github.com/launchdarkly/ldcli/internal/sync" + syncapi "github.com/launchdarkly/ldcli/internal/sync/api" + synclocal "github.com/launchdarkly/ldcli/internal/sync/local" + syncmanifest "github.com/launchdarkly/ldcli/internal/sync/manifest" +) + +var _ Settings = syncapi.Client{} + +// recordingSettings stands in for the LaunchDarkly sync settings API. It +// answers ResolveOwnership from ownership, fails the reads and claims of the +// projects in resolveErrs and claimErrs, records every claim, and records +// whether a claimed variation's file was already on disk when it was claimed. +type recordingSettings struct { + root string + ownership map[syncdomain.ResourceID]syncdomain.Ownership + resolveErrs map[string]error + claimErrs map[string]error + + claims []claim +} + +type claim struct { + projectKey string + ids []syncdomain.ResourceID + // writtenBefore names each claimed variation whose file existed when the + // claim was made. + writtenBefore []syncdomain.ResourceID +} + +func (settings *recordingSettings) ResolveOwnership( + projectKey string, ids []syncdomain.ResourceID, +) (map[syncdomain.ResourceID]syncdomain.Ownership, error) { + if err := settings.resolveErrs[projectKey]; err != nil { + return nil, err + } + result := make(map[syncdomain.ResourceID]syncdomain.Ownership, len(ids)) + for _, id := range ids { + if id.ProjectKey != projectKey { + return nil, fmt.Errorf("%s is not in project %q", id.LookupKey, projectKey) + } + // A variation without a stored rule resolves to the project default + // with no matched pattern. + owner, ok := settings.ownership[id] + if !ok { + owner = syncdomain.Ownership{Direction: syncdomain.SyncDirectionServerCanonical} + } + result[id] = owner + } + return result, nil +} + +func (settings *recordingSettings) ClaimForCode(projectKey string, ids []syncdomain.ResourceID) error { + recorded := claim{projectKey: projectKey, ids: slices.Clone(ids)} + for _, id := range ids { + if _, err := os.Stat(variationPath(settings.root, id)); err == nil { + recorded.writtenBefore = append(recorded.writtenBefore, id) + } + } + settings.claims = append(settings.claims, recorded) + return settings.claimErrs[projectKey] +} + +// claimedIDs returns every claimed variation, in claim order. +func (settings *recordingSettings) claimedIDs() []syncdomain.ResourceID { + var ids []syncdomain.ResourceID + for _, claim := range settings.claims { + ids = append(ids, claim.ids...) + } + return ids +} + +func variationPath(root string, id syncdomain.ResourceID) string { + configKey, variationKey, err := id.VariationKeys() + if err != nil { + panic(err) + } + return filepath.Join(root, syncdomain.RootDir, id.ProjectKey, "configs", configKey, variationKey+".prompt.md") +} + +func ownershipCatalog(variationKeys ...string) *fakeCatalog { + config := syncapi.Config{Key: "config", Mode: syncdomain.VariationModeAgent} + for _, key := range variationKeys { + config.Variations = append(config.Variations, syncdomain.Variation{ + Key: key, Name: key, Mode: syncdomain.VariationModeAgent, Instructions: "Be helpful.", + }) + } + return &fakeCatalog{config: config} +} + +type ownershipRun struct { + root string + settings *recordingSettings + baselines *memoryManifestStore + output bytes.Buffer + errOutput bytes.Buffer +} + +func newOwnershipRun(t *testing.T) *ownershipRun { + root := t.TempDir() + return &ownershipRun{ + root: root, + settings: &recordingSettings{ + root: root, + ownership: map[syncdomain.ResourceID]syncdomain.Ownership{}, + resolveErrs: map[string]error{}, + claimErrs: map[string]error{}, + }, + baselines: &memoryManifestStore{manifest: syncmanifest.New()}, + } +} + +func (run *ownershipRun) options(catalog Catalog, selections ...syncdomain.ResourceID) Options { + return Options{ + Catalog: catalog, + Store: synclocal.NewStore(run.root), + Baselines: run.baselines, + Settings: run.settings, + Input: bytes.NewBuffer(nil), + Output: &run.output, + ErrorOutput: &run.errOutput, + Initial: true, + Selections: selections, + } +} + +// singleAdd adds the new variation project/config/variation. +func singleAdd(t *testing.T) (*ownershipRun, syncdomain.ResourceID, Options) { + run := newOwnershipRun(t) + id := syncdomain.VariationID("project", "config", "variation") + return run, id, run.options(ownershipCatalog("variation"), id) +} + +// multiProjectAdd adds two new variations in project alpha and one in beta. +func multiProjectAdd(t *testing.T) (*ownershipRun, Options) { + run := newOwnershipRun(t) + options := run.options(ownershipCatalog("first", "second"), + syncdomain.VariationID("alpha", "config", "first"), + syncdomain.VariationID("alpha", "config", "second"), + syncdomain.VariationID("beta", "config", "first"), + ) + return run, options +} + +func (run *ownershipRun) assertWritten(t *testing.T, id syncdomain.ResourceID) { + t.Helper() + _, err := os.Stat(variationPath(run.root, id)) + assert.NoError(t, err, "the file of %s was not written", id.LookupKey) +} + +func (run *ownershipRun) assertNothingWritten(t *testing.T) { + t.Helper() + _, err := os.Stat(filepath.Join(run.root, syncdomain.RootDir)) + assert.ErrorIs(t, err, os.ErrNotExist, "the add wrote files") + assert.Empty(t, run.baselines.manifest.Resources, "the add recorded a lock entry") +} + +func settingsNotFound() error { + return errors.New(`{"code":"not_found","statusCode":404}`) +} + +func settingsForbidden() error { + return fmt.Errorf("%w: your role needs the updateSyncSettings action", syncapi.ErrSyncSettingsForbidden) +} + +// lineWith returns the first line of text that contains every word. +func lineWith(text string, words ...string) (string, bool) { + for _, line := range strings.Split(text, "\n") { + found := true + for _, word := range words { + found = found && strings.Contains(line, word) + } + if found { + return line, true + } + } + return "", false +} + +func TestRunClaimsAVariationWithoutARuleBeforeWritingIt(t *testing.T) { + run, id, options := singleAdd(t) + + require.NoError(t, Run(options)) + + require.Len(t, run.settings.claims, 1) + assert.Equal(t, "project", run.settings.claims[0].projectKey) + assert.Equal(t, []syncdomain.ResourceID{id}, run.settings.claims[0].ids) + assert.Empty(t, run.settings.claims[0].writtenBefore, "the claim must come before the files are written") + run.assertWritten(t, id) + assert.True(t, run.baselines.manifest.Has(id)) +} + +func TestRunPullsButDoesNotClaimAVariationManagedInLaunchDarkly(t *testing.T) { + run := newOwnershipRun(t) + managed := syncdomain.VariationID("project", "config", "managed") + unowned := syncdomain.VariationID("project", "config", "unowned") + run.settings.ownership[managed] = syncdomain.Ownership{ + Direction: syncdomain.SyncDirectionServerCanonical, MatchedPattern: "config/managed", + } + + err := Run(run.options(ownershipCatalog("managed", "unowned"), managed, unowned)) + + require.NoError(t, err) + assert.Equal(t, []syncdomain.ResourceID{unowned}, run.settings.claimedIDs()) + run.assertWritten(t, managed) + run.assertWritten(t, unowned) + assert.Contains(t, run.errOutput.String(), + "config/managed is managed in LaunchDarkly; pulled, not pushed. "+ + "To manage it in code, switch it to Code in LaunchDarkly.") + assert.NotContains(t, run.errOutput.String(), "config/unowned is managed in LaunchDarkly") +} + +func TestRunClaimsOncePerProject(t *testing.T) { + run, options := multiProjectAdd(t) + + require.NoError(t, Run(options)) + + require.Len(t, run.settings.claims, 2, "one claim per project") + byProject := map[string][]syncdomain.ResourceID{} + for _, claim := range run.settings.claims { + require.NotContains(t, byProject, claim.projectKey, "project %s was claimed twice", claim.projectKey) + byProject[claim.projectKey] = claim.ids + assert.Empty(t, claim.writtenBefore, "the claim must come before the files are written") + } + assert.ElementsMatch(t, []syncdomain.ResourceID{ + syncdomain.VariationID("alpha", "config", "first"), syncdomain.VariationID("alpha", "config", "second"), + }, byProject["alpha"]) + assert.Equal(t, []syncdomain.ResourceID{syncdomain.VariationID("beta", "config", "first")}, byProject["beta"]) +} + +// Only a variation's first entry in the repo is claimed. The lock tracks +// this one, but its file is gone, so the add writes it again. +func TestRunDoesNotClaimAVariationAlreadyTrackedInTheLock(t *testing.T) { + run := newOwnershipRun(t) + tracked := syncdomain.VariationID("project", "config", "tracked") + fresh := syncdomain.VariationID("project", "config", "fresh") + run.baselines.manifest.SetFingerprint(tracked, "previous") + + err := Run(run.options(ownershipCatalog("tracked", "fresh"), tracked, fresh)) + + require.NoError(t, err) + assert.Equal(t, []syncdomain.ResourceID{fresh}, run.settings.claimedIDs()) +} + +func TestRunDryRunDoesNotClaim(t *testing.T) { + run, _, options := singleAdd(t) + options.DryRun = true + + require.NoError(t, Run(options)) + + assert.Empty(t, run.settings.claims, "a dry run must not change the sync settings") + run.assertNothingWritten(t) +} + +// The CLI renders a sync problem with its message and suggestion. A forbidden +// claim must reach it as one, or the user sees "unknown error occurred". +func TestRunWritesNothingWhenTheClaimIsForbidden(t *testing.T) { + run, _, options := singleAdd(t) + run.settings.claimErrs["project"] = settingsForbidden() + + err := Run(options) + + require.ErrorIs(t, err, syncapi.ErrSyncSettingsForbidden) + problem, ok := syncdomain.ProblemFrom(err) + require.True(t, ok, "want a sync problem, got %v", err) + assert.Equal(t, syncdomain.ErrorSyncSettingsForbidden, problem.Code) + assert.Contains(t, problem.Error(), "code ownership") + assert.Contains(t, problem.Error(), "updateSyncSettings") + assert.Contains(t, problem.Error(), "proj/project:ai-code-sync/*") + require.Len(t, run.settings.claims, 1) + run.assertNothingWritten(t) +} + +// A claim that fails with an error other than 404 or 403 stops the add +// before anything is written. +func TestRunWritesNothingWhenTheSettingsFail(t *testing.T) { + run, _, options := singleAdd(t) + run.settings.claimErrs["project"] = errors.New(`{"code":"internal","statusCode":500}`) + + err := Run(options) + + require.ErrorContains(t, err, "statusCode\":500") + run.assertNothingWritten(t) +} + +func TestRunWarnsAndContinuesWithYesWhenSettingsAreUnavailable(t *testing.T) { + run, id, options := singleAdd(t) + run.settings.resolveErrs["project"] = settingsNotFound() + options.Yes = true + + require.NoError(t, Run(options)) + + assert.Contains(t, run.errOutput.String(), "ownership") + run.assertWritten(t, id) + assert.True(t, run.baselines.manifest.Has(id)) +} + +func TestRunRequiresYesWithoutATerminalWhenSettingsAreUnavailable(t *testing.T) { + run, _, options := singleAdd(t) + run.settings.resolveErrs["project"] = settingsNotFound() + + err := Run(options) + + require.ErrorContains(t, err, "--yes") + run.assertNothingWritten(t) +} + +// On a terminal, a 404 from the sync settings warns on the error output and +// asks, on the output that the variation picker uses, whether to add without +// recording ownership. A yes adds. (A no: see the cancel test below.) +func TestRunAsksOnATerminalWhenSettingsAreUnavailable(t *testing.T) { + run, id, options := singleAdd(t) + run.settings.resolveErrs["project"] = settingsNotFound() + // One byte per read, so the question reads only its own line. + options.Input = iotest.OneByteReader(strings.NewReader("y\n")) + var checked []io.Writer + options.isTerminal = func(_ io.Reader, writer io.Writer) bool { + checked = append(checked, writer) + return true + } + + require.NoError(t, Run(options)) + + assert.Contains(t, run.output.String(), "Add without recording ownership? [y/N]") + assert.NotContains(t, run.errOutput.String(), "[y/N]") + assert.Contains(t, run.errOutput.String(), "ownership", "the warning stays on the error output") + require.NotEmpty(t, checked) + for _, writer := range checked { + assert.True(t, writer == io.Writer(&run.output), "the terminal check uses the output, not the error output") + } + run.assertWritten(t, id) + assert.True(t, run.baselines.manifest.Has(id)) +} + +// The settings-unavailable question honors the context of the command. With +// a context that has already ended, Run does not wait for an answer and adds +// nothing, even though the input would answer yes. +func TestRunStopsTheSettingsQuestionWhenTheContextEnded(t *testing.T) { + run, _, options := singleAdd(t) + run.settings.resolveErrs["project"] = settingsNotFound() + ctx, cancel := context.WithCancel(context.Background()) + cancel() + options.Context = ctx + options.Input = strings.NewReader("y\n") + options.isTerminal = func(io.Reader, io.Writer) bool { return true } + + done := make(chan error, 1) + go func() { done <- Run(options) }() + + select { + case err := <-done: + require.ErrorIs(t, err, context.Canceled) + case <-time.After(5 * time.Second): + t.Fatal("Run waited for input after the context ended") + } + assert.NotContains(t, run.output.String()+run.errOutput.String(), "[y/N]", "no question is asked") + run.assertNothingWritten(t) +} + +// A 404 in one project must not leave another project claimed when the user +// then cancels the add, so every project is read before any claim. +func TestRunDoesNotClaimAnyProjectWhenTheUserCancelsAfterA404(t *testing.T) { + run, options := multiProjectAdd(t) + run.settings.resolveErrs["beta"] = settingsNotFound() + options.Input = iotest.OneByteReader(strings.NewReader("n\n")) + options.isTerminal = func(io.Reader, io.Writer) bool { return true } + + require.NoError(t, Run(options)) + + assert.Empty(t, run.settings.claims, "a canceled add must not claim any project") + assert.Contains(t, run.output.String(), "Add canceled.") + _, named := lineWith(run.errOutput.String(), "beta") + assert.True(t, named, "the settings-unavailable warning names beta: %q", run.errOutput.String()) + run.assertNothingWritten(t) +} + +// A generic read failure in one project stops the add before any claim, even +// when another project was already read. +func TestRunDoesNotClaimAnyProjectWhenAReadFails(t *testing.T) { + run, options := multiProjectAdd(t) + run.settings.resolveErrs["beta"] = errors.New(`{"code":"internal","statusCode":500}`) + + err := Run(options) + + require.ErrorContains(t, err, "statusCode\":500") + assert.Empty(t, run.settings.claims, "a failed read must not leave a claim") + run.assertNothingWritten(t) +} + +// A 403 on the claim of the second project comes after the first project was +// claimed. The error says so, instead of saying that nothing changed. +func TestRunForbiddenClaimInTheSecondProjectSaysTheFirstWasClaimed(t *testing.T) { + run, options := multiProjectAdd(t) + run.settings.claimErrs["beta"] = settingsForbidden() + + err := Run(options) + + require.ErrorIs(t, err, syncapi.ErrSyncSettingsForbidden) + require.Len(t, run.settings.claims, 2) + _, says := lineWith(err.Error(), "alpha", "already") + assert.True(t, says, "the error says alpha was already claimed for code: %q", err.Error()) + assert.NotContains(t, err.Error(), "nothing changed") + run.assertNothingWritten(t) +} + +// The claim comes before the files and the baseline. When a later step +// fails, the error says that the variations were claimed for code, so the +// user knows that the add can run again. The check hides the temporary +// directory, because its path carries the test name. +func TestRunSaysTheVariationsAreOwnedByCodeWhenALaterStepFails(t *testing.T) { + run, id, options := singleAdd(t) + // Store.Bootstrap refuses to replace an existing managed directory. + require.NoError(t, os.Mkdir(filepath.Join(run.root, syncdomain.RootDir), 0o755)) + + err := Run(options) + + require.Error(t, err) + require.Equal(t, []syncdomain.ResourceID{id}, run.settings.claimedIDs(), "the claim ran before the failure") + message := strings.ReplaceAll(err.Error(), run.root, "") + assert.Contains(t, message, "claimed", "the error says the variations were claimed for code: %q", message) + _, statErr := os.Stat(variationPath(run.root, id)) + assert.ErrorIs(t, statErr, os.ErrNotExist, "the add kept the variation file") +} diff --git a/internal/sync/prompt/create_test.go b/internal/sync/prompt/create_test.go index 18fd7e353..200768040 100644 --- a/internal/sync/prompt/create_test.go +++ b/internal/sync/prompt/create_test.go @@ -648,7 +648,18 @@ func TestCreateAdoptsAVariationWhoseModelConfigHasANewerVersion(t *testing.T) { assert.Equal(t, "Bootstrapped 1 variation file in .launchdarkly.\n", stdout) assert.Equal(t, "Note: variation production/support/escalation already exists in LaunchDarkly with this content, "+ "so add started syncing it.\n", notes) - requireOnlyReads(t, api.requests) + // The adoption is a first pull, so it only reads, records the manifest, + // and claims the variation for code. + for _, request := range api.requests { + if strings.HasPrefix(request, http.MethodGet+" ") { + continue + } + require.True(t, strings.HasPrefix(request, http.MethodPatch+" ") && + (strings.HasSuffix(request, "/configs/sync/manifests") || strings.HasSuffix(request, "/configs/sync/settings")), + request) + } + assert.Equal(t, 1, countRequests(api.requests, http.MethodPatch, "/configs/sync/settings"), "one claim") + assert.Equal(t, []string{"variation:support/escalation code_canonical"}, codeClaims(api.settingsWrites)) assert.Contains(t, readLocalFile(t, root, escalationFile), "modelConfigVersion: 2\n", "the file takes the version from LaunchDarkly") } diff --git a/internal/sync/prompt/runner.go b/internal/sync/prompt/runner.go index c3b99904e..3bbe566dc 100644 --- a/internal/sync/prompt/runner.go +++ b/internal/sync/prompt/runner.go @@ -235,6 +235,7 @@ func (runner Runner) bootstrapOptions(options Options, workspace syncWorkspace, api := runner.api(options) kind := options.Scope.KeyKind() return syncbootstrap.Options{ + Context: options.Context, Kind: kind, Catalog: api, Attachments: api, @@ -249,6 +250,9 @@ func (runner Runner) bootstrapOptions(options Options, workspace syncWorkspace, Selections: selections(action.Resources, action.Filter, kind), NoInput: options.NoInput, Diff: options.Diff, + Settings: api, + ErrorOutput: options.ErrorOutput, + Yes: options.Yes, } }