Terraform Testing Environment Variables

I am working on unit testing a kubernetes module using the azurerm provider. The resource definition looks like this

resource "azurerm_kubernetes_cluster" "cluster" {
***attributes***

dynamic "service_principal" {
    content {
      client_id     = var.cluster_config.service_principal.id
      client_secret = var.cluster_config.service_principal.client_secret
    }
  }
}

The cluster_config that is being referenced is an object that looks like

variable "cluster_config" {
type = object({
service_principal = object({
      id            = string
      client_secret = string
})
}

I am trying to test this by referencing the environmental variable in my terminal for the service principal id and client_secret. We don’t want to save these to source control; each user and the builds have saved them as part of their context.

In my test.tftest.hcl file, I have tried the following two things:

First, is the version below

variables {
cluster_config = {
service_principal={
      id = var.TF_VAR_azure_service_principal_app_id
      client_secret = var.TF_VAR_azure_service_principal_client_secret
    }
}
}

This is the second version

variables {
cluster_config = {
service_principal={
      id = env(TF_VAR_azure_service_principal_app_id)
      client_secret = env(TF_VAR_azure_service_principal_client_secret)
    }
}
}

Neither of these work. I have read where I may need to use a .env file then hardcode the values in that file though that’s not really desirable. Does anyone know how I could bring in these environmental variables when running terraform test locally? It would be nice to have a solution where another person could run the same test locally without any real special setup on their end to run it.

I guess the big question is: what exactly are you trying to test?

If at all possible, for unit testing, it’s probably better to mock these values / use fake ones? Hopefully you can assume that the provider itself handles these values properly, so if it’s at all possible, that would be my suggestion.