Closed kamsz closed 4 years ago
Not currently, but that's a great idea. A TerraformBinary
field, which if empty defaults to terraform
, would be a great addition to terraform.Options.
@brikis98 I'd stick with a separate module to include additional functionalities of Terragrunt like apply-all.
I think it would be cleaner to avoid duplicating all those code paths. To handle apply-all
, we could expose a RunTerraformCommand(command string, args[]string, options *terraform.Options)
method.
I'll try to work on this then.
I'm confused here: doesn't gruntwork exclusively use terragrunt to work on infrastructure? And shouldn't all "dev" "staging" and "production" folders have terratests written for them (because that is what you seem to suggest in both of your books). So how does gruntwork actually test terragrunt - do you just write terratests for modules, and use another tool to do smoke tests on terragrunt deployments? @brikis98
I'm confused here: doesn't gruntwork exclusively use terragrunt to work on infrastructure?
Not exclusively. All of the modules in our IaC Library, where we do the majority of our testing, are written in pure Terraform and do NOT require Terragrunt at all. Also, many of the modules are written in Go, Python, Bash, etc too, as the IaC Library is designed to solve problems across the entire infrastructure, and not just those problems that can be solved by Terraform.
So how does gruntwork actually test terragrunt
Terragrunt itself is tested in this repo. See the _test.go
files.
do smoke tests on terragrunt deployments
Correct
@brikis98
Thank you for the fast response and time, you seem like an extremely busy person.
After further analyzing the terragrunt and terratest repos let me see if you think how I'm going to test my future configurations make sense. I tried looking for people using writing go tests for a terragrunt project layout, but couldn't find any - so maybe tons of people are using terragrunt but not actually writing any tests for their infrastructure?.
I want to take a docker image I have and get it running in ec2. So first I
Give me as much criticism as you feel like, I'm trying to learn the right way of doing things from you. I've been looking at your code, and half way done reading your book "hello startup". I really appreciate your work and writing.
@brikis98 does that seem right or is that overkill?
Sorry, been buried, will take a look when I get a chance
@so87 Have you ever resolved unit testing (terratest) and terragrunt items mentioned above? Interested in any approach. Working through some of the same items you mentioned above.
This is OT to the terragrunt testing workflow, but circling back on the OP, we've released a few functions since the issue was opened that help with testing Terragrunt:
terraform.Options
exposes TerraformBinary
(https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/options.go#L11) which you can use to point to Terragruntapply-all
and plan-all
: terraform.TgApplyAll and terraform.TgPlanAllExitCode.I recently used the option TerraformBinary
and the apply-all
and plan-all
functions to write tests for terraform modules which are usually used using terragrunt. I happened on them by chance while looking at some test implementations.
I think some examples to illustrate the use of the option and functions would be helpful.
As mentioned in this comment, you can now test Terragrunt.
@kihahu @yorinasub17 @so87 @brikis98
Can any one of you share me piece of code which is written to invoke terragrunt(HCL) files from terratest(GO) script ?
I am getting "github.com/gruntwork-io/terratest/modules/terraform.(*TgInvalidBinary).Error" error for below code
Tryin hard to get some example code which helps me to invoke terragrunt files from terratest , please help
Try setting the TerraformBinary
attribute on the Options
struct to the path to terragrunt binary, and that should do the trick.
Exploring options to swap Terraform with Terragrunt via options.TerraformDir
at the moment.
This is extremely useful while testing Terraform modules on different combinations of the providers at the same time. Think about safe and scalable provider upgrade story:
Basically, you do something like that:
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "azurerm" {
version = "=${get_env("TG_PROVIDER_AZURERM_VERSION", "2.40.0")}"
features {}
}
}
and the you can run your test in parallel setting different env variables for TG_PROVIDER_AZURERM_VERSION
to test your module under different provider verssion:
Running such tests in parallel with different env variable setup unlocks easy and scalable upgrade story, testing and also troubleshooting should providers have funky behaviors.
All that is harder to do with native TF, not much options to override provider setup version (unless I miss something). Would be so great to have TerraTest support for TG, of course.
Would be good to get sample docs for terragrunt with terratest as @pandu-bhojaraj-mf mentioned in its comments. There is literally no guide on such a simple swap out binary task
Here's a quick Terragrunt test I got working using the existing example terragrunt-example
:
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
func TestTerragruntExample(t *testing.T) {
// website::tag::2:: Construct the terraform options with default retryable errors to handle the most common
// retryable errors in terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// website::tag::1:: Set the path to the Terraform code that will be tested.
TerraformDir: "../examples/terragrunt-example",
TerraformBinary: "terragrunt.exe", // ensure terragrunt is on your PATH, or provide the full path here
})
// website::tag::5:: Clean up resources with "terragrunt destroy" at the end of the test.
defer terraform.Destroy(t, terraformOptions)
// website::tag::3:: Run "terragrunt init" and "terragrunt apply". Fail the test if there are any errors.
terraform.InitAndApply(t, terraformOptions)
// website::tag::4:: Run `terragrunt output` to get the values of output variables and check they have the expected values.
output := terraform.Output(t, terraformOptions, "output")
assert.Equal(t, "one input another input", output)
}
cd test
go test -v -run TestTerragruntExample
Hi folks, I just merged in a terragrunt example that is exposed on our site, which is going to be very similar to the one above: https://terratest.gruntwork.io/examples/ (it's unfortunately in the overflow, hidden in the dropdown)
Hopefully folks can use that going forward!
still don't see it but thanks for the link. Screenshot would be good to find that dropdown
Here's a screenshot:
Is it possible to swap executable that is used from
terraform
toterragrunt
?