【メモ】terraform testを実行するには、ルートディレクトリの配下にテストコードを配置する必要がある
要約
これはOK
. ├── main.tf └── test.tftest.hcl or . ├── main.tf └── tests └── test.tftest.hcl
これはだめ
.
├── module
│ └── main.tf
└── tests
└── test.tftest.hcl
概要
表題の通りですが、 terraform test を実行する際のテスト定義ファイル ( .tftest.hcl or .tftest.json ) は、 terraform test コマンドを実行したディレクトリ配下に配置しないと読み取られません。
こちらのTerraformドキュメントには以下のように記載されています。
Terraform loads all test files within your root configuration directory. Terraform also loads the test files within the testing directory. You can override the location of the testing directory by appending the -test-directory flag to commands that load test files. The default testing directory is tests relative to your configuration directory.
試しに以下のようなファイル構造のディレクトリにおいて、 main.tf の存在するパスで terraform test コマンドを実行します。
. ├── module │ └── main.tf # ここでterraform testコマンドを実行 └── tests └── test.tftest.hcl
すると以下のような結果となり、テストが実行されませんでした。
$ terraform test Success! 0 passed, 0 failed.
今度は -test-directory でディレクトリを指定してみます。すると Invalid testing directory というエラーが発生し、 main.tf のあるディレクトリ配下を指定しなければならないことが分かります。
$ terraform test -test-directory=../tests ╷ │ Error: Invalid testing directory │ │ The testing directory must be a relative path pointing to a directory local to the configuration directory. ╵
本当は汎用的なプログラミング言語のようにテストコード専用のディレクトリを置きたかったのですが、 terraform test では今のところできないようです (こちらのTerraformドキュメントを読むと、moduleに対するテストを想定しているように読めたので、そういった使い方はできないのでしょう) 。