WSL2でTerraTestのQuick startやってみた



$ go version
go version go1.18.1 linux/amd64



$ tree .
Command 'tree' not found, but can be installed with:
sudo apt install tree

はいってねえええええ!!! sudo apt install tree します。ハイ。

$ tree
├── examples
│   └── terraform-hello-world-example
│       └── main.tf
└── test
    └── terraform_hello_world_example_test.go

Terraformのファイルとテスト用のGoのファイルの中身は https://terratest.gruntwork.io/ こちらに記載されています。

それか、gruntwork-io/terratestgit cloneして持ってきてもOKです。他のサンプルも色々と入っています。必要なファイルを上記のように配置してください。

$ git clone https://github.com/gruntwork-io/terratest.git
Cloning into 'terratest'...
remote: Enumerating objects: 18341, done.
remote: Counting objects: 100% (249/249), done.
remote: Compressing objects: 100% (147/147), done.
remote: Total 18341 (delta 117), reused 197 (delta 94), pack-reused 18092
Receiving objects: 100% (18341/18341), 19.99 MiB | 5.80 MiB/s, done.
Resolving deltas: 100% (12601/12601), done.

Terraformのディレクトリでterraform init && terraform applyします。

$ cd examples/terraform-hello-world-example/
$ terraform init

Initializing the backend...

Initializing provider plugins...

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 apply

Changes to Outputs:
  + hello_world = "Hello, World!"

You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.


hello_world = "Hello, World!"
$ terraform output
hello_world = "Hello, World!"


$ ls
main.tf  terraform.tfstate


$ cd ../../test
$ go mod init TestTerraformHelloWorldExample
go: creating new go.mod: module TestTerraformHelloWorldExample
go: to add module requirements and sums:
        go mod tidy
$ go mod tidy
go: finding module for package github.com/stretchr/testify/assert
go: finding module for package github.com/gruntwork-io/terratest/modules/terraform
go: found github.com/gruntwork-io/terratest/modules/terraform in github.com/gruntwork-io/terratest v0.40.22
go: found github.com/stretchr/testify/assert in github.com/stretchr/testify v1.8.0
$ go test -v -run TestTerraformHelloWorldExample
=== RUN   TestTerraformHelloWorldExample
TestTerraformHelloWorldExample 2022-09-14T02:00:55+09:00 retry.go:91: terraform [init -upgrade=false]
--- PASS: TestTerraformHelloWorldExample (2.00s)
ok      TestTerraformHelloWorldExample  2.007s