hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 442 forks source link

Design CDK for Terraform integration testing #1093

Open danieldreier opened 2 years ago

danieldreier commented 2 years ago

This is a research and design task to design and plan a workflow for integration testing of CDK for Terraform applications. This ticket is a placeholder for the sake of long-term planning, and should be rewritten with more defined scope before work starts.

The scope of this issue is to propose an approach to integration testing, ideally aligned with the AWS CDK approach linked below. An internal HashiCorp RFC is needed for this issue before implementation work starts.

Reference Material

marcus-sa commented 2 years ago

Any consideration regarding adding support for something like https://www.terraform.io/docs/language/modules/testing-experiment.html ?

JohnCalhoun commented 2 years ago

need this feature and would love to help provider feedback or ideas

skorfmann commented 2 years ago

need this feature and would love to help provider feedback or ideas

Which use-cases do you have in mind in particular?

skorfmann commented 2 years ago

@marcus-sa

Any consideration regarding adding support for something like https://www.terraform.io/docs/language/modules/testing-experiment.html ?

No, not so far - but curious to learn how you'd use this in the context of cdktf

marcus-sa commented 2 years ago

No, not so far - but curious to learn how you'd use this in the context of cdktf

@skorfmann I've done something like this instead. Way easier than I initially thought.

I might create a repository template for showcasing how one might structure their own project using Terraform CDK. There are quite a few workarounds still needed, like explicitly overriding the logical id of TerraformVariable for it to be used.

JohnCalhoun commented 2 years ago

yeah the overriding of TerraformVariable is super needed for this. But right now I can test up and including terraform plan using to toPlanSuccessfully(), but want i want to do next is actualy launch the stack (from the .test.ts jest file) and then get outputs from the stack and run AWS queries against the stack (like invoke a lambda function and check its return value).

I built something sort of like this at my previous job but I want to do it this time in a more community supported way, I dont want to base our whole codebase testing on a hack that the community will do better anyways.

marcus-sa commented 2 years ago

yeah the overriding of TerraformVariable is super needed for this. But right now I can test up and including terraform plan using to toPlanSuccessfully(), but want i want to do next is actualy launch the stack (from the .test.ts jest file) and then get outputs from the stack and run AWS queries against the stack (like invoke a lambda function and check its return value).

I built something sort of like this at my previous job but I want to do it this time in a more community supported way, I dont want to base our whole codebase testing on a hack that the community will do better anyways.

If you refer to the terraform.ts file in the gist I provided, then that can be used to synthesize, initialize and apply a stack, and upon finalizing it'll destroy the stack.

Idk about AWS, but for GCP there's NodeJS clients for all their services that would allow you to do some end-to-end testing.

I completely agree that an ideal solution would be community driven and integrated directly into Terraform CDK.

I also had to do massive amounts of workarounds to be able to fetch the plan of a pull request run in Terraform Cloud.