-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1304 from flux-iac/print-plan
Print plan before apply
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-exec/tfexec" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func Test_printHumanReadablePlanIfEnabled(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
ctx := context.Background() | ||
|
||
defer func() { | ||
g.Expect(os.Unsetenv("LOG_HUMAN_READABLE_PLAN")).Should(Succeed()) | ||
}() | ||
|
||
var tfShowPlanFileRawCalled int | ||
expectedPlan := TFPlanName | ||
tfShowPlanFileRaw := func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) { | ||
g.Expect(planPath).To(Equal(expectedPlan)) | ||
tfShowPlanFileRawCalled++ | ||
return "", nil | ||
} | ||
|
||
// When plan is enabled, then it should be called once | ||
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "1")).Should(Succeed()) | ||
g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).Should(Succeed()) | ||
g.Expect(tfShowPlanFileRawCalled).To(Equal(1)) | ||
|
||
// When the planName is non-empty, then it should use the planName | ||
expectedPlan = "foo" | ||
g.Expect(printHumanReadablePlanIfEnabled(ctx, expectedPlan, tfShowPlanFileRaw)).Should(Succeed()) | ||
g.Expect(tfShowPlanFileRawCalled).To(Equal(2)) | ||
|
||
// When it is disabled, then it should not be called | ||
expectedPlan = TFPlanName | ||
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "0")).Should(Succeed()) | ||
g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).Should(Succeed()) | ||
g.Expect(tfShowPlanFileRawCalled).To(Equal(2)) | ||
|
||
// When tfShowPlanFileRaw fails, then it should return an error | ||
g.Expect(os.Setenv("LOG_HUMAN_READABLE_PLAN", "1")).Should(Succeed()) | ||
tfShowPlanFileRaw = func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error) { | ||
return "", fmt.Errorf("error") | ||
} | ||
|
||
g.Expect(printHumanReadablePlanIfEnabled(ctx, "", tfShowPlanFileRaw)).ShouldNot(Succeed()) | ||
} |