Skip to content

Commit

Permalink
Add config plan check func for acceptance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhung committed Apr 30, 2024
1 parent 75d93a7 commit de850d7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.25.0 (Apr 30, 2024)

IMPROVEMENTS:

* Add plan checking func for acceptance test

## 1.24.0 (Apr 12, 2024)

IMPROVEMENTS:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/samber/lo v1.39.0
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/skeema/knownhosts v1.2.0 h1:h9r9cf0+u7wSE+M183ZtMGgOJKiL96brpaz5ekfJCpM=
Expand Down
85 changes: 83 additions & 2 deletions testutil/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package testutil

import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/rand"
"reflect"
"strings"
"testing"
"text/template"
"time"

"github.com/go-resty/resty/v2"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/samber/lo"
)

func RandomInt() int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(10000000)
}

Expand Down Expand Up @@ -82,3 +86,80 @@ func MkNames(name, resource string) (int, string, string) {
n := fmt.Sprintf("%s%d", name, id)
return id, fmt.Sprintf("%s.%s", resource, n), n
}

var ConfigPlanChecks = resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{
DebugPlan("PostApplyPreRefresh"),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
DebugPlan("PostApplyPostRefresh"),
},
}

var _ plancheck.PlanCheck = PlanCheck{}

type PlanCheck struct {
Stage string
}

func (p PlanCheck) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) {
var err error

rc, err := json.Marshal(req.Plan.ResourceChanges[0])
if err != nil {
resp.Error = err
return
}

pv, err := json.Marshal(req.Plan.PlannedValues)
if err != nil {
resp.Error = err
return
}

ps, err := json.Marshal(req.Plan.PriorState)
if err != nil {
resp.Error = err
return
}

rd, err := json.Marshal(req.Plan.ResourceDrift)
if err != nil {
resp.Error = err
return
}

tflog.Debug(ctx, "CheckPlan", map[string]interface{}{
"stage": p.Stage,
"req.Plan.ResourceChanges.ResourceDrift": string(rd),
"req.Plan.ResourceChanges": string(rc),
"req.Plan.PlannedValues": string(pv),
"req.Plan.PriorState": string(ps),
})

if len(req.Plan.ResourceDrift) > 0 {
drifts := lo.Map(req.Plan.ResourceDrift, func(c *tfjson.ResourceChange, index int) string {
return fmt.Sprintf("Name: %s, Before: %v, After: %v", c.Name, c.Change.Before, c.Change.After)
})
resp.Error = fmt.Errorf("expected empty plan, but has resouce drifts(s): %v", strings.Join(drifts, ", "))
return
}

var errStrings []string
for _, rc := range req.Plan.ResourceChanges {
if !rc.Change.Actions.NoOp() {
errStrings = append(errStrings, fmt.Sprintf("expected empty plan, but %s has planned action(s): %v\n\nbefore: %v\n\nafter: %v\n\nunknown: %v", rc.Address, rc.Change.Actions, rc.Change.Before, rc.Change.After, rc.Change.AfterUnknown))
}
}

if len(errStrings) > 0 {
resp.Error = fmt.Errorf(strings.Join(errStrings, "\n"))
return
}
}

func DebugPlan(stage string) plancheck.PlanCheck {
return PlanCheck{
Stage: stage,
}
}

0 comments on commit de850d7

Please sign in to comment.