however, then I create a run block, the number of subnet ids seems to be 0. Can I ask why is that the case? Can I also ask how to mock multiple subnets? I want to be able to simulate getting data from multiple subnets
@Kurtyjlee While I am new to Terraform tests, if I understand how mocks work, I donβt think mocking aws_subnet resources would make them discoverable by a data source. After all, the actual data source would find subnets via AWS API, not from the Terraform state.
You can mock the data source by providing a default or overridden value for the ids attribute, though doing so would not be testing the logic of the actual data source.
Your other option is to consider creating a VPC and subnets as a setup module (following typical unit test concepts), so that there are real AWS resources for the actual data source to fetch. Hereβs an example - hope is helps.
main.tf (the configuration with the data source that you want to test):
run "setup" {
module {
source = "./testing/setup"
}
}
run "app_tier_subnets_found" {
command = apply
assert {
condition = length(data.aws_subnets.app.ids) == 2
error_message = "The number of app-tier subnets found is incorrect"
}
}
Console log from running the tests:
$ terraform init
Initializing the backend...
Initializing modules...
- test.main.setup in testing\setup
Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v5.84.0
Terraform has made some changes to the provider dependency selections recorded
in the .terraform.lock.hcl file. Review those changes and commit them to your
version control system if they represent changes you intended to make.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ terraform test
main.tftest.hcl... in progress
run "setup"... pass
run "app_tier_subnets_found"... pass
main.tftest.hcl... tearing down
main.tftest.hcl... pass
Success! 2 passed, 0 failed.
$
Hi @acwwat, thank you for the response! I tested the code and got this error
β·
β Error: Test assertion failed
β
β on tests/networking.tftest.hcl line 14, in run "app_tier_subnets_found":
β 14: condition = length(data.aws_subnets.app.ids) == 2
β βββββββββββββββββ
β β data.aws_subnets.app.ids is empty list of string
β
β The number of app-tier subnets found is incorrect
Do you know what might be wrong? Does it have to do with my providers?
@Kurtyjlee I believe that including mock_provider in tests/networking.tftest.hcl is causing all resources and data sources to be mocked. Since we want the resources defined in testing/setup/main.tf to be created, youβll need to remove the mock provider.
If you must use both the real and mock providers, you can specify the appropriate providers configuration in each run block, similar to the last example in described in the Mock Providers section of the documentation.
I am currently working in an environment where access to the aws account is restricted to github workflows on another codebase. Can I check if this can be done without credentials to an aws account? is there a way for me to mock the provider such that resources can be created within that provider and be used for testing?
@Kurtyjlee It really depends on what you are trying to test. You can certainly mock your aws_subnets data sources as well, but does that really help you validate your configuration? Or are there other resources that depend on the aws_subnets data source that you want to test? If so, mocking the data source and the downstream resources could make sense.
Terraform testing is unfortunately outside my area of expertise as I am used to doing end-to-end tests, so you may need to refer to other resources (like this article from AWS or this article from Spacelift) or start another thread in the Terraform category (while tagging testing).
Hi @Kurtyjlee - itβs not currently possible to get a mock provider to return created resources as data sources.
Iβd recommend just overriding the data source directly with an override_data block. You can then control exactly what values are returned for that data in your future tests. If you want to just validate your conditions on the data source, the overridden data source will let you do this, as you can control the returned values to test pass and fail conditions for your condition.
This also works if you want to just use the output of the data source in other parts of your configuration - you can just control exactly what the data source says exists in the provider.
Hi @liamcervante, thanks for the response! I am still a little unclear on how to use it for my use case of testing the βaws_subnetsβ data block condition. Do you mind giving an example on how to use the override_data block for this use case?
The data source will now return those two values for the ids field. You can also combine this with a mock provider more generally, so you can test your configuration without needing an AWS account.
Can I check how do I use this to validate the data fetching for the subnet ids?
I would like to test if the subnets fetched from the data block has the tag Tier=βappβ. With this override block, it merely overrides the subnet ids for the data block?