Skip to content

Commit 238fcf3

Browse files
committed
Refactor Google provider tests to avoid sharing state between tests and
relying upon the implementation under test to establish the initial test conditions. Also make mock interface implementations more representative of real behaviour.
1 parent 7e8d821 commit 238fcf3

File tree

2 files changed

+735
-723
lines changed

2 files changed

+735
-723
lines changed

provider/google/google.go

+38-29
Original file line numberDiff line numberDiff line change
@@ -49,59 +49,60 @@ type managedZonesListCallInterface interface {
4949
}
5050

5151
type managedZonesServiceInterface interface {
52-
Create(project string, managedzone *dns.ManagedZone) managedZonesCreateCallInterface
53-
List(project string) managedZonesListCallInterface
52+
Create(managedZone *dns.ManagedZone) managedZonesCreateCallInterface
53+
List() managedZonesListCallInterface
5454
}
5555

5656
type resourceRecordSetsListCallInterface interface {
5757
Pages(ctx context.Context, f func(*dns.ResourceRecordSetsListResponse) error) error
5858
}
5959

6060
type resourceRecordSetsClientInterface interface {
61-
List(project string, managedZone string) resourceRecordSetsListCallInterface
61+
List(managedZone string) resourceRecordSetsListCallInterface
6262
}
6363

6464
type changesCreateCallInterface interface {
6565
Do(opts ...googleapi.CallOption) (*dns.Change, error)
6666
}
6767

6868
type changesServiceInterface interface {
69-
Create(project string, managedZone string, change *dns.Change) changesCreateCallInterface
69+
Create(managedZone string, change *dns.Change) changesCreateCallInterface
7070
}
7171

7272
type resourceRecordSetsService struct {
73+
project string
7374
service *dns.ResourceRecordSetsService
7475
}
7576

76-
func (r resourceRecordSetsService) List(project string, managedZone string) resourceRecordSetsListCallInterface {
77-
return r.service.List(project, managedZone)
77+
func (r resourceRecordSetsService) List(managedZone string) resourceRecordSetsListCallInterface {
78+
return r.service.List(r.project, managedZone)
7879
}
7980

8081
type managedZonesService struct {
82+
project string
8183
service *dns.ManagedZonesService
8284
}
8385

84-
func (m managedZonesService) Create(project string, managedzone *dns.ManagedZone) managedZonesCreateCallInterface {
85-
return m.service.Create(project, managedzone)
86+
func (m managedZonesService) Create(managedZone *dns.ManagedZone) managedZonesCreateCallInterface {
87+
return m.service.Create(m.project, managedZone)
8688
}
8789

88-
func (m managedZonesService) List(project string) managedZonesListCallInterface {
89-
return m.service.List(project)
90+
func (m managedZonesService) List() managedZonesListCallInterface {
91+
return m.service.List(m.project)
9092
}
9193

9294
type changesService struct {
95+
project string
9396
service *dns.ChangesService
9497
}
9598

96-
func (c changesService) Create(project string, managedZone string, change *dns.Change) changesCreateCallInterface {
97-
return c.service.Create(project, managedZone, change)
99+
func (c changesService) Create(managedZone string, change *dns.Change) changesCreateCallInterface {
100+
return c.service.Create(c.project, managedZone, change)
98101
}
99102

100103
// GoogleProvider is an implementation of Provider for Google CloudDNS.
101104
type GoogleProvider struct {
102105
provider.BaseProvider
103-
// The Google project to work in
104-
project string
105106
// Enabled dry-run will print any modifying actions rather than execute them.
106107
dryRun bool
107108
// Max batch size to submit to Google Cloud DNS per transaction.
@@ -155,17 +156,25 @@ func NewGoogleProvider(ctx context.Context, project string, domainFilter endpoin
155156
zoneTypeFilter := provider.NewZoneTypeFilter(zoneVisibility)
156157

157158
provider := &GoogleProvider{
158-
project: project,
159-
dryRun: dryRun,
160-
batchChangeSize: batchChangeSize,
161-
batchChangeInterval: batchChangeInterval,
162-
domainFilter: domainFilter,
163-
zoneTypeFilter: zoneTypeFilter,
164-
zoneIDFilter: zoneIDFilter,
165-
resourceRecordSetsClient: resourceRecordSetsService{dnsClient.ResourceRecordSets},
166-
managedZonesClient: managedZonesService{dnsClient.ManagedZones},
167-
changesClient: changesService{dnsClient.Changes},
168-
ctx: ctx,
159+
dryRun: dryRun,
160+
batchChangeSize: batchChangeSize,
161+
batchChangeInterval: batchChangeInterval,
162+
domainFilter: domainFilter,
163+
zoneTypeFilter: zoneTypeFilter,
164+
zoneIDFilter: zoneIDFilter,
165+
managedZonesClient: managedZonesService{
166+
project: project,
167+
service: dnsClient.ManagedZones,
168+
},
169+
resourceRecordSetsClient: resourceRecordSetsService{
170+
project: project,
171+
service: dnsClient.ResourceRecordSets,
172+
},
173+
changesClient: changesService{
174+
project: project,
175+
service: dnsClient.Changes,
176+
},
177+
ctx: ctx,
169178
}
170179

171180
return provider, nil
@@ -193,12 +202,12 @@ func (p *GoogleProvider) Zones(ctx context.Context) (map[string]*dns.ManagedZone
193202
}
194203

195204
log.Debugf("Matching zones against domain filters: %v", p.domainFilter)
196-
if err := p.managedZonesClient.List(p.project).Pages(ctx, f); err != nil {
205+
if err := p.managedZonesClient.List().Pages(ctx, f); err != nil {
197206
return nil, provider.NewSoftError(fmt.Errorf("failed to list zones: %w", err))
198207
}
199208

200209
if len(zones) == 0 {
201-
log.Warnf("No zones in the project, %s, match domain filters: %v", p.project, p.domainFilter)
210+
log.Warnf("No zones match domain filters: %v", p.domainFilter)
202211
}
203212

204213
for _, zone := range zones {
@@ -227,7 +236,7 @@ func (p *GoogleProvider) Records(ctx context.Context) (endpoints []*endpoint.End
227236
}
228237

229238
for _, z := range zones {
230-
if err := p.resourceRecordSetsClient.List(p.project, z.Name).Pages(ctx, f); err != nil {
239+
if err := p.resourceRecordSetsClient.List(z.Name).Pages(ctx, f); err != nil {
231240
return nil, provider.NewSoftError(fmt.Errorf("failed to list records in zone %s: %w", z.Name, err))
232241
}
233242
}
@@ -301,7 +310,7 @@ func (p *GoogleProvider) submitChange(ctx context.Context, change *dns.Change) e
301310
continue
302311
}
303312

304-
if _, err := p.changesClient.Create(p.project, zone, c).Do(); err != nil {
313+
if _, err := p.changesClient.Create(zone, c).Do(); err != nil {
305314
return provider.NewSoftError(fmt.Errorf("failed to create changes: %w", err))
306315
}
307316

0 commit comments

Comments
 (0)