Skip to content

Commit

Permalink
Change mock hook signatures to have ...options in the end
Browse files Browse the repository at this point in the history
- k8s-cloud-provider in a new version added new argument options
  ...cloud.Option to every mock hook function. So we need to add this
  argument to every usage
  • Loading branch information
panslava committed Dec 9, 2023
1 parent b407ef1 commit 33ab705
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pkg/backends/features/securitypolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func TestEnsureSecurityPolicy(t *testing.T) {
mockSecurityPolicies := make(map[string]*compute.SecurityPolicyReference)
mockSecurityPolicyLock := sync.Mutex{}
setSecurityPolicyHook := func(_ context.Context, key *meta.Key, ref *compute.SecurityPolicyReference, _ *cloud.MockBackendServices) error {
setSecurityPolicyHook := func(_ context.Context, key *meta.Key, ref *compute.SecurityPolicyReference, _ *cloud.MockBackendServices, _ ...cloud.Option) error {
mockSecurityPolicyLock.Lock()
mockSecurityPolicies[key.Name] = ref
mockSecurityPolicyLock.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends/ig_linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestLinkWithCreationModeError(t *testing.T) {
// and verify that a backend with the other balancingMode is
// updated properly.
for i, bm := range modes {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices) error {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices, options ...cloud.Option) error {
for _, b := range be.Backends {
if b.BalancingMode == string(bm) {
return &googleapi.Error{Code: http.StatusBadRequest}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestSyncChaosMonkey(t *testing.T) {
// Add hook to keep track of how many calls are made.
// TODO(rramkumar): This is a hack. Implement function call counters in generated code.
createCalls := 0
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, obj *compute.BackendService, m *cloud.MockBackendServices) (bool, error) {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, obj *compute.BackendService, m *cloud.MockBackendServices, options ...cloud.Option) (bool, error) {
createCalls += 1
return false, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/backends/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ func TestSyncQuota(t *testing.T) {
bsCreated += 1
return false, nil
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices) (bool, error) {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockBackendServices, options ...cloud.Option) (bool, error) {
return insertFunc(ctx, key, be.Name)
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBetaBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *computebeta.BackendService, m *cloud.MockBetaBackendServices) (bool, error) {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBetaBackendServices.InsertHook = func(ctx context.Context, key *meta.Key, be *computebeta.BackendService, m *cloud.MockBetaBackendServices, options ...cloud.Option) (bool, error) {
return insertFunc(ctx, key, be.Name)
}
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockBackendServices) (bool, error) {
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockBackendServices, options ...cloud.Option) (bool, error) {
bsCreated -= 1
return false, nil
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/healthchecks/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,27 +1270,27 @@ func (f *syncSPFixture) setupExistingRegionalHCFunc(region string, hc *compute.H

func setupMockUpdate(mock *cloud.MockGCE) {
// Mock does not know how to autogenerate update() methods.
mock.MockHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *compute.HealthCheck, m *cloud.MockHealthChecks) error {
mock.MockHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *compute.HealthCheck, m *cloud.MockHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
mock.MockAlphaHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computealpha.HealthCheck, m *cloud.MockAlphaHealthChecks) error {
mock.MockAlphaHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computealpha.HealthCheck, m *cloud.MockAlphaHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
mock.MockBetaHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computebeta.HealthCheck, m *cloud.MockBetaHealthChecks) error {
mock.MockBetaHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computebeta.HealthCheck, m *cloud.MockBetaHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
mock.MockRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *compute.HealthCheck, m *cloud.MockRegionHealthChecks) error {
mock.MockRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *compute.HealthCheck, m *cloud.MockRegionHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
mock.MockAlphaRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computealpha.HealthCheck, m *cloud.MockAlphaRegionHealthChecks) error {
mock.MockAlphaRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computealpha.HealthCheck, m *cloud.MockAlphaRegionHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
mock.MockBetaRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computebeta.HealthCheck, m *cloud.MockBetaRegionHealthChecks) error {
mock.MockBetaRegionHealthChecks.UpdateHook = func(ctx context.Context, k *meta.Key, hc *computebeta.HealthCheck, m *cloud.MockBetaRegionHealthChecks, options ...cloud.Option) error {
m.Objects[*k].Obj = hc
return nil
}
Expand Down Expand Up @@ -1848,7 +1848,7 @@ func TestSyncServicePort(t *testing.T) {

// Check that resync should not have an effect and does not issue
// an update to GCE. Hook Update() to fail.
mock.MockHealthChecks.UpdateHook = func(context.Context, *meta.Key, *compute.HealthCheck, *cloud.MockHealthChecks) error {
mock.MockHealthChecks.UpdateHook = func(context.Context, *meta.Key, *compute.HealthCheck, *cloud.MockHealthChecks, ...cloud.Option) error {
t.Fatalf("resync should not result in an update")
return nil
}
Expand Down Expand Up @@ -2154,7 +2154,7 @@ func TestCustomHealthcheckRemoval(t *testing.T) {

// Check that resync should not have an effect and does not issue
// an update to GCE. Hook Update() to fail.
mock.MockHealthChecks.UpdateHook = func(context.Context, *meta.Key, *compute.HealthCheck, *cloud.MockHealthChecks) error {
mock.MockHealthChecks.UpdateHook = func(context.Context, *meta.Key, *compute.HealthCheck, *cloud.MockHealthChecks, ...cloud.Option) error {
t.Fatalf("resync should not result in an update")
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/l4lb/l4netlbcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func checkBackendServiceCommon(lc *L4NetLBController, svc *v1.Service) (*composi
return bs, nil
}

func updateRegionBackendServiceWithLockHook(ctx context.Context, key *meta.Key, obj *ga.BackendService, m *cloud.MockRegionBackendServices) error {
func updateRegionBackendServiceWithLockHook(ctx context.Context, key *meta.Key, obj *ga.BackendService, m *cloud.MockRegionBackendServices, options ...cloud.Option) error {
_, err := m.Get(ctx, key)
if err != nil {
return err
Expand Down Expand Up @@ -707,7 +707,7 @@ func TestProcessRBSServiceTypeTransition(t *testing.T) {
func TestServiceDeletionWhenInstanceGroupInUse(t *testing.T) {
svc, lc := createAndSyncNetLBSvc(t)

(lc.ctx.Cloud.Compute().(*cloud.MockGCE)).MockInstanceGroups.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockInstanceGroups) (bool, error) {
(lc.ctx.Cloud.Compute().(*cloud.MockGCE)).MockInstanceGroups.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockInstanceGroups, options ...cloud.Option) (bool, error) {
err := &googleapi.Error{
Code: http.StatusBadRequest,
Message: "GetErrorInstanceGroupHook: Cannot delete instance group being used by another service",
Expand Down Expand Up @@ -1135,7 +1135,7 @@ func TestControllerUserIPWithStandardNetworkTier(t *testing.T) {
}
}

type getForwardingRuleHook func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, *ga.ForwardingRule, error)
type getForwardingRuleHook func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules, options ...cloud.Option) (bool, *ga.ForwardingRule, error)

func TestIsRBSBasedService(t *testing.T) {
testCases := []struct {
Expand Down
5 changes: 3 additions & 2 deletions pkg/loadbalancers/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package loadbalancers
import (
"context"
"fmt"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
"google.golang.org/api/compute/v1"
Expand All @@ -37,14 +38,14 @@ func (t *testIP) ip() string {
return fmt.Sprintf("0.0.0.%v", t.start)
}

func InsertGlobalForwardingRuleHook(ctx context.Context, key *meta.Key, obj *compute.ForwardingRule, m *cloud.MockGlobalForwardingRules) (b bool, e error) {
func InsertGlobalForwardingRuleHook(ctx context.Context, key *meta.Key, obj *compute.ForwardingRule, m *cloud.MockGlobalForwardingRules, options ...cloud.Option) (b bool, e error) {
if obj.IPAddress == "" {
obj.IPAddress = "0.0.0.1"
}
return false, nil
}

func InsertForwardingRuleHook(ctx context.Context, key *meta.Key, obj *compute.ForwardingRule, m *cloud.MockForwardingRules) (b bool, e error) {
func InsertForwardingRuleHook(ctx context.Context, key *meta.Key, obj *compute.ForwardingRule, m *cloud.MockForwardingRules, options ...cloud.Option) (b bool, e error) {
if obj.IPAddress == "" {
obj.IPAddress = "10.0.0.1"
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/loadbalancers/l4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ func TestEnsureInternalLoadBalancerModifyProtocol(t *testing.T) {
}
// This function simulates the error where backend service protocol cannot be changed
// before deleting the forwarding rule.
c.MockRegionBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockRegionBackendServices) error {
c.MockRegionBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, be *compute.BackendService, m *cloud.MockRegionBackendServices, options ...cloud.Option) error {
// Check FRnames with both protocols to make sure there is no leak or incorrect update.
frNames := []string{l4.getFRNameWithProtocol("TCP"), l4.getFRNameWithProtocol("UDP")}
for _, name := range frNames {
Expand All @@ -1357,7 +1357,7 @@ func TestEnsureInternalLoadBalancerModifyProtocol(t *testing.T) {
return mock.UpdateRegionBackendServiceHook(ctx, key, be, m)
}
// Before deleting forwarding rule, check, that the address was reserved
c.MockForwardingRules.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, error) {
c.MockForwardingRules.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules, options ...cloud.Option) (bool, error) {
fr, err := c.MockForwardingRules.Get(ctx, key)
// if forwarding rule not exists, don't need to check if address reserved
if utils.IsNotFoundError(err) {
Expand Down Expand Up @@ -1475,7 +1475,7 @@ func TestDualStackInternalLoadBalancerModifyProtocol(t *testing.T) {
// This function simulates the error where backend service protocol cannot be changed
// before deleting the forwarding rule.
c := l4.cloud.Compute().(*cloud.MockGCE)
c.MockRegionBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, bs *compute.BackendService, m *cloud.MockRegionBackendServices) error {
c.MockRegionBackendServices.UpdateHook = func(ctx context.Context, key *meta.Key, bs *compute.BackendService, m *cloud.MockRegionBackendServices, options ...cloud.Option) error {
// Check FR names with both protocols to make sure there is no leak or incorrect update.
frNames := []string{l4.getFRNameWithProtocol("TCP"), l4.getFRNameWithProtocol("UDP"), l4.getIPv6FRNameWithProtocol("TCP"), l4.getIPv6FRNameWithProtocol("UDP")}
for _, name := range frNames {
Expand All @@ -1495,7 +1495,7 @@ func TestDualStackInternalLoadBalancerModifyProtocol(t *testing.T) {
return mock.UpdateRegionBackendServiceHook(ctx, key, bs, m)
}
// Before deleting forwarding rule, check, that the address was reserved
c.MockForwardingRules.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, error) {
c.MockForwardingRules.DeleteHook = func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules, options ...cloud.Option) (bool, error) {
fr, err := c.MockForwardingRules.Get(ctx, key)
// if forwarding rule not exists, don't need to check if address reserved
if utils.IsNotFoundError(err) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/loadbalancers/l4netlb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,9 +1539,9 @@ func checkMetrics(m metrics.L4NetLBServiceLegacyState, isManaged, isPremium, isU
return nil
}

func assertAddressOldReservedHook(t *testing.T, gceCloud *gce.Cloud) func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules) (bool, error) {
func assertAddressOldReservedHook(t *testing.T, gceCloud *gce.Cloud) func(ctx context.Context, key *meta.Key, m *cloud.MockForwardingRules, options ...cloud.Option) (bool, error) {
mockGCE := gceCloud.Compute().(*cloud.MockGCE)
return func(ctx context.Context, key *meta.Key, _ *cloud.MockForwardingRules) (bool, error) {
return func(ctx context.Context, key *meta.Key, _ *cloud.MockForwardingRules, _ ...cloud.Option) (bool, error) {
fr, err := mockGCE.MockForwardingRules.Get(ctx, key)
// if forwarding rule not exists, don't need to check if address reserved
if utils.IsNotFoundError(err) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/loadbalancers/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ func newTestJig(t *testing.T) *testJig {
mockGCE.MockTargetHttpProxies.SetUrlMapHook = mock.SetURLMapTargetHTTPProxyHook
mockGCE.MockTargetHttpsProxies.SetUrlMapHook = mock.SetURLMapTargetHTTPSProxyHook
mockGCE.MockTargetHttpsProxies.SetSslCertificatesHook = mock.SetSslCertificateTargetHTTPSProxyHook
mockGCE.MockSslCertificates.InsertHook = func(ctx context.Context, key *meta.Key, obj *compute.SslCertificate, m *cloud.MockSslCertificates) (b bool, e error) {
mockGCE.MockSslCertificates.InsertHook = func(ctx context.Context, key *meta.Key, obj *compute.SslCertificate, m *cloud.MockSslCertificates, options ...cloud.Option) (b bool, e error) {
if len(m.Objects) >= FakeCertQuota {
return true, fmt.Errorf("error exceeded fake cert quota")
}
return false, nil
}
mockGCE.MockTargetHttpsProxies.SetSslCertificatesHook = func(ctx context.Context, key *meta.Key, request *compute.TargetHttpsProxiesSetSslCertificatesRequest, proxies *cloud.MockTargetHttpsProxies) error {
mockGCE.MockTargetHttpsProxies.SetSslCertificatesHook = func(ctx context.Context, key *meta.Key, request *compute.TargetHttpsProxiesSetSslCertificatesRequest, proxies *cloud.MockTargetHttpsProxies, _ ...cloud.Option) error {
tp, err := proxies.Get(ctx, key)
if err != nil {
return &googleapi.Error{
Expand Down Expand Up @@ -110,7 +110,7 @@ func newTestJig(t *testing.T) *testJig {
tp.SslCertificates = request.SslCertificates
return nil
}
mockGCE.MockTargetHttpsProxies.SetSslPolicyHook = func(ctx context.Context, key *meta.Key, ref *compute.SslPolicyReference, proxies *cloud.MockTargetHttpsProxies) error {
mockGCE.MockTargetHttpsProxies.SetSslPolicyHook = func(ctx context.Context, key *meta.Key, ref *compute.SslPolicyReference, proxies *cloud.MockTargetHttpsProxies, _ ...cloud.Option) error {
tps, err := proxies.Get(ctx, key)
fmt.Printf("tps = %+v, err = %v\n\n", tps, err)
if err != nil {
Expand Down Expand Up @@ -1327,7 +1327,7 @@ func TestPoolSyncNoChanges(t *testing.T) {

// Add hook to keep track of how many calls are made.
updateCalls := 0
j.mock.MockUrlMaps.UpdateHook = func(ctx context.Context, key *meta.Key, obj *compute.UrlMap, m *cloud.MockUrlMaps) error {
j.mock.MockUrlMaps.UpdateHook = func(ctx context.Context, key *meta.Key, obj *compute.UrlMap, m *cloud.MockUrlMaps, options ...cloud.Option) error {
updateCalls += 1
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/readiness/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func TestPoll(t *testing.T) {
step = "ListNetworkEndpoint return error response"
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
m := (fakeGCE.Compute().(*cloud.MockGCE))
m.MockNetworkEndpointGroups.ListNetworkEndpointsHook = func(ctx context.Context, key *meta.Key, obj *compute.NetworkEndpointGroupsListEndpointsRequest, filter *filter.F, m *cloud.MockNetworkEndpointGroups) ([]*compute.NetworkEndpointWithHealthStatus, error) {
m.MockNetworkEndpointGroups.ListNetworkEndpointsHook = func(ctx context.Context, key *meta.Key, obj *compute.NetworkEndpointGroupsListEndpointsRequest, filter *filter.F, m *cloud.MockNetworkEndpointGroups, options ...cloud.Option) ([]*compute.NetworkEndpointWithHealthStatus, error) {
return nil, fmt.Errorf("random error from GCE")
}
poller.negCloud = negtypes.NewAdapter(fakeGCE)
Expand Down
2 changes: 1 addition & 1 deletion pkg/neg/syncers/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ func TestEnableDegradedMode(t *testing.T) {
fakeCloud := negtypes.NewAdapter(fakeGCE)
mockGCE := fakeGCE.Compute().(*cloud.MockGCE)
ipOutOfRange := "1.1.1.1"
mockGCE.MockNetworkEndpointGroups.AttachNetworkEndpointsHook = func(ctx context.Context, key *meta.Key, obj *compute.NetworkEndpointGroupsAttachEndpointsRequest, m *cloud.MockNetworkEndpointGroups) error {
mockGCE.MockNetworkEndpointGroups.AttachNetworkEndpointsHook = func(ctx context.Context, key *meta.Key, obj *compute.NetworkEndpointGroupsAttachEndpointsRequest, m *cloud.MockNetworkEndpointGroups, options ...cloud.Option) error {
for _, newEP := range obj.NetworkEndpoints {
if newEP.IpAddress == ipOutOfRange {
return &googleapi.Error{
Expand Down
Loading

0 comments on commit 33ab705

Please sign in to comment.