I am trying to mock the Vault provider in my Terraform tests, but I am facing difficulties in configuring the mock correctly. Here is an example from my provider.tf file:
terraform {
required_providers {
vault = {
source = "hashicorp/vault"
version = "~> 3.4"
}
}
}
data “vault_generic_secret” “some_name” {
path = “my/path/test”
}
And here is an example from my test file some.tftest.hcl
:
mock_provider “vault” {
…
}
Could someone help me understand how to properly configure the mock for the Vault provider? Any examples or documentation would be very helpful.
Thank you!
Hi @leonardobenedet - sorry for the delay here! Hopefully you’ve solved this in the meantime.
If not, can you share the error message you are receiving when trying this? Or the version of Terraform you are using?
The following works for me:
# main.tf
terraform {
required_providers {
vault = {
source = "hashicorp/vault"
version = "~> 3.4"
}
}
}
data "vault_generic_secret" "some_name" {
path = "my/path/test"
}
# main.tftest.hcl
mock_provider "vault" {
mock_data "vault_generic_secret" {
defaults = {
data = {
key = "value"
}
}
}
}
run "test" {
assert {
condition = data.vault_generic_secret.some_name.data.key == "value"
error_message = "wrong data"
}
}
What I’ve done there is set up a mock provider that is automatically provided to the test. The mock provider will return the same data for every vault_generic_secret within the referenced by the main vault provider. I used Terraform v1.9.5 while testing the above so you can see if the same version works for you!
Again, sorry for the delay - I’m happy to answer any follow up questions!