2021-03-31 14:33:25 +00:00
|
|
|
package test
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ScanResult struct {
|
|
|
|
*require.Assertions
|
2021-03-31 14:33:25 +00:00
|
|
|
*analyser.Analysis
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:25 +00:00
|
|
|
func NewScanResult(t *testing.T, analysis *analyser.Analysis) *ScanResult {
|
2020-12-09 15:31:34 +00:00
|
|
|
return &ScanResult{
|
|
|
|
Assertions: require.New(t),
|
|
|
|
Analysis: analysis,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertResourceUnmanaged(id, ty string) {
|
|
|
|
for _, u := range r.Unmanaged() {
|
|
|
|
if u.TerraformType() == ty && u.TerraformId() == id {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Failf("Resource not unmanaged", "%s(%s)", id, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertResourceDeleted(id, ty string) {
|
|
|
|
for _, u := range r.Deleted() {
|
|
|
|
if u.TerraformType() == ty && u.TerraformId() == id {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Failf("Resource not deleted", "%s(%s)", id, ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertResourceDriftCount(id, ty string, count int) {
|
|
|
|
for _, u := range r.Differences() {
|
|
|
|
if u.Res.TerraformType() == ty && u.Res.TerraformId() == id {
|
|
|
|
r.Equal(count, len(u.Changelog))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.Failf("no differences found", "%s(%s)", id, ty)
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:14:26 +00:00
|
|
|
func (r *ScanResult) AssertResourceHasDrift(id, ty string, change analyser.Change) {
|
2020-12-09 15:31:34 +00:00
|
|
|
found := false
|
|
|
|
for _, u := range r.Differences() {
|
|
|
|
if u.Res.TerraformType() == ty && u.Res.TerraformId() == id {
|
|
|
|
changelogStr, _ := json.MarshalIndent(u.Changelog, "", " ")
|
|
|
|
changeStr, _ := json.MarshalIndent(change, "", " ")
|
|
|
|
r.Contains(u.Changelog, change, fmt.Sprintf("Change not found\nCHANGE: %s\nCHANGELOG:\n%s", changeStr, changelogStr))
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2021-01-28 18:57:04 +00:00
|
|
|
r.Failf("no differences found", "%s (%s)", id, ty)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertResourceHasNoDrift(id, ty string) {
|
|
|
|
for _, u := range r.Differences() {
|
|
|
|
if u.Res.TerraformType() == ty && u.Res.TerraformId() == id {
|
|
|
|
changelogStr, _ := json.MarshalIndent(u.Changelog, "", " ")
|
|
|
|
r.Failf("resource has drifted", "%s(%s) :\n %v", id, ty, changelogStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertCoverage(expected int) {
|
2021-04-19 13:42:10 +00:00
|
|
|
r.Equal(expected, r.Coverage())
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertDriftCountTotal(count int) {
|
|
|
|
driftCount := 0
|
|
|
|
for _, diff := range r.Differences() {
|
|
|
|
driftCount += len(diff.Changelog)
|
|
|
|
}
|
|
|
|
r.Equal(count, driftCount)
|
|
|
|
}
|
2021-01-07 16:53:17 +00:00
|
|
|
|
2021-02-03 17:01:21 +00:00
|
|
|
func (r *ScanResult) AssertDeletedCount(count int) {
|
|
|
|
r.Equal(count, len(r.Deleted()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ScanResult) AssertManagedCount(count int) {
|
|
|
|
r.Equal(count, len(r.Managed()))
|
|
|
|
}
|
|
|
|
|
2021-02-23 12:44:48 +00:00
|
|
|
func (r *ScanResult) AssertUnmanagedCount(count int) {
|
|
|
|
r.Equal(count, len(r.Unmanaged()))
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:53:17 +00:00
|
|
|
func (r ScanResult) AssertInfrastructureIsInSync() {
|
2021-01-22 17:06:17 +00:00
|
|
|
r.Equal(
|
|
|
|
true,
|
|
|
|
r.Analysis.IsSync(),
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Infrastructure is not in sync: %+v\nUnmanaged:\n%+v\nDeleted:\n%+v\nDifferences:\n%+v\n",
|
|
|
|
r.Analysis.Summary(),
|
|
|
|
r.Analysis.Unmanaged(),
|
|
|
|
r.Analysis.Deleted(),
|
|
|
|
r.Analysis.Differences(),
|
|
|
|
),
|
|
|
|
)
|
2021-01-07 16:53:17 +00:00
|
|
|
}
|
2021-04-19 13:42:10 +00:00
|
|
|
|
|
|
|
func (r ScanResult) AssertInfrastructureIsNotSync() {
|
|
|
|
r.Equal(
|
|
|
|
false,
|
|
|
|
r.Analysis.IsSync(),
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Infrastructure is in sync: %+v\nUnmanaged:\n%+v\nDeleted:\n%+v\nDifferences:\n%+v\n",
|
|
|
|
r.Analysis.Summary(),
|
|
|
|
r.Analysis.Unmanaged(),
|
|
|
|
r.Analysis.Deleted(),
|
|
|
|
r.Analysis.Differences(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|