Skip to content

Commit

Permalink
Added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
akselleirv committed Jun 6, 2024
1 parent 87cde18 commit bde16cc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
33 changes: 20 additions & 13 deletions runner/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,9 @@ func (r *TerraformRunnerServer) Apply(ctx context.Context, req *ApplyRequest) (*
applyOpt = append(applyOpt, tfexec.Parallelism(int(req.Parallelism)))
}

if os.Getenv("LOG_HUMAN_READABLE_PLAN") == "1" {
planName := TFPlanName
if req.DirOrPlan != "" {
planName = req.DirOrPlan
}

rawOutput, err := r.tfShowPlanFileRaw(ctx, planName)
if err != nil {
log.Error(err, "unable to get the plan output for human")
return nil, err
}

fmt.Println(rawOutput)
if err := printHumanReadablePlanIfEnabled(ctx, req.DirOrPlan, r.tfShowPlanFileRaw); err != nil {
log.Error(err, "unable to print plan")
return nil, err
}

if err := r.tf.Apply(ctx, applyOpt...); err != nil {
Expand All @@ -416,6 +406,23 @@ func (r *TerraformRunnerServer) Apply(ctx context.Context, req *ApplyRequest) (*
return &ApplyReply{Message: "ok"}, nil
}

func printHumanReadablePlanIfEnabled(ctx context.Context, planName string, tfShowPlanFileRaw func(ctx context.Context, planPath string, opts ...tfexec.ShowOption) (string, error)) error {
if os.Getenv("LOG_HUMAN_READABLE_PLAN") == "1" {
if planName == "" {
planName = TFPlanName
}

rawOutput, err := tfShowPlanFileRaw(ctx, planName)
if err != nil {
return err
}

fmt.Println(rawOutput)
}

return nil
}

func getInventoryFromTerraformModule(m *tfjson.StateModule) []*Inventory {
var result []*Inventory
for _, r := range m.Resources {
Expand Down
52 changes: 52 additions & 0 deletions runner/server_test.go
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())
}

0 comments on commit bde16cc

Please sign in to comment.