driftctl/pkg/cmd/scan/output/html_test.go

245 lines
5.5 KiB
Go
Raw Normal View History

2021-04-21 16:51:58 +00:00
package output
import (
"io/ioutil"
"path"
"testing"
2021-06-08 15:14:28 +00:00
"time"
2021-04-21 16:51:58 +00:00
2021-06-07 13:21:57 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
2021-06-07 17:26:39 +00:00
"github.com/r3labs/diff/v2"
2021-04-21 16:51:58 +00:00
"github.com/stretchr/testify/assert"
"github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/cloudskiff/driftctl/test/goldenfile"
)
func TestHTML_Write(t *testing.T) {
tests := []struct {
name string
goldenfile string
2021-06-07 17:26:39 +00:00
analysis func() *analyser.Analysis
2021-04-21 16:51:58 +00:00
err error
}{
2021-06-08 15:14:28 +00:00
{
name: "test html output when there's no resources",
goldenfile: "output_empty.html",
analysis: func() *analyser.Analysis {
a := &analyser.Analysis{}
2021-06-10 13:25:54 +00:00
a.Date = time.Date(2021, 06, 10, 0, 0, 0, 0, &time.Location{})
2021-07-29 09:50:35 +00:00
a.ProviderName = "AWS"
a.ProviderVersion = "3.19.0"
2021-06-08 15:14:28 +00:00
return a
},
err: nil,
},
{
name: "test html output when infrastructure is in sync",
goldenfile: "output_sync.html",
analysis: func() *analyser.Analysis {
a := &analyser.Analysis{}
2021-06-10 13:25:54 +00:00
a.Date = time.Date(2021, 06, 10, 0, 0, 0, 0, &time.Location{})
2021-06-08 15:14:28 +00:00
a.Duration = 72 * time.Second
a.AddManaged(
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-08 15:14:28 +00:00
Id: "deleted-id-3",
Type: "aws_deleted_resource",
},
)
2021-07-29 09:50:35 +00:00
a.ProviderName = "AWS"
a.ProviderVersion = "3.19.0"
2021-06-08 15:14:28 +00:00
return a
},
err: nil,
},
2021-04-21 16:51:58 +00:00
{
name: "test html output",
goldenfile: "output.html",
2021-06-07 17:26:39 +00:00
analysis: func() *analyser.Analysis {
a := fakeAnalysisWithAlerts()
2021-06-10 13:25:54 +00:00
a.Date = time.Date(2021, 06, 10, 0, 0, 0, 0, &time.Location{})
2021-06-08 15:14:28 +00:00
a.Duration = 91 * time.Second
2021-06-07 17:26:39 +00:00
a.AddDeleted(
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "deleted-id-3",
Type: "aws_deleted_resource",
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "deleted-id-4",
Type: "aws_deleted_resource",
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "deleted-id-5",
Type: "aws_deleted_resource",
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "deleted-id-6",
Type: "aws_deleted_resource",
},
)
a.AddUnmanaged(
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "unmanaged-id-3",
Type: "aws_unmanaged_resource",
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "unmanaged-id-4",
Type: "aws_unmanaged_resource",
},
2021-08-09 14:03:04 +00:00
&resource.Resource{
2021-06-07 17:26:39 +00:00
Id: "unmanaged-id-5",
Type: "aws_unmanaged_resource",
},
)
a.AddDifference(analyser.Difference{
2021-08-09 14:03:04 +00:00
Res: &resource.Resource{
Id: "diff-id-2",
Type: "aws_diff_resource",
Source: &resource.TerraformStateSource{
State: "tfstate://state.tfstate",
Module: "module",
Name: "name",
2021-06-10 16:35:13 +00:00
},
}, Changelog: []analyser.Change{
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"path", "to", "fields", "0"},
From: "value",
To: nil,
},
2021-06-10 16:35:13 +00:00
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"path", "to", "fields", "1"},
From: 12,
To: "12",
},
2021-06-10 16:35:13 +00:00
},
{
Change: diff.Change{
Type: diff.DELETE,
Path: []string{"group_ids"},
From: []string{"a071314398026"},
To: nil,
},
2021-06-11 16:43:51 +00:00
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"Policies", "0"},
2021-08-09 14:03:04 +00:00
From: resource.Resource{},
To: resource.Resource{Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd", Type: "aws_iam_policy"},
},
2021-06-10 16:35:13 +00:00
},
{
Change: diff.Change{
Type: diff.CREATE,
Path: []string{"Tags", "0", "Name"},
From: nil,
To: "test",
},
2021-06-07 17:26:39 +00:00
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"InstanceInitiatedShutdownBehavior"},
From: "",
To: nil,
},
},
}})
2021-07-29 09:50:35 +00:00
a.ProviderName = "AWS"
a.ProviderVersion = "3.19.0"
2021-06-07 17:26:39 +00:00
return a
2021-04-21 16:51:58 +00:00
},
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := t.TempDir()
tempFile, err := ioutil.TempFile(tempDir, "result")
if err != nil {
t.Fatal(err)
}
c := NewHTML(tempFile.Name())
2021-06-07 17:26:39 +00:00
err = c.Write(tt.analysis())
2021-04-21 16:51:58 +00:00
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
}
2021-04-22 16:21:00 +00:00
got, err := ioutil.ReadFile(tempFile.Name())
2021-04-21 16:51:58 +00:00
if err != nil {
t.Fatal(err)
}
expectedFilePath := path.Join("./testdata/", tt.goldenfile)
if *goldenfile.Update == tt.goldenfile {
2021-04-22 16:21:00 +00:00
if err := ioutil.WriteFile(expectedFilePath, got, 0600); err != nil {
2021-04-21 16:51:58 +00:00
t.Fatal(err)
}
}
expected, err := ioutil.ReadFile(expectedFilePath)
if err != nil {
t.Fatal(err)
}
2021-06-01 09:38:47 +00:00
assert.Equal(t, string(expected), string(got))
2021-04-21 16:51:58 +00:00
})
}
}
2021-06-07 13:21:57 +00:00
func TestHTML_DistinctResourceTypes(t *testing.T) {
tests := []struct {
name string
2021-08-09 14:03:04 +00:00
resources []*resource.Resource
2021-06-07 13:21:57 +00:00
value []string
}{
{
name: "test empty array",
2021-08-09 14:03:04 +00:00
resources: []*resource.Resource{},
2021-06-07 13:21:57 +00:00
value: []string{},
},
{
name: "test empty array",
2021-08-09 14:03:04 +00:00
resources: []*resource.Resource{
{
2021-06-07 13:21:57 +00:00
Id: "deleted-id-1",
Type: "aws_deleted_resource",
},
2021-08-09 14:03:04 +00:00
{
2021-06-07 13:21:57 +00:00
Id: "unmanaged-id-1",
Type: "aws_unmanaged_resource",
},
2021-08-09 14:03:04 +00:00
{
2021-06-07 13:21:57 +00:00
Id: "unmanaged-id-2",
Type: "aws_unmanaged_resource",
},
2021-08-09 14:03:04 +00:00
{
2021-06-07 13:21:57 +00:00
Id: "diff-id-1",
Type: "aws_diff_resource",
},
2021-08-09 14:03:04 +00:00
{
2021-06-07 13:21:57 +00:00
Id: "deleted-id-2",
Type: "aws_deleted_resource",
},
},
value: []string{"aws_deleted_resource", "aws_unmanaged_resource", "aws_diff_resource"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := distinctResourceTypes(tt.resources)
assert.Equal(t, tt.value, got)
})
}
}