func TestBackendPoolAdd(t *testing.T) { f := NewFakeBackendServices() fakeIGs := instances.NewFakeInstanceGroups(sets.NewString()) pool := newBackendPool(f, fakeIGs, false) namer := utils.Namer{} // Add a backend for a port, then re-add the same port and // make sure it corrects a broken link from the backend to // the instance group. nodePort := int64(8080) pool.Add(nodePort) beName := namer.BeName(nodePort) // Check that the new backend has the right port be, err := f.GetBackendService(beName) if err != nil { t.Fatalf("Did not find expected backend %v", beName) } if be.Port != nodePort { t.Fatalf("Backend %v has wrong port %v, expected %v", be.Name, be.Port, nodePort) } // Check that the instance group has the new port var found bool for _, port := range fakeIGs.Ports { if port == nodePort { found = true } } if !found { t.Fatalf("Port %v not added to instance group", nodePort) } // Mess up the link between backend service and instance group. // This simulates a user doing foolish things through the UI. f.calls = []int{} be, err = f.GetBackendService(beName) be.Backends[0].Group = "test edge hop" f.UpdateBackendService(be) pool.Add(nodePort) for _, call := range f.calls { if call == utils.Create { t.Fatalf("Unexpected create for existing backend service") } } gotBackend, err := f.GetBackendService(beName) if err != nil { t.Fatalf("Failed to find a backend with name %v: %v", beName, err) } gotGroup, err := fakeIGs.GetInstanceGroup(namer.IGName(), defaultZone) if err != nil { t.Fatalf("Failed to find instance group %v", namer.IGName()) } if gotBackend.Backends[0].Group != gotGroup.SelfLink { t.Fatalf( "Broken instance group link: %v %v", gotBackend.Backends[0].Group, gotGroup.SelfLink) } }