test updateExternalLoadBalancer removes nodes

This commit is contained in:
Ashley Gau 2018-02-07 12:42:57 -08:00
parent 60ed92b16d
commit 378b177f02
2 changed files with 51 additions and 5 deletions

View File

@ -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
}

View File

@ -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,
)
}