driftctl/enumeration/remote/azurerm_resources_scanner_t...

109 lines
3.3 KiB
Go
Raw Normal View History

2021-10-04 10:40:55 +00:00
package remote
import (
"testing"
"github.com/snyk/driftctl/enumeration"
"github.com/snyk/driftctl/enumeration/remote/azurerm"
"github.com/snyk/driftctl/enumeration/remote/azurerm/repository"
"github.com/snyk/driftctl/enumeration/remote/common"
error2 "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/terraform"
2021-10-04 10:40:55 +00:00
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
2022-01-06 07:26:20 +00:00
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
2021-10-04 10:40:55 +00:00
"github.com/pkg/errors"
"github.com/snyk/driftctl/enumeration/resource"
resourceazure "github.com/snyk/driftctl/enumeration/resource/azurerm"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/mocks"
2021-10-04 10:40:55 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestAzurermResourceGroups(t *testing.T) {
dummyError := errors.New("this is an error")
tests := []struct {
test string
mocks func(*repository.MockResourcesRepository, *mocks.AlerterInterface)
2021-10-04 10:40:55 +00:00
assertExpected func(t *testing.T, got []*resource.Resource)
wantErr error
}{
{
test: "no resource group",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
2021-10-04 10:40:55 +00:00
repository.On("ListAllResourceGroups").Return([]*armresources.ResourceGroup{}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 0)
},
},
{
test: "error listing resource groups",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
2021-10-04 10:40:55 +00:00
repository.On("ListAllResourceGroups").Return(nil, dummyError)
},
wantErr: error2.NewResourceListingError(dummyError, resourceazure.AzureResourceGroupResourceType),
},
{
test: "multiple resource groups",
mocks: func(repository *repository.MockResourcesRepository, alerter *mocks.AlerterInterface) {
2021-10-04 10:40:55 +00:00
repository.On("ListAllResourceGroups").Return([]*armresources.ResourceGroup{
{
ID: to.StringPtr("group1"),
Name: to.StringPtr("group1"),
},
{
ID: to.StringPtr("group2"),
Name: to.StringPtr("group2"),
},
}, nil)
},
assertExpected: func(t *testing.T, got []*resource.Resource) {
assert.Len(t, got, 2)
assert.Equal(t, got[0].ResourceId(), "group1")
assert.Equal(t, got[0].ResourceType(), resourceazure.AzureResourceGroupResourceType)
assert.Equal(t, got[1].ResourceId(), "group2")
assert.Equal(t, got[1].ResourceType(), resourceazure.AzureResourceGroupResourceType)
},
},
}
2022-07-21 08:37:03 +00:00
factory := terraform.NewTerraformResourceFactory()
2021-10-04 10:40:55 +00:00
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
scanOptions := ScannerOptions{}
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockResourcesRepository{}
2021-10-04 10:40:55 +00:00
c.mocks(fakeRepo, alerter)
var repo repository.ResourcesRepository = fakeRepo
2021-10-04 10:40:55 +00:00
remoteLibrary.AddEnumerator(azurerm.NewAzurermResourceGroupEnumerator(repo, factory))
testFilter := &enumeration.MockFilter{}
2021-10-04 10:40:55 +00:00
testFilter.On("IsTypeIgnored", mock.Anything).Return(false)
s := NewScanner(remoteLibrary, alerter, scanOptions, testFilter)
got, err := s.Resources()
assert.Equal(tt, c.wantErr, err)
if err != nil {
return
}
c.assertExpected(tt, got)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
})
}
}