diff --git a/infra/examples/simple_example/README.md b/infra/examples/simple_example/README.md index b0a1040a..ef26cb31 100644 --- a/infra/examples/simple_example/README.md +++ b/infra/examples/simple_example/README.md @@ -13,6 +13,7 @@ This example illustrates how to use the `dynamic-python-webapp` module. | Name | Description | |------|-------------| +| firebase\_url | Firebase URL | | usage | Connection details for the project | diff --git a/infra/examples/simple_example/outputs.tf b/infra/examples/simple_example/outputs.tf index 83555e6f..6f6f3abe 100644 --- a/infra/examples/simple_example/outputs.tf +++ b/infra/examples/simple_example/outputs.tf @@ -19,3 +19,8 @@ output "usage" { description = "Connection details for the project" value = module.dynamic-python-webapp.usage } + +output "firebase_url" { + description = "Firebase URL" + value = module.dynamic-python-webapp.firebase_url +} diff --git a/infra/outputs.tf b/infra/outputs.tf index 3788a69a..3cbc5e34 100644 --- a/infra/outputs.tf +++ b/infra/outputs.tf @@ -15,12 +15,13 @@ */ locals { - server_url = google_cloud_run_v2_service.server.uri + server_url = google_cloud_run_v2_service.server.uri + firebase_url = "https://${var.project_id}.web.app" } output "firebase_url" { description = "Firebase URL" - value = "https://${var.project_id}.web.app" + value = local.firebase_url } locals { @@ -55,7 +56,7 @@ output "usage" { sensitive = true value = <<-EOF This deployment is now ready for use! - https://${var.project_id}.web.app + ${local.firebase_url} API Login: ${google_cloud_run_v2_service.server.uri}/admin Username: admin diff --git a/infra/postdeployment.tf b/infra/postdeployment.tf index 356369f2..491f19d8 100644 --- a/infra/postdeployment.tf +++ b/infra/postdeployment.tf @@ -81,9 +81,9 @@ resource "google_compute_instance" "gce_init" { echo "Running init database migration" gcloud beta run jobs execute ${google_cloud_run_v2_job.setup.name} --wait --project ${var.project_id} --region ${var.region} - echo "Running client deploy" gcloud beta run jobs execute ${google_cloud_run_v2_job.client.name} --wait --project ${var.project_id} --region ${var.region} +curl -X PURGE "${local.firebase_url}/" echo "Warm up API" curl ${local.server_url}/api/products/?warmup diff --git a/infra/test/integration/go.mod b/infra/test/integration/go.mod index 67247670..a7220e10 100644 --- a/infra/test/integration/go.mod +++ b/infra/test/integration/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test v0.5.1 + github.com/gruntwork-io/terratest v0.41.11 github.com/stretchr/testify v1.8.2 ) @@ -30,7 +31,6 @@ require ( github.com/google/uuid v1.3.0 // indirect github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect github.com/googleapis/gax-go/v2 v2.7.0 // indirect - github.com/gruntwork-io/terratest v0.41.11 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-getter v1.7.0 // indirect diff --git a/infra/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 17b830d5..94665067 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -25,12 +25,32 @@ 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/gruntwork-io/terratest/modules/terraform" "github.com/stretchr/testify/assert" ) func TestSimpleExample(t *testing.T) { example := tft.NewTFBlueprintTest(t) + example.DefineApply(func(assert *assert.Assertions) { + example.DefaultApply(assert) + + // Use of this module as part of a Jump Start Solution triggers a URL + // request when terraform apply completes. This primes the Firebase Hosting + // CDN with a platform-supplied 404 page. + // + // This extension of apply is meant to emulate that behavior. We confirm + // the 404 behavior here to boost confidence that the frontend test in + // example.DefineVerify proves the 404 page is fixed. + // + // If the check for "Site Not Found" is flaky, remove it in favor of + // a simpler HTTP request. + // + // https://github.com/GoogleCloudPlatform/terraform-dynamic-python-webapp/issues/64 + firebase_url := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") + assertErrorResponseContains(assert, firebase_url, http.StatusNotFound, "Site Not Found") + }) + example.DefineVerify(func(assert *assert.Assertions) { example.DefaultVerify(assert) @@ -109,12 +129,20 @@ func assertResponseContains(assert *assert.Assertions, url string, text ...strin } } +func assertErrorResponseContains(assert *assert.Assertions, url string, wantCode int, text string) { + code, responseBody, err := httpGetRequest(url) + assert.Nil(err) + assert.Equal(code, wantCode) + assert.Containsf(responseBody, text, "couldn't find %q in response body", text) +} + func httpGetRequest(url string) (statusCode int, body string, err error) { res, err := http.Get(url) if err != nil { return 0, "", err } + defer res.Body.Close() + buffer, err := io.ReadAll(res.Body) - res.Body.Close() return res.StatusCode, string(buffer), err }