Skip to content

Commit

Permalink
Leverage AssertResponseWithRetry
Browse files Browse the repository at this point in the history
  • Loading branch information
NimJay committed Sep 20, 2024
1 parent dd30f9d commit 185b8ff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
2 changes: 1 addition & 1 deletion test/integration/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/terraform-google-three-tier-web-app/test/integration"
test "github.com/GoogleCloudPlatform/terraform-google-three-tier-web-app/test/integration"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion test/integration/simple_example/simple_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/GoogleCloudPlatform/terraform-google-three-tier-web-app/test/integration"
test "github.com/GoogleCloudPlatform/terraform-google-three-tier-web-app/test/integration"
"github.com/stretchr/testify/assert"
)

Expand Down
34 changes: 9 additions & 25 deletions test/integration/test_deployment_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,22 @@
package test

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/stretchr/testify/assert"
)

func TestDeploymentUrl(t *testing.T, assert *assert.Assertions, url string) error {
for attemptNum := 1; attemptNum <= 60; attemptNum++ {

response, err := http.Get(url)
if err != nil {
t.Logf("Deployment URL HTTP request error: %s\n", err)

} else if 200 <= response.StatusCode && response.StatusCode <= 299 { // Got some 200 response
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
responseBodyString := string(responseBody)
assert.Containsf(responseBodyString, "<title>Todo</title>", "Couldn't find text '<title>Todo</title>' in deployment's response")
return nil

} else { // Got a non-200 response
t.Logf("Deployment URL responded with status code: %d.\n", response.StatusCode)
}

// Wait before retrying
time.Sleep(4 * time.Second)
httpRequest, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

return fmt.Errorf("Deployment URL %s failed to respond with a 200 status code even after a few minutes.", url)
maxRetries := 60
waitBetweenRetries := 4 * time.Second
assertHttp := utils.NewAssertHTTP(utils.WithHTTPRequestRetries(maxRetries, waitBetweenRetries))
assertHttp.AssertResponseWithRetry(t, httpRequest, http.StatusOK, "<title>Todo</title>")
return nil
}

0 comments on commit 185b8ff

Please sign in to comment.