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

235 lines
5.4 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"
testresource "github.com/cloudskiff/driftctl/test/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-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(
&testresource.FakeResource{
Id: "deleted-id-3",
Type: "aws_deleted_resource",
},
)
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(
&testresource.FakeResource{
Id: "deleted-id-3",
Type: "aws_deleted_resource",
},
&testresource.FakeResource{
Id: "deleted-id-4",
Type: "aws_deleted_resource",
},
&testresource.FakeResource{
Id: "deleted-id-5",
Type: "aws_deleted_resource",
},
&testresource.FakeResource{
Id: "deleted-id-6",
Type: "aws_deleted_resource",
},
)
a.AddUnmanaged(
&testresource.FakeResource{
Id: "unmanaged-id-3",
Type: "aws_unmanaged_resource",
},
&testresource.FakeResource{
Id: "unmanaged-id-4",
Type: "aws_unmanaged_resource",
},
&testresource.FakeResource{
Id: "unmanaged-id-5",
Type: "aws_unmanaged_resource",
},
)
a.AddDifference(analyser.Difference{Res: &testresource.FakeResource{
2021-06-10 16:35:13 +00:00
Id: "diff-id-2",
2021-06-07 17:26:39 +00:00
Type: "aws_diff_resource",
}, Changelog: []analyser.Change{
{
Change: diff.Change{
Type: diff.DELETE,
2021-06-10 16:35:13 +00:00
Path: []string{"path", "to", "fields", "0"},
2021-06-11 16:43:51 +00:00
From: "value",
2021-06-10 16:35:13 +00:00
To: nil,
},
},
{
Change: diff.Change{
2021-06-11 16:43:51 +00:00
Type: diff.UPDATE,
2021-06-10 16:35:13 +00:00
Path: []string{"path", "to", "fields", "1"},
2021-06-11 16:43:51 +00:00
From: 12,
To: "12",
2021-06-10 16:35:13 +00:00
},
},
{
Change: diff.Change{
Type: diff.DELETE,
2021-06-11 16:43:51 +00:00
Path: []string{"group_ids"},
2021-06-10 16:35:13 +00:00
From: []string{"a071314398026"},
To: nil,
},
},
2021-06-11 16:43:51 +00:00
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"Policies", "0"},
From: testresource.FakeResource{},
To: testresource.FakeResource{Id: "093cd6ba-cf6d-4800-b252-6a50ca8903cd", Type: "aws_iam_policy"},
},
},
2021-06-10 16:35:13 +00:00
{
Change: diff.Change{
Type: diff.CREATE,
2021-06-11 16:43:51 +00:00
Path: []string{"Tags", "0", "Name"},
2021-06-07 17:26:39 +00:00
From: nil,
2021-06-11 16:43:51 +00:00
To: "test",
2021-06-10 16:35:13 +00:00
},
},
{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"InstanceInitiatedShutdownBehavior"},
From: "",
To: nil,
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
resources []resource.Resource
value []string
}{
{
name: "test empty array",
resources: []resource.Resource{},
value: []string{},
},
{
name: "test empty array",
resources: []resource.Resource{
2021-06-07 17:26:39 +00:00
&testresource.FakeResource{
2021-06-07 13:21:57 +00:00
Id: "deleted-id-1",
Type: "aws_deleted_resource",
},
2021-06-07 17:26:39 +00:00
&testresource.FakeResource{
2021-06-07 13:21:57 +00:00
Id: "unmanaged-id-1",
Type: "aws_unmanaged_resource",
},
2021-06-07 17:26:39 +00:00
&testresource.FakeResource{
2021-06-07 13:21:57 +00:00
Id: "unmanaged-id-2",
Type: "aws_unmanaged_resource",
},
2021-06-07 17:26:39 +00:00
&testresource.FakeResource{
2021-06-07 13:21:57 +00:00
Id: "diff-id-1",
Type: "aws_diff_resource",
},
2021-06-07 17:26:39 +00:00
&testresource.FakeResource{
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)
})
}
}