From c88032b36b8aac6ad20732e4faca41cd4ff0ccfd Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 3 May 2023 10:29:07 -0700 Subject: [PATCH 1/7] fix: frontend loads once deploy completes --- infra/outputs.tf | 7 ++++--- infra/postdeployment.tf | 2 +- infra/test/integration/go.mod | 2 +- .../simple_example/simple_example_test.go | 20 +++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) 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..2565a07f 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.client_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..98158d54 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 + u := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") + assertResponseContains(assert, u, "Site Not Found") + }) + example.DefineVerify(func(assert *assert.Assertions) { example.DefaultVerify(assert) From 2f2d030760b4ed784e7a369a063bb1b073d73346 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 3 May 2023 10:35:01 -0700 Subject: [PATCH 2/7] fix: URL variable name --- infra/postdeployment.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infra/postdeployment.tf b/infra/postdeployment.tf index 2565a07f..491f19d8 100644 --- a/infra/postdeployment.tf +++ b/infra/postdeployment.tf @@ -83,7 +83,7 @@ gcloud beta run jobs execute ${google_cloud_run_v2_job.setup.name} --wait --proj 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.client_url}/" +curl -X PURGE "${local.firebase_url}/" echo "Warm up API" curl ${local.server_url}/api/products/?warmup From 6d10cc776705a114b51fdf929677373dad8d193e Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 3 May 2023 13:01:52 -0700 Subject: [PATCH 3/7] temporary: add tf outputs debugging --- .../test/integration/simple_example/simple_example_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/infra/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 98158d54..8b94adb7 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -35,6 +35,12 @@ func TestSimpleExample(t *testing.T) { example.DefineApply(func(assert *assert.Assertions) { example.DefaultApply(assert) + // Temporary: Output debugging. + m := terraform.OutputAll(t, example.GetTFOptions()) + for k, v := range m { + t.Logf("Output %s: %s\n", k, v) + } + // 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. From b9a52ab4b6d975bf72399651bb63662c84920712 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Wed, 3 May 2023 13:25:14 -0700 Subject: [PATCH 4/7] fix: add firebase_url output to test terraform root --- infra/examples/simple_example/outputs.tf | 5 +++++ .../test/integration/simple_example/simple_example_test.go | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) 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/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 8b94adb7..98158d54 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -35,12 +35,6 @@ func TestSimpleExample(t *testing.T) { example.DefineApply(func(assert *assert.Assertions) { example.DefaultApply(assert) - // Temporary: Output debugging. - m := terraform.OutputAll(t, example.GetTFOptions()) - for k, v := range m { - t.Logf("Output %s: %s\n", k, v) - } - // 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. From b5731d0ad940893e9af3c3edcaa6d6194e6fc3e6 Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Thu, 4 May 2023 01:20:51 +0000 Subject: [PATCH 5/7] make generate_docs --- infra/examples/simple_example/README.md | 1 + 1 file changed, 1 insertion(+) 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 | From 2c2f8eaa670262e2d7320cd20d42c7ea33eb1e8a Mon Sep 17 00:00:00 2001 From: Katie McLaughlin Date: Thu, 4 May 2023 14:24:37 +1000 Subject: [PATCH 6/7] temporary: rework site check, presume error --- .../integration/simple_example/simple_example_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/infra/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 98158d54..64a41e60 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -47,8 +47,12 @@ func TestSimpleExample(t *testing.T) { // a simpler HTTP request. // // https://github.com/GoogleCloudPlatform/terraform-dynamic-python-webapp/issues/64 - u := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") - assertResponseContains(assert, u, "Site Not Found") + firebase_url := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") + fragment := "Site Not Found" + code, responseBody, err := httpGetRequest(firebase_url) + assert.Nil(err) + assert.Equal(code, 404) + assert.Containsf(responseBody, fragment, "couldn't find %q in response body", fragment) }) example.DefineVerify(func(assert *assert.Assertions) { From f00530d9632d7eda52103012c752d860f0e98101 Mon Sep 17 00:00:00 2001 From: Adam Ross Date: Thu, 4 May 2023 16:00:26 -0700 Subject: [PATCH 7/7] refactor: move error response assert to helper --- .../simple_example/simple_example_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/infra/test/integration/simple_example/simple_example_test.go b/infra/test/integration/simple_example/simple_example_test.go index 64a41e60..94665067 100644 --- a/infra/test/integration/simple_example/simple_example_test.go +++ b/infra/test/integration/simple_example/simple_example_test.go @@ -48,11 +48,7 @@ func TestSimpleExample(t *testing.T) { // // https://github.com/GoogleCloudPlatform/terraform-dynamic-python-webapp/issues/64 firebase_url := terraform.OutputRequired(t, example.GetTFOptions(), "firebase_url") - fragment := "Site Not Found" - code, responseBody, err := httpGetRequest(firebase_url) - assert.Nil(err) - assert.Equal(code, 404) - assert.Containsf(responseBody, fragment, "couldn't find %q in response body", fragment) + assertErrorResponseContains(assert, firebase_url, http.StatusNotFound, "Site Not Found") }) example.DefineVerify(func(assert *assert.Assertions) { @@ -133,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 }