add test for repo and some fix

main
Martin Guibert 2021-10-27 16:29:45 +02:00
parent 8247f2a41f
commit 0a8a885aaa
No known key found for this signature in database
GPG Key ID: 990E40316943BAA6
4 changed files with 512 additions and 11 deletions

View File

@ -0,0 +1,58 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import (
context "context"
armprivatedns "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
mock "github.com/stretchr/testify/mock"
)
// mockPrivateDNSRecordSetListPager is an autogenerated mock type for the privateDNSRecordSetListPager type
type mockPrivateDNSRecordSetListPager struct {
mock.Mock
}
// Err provides a mock function with given fields:
func (_m *mockPrivateDNSRecordSetListPager) Err() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// NextPage provides a mock function with given fields: ctx
func (_m *mockPrivateDNSRecordSetListPager) NextPage(ctx context.Context) bool {
ret := _m.Called(ctx)
var r0 bool
if rf, ok := ret.Get(0).(func(context.Context) bool); ok {
r0 = rf(ctx)
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// PageResponse provides a mock function with given fields:
func (_m *mockPrivateDNSRecordSetListPager) PageResponse() armprivatedns.RecordSetsListResponse {
ret := _m.Called()
var r0 armprivatedns.RecordSetsListResponse
if rf, ok := ret.Get(0).(func() armprivatedns.RecordSetsListResponse); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(armprivatedns.RecordSetsListResponse)
}
return r0
}

View File

@ -0,0 +1,29 @@
// Code generated by mockery v0.0.0-dev. DO NOT EDIT.
package repository
import (
armprivatedns "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
mock "github.com/stretchr/testify/mock"
)
// mockPrivateRecordSetClient is an autogenerated mock type for the privateRecordSetClient type
type mockPrivateRecordSetClient struct {
mock.Mock
}
// List provides a mock function with given fields: resourceGroupName, privateZoneName, options
func (_m *mockPrivateRecordSetClient) List(resourceGroupName string, privateZoneName string, options *armprivatedns.RecordSetsListOptions) privateDNSRecordSetListPager {
ret := _m.Called(resourceGroupName, privateZoneName, options)
var r0 privateDNSRecordSetListPager
if rf, ok := ret.Get(0).(func(string, string, *armprivatedns.RecordSetsListOptions) privateDNSRecordSetListPager); ok {
r0 = rf(resourceGroupName, privateZoneName, options)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(privateDNSRecordSetListPager)
}
}
return r0
}

View File

@ -66,7 +66,7 @@ func NewPrivateDNSRepository(con *arm.Connection, config common.AzureProviderCon
}
func (s *privateDNSRepository) listAllRecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("listAllRecords-%s", *zone.ID)
cacheKey := fmt.Sprintf("privateDNSlistAllRecords-%s", *zone.ID)
v := s.cache.GetAndLock(cacheKey)
defer s.cache.Unlock(cacheKey)
if v != nil {
@ -116,11 +116,13 @@ func (s *privateDNSRepository) ListAllARecords(zone *armprivatedns.PrivateZone)
}
s.cache.Put(cacheKey, results)
return results, nil
}
func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZone) ([]*armprivatedns.RecordSet, error) {
cacheKey := fmt.Sprintf("ListAllAAAARecords-%s", *zone.ID)
cacheKey := fmt.Sprintf("privateDNSListAllAAAARecords-%s", *zone.ID)
if v := s.cache.Get(cacheKey); v != nil {
return v.([]*armprivatedns.RecordSet), nil
}
@ -138,6 +140,8 @@ func (s *privateDNSRepository) ListAllAAAARecords(zone *armprivatedns.PrivateZon
}
s.cache.Put(cacheKey, results)
return results, nil
}

View File

