Skip to content

Commit 89eeaed

Browse files
authored
[WIP] backend/enhanced: start with absolute configuration path (#22096)
* backend/enhanced: start with absolute config path We recently started normalizing the config path before all "command" operations, which was necessary for consistency but had unexpected consequences for remote backend operations, specifically when a vcs root with a working directory are configured. This PR de-normalizes the path back to an absolute path. * Check the error and add a test It turned out all required logic was already present, so I just needed to add a test for this specific use case.
1 parent e39fa55 commit 89eeaed

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

backend/remote/backend_plan.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,18 @@ func (b *Remote) plan(stopCtx, cancelCtx context.Context, op *backend.Operation,
138138

139139
var configDir string
140140
if op.ConfigDir != "" {
141+
// De-normalize the configuration directory path.
142+
configDir, err = filepath.Abs(op.ConfigDir)
143+
if err != nil {
144+
return nil, generalError(
145+
"Failed to get absolute path of the configuration directory: %v", err)
146+
}
147+
141148
// Make sure to take the working directory into account by removing
142149
// the working directory from the current path. This will result in
143150
// a path that points to the expected root of the workspace.
144151
configDir = filepath.Clean(strings.TrimSuffix(
145-
filepath.Clean(op.ConfigDir),
152+
filepath.Clean(configDir),
146153
filepath.Clean(w.WorkingDirectory),
147154
))
148155
} else {

backend/remote/backend_plan_test.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ func TestRemote_planWithWorkingDirectory(t *testing.T) {
622622
WorkingDirectory: tfe.String("terraform"),
623623
}
624624

625-
// Configure the workspace to use a custom working direcrtory.
625+
// Configure the workspace to use a custom working directory.
626626
_, err := b.client.Workspaces.Update(context.Background(), b.organization, b.workspace, options)
627627
if err != nil {
628628
t.Fatalf("error configuring working directory: %v", err)
@@ -655,6 +655,61 @@ func TestRemote_planWithWorkingDirectory(t *testing.T) {
655655
}
656656
}
657657

658+
func TestRemote_planWithWorkingDirectoryFromCurrentPath(t *testing.T) {
659+
b, bCleanup := testBackendDefault(t)
660+
defer bCleanup()
661+
662+
options := tfe.WorkspaceUpdateOptions{
663+
WorkingDirectory: tfe.String("terraform"),
664+
}
665+
666+
// Configure the workspace to use a custom working directory.
667+
_, err := b.client.Workspaces.Update(context.Background(), b.organization, b.workspace, options)
668+
if err != nil {
669+
t.Fatalf("error configuring working directory: %v", err)
670+
}
671+
672+
wd, err := os.Getwd()
673+
if err != nil {
674+
t.Fatalf("error getting current working directory: %v", err)
675+
}
676+
677+
// We need to change into the configuration directory to make sure
678+
// the logic to upload the correct slug is working as expected.
679+
if err := os.Chdir("./testdata/plan-with-working-directory/terraform"); err != nil {
680+
t.Fatalf("error changing directory: %v", err)
681+
}
682+
defer os.Chdir(wd) // Make sure we change back again when were done.
683+
684+
// For this test we need to give our current directory instead of the
685+
// full path to the configuration as we already changed directories.
686+
op, configCleanup := testOperationPlan(t, ".")
687+
defer configCleanup()
688+
689+
op.Workspace = backend.DefaultStateName
690+
691+
run, err := b.Operation(context.Background(), op)
692+
if err != nil {
693+
t.Fatalf("error starting operation: %v", err)
694+
}
695+
696+
<-run.Done()
697+
if run.Result != backend.OperationSuccess {
698+
t.Fatalf("operation failed: %s", b.CLI.(*cli.MockUi).ErrorWriter.String())
699+
}
700+
if run.PlanEmpty {
701+
t.Fatalf("expected a non-empty plan")
702+
}
703+
704+
output := b.CLI.(*cli.MockUi).OutputWriter.String()
705+
if !strings.Contains(output, "Running plan in the remote backend") {
706+
t.Fatalf("expected remote backend header in output: %s", output)
707+
}
708+
if !strings.Contains(output, "1 to add, 0 to change, 0 to destroy") {
709+
t.Fatalf("expected plan summery in output: %s", output)
710+
}
711+
}
712+
658713
func TestRemote_planPolicyPass(t *testing.T) {
659714
b, bCleanup := testBackendDefault(t)
660715
defer bCleanup()

0 commit comments

Comments
 (0)