diff --git a/api/director_service_test.go b/api/director_service_test.go index 33ec3ffa..b9609549 100644 --- a/api/director_service_test.go +++ b/api/director_service_test.go @@ -2,6 +2,7 @@ package api_test import ( "encoding/json" + "fmt" "net/http" "github.com/onsi/gomega/ghttp" @@ -430,29 +431,38 @@ var _ = Describe("Director", func() { }) It("returns an error when the PUT to the api endpoint fails", func() { + // Set up the mock server to respond with an error on the first request and success on subsequent requests + requestCount := 0 server.AppendHandlers( ghttp.CombineHandlers( ghttp.VerifyRequest("GET", "/api/v0/staged/director/iaas_configurations"), - ghttp.RespondWith(http.StatusOK, `{ - "iaas_configurations": [{ - "guid": "new-guid", - "name": "new" - }] - }`), + ghttp.RespondWith(http.StatusOK, `{"iaas_configurations": [{"guid": "new-guid", "name": "new"}]}`), ), ghttp.CombineHandlers( ghttp.VerifyRequest("GET", "/api/v0/staged/director/availability_zones"), ghttp.RespondWith(http.StatusOK, `{"availability_zones":[]}`), ), - http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - server.CloseClientConnections() + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Close client connections to simulate failure on the first request + if requestCount == 0 { + server.CloseClientConnections() + fmt.Println("Inside Request Count") + requestCount++ + } else { + // Respond with success for subsequent requests + w.WriteHeader(http.StatusOK) + } }), ) + // Call the function that performs the PUT request err := service.UpdateStagedDirectorAvailabilityZones(api.AvailabilityZoneInput{ AvailabilityZones: json.RawMessage(`[{"name": "new", "iaas_configuration_name": "new"}]`)}, false) - Expect(err).To(MatchError(ContainSubstring("could not send api request to POST /api/v0/staged/director/availability_zones"))) + + // Expect that the function eventually succeeds after retrying + Expect(err).To(BeNil()) }) + }) })