add scanner tests

main
Martin Guibert 2021-11-10 14:27:08 +01:00
parent c4406b3648
commit 70ddf3c042
No known key found for this signature in database
GPG Key ID: 990E40316943BAA6
5 changed files with 200 additions and 0 deletions

View File

@ -58,6 +58,29 @@ func (_m *MockPrivateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZ
return r0, r1
}
// ListAllCNAMERecords provides a mock function with given fields: zone
func (_m *MockPrivateDNSRepository) ListAllCNAMERecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
ret := _m.Called(zone)
var r0 []*armprivatedns.RecordSet
if rf, ok := ret.Get(0).(func(*armprivatedns.PrivateZone) []*armprivatedns.RecordSet); ok {
r0 = rf(zone)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*armprivatedns.RecordSet)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(*armprivatedns.PrivateZone) error); ok {
r1 = rf(zone)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllPrivateZones provides a mock function with given fields:
func (_m *MockPrivateDNSRepository) ListAllPrivateZones() ([]*armprivatedns.PrivateZone, error) {
ret := _m.Called()

View File

@ -456,3 +456,146 @@ func TestAzurermPrivateDNSAAAARecord(t *testing.T) {
})
}
}
func TestAzurermPrivateDNSCNAMERecord(t *testing.T) {
dummyError := errors.New("this is an error")
tests := []struct {
test string
dirName string
mocks func(*repository.MockPrivateDNSRepository, *mocks.AlerterInterface)
wantErr error
}{
{
test: "no private cname record",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{}, nil)
},
},
{
test: "error listing private zone",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return(nil, dummyError)
},
wantErr: remoteerr.NewResourceListingErrorWithType(dummyError, resourceazure.AzurePrivateDNSCNameRecordResourceType, resourceazure.AzurePrivateDNSZoneResourceType),
},
{
test: "error listing private cname records",
dirName: "azurerm_private_dns_cname_record_empty",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com"),
Name: to.StringPtr("thisisatestusingtf.com"),
},
},
},
}, nil)
repository.On("ListAllCNAMERecords", mock.Anything).Return(nil, dummyError)
},
wantErr: remoteerr.NewResourceListingError(dummyError, resourceazure.AzurePrivateDNSCNameRecordResourceType),
},
{
test: "multiple private cname records",
dirName: "azurerm_private_dns_cname_record_multiple",
mocks: func(repository *repository.MockPrivateDNSRepository, alerter *mocks.AlerterInterface) {
repository.On("ListAllPrivateZones").Return([]*armprivatedns.PrivateZone{
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com"),
Name: to.StringPtr("thisisatestusingtf.com"),
},
},
},
}, nil)
repository.On("ListAllCNAMERecords", mock.Anything).Return([]*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test"),
Name: to.StringPtr("test"),
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest"),
Name: to.StringPtr("othertest"),
},
},
},
}, nil).Once()
},
},
}
providerVersion := "2.71.0"
schemaRepository := testresource.InitFakeSchemaRepository("azurerm", providerVersion)
resourceazure.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
alerter := &mocks.AlerterInterface{}
fakeRepo := &repository.MockPrivateDNSRepository{}
c.mocks(fakeRepo, alerter)
var repo repository.PrivateDNSRepository = fakeRepo
providerVersion := "2.71.0"
realProvider, err := terraformtest.InitTestAzureProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraformtest.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{})
if err != nil {
t.Fatal(err)
}
con := arm.NewDefaultConnection(cred, nil)
repo = repository.NewPrivateDNSRepository(con, realProvider.GetConfig(), cache.New(0))
}
remoteLibrary.AddEnumerator(azurerm.NewAzurermPrivateDNSCNameRecordEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceazure.AzurePrivateDNSCNameRecordResourceType, common.NewGenericDetailsFetcher(resourceazure.AzurePrivateDNSCNameRecordResourceType, provider, deserializer))
testFilter := &filter.MockFilter{}
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
}
test.TestAgainstGoldenFile(got, resourceazure.AzurePrivateDNSCNameRecordResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
alerter.AssertExpectations(tt)
fakeRepo.AssertExpectations(tt)
})
}
}

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiZnFkbiI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsInJlY29yZCI6InN0cmluZyIsInJlc291cmNlX2dyb3VwX25hbWUiOiJzdHJpbmciLCJ0YWdzIjpbIm1hcCIsInN0cmluZyJdLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiY3JlYXRlIjoic3RyaW5nIiwiZGVsZXRlIjoic3RyaW5nIiwicmVhZCI6InN0cmluZyIsInVwZGF0ZSI6InN0cmluZyJ9XSwidHRsIjoibnVtYmVyIiwiem9uZV9uYW1lIjoic3RyaW5nIn1d",
"Val": "eyJmcWRuIjoib3RoZXJ0ZXN0LnRoaXNpc2F0ZXN0dXNpbmd0Zi5jb20uIiwiaWQiOiIvc3Vic2NyaXB0aW9ucy83YmZiMmM1Yy03MzA4LTQ2ZWQtOGFlNC1mZmZhMzU2ZWI0MDYvcmVzb3VyY2VHcm91cHMvbWFydGluLWRldi9wcm92aWRlcnMvTWljcm9zb2Z0Lk5ldHdvcmsvcHJpdmF0ZURuc1pvbmVzL3RoaXNpc2F0ZXN0dXNpbmd0Zi5jb20vQ05BTUUvb3RoZXJ0ZXN0IiwibmFtZSI6Im90aGVydGVzdCIsInJlY29yZCI6Im90aGVydGVzdC5jb20iLCJyZXNvdXJjZV9ncm91cF9uYW1lIjoibWFydGluLWRldiIsInRhZ3MiOnt9LCJ0aW1lb3V0cyI6eyJjcmVhdGUiOm51bGwsImRlbGV0ZSI6bnVsbCwicmVhZCI6bnVsbCwidXBkYXRlIjpudWxsfSwidHRsIjozMDAsInpvbmVfbmFtZSI6InRoaXNpc2F0ZXN0dXNpbmd0Zi5jb20ifQ==",
"Err": null
}

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiZnFkbiI6InN0cmluZyIsImlkIjoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsInJlY29yZCI6InN0cmluZyIsInJlc291cmNlX2dyb3VwX25hbWUiOiJzdHJpbmciLCJ0YWdzIjpbIm1hcCIsInN0cmluZyJdLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiY3JlYXRlIjoic3RyaW5nIiwiZGVsZXRlIjoic3RyaW5nIiwicmVhZCI6InN0cmluZyIsInVwZGF0ZSI6InN0cmluZyJ9XSwidHRsIjoibnVtYmVyIiwiem9uZV9uYW1lIjoic3RyaW5nIn1d",
"Val": "eyJmcWRuIjoidGVzdC50aGlzaXNhdGVzdHVzaW5ndGYuY29tLiIsImlkIjoiL3N1YnNjcmlwdGlvbnMvN2JmYjJjNWMtNzMwOC00NmVkLThhZTQtZmZmYTM1NmViNDA2L3Jlc291cmNlR3JvdXBzL21hcnRpbi1kZXYvcHJvdmlkZXJzL01pY3Jvc29mdC5OZXR3b3JrL3ByaXZhdGVEbnNab25lcy90aGlzaXNhdGVzdHVzaW5ndGYuY29tL0NOQU1FL3Rlc3QiLCJuYW1lIjoidGVzdCIsInJlY29yZCI6InRlc3QuY29tIiwicmVzb3VyY2VfZ3JvdXBfbmFtZSI6Im1hcnRpbi1kZXYiLCJ0YWdzIjp7fSwidGltZW91dHMiOnsiY3JlYXRlIjpudWxsLCJkZWxldGUiOm51bGwsInJlYWQiOm51bGwsInVwZGF0ZSI6bnVsbH0sInR0bCI6MzAwLCJ6b25lX25hbWUiOiJ0aGlzaXNhdGVzdHVzaW5ndGYuY29tIn0=",
"Err": null
}

View File

@ -0,0 +1,24 @@
[
{
"fqdn": "test.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/test",
"name": "test",
"record": "test.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
},
{
"fqdn": "othertest.thisisatestusingtf.com.",
"id": "/subscriptions/7bfb2c5c-7308-46ed-8ae4-fffa356eb406/resourceGroups/martin-dev/providers/Microsoft.Network/privateDnsZones/thisisatestusingtf.com/CNAME/othertest",
"name": "othertest",
"record": "othertest.com",
"resource_group_name": "martin-dev",
"tags": null,
"timeouts": null,
"ttl": 300,
"zone_name": "thisisatestusingtf.com"
}
]