Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions infra/examples/simple_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Expand Down
5 changes: 5 additions & 0 deletions infra/examples/simple_example/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions infra/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion infra/postdeployment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion infra/test/integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
30 changes: 29 additions & 1 deletion infra/test/integration/simple_example/simple_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}