How to mock a remote call so the Internet is not required during testing

I think my question is rather simple. I want to define some Terraform tests using mock_provider. Of course, I don’t want those provider to actually make remote calls (thus requiring the Internet). I simply want to override various behaviors in those providers to return mock data.

Concrete example
I am using the following provider:

    http = {
      source  = "hashicorp/http"
      version = "~> 3.4"
    }

I am making simple HTTP calls (expecting 200 response codes):

lifecycle {
    postcondition {
      condition     = contains([200], data.http.validate_backstage_system.status_code) 
      error_message = "System not found in Backstage"
    }
  }


data "http" "validate_backstage_system" {
  url = "https://server.com/api/catalog/entities/by-name/system/default/${var.my_system}"
}

I am mocking the provider using:

mock_provider "http"    {}

================================
I always get the following response:

╷
│ Error: Resource postcondition failed
│
│   on datadog.tf line 115, in resource "datadog_service_level_objective" "dd_slo":
│  115:       condition     = contains([200], data.http.validate_backstage_system.status_code)  # located here below
│     ├────────────────
│     │ data.http.validate_backstage_system.status_code is 0
│
│ System not found in Backstage

The bottom-line question is:

  • how do I define the mock_provider so that I can avoid making the actual HTTP calls?
  • it’s got to be some version of override_module, override_data, or override_resource
  • but ALL examples on the web are only for aws and there’s very, very little info anywhere for the mock_provider ability

I will answer my own question. After the better part of about 2 hours, I’ve finally found this solution:

mock_provider "http" {
  override_data {
    target = data.http.validate_backstage_system
    values = { status_code = "200"}
  }
}

I really just wish the mock_provider docs provided more examples than just for aws …and for more relatively common use cases.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.