@ -4,6 +4,7 @@ import (
"reflect"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/pkg/errors"
@ -11,34 +12,35 @@ import (
"github.com/stretchr/testify/mock"
)
// region PrivateZone
func Test_ListAllPrivateZones_MultiplesResults(t *testing.T) {
expected := []*armprivatedns.PrivateZone{
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone1"),
ID: to.StringPtr("zone1"),
},
},
},
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone2"),
ID: to.StringPtr("zone2"),
},
},
},
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone3"),
ID: to.StringPtr("zone3"),
},
},
},
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone4"),
ID: to.StringPtr("zone4"),
},
},
},
@ -57,14 +59,14 @@ func Test_ListAllPrivateZones_MultiplesResults(t *testing.T) {
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone1"),
ID: to.StringPtr("zone1"),
},
},
},
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone2"),
ID: to.StringPtr("zone2"),
},
},
},
@ -79,14 +81,14 @@ func Test_ListAllPrivateZones_MultiplesResults(t *testing.T) {
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone3"),
ID: to.StringPtr("zone3"),
},
},
},
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone4"),
ID: to.StringPtr("zone4"),
},
},
},
@ -125,7 +127,7 @@ func Test_ListAllPrivateZones_MultiplesResults_WithCache(t *testing.T) {
{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: func(s string) *string { return &s }("zone1"),
ID: to.StringPtr("zone1"),
},
},
},
@ -178,3 +180,411 @@ func Test_ListAllPrivateZones_Error(t *testing.T) {
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
// endregion
// region ARecord
func Test_ListAllARecords_MultiplesResults(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
mockPager := &mockPrivateDNSRecordSetListPager{}
mockPager.On("Err").Return(nil).Times(3)
mockPager.On("NextPage", mock.Anything).Return(true).Times(2)
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record2"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
ARecords: []*armprivatedns.ARecord{
{IPv4Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record4"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllARecords() error = %v", err)
return
}
mockPager.AssertExpectations(t)
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllARecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllARecords_MultiplesResults_WithCache(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllARecords() error = %v", err)
return
}
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllARecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllARecords_Error(t *testing.T) {
fakeClient := &mockPrivateRecordSetClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockPrivateDNSRecordSetListPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{}).Times(1)
fakeClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
s := &privateDNSRepository{
recordClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
// endregion
// region AAAAAAARecord
func Test_ListAllAAAARecords_MultiplesResults(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
mockPager := &mockPrivateDNSRecordSetListPager{}
mockPager.On("Err").Return(nil).Times(3)
mockPager.On("NextPage", mock.Anything).Return(true).Times(2)
mockPager.On("NextPage", mock.Anything).Return(false).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record2"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{
RecordSetsListResult: armprivatedns.RecordSetsListResult{
RecordSetListResult: armprivatedns.RecordSetListResult{
Value: []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record3"),
},
},
Properties: &armprivatedns.RecordSetProperties{
AaaaRecords: []*armprivatedns.AaaaRecord{
{IPv6Address: to.StringPtr("ip")},
},
},
},
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record4"),
},
},
Properties: &armprivatedns.RecordSetProperties{},
},
},
},
},
}).Times(1)
fakeRecordSetClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Put", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", expected).Return(true).Times(1)
c.On("GetAndLock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(nil).Times(1)
c.On("Unlock", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return().Times(1)
c.On("Put", "privateDNSlistAllRecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com", mock.Anything).Return(true).Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllAAAARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllAAAARecords() error = %v", err)
return
}
mockPager.AssertExpectations(t)
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllAAAARecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllAAAARecords_MultiplesResults_WithCache(t *testing.T) {
expected := []*armprivatedns.RecordSet{
{
ProxyResource: armprivatedns.ProxyResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("record1"),
},
},
},
}
fakeRecordSetClient := &mockPrivateRecordSetClient{}
c := &cache.MockCache{}
c.On("Get", "privateDNSListAllAAAARecords-/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com").Return(expected).Times(1)
s := &privateDNSRepository{
recordClient: fakeRecordSetClient,
cache: c,
}
got, err := s.ListAllAAAARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
if err != nil {
t.Errorf("ListAllAAAARecords() error = %v", err)
return
}
fakeRecordSetClient.AssertExpectations(t)
c.AssertExpectations(t)
if !reflect.DeepEqual(got, expected) {
t.Errorf("ListAllAAAARecords() got = %v, want %v", got, expected)
}
}
func Test_ListAllAAAARecords_Error(t *testing.T) {
fakeClient := &mockPrivateRecordSetClient{}
expectedErr := errors.New("unexpected error")
mockPager := &mockPrivateDNSRecordSetListPager{}
mockPager.On("Err").Return(expectedErr).Times(1)
mockPager.On("NextPage", mock.Anything).Return(true).Times(1)
mockPager.On("PageResponse").Return(armprivatedns.RecordSetsListResponse{}).Times(1)
fakeClient.On("List", "rgid", "zone", (*armprivatedns.RecordSetsListOptions)(nil)).Return(mockPager)
s := &privateDNSRepository{
recordClient: fakeClient,
cache: cache.New(0),
}
got, err := s.ListAllAAAARecords(&armprivatedns.PrivateZone{
TrackedResource: armprivatedns.TrackedResource{
Resource: armprivatedns.Resource{
ID: to.StringPtr("/subscriptions/subid/resourceGroups/rgid/providers/Microsoft.Network/privateDnsZones/zone.com"),
Name: to.StringPtr("zone"),
},
},
})
mockPager.AssertExpectations(t)
fakeClient.AssertExpectations(t)
assert.Equal(t, expectedErr, err)
assert.Nil(t, got)
}
// endregion