Ryan
Ryan

Reputation: 11

Best Practice on inspecting Logs in Terraform Test

I am implementing end to end testing with Terraform Test for a Cloud Run solution. The desired outcomes is that a file uploaded to a storage bucket, triggers a pubsub message, which in turn triggers a Cloud Run Service.

Our Cloud Run image responds to pub-sub with a 200 response, this is typically visible in the logs. So I wanted to write a test that inspects the logs to look for this response.

Would anyone please be able to help me understand what the best practice is for testing this kind of functionality?

Currently, I create all the resources to be tested in /tests/setup. This includes a log metric and a policy alert, which i believe I need to be able to inspect the logs.

resource "google_monitoring_alert_policy" "successful_200_response" {
  project      = "wrk-ir1-prd-wrk02-svp-90548"
  display_name = "successful_200_response"
  combiner     = "OR"
  conditions {
    display_name = "Cloud Run 200 Responses Condition"
    condition_threshold {
      filter          = <<EOF
metric.type="logging.googleapis.com/user/cloud-run-200-responses" AND resource.type="cloud_run_revision"
EOF
      comparison      = "COMPARISON_GT"
      threshold_value = 0
      duration        = "60s"
      aggregations {
        alignment_period     = "60s"
        per_series_aligner   = "ALIGN_RATE"
        cross_series_reducer = "REDUCE_SUM"
      }
    }
  }
}

Then I run this e2e-tests.tftest.hcl file:

run "e2e_tests" {
  module {
    source = "./tests/setup"
  }
  assert {
    condition     = google_monitoring_alert_policy.successful_200_response.conditions[0].condition_threshold[0].threshold_value > 0
    error_message = "No 200 HTTP response from Cloud Run on pubsub trigger"
  }
}

This feels very clunky having to create logging and monitoring resources for the testing. The log metric also has to be created ahead of time, as it can take up to 10 minutes to create. So it either slows my pipeline down massively or it means that the tests have requirements outside of their state file. Both very suboptimal outcomes.

Is there a better way to inspect the logs with Terraform Test, I feel like I am missing something! Does anyone know of any better ways to test this?

Upvotes: 0

Views: 48

Answers (0)

Related Questions