Skip to content

Commit

Permalink
added retry logic to director_service_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
bjanice75 committed Mar 20, 2024
1 parent 59b798d commit 3eb72b1
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions api/director_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api_test

import (
"encoding/json"
"fmt"
"net/http"

"github.com/onsi/gomega/ghttp"
Expand Down Expand Up @@ -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())
})

})
})

Expand Down

0 comments on commit 3eb72b1

Please sign in to comment.