diff --git a/pkg/cloudprovider/providers/gce/cloud/mock/mock.go b/pkg/cloudprovider/providers/gce/cloud/mock/mock.go index d681149287d..50d486e580d 100644 --- a/pkg/cloudprovider/providers/gce/cloud/mock/mock.go +++ b/pkg/cloudprovider/providers/gce/cloud/mock/mock.go @@ -50,3 +50,26 @@ func AddInstanceHook(m *cloud.MockTargetPools, ctx context.Context, key *meta.Ke return nil } + +func RemoveInstanceHook(m *cloud.MockTargetPools, ctx context.Context, key *meta.Key, req *ga.TargetPoolsRemoveInstanceRequest) error { + pool, err := m.Get(ctx, key) + if err != nil { + return &googleapi.Error{ + Code: http.StatusNotFound, + Message: fmt.Sprintf("Key: %s was not found in TargetPools", key.String()), + } + } + + for _, instanceToRemove := range req.Instances { + for i, instance := range pool.Instances { + if instanceToRemove.Instance == instance { + // Delete instance from pool.Instances without preserving order + pool.Instances[i] = pool.Instances[len(pool.Instances)-1] + pool.Instances = pool.Instances[:len(pool.Instances)-1] + break + } + } + } + + return nil +} diff --git a/pkg/cloudprovider/providers/gce/gce_loadbalancer_external_test.go b/pkg/cloudprovider/providers/gce/gce_loadbalancer_external_test.go index 92166fb5be3..c1b8bb10903 100644 --- a/pkg/cloudprovider/providers/gce/gce_loadbalancer_external_test.go +++ b/pkg/cloudprovider/providers/gce/gce_loadbalancer_external_test.go @@ -283,6 +283,7 @@ func fakeGCECloud() (*GCECloud, error) { cloud := cloud.NewMockGCE() cloud.MockTargetPools.AddInstanceHook = mock.AddInstanceHook + cloud.MockTargetPools.RemoveInstanceHook = mock.RemoveInstanceHook gce := GCECloud{ region: gceRegion, @@ -417,21 +418,43 @@ func TestUpdateExternalLoadBalancer(t *testing.T) { newNodes, err := createAndInsertNodes(gce, []string{nodeName, newNodeName}) assert.NoError(t, err) + // Add the new node, then check that it is properly added to the TargetPool err = gce.updateExternalLoadBalancer(clusterName, apiService, newNodes) assert.NoError(t, err) lbName := cloudprovider.GetLoadBalancerName(apiService) - // Check that TargetPool is updated with the new node pool, err := gce.GetTargetPool(lbName, gceRegion) require.NoError(t, err) + // TODO: when testify is updated to v1.2.0+, use ElementsMatch instead + assert.Contains( + t, + pool.Instances, + fmt.Sprintf("/zones/%s/instances/%s", zoneName, nodeName), + ) + + assert.Contains( + t, + pool.Instances, + fmt.Sprintf("/zones/%s/instances/%s", zoneName, newNodeName), + ) + + newNodes, err = createAndInsertNodes(gce, []string{nodeName}) + assert.NoError(t, err) + + // Remove the new node by calling updateExternalLoadBalancer with a list + // only containing the old node, and test that the TargetPool no longer + // contains the new node. + err = gce.updateExternalLoadBalancer(clusterName, apiService, newNodes) + assert.NoError(t, err) + + pool, err = gce.GetTargetPool(lbName, gceRegion) + require.NoError(t, err) + assert.Equal( t, - []string{ - fmt.Sprintf("/zones/%s/instances/%s", zoneName, nodeName), - fmt.Sprintf("/zones/%s/instances/%s", zoneName, newNodeName), - }, + []string{fmt.Sprintf("/zones/%s/instances/%s", zoneName, nodeName)}, pool.Instances, ) }