Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

huangapple go评论118阅读模式
英文:

Issue installing Terratest using Go Task's Yaml Azure pipeline - issue triggering terratest tests in sub-folder

问题

我在安装Terratest时遇到了问题,使用Azure YAML管道时出现了以下错误:

  1. C:\hostedtoolcache\windows\go.17.1\x64\bin\go.exe install -v github.com/gruntwork-io/terratest@v0.40.6
  2. go: downloading github.com/gruntwork-io/terratest v0.40.6
  3. go install: github.com/gruntwork-io/terratest@v0.40.6: module github.com/gruntwork-io/terratest@v0.40.6 found, but does not contain package github.com/gruntwork-io/terratest
  4. ##[error]The Go task failed with an error: Error: The process 'C:\hostedtoolcache\windows\go.17.1\x64\bin\go.exe' failed with exit code 1
  5. Finishing: Install Go Terratest module - v0.40.6

我安装Terratest的代码如下:

  1. - task: Go@0
  2. displayName: Install Go Terratest module - v$(TERRATEST_VERSION)
  3. inputs:
  4. command: custom
  5. customCommand: install
  6. arguments: $(TF_LOG) github.com/gruntwork-io/terratest@v$(TERRATEST_VERSION)
  7. workingDirectory: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)

也许我在使用Terratest时犯了一些错误。

下面是我的代码树截图:

Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

我在(例如)"_Terraform\azure_v2_X\ResourceModules"子目录中有Terraform代码,而在"_Terraform\azure_v2_X\Tests_Unit_ResourceModules"子目录中有Terratest测试(在屏幕截图中是app_configuration资源模块的测试)。

在我的Terratest模块中,我按照以下代码调用我的资源模块:

  1. ######test in a un isolated Resource Group defined in locals
  2. module "app_configuration_tobetested" {
  3. source = "../../ResourceModules/app_configuration"
  4. resource_group_name = local.rg_name
  5. location = local.location
  6. environment = var.ENVIRONMENT
  7. sku = "standard"
  8. // rem : here app_service_shared prefix and app_config_shared prefix are the same !
  9. app_service_prefix = module.app_configuration_list_fortests.settings.frontEnd_prefix
  10. # stage = var.STAGE
  11. app_config_list = module.app_configuration_list_fortests.settings.list_app_config
  12. }

在我的Go文件中,我根据我想要的预期结果测试我的模块结果:

  1. package RM_app_configuration_Test
  2. import (
  3. "os"
  4. "testing"
  5. // "github.com/gruntwork-io/terratest/modules/azure"
  6. "github.com/gruntwork-io/terratest/modules/terraform"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var (
  10. globalBackendConf = make(map[string]interface{})
  11. globalEnvVars = make(map[string]string)
  12. )
  13. func TestTerraform_RM_app_configuration(t *testing.T) {
  14. t.Parallel()
  15. // terraform Directory
  16. fixtureFolder := "./"
  17. // backend specification
  18. strlocal := "RMapCfg_"
  19. // input value
  20. inputStage := "sbx_we"
  21. inputEnvironment := "SBX"
  22. inputApplication := "DEMO"
  23. // expected value
  24. expectedRsgName := "z-adf-ftnd-shrd-sbx-ew1-rgp01"
  25. // expectedAppCfgPrefix := "z-adf-ftnd-shrd"
  26. expectedAppConfigReader_ID := "[/subscriptions/f04c8fd5-d013-41c3-9102-43b25880d2e2/resourceGroups/z-adf-ftnd-shrd-sbx-ew1-rgp01/providers/Microsoft.AppConfiguration/configurationStores/z-adf-ftnd-shrd-sbx-ew1-blue-sbx-cfg01 /subscriptions/f04c8fd5-d013-41c3-9102-43b25880d2e2/resourceGroups/z-adf-ftnd-shrd-sbx-ew1-rgp01/providers/Microsoft.AppConfiguration/configurationStores/z-adf-ftnd-shrd-sbx-ew1-green-sbx-cfg01]"
  27. // getting enVars from environment variables
  28. /*
  29. Go and Terraform uses two differents methods for Azure authentification.
  30. ** Terraform authentification is explained bellow :
  31. - https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret#configuring-the-service-principal-in-terraform
  32. ** Go authentification is explained bellow
  33. - https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-environment-based-authentication
  34. ** Terratest is using both authentification methods regarding the work it has to be done :
  35. - azure existences tests uses Go azure authentification :
  36. - https://github.com/gruntwork-io/terratest/blob/master/modules/azure/authorizer.go#L11
  37. - terraform commands uses terraform authentification :
  38. - https://github.com/gruntwork-io/terratest/blob/0d654bd2ab781a52e495f61230cf892dfba9731b/modules/terraform/cmd.go#L12
  39. - https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret#configuring-the-service-principal-in-terraform
  40. so both authentification methods have to be implemented
  41. */
  42. // getting terraform EnvVars from Azure Go environment variables
  43. ARM_CLIENT_ID := os.Getenv("AZURE_CLIENT_ID")
  44. ARM_CLIENT_SECRET := os.Getenv("AZURE_CLIENT_SECRET")
  45. ARM_TENANT_ID := os.Getenv("AZURE_TENANT_ID")
  46. ARM_SUBSCRIPTION_ID := os.Getenv("ARM_SUBSCRIPTION_ID")
  47. if ARM_CLIENT_ID != "" {
  48. globalEnvVars["ARM_CLIENT_ID"] = ARM_CLIENT_ID
  49. globalEnvVars["ARM_CLIENT_SECRET"] = ARM_CLIENT_SECRET
  50. globalEnvVars["ARM_SUBSCRIPTION_ID"] = ARM_SUBSCRIPTION_ID
  51. globalEnvVars["ARM_TENANT_ID"] = ARM_TENANT_ID
  52. }
  53. // getting terraform backend from environment variables
  54. resource_group_name := os.Getenv("resource_group_name")
  55. storage_account_name := os.Getenv("storage_account_name")
  56. container_name := os.Getenv("container_name")
  57. key := strlocal + os.Getenv("key")
  58. if resource_group_name != "" {
  59. globalBackendConf["resource_group_name"] = resource_group_name
  60. globalBackendConf["storage_account_name"] = storage_account_name
  61. globalBackendConf["container_name"] = container_name
  62. globalBackendConf["key"] = key
  63. }
  64. // User Terratest to deploy the infrastructure
  65. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
  66. // website::tag::1::Set the path to the Terraform code that will be tested.
  67. // The path to where our Terraform code is located
  68. TerraformDir: fixtureFolder,
  69. // Variables to pass to our Terraform code using -var options
  70. Vars: map[string]interface{}{
  71. "STAGE": inputStage,
  72. "ENVIRONMENT": inputEnvironment,
  73. "APPLICATION": inputApplication,
  74. },
  75. EnvVars: globalEnvVars,
  76. // backend values to set when initialziing Terraform
  77. BackendConfig: globalBackendConf,
  78. // Disable colors in Terraform commands so its easier to parse stdout/stderr
  79. NoColor: true,
  80. })
  81. // website::tag::4::Clean up resources with "terraform destroy". Using "defer" runs the command at the end of the test, whether the test succeeds or fails.
  82. // At the end of the test, run `terraform destroy` to clean up any resources that were created
  83. defer terraform.Destroy(t, terraformOptions)
  84. // website::tag::2::Run "terraform init" and "terraform apply".
  85. // This will run `terraform init` and `terraform apply` and fail the test if there are any errors
  86. terraform.InitAndApply(t, terraformOptions)
  87. // tests the resource_group for the app_configuration
  88. /*
  89. actualAppConfigReaderPrefix := terraform.Output(t, terraformOptions, "app_configuration_tested_prefix")
  90. assert.Equal(t, expectedAppCfgprefix, actualAppConfigReaderPrefix)
  91. */
  92. actualRSGReaderName := terraform.Output(t, terraformOptions, "app_configuration_tested_RG_name")
  93. assert.Equal(t, expectedRsgName, actualRSGReaderName)
  94. actualAppConfigReader_ID := terraform.Output(t, terraformOptions, "app_configuration_tobetested_id")
  95. assert.Equal(t, expectedAppConfigReader_ID, actualAppConfigReader_ID)
  96. }

事实上,在本地,我可以从我的主文件夹"Terraform\Azure_v2_X\Tests_Unit_ResourceModules"执行以下命令以触发所有测试:

  • (来自Go v1.11
  1. Go test ./...

使用Go版本1.12,我可以设置GO111MODULE=auto以获得相同的结果。
但是,使用Go 1.17,我现在必须设置GO111MODULE=off来触发我的测试。

目前,我有两个主要问题困扰着我:

1)我如何从Azure管道中导入Terratest(和其他)模块?

2)我应该如何正确使用Terratest的Go模块?
我在我的主文件夹"_Terraform\Azure_v2_X\Tests_Unit_ResourceModules"中没有Go代码,并希望在Azure管道中通过一个简单的命令行触发所有子文件夹的Go测试。

谢谢您能提供的任何帮助。

最好的问候,

英文:

I'm facing this issue while installing terratest by azure yaml pipeline :

  1. C:\hostedtoolcache\windows\go\1.17.1\x64\bin\go.exe install -v github.com/gruntwork-io/terratest@v0.40.6
  2. go: downloading github.com/gruntwork-io/terratest v0.40.6
  3. go install: github.com/gruntwork-io/terratest@v0.40.6: module github.com/gruntwork-io/terratest@v0.40.6 found, but does not contain package github.com/gruntwork-io/terratest
  4. ##[error]The Go task failed with an error: Error: The process 'C:\hostedtoolcache\windows\go\1.17.1\x64\bin\go.exe' failed with exit code 1
  5. Finishing: Install Go Terratest module - v0.40.6

My code for installation is bellow :

  1. - task: Go@0
  2. displayName: Install Go Terratest module - v$(TERRATEST_VERSION)
  3. inputs:
  4. command: custom
  5. customCommand: install
  6. arguments: $(TF_LOG) github.com/gruntwork-io/terratest@v$(TERRATEST_VERSION)
  7. workingDirectory: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)

But peharps I made mistakes in the use of terratest.

Bellow is a screenshot of my code tree :

Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

I have terraform code in (for exemple) Terraform\azure_v2_X\ResourceModules sub-directory, and terratest test in Terraform\azure_v2_X\Tests_Unit_ResourceModules subdirectories (in screenshot app_configuration tests for app_configuration resourceModules).

In my terratest module, I'm calling for my resourceModule as in the following code :

  1. ######test in a un isolated Resource Group defined in locals
  2. module "app_configuration_tobetested" {
  3. source = "../../ResourceModules/app_configuration"
  4. resource_group_name = local.rg_name
  5. location = local.location
  6. environment = var.ENVIRONMENT
  7. sku = "standard"
  8. // rem : here app_service_shared prefix and app_config_shared prefix are the same !
  9. app_service_prefix = module.app_configuration_list_fortests.settings.frontEnd_prefix
  10. # stage = var.STAGE
  11. app_config_list = module.app_configuration_list_fortests.settings.list_app_config
  12. }

And in my Go file, I test my module result regarding the expected result I want :

  1. package RM_app_configuration_Test
  2. import (
  3. "os"
  4. "testing"
  5. // "github.com/gruntwork-io/terratest/modules/azure"
  6. "github.com/gruntwork-io/terratest/modules/terraform"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var (
  10. globalBackendConf = make(map[string]interface{})
  11. globalEnvVars = make(map[string]string)
  12. )
  13. func TestTerraform_RM_app_configuration(t *testing.T) {
  14. t.Parallel()
  15. // terraform Directory
  16. fixtureFolder := "./"
  17. // backend specification
  18. strlocal := "RMapCfg_"
  19. // input value
  20. inputStage := "sbx_we"
  21. inputEnvironment := "SBX"
  22. inputApplication := "DEMO"
  23. // expected value
  24. expectedRsgName := "z-adf-ftnd-shrd-sbx-ew1-rgp01"
  25. // expectedAppCfgPrefix := "z-adf-ftnd-shrd"
  26. expectedAppConfigReader_ID := "[/subscriptions/f04c8fd5-d013-41c3-9102-43b25880d2e2/resourceGroups/z-adf-ftnd-shrd-sbx-ew1-rgp01/providers/Microsoft.AppConfiguration/configurationStores/z-adf-ftnd-shrd-sbx-ew1-blue-sbx-cfg01 /subscriptions/f04c8fd5-d013-41c3-9102-43b25880d2e2/resourceGroups/z-adf-ftnd-shrd-sbx-ew1-rgp01/providers/Microsoft.AppConfiguration/configurationStores/z-adf-ftnd-shrd-sbx-ew1-green-sbx-cfg01]"
  27. // getting enVars from environment variables
  28. /*
  29. Go and Terraform uses two differents methods for Azure authentification.
  30. ** Terraform authentification is explained bellow :
  31. - https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret#configuring-the-service-principal-in-terraform
  32. ** Go authentification is explained bellow
  33. - https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-environment-based-authentication
  34. ** Terratest is using both authentification methods regarding the work it has to be done :
  35. - azure existences tests uses Go azure authentification :
  36. - https://github.com/gruntwork-io/terratest/blob/master/modules/azure/authorizer.go#L11
  37. - terraform commands uses terraform authentification :
  38. - https://github.com/gruntwork-io/terratest/blob/0d654bd2ab781a52e495f61230cf892dfba9731b/modules/terraform/cmd.go#L12
  39. - https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret#configuring-the-service-principal-in-terraform
  40. so both authentification methods have to be implemented
  41. */
  42. // getting terraform EnvVars from Azure Go environment variables
  43. ARM_CLIENT_ID := os.Getenv("AZURE_CLIENT_ID")
  44. ARM_CLIENT_SECRET := os.Getenv("AZURE_CLIENT_SECRET")
  45. ARM_TENANT_ID := os.Getenv("AZURE_TENANT_ID")
  46. ARM_SUBSCRIPTION_ID := os.Getenv("ARM_SUBSCRIPTION_ID")
  47. if ARM_CLIENT_ID != "" {
  48. globalEnvVars["ARM_CLIENT_ID"] = ARM_CLIENT_ID
  49. globalEnvVars["ARM_CLIENT_SECRET"] = ARM_CLIENT_SECRET
  50. globalEnvVars["ARM_SUBSCRIPTION_ID"] = ARM_SUBSCRIPTION_ID
  51. globalEnvVars["ARM_TENANT_ID"] = ARM_TENANT_ID
  52. }
  53. // getting terraform backend from environment variables
  54. resource_group_name := os.Getenv("resource_group_name")
  55. storage_account_name := os.Getenv("storage_account_name")
  56. container_name := os.Getenv("container_name")
  57. key := strlocal + os.Getenv("key")
  58. if resource_group_name != "" {
  59. globalBackendConf["resource_group_name"] = resource_group_name
  60. globalBackendConf["storage_account_name"] = storage_account_name
  61. globalBackendConf["container_name"] = container_name
  62. globalBackendConf["key"] = key
  63. }
  64. // User Terratest to deploy the infrastructure
  65. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
  66. // website::tag::1::Set the path to the Terraform code that will be tested.
  67. // The path to where our Terraform code is located
  68. TerraformDir: fixtureFolder,
  69. // Variables to pass to our Terraform code using -var options
  70. Vars: map[string]interface{}{
  71. "STAGE": inputStage,
  72. "ENVIRONMENT": inputEnvironment,
  73. "APPLICATION": inputApplication,
  74. },
  75. EnvVars: globalEnvVars,
  76. // backend values to set when initialziing Terraform
  77. BackendConfig: globalBackendConf,
  78. // Disable colors in Terraform commands so its easier to parse stdout/stderr
  79. NoColor: true,
  80. })
  81. // website::tag::4::Clean up resources with "terraform destroy". Using "defer" runs the command at the end of the test, whether the test succeeds or fails.
  82. // At the end of the test, run `terraform destroy` to clean up any resources that were created
  83. defer terraform.Destroy(t, terraformOptions)
  84. // website::tag::2::Run "terraform init" and "terraform apply".
  85. // This will run `terraform init` and `terraform apply` and fail the test if there are any errors
  86. terraform.InitAndApply(t, terraformOptions)
  87. // tests the resource_group for the app_configuration
  88. /*
  89. actualAppConfigReaderPrefix := terraform.Output(t, terraformOptions, "app_configuration_tested_prefix")
  90. assert.Equal(t, expectedAppCfgprefix, actualAppConfigReaderPrefix)
  91. */
  92. actualRSGReaderName := terraform.Output(t, terraformOptions, "app_configuration_tested_RG_name")
  93. assert.Equal(t, expectedRsgName, actualRSGReaderName)
  94. actualAppConfigReader_ID := terraform.Output(t, terraformOptions, "app_configuration_tobetested_id")
  95. assert.Equal(t, expectedAppConfigReader_ID, actualAppConfigReader_ID)
  96. }

The fact is locally, I can do, from My main folder Terraform\Azure_v2_X\Tests_Unit_ResourceModules the following command to trigger all my tests in a raw :

  • (from Go v1.11)
  1. Go test ./...

With Go version 1.12, I could set GO111MODULE=auto to have the same results.
But with Go 1.17, I have now to set GO111MODULE=off to trigger my tetst.

For now, I have 2 main questions that nagging me :

  1. How can I Go import Terratest (and other) modules from azure Pipeline ?

  2. What I have to do to correctly use Go Modules with terratest ?
    I have no Go code in my main folder _Terraform\Azure_v2_X\Tests_Unit_ResourceModules_ and would like to trigger all the sub_folder go tests in a simple command line in my Azure Pipeline.

Thank you for any help you could give.

Best regards,

答案1

得分: 1

我将再次回答我的问题。 Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

所以,现在使用以下版本:

  • -- GOVERSION: 1.17.1
  • -- TERRAFORM_VERSION : 1.1.7
  • -- TERRATEST_VERSION: 0.40.6

关于terratest测试,文件夹层次结构已经发生了以下变化:

Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

我不再尝试导入我的Terratest模块。(所以上面的第1点已经得到了回答,显然)

现在我只需要:

  1. 对我的每个terratest模块进行Go mod操作
  2. 使用脚本逐个触发每个模块

所以我的流水线现在变成了以下内容:

  • task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
    displayName: Install Terraform $(TERRAFORM_VERSION)
    inputs:
    terraformVersion: $(TERRAFORM_VERSION)

  • task: GoTool@0
    displayName: 'Use Go $(GOVERSION)'
    inputs:
    version: $(GOVERSION)
    goPath: $(GOPATH)
    goBin: $(GOBIN)

  • task: PowerShell@2
    displayName: run Terratest for $(pathToTerraformRootModule)
    inputs:
    targettype : 'filePath'
    filePath: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)/$(Run_Terratest_script)
    workingDirectory: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)
    env:

    see https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-environment-based-authentication

    for Azure authentification with Go

    ARM_SUBSCRIPTION_ID: $(TF_VAR_ARM_SUBSCRIPTION_ID)
    AZURE_CLIENT_ID: $(TF_VAR_ARM_CLIENT_ID)
    AZURE_TENANT_ID: $(TF_VAR_ARM_TENANT_ID)
    AZURE_CLIENT_SECRET: $(TF_VAR_ARM_CLIENT_SECRET) # set as pipeline secret
    resource_group_name: $(storageAccountResourceGroup)
    storage_account_name: $(storageAccount)
    container_name: $(stateBlobContainer)
    key: '$(MODULE)-$(TF_VAR_APPLICATION)-$(TF_VAR_ENVIRONMENT).tfstate'
    GO111MODULE: 'auto'

在我的terratest子文件夹的主文件夹中,我有以下内容:run_terratests.ps1脚本和Terratests列表文件:

run_terratests.ps1

this file is based on https://github.com/google/go-cloud/blob/master/internal/testing/runchecks.sh

This script runs all go Terratest suites,

compatibility checks, consistency checks, Wire, etc.

$moduleListFile = "Terratests"

regex to filter : not began with

$regexFilter = "^[^#]"

read the ModuleListFile

[object] $arrayFromFile = Get-Content -Path $moduleListFile | Where-Object { $_ -match $regexFilter} | ConvertFrom-String -PropertyNames folder, totest

$result = 0 # set no error by default

get the actual folder

$main_path = Get-Location | select -ExpandProperty "Path"
#read the array to show if to be tested !
foreach ($line in $arrayFromFile) {
# write-Host $line
if ($line.totest -eq "yes") {
$path = $line.folder
set-location $main_path$path
$myPath = Get-Location
# Write-Host $myPath
# trigger terratest for files
Go test ./...
}
if ($false -eq $?)
{
$result = 1
}
}

back to school Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

set-location $main_path

if ($result -eq 1)
{
Write-Error "Msbuild exit code indicate test failure."
Write-Host "##vso[task.logissue type=error]Msbuild exit code indicate test failure."
exit(1)
}

代码

if ($false -eq $?)
{
$result = 1
}

用于在测试错误时使流水线失败,而不会跳过其他测试。

Terratests

this file lists all the modules to be tested in the "Tests_Unit_ConfigHelpers" repository.

it us used by the "run_terratest.ps1" powershell script to trigger terratest for each test.

Any line that doesn't begin with a '#' character and isn't empty is treated

as a path relative to the top of the repository that has a module in it.

The 'tobetested' field specifies whether this is a module that have to be tested.

this file is based on https://github.com/google/go-cloud/blob/master/allmodules

module-directory tobetested

azure_constants yes
configure_app_srv_etc yes
configure_frontdoor_etc yes
configure_hostnames yes
constants yes
FrontEnd_AppService_slots/_main yes
FrontEnd_AppService_slots/settings yes
merge_maps_of_strings yes
name yes
name_template yes
network/hostname_generator yes
network/hostnames_generator yes
replace_2vars_into_string_etc yes
replace_var_into_string_etc yes
sorting_map_with_an_other_map yes

每个terratest文件夹中的更改是我将添加go.mod和go.sum文件:

$ go mod init mytest
go: creating new go.mod: module mytest
go: to add module requirements and sums:
go mod tidy

$ go mod tidy

link each of the go modules needed for your terratest module

因此,通过powershell脚本中的go test ./...将下载所需的go模块并运行特定测试的测试。

感谢阅读,如果您认为这有帮助,请投票支持 Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

英文:

I will once again answer my own question. Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

so, for now, using the following versions :

  • -- GOVERSION: 1.17.1
  • -- TERRAFORM_VERSION : 1.1.7
  • -- TERRATEST_VERSION: 0.40.6

The folder hierarchy has changed the following, regarding terratest tests :

Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

I do no longer try to Go import my Terratest module. (so point 1) above is ansered, obviously)

I now just have to :

  1. Go mod each of my terratest modules
  2. Trigger each of them individually one by one, using script

so my pipeline just became the following :

  1. - task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
  2. displayName: Install Terraform $(TERRAFORM_VERSION)
  3. inputs:
  4. terraformVersion: $(TERRAFORM_VERSION)
  5. - task: GoTool@0
  6. displayName: 'Use Go $(GOVERSION)'
  7. inputs:
  8. version: $(GOVERSION)
  9. goPath: $(GOPATH)
  10. goBin: $(GOBIN)
  11. - task: PowerShell@2
  12. displayName: run Terratest for $(pathToTerraformRootModule)
  13. inputs:
  14. targettype : 'filePath'
  15. filePath: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)/$(Run_Terratest_script)
  16. workingDirectory: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)
  17. env:
  18. # see https://learn.microsoft.com/en-us/azure/developer/go/azure-sdk-authorization#use-environment-based-authentication
  19. # for Azure authentification with Go
  20. ARM_SUBSCRIPTION_ID: $(TF_VAR_ARM_SUBSCRIPTION_ID)
  21. AZURE_CLIENT_ID: $(TF_VAR_ARM_CLIENT_ID)
  22. AZURE_TENANT_ID: $(TF_VAR_ARM_TENANT_ID)
  23. AZURE_CLIENT_SECRET: $(TF_VAR_ARM_CLIENT_SECRET) # set as pipeline secret
  24. resource_group_name: $(storageAccountResourceGroup)
  25. storage_account_name: $(storageAccount)
  26. container_name: $(stateBlobContainer)
  27. key: '$(MODULE)-$(TF_VAR_APPLICATION)-$(TF_VAR_ENVIRONMENT).tfstate'
  28. GO111MODULE: 'auto'

And in my main folder for my terratest sub-folders, I have the run_terratests.ps1 script and the Terratests list file as bellow :

run_terratests.ps1

  1. # this file is based on https://github.com/google/go-cloud/blob/master/internal/testing/runchecks.sh
  2. #
  3. # This script runs all go Terratest suites,
  4. # compatibility checks, consistency checks, Wire, etc.
  5. $moduleListFile = "./Terratests"
  6. # regex to filter : not began with #
  7. $regexFilter = "^[^#]"
  8. # read the ModuleListFile
  9. [object] $arrayFromFile = Get-Content -Path $moduleListFile | Where-Object { $_ -match $regexFilter} | ConvertFrom-String -PropertyNames folder, totest
  10. $result = 0 # set no error by default
  11. # get the actual folder
  12. $main_path = Get-Location | select -ExpandProperty "Path"
  13. #read the array to show if to be tested !
  14. foreach ($line in $arrayFromFile) {
  15. # write-Host $line
  16. if ($line.totest -eq "yes") {
  17. $path = $line.folder
  18. set-location $main_path\$path
  19. $myPath = Get-Location
  20. # Write-Host $myPath
  21. # trigger terratest for files
  22. Go test ./...
  23. }
  24. if ($false -eq $?)
  25. {
  26. $result = 1
  27. }
  28. }
  29. # back to school :D
  30. set-location $main_path
  31. if ($result -eq 1)
  32. {
  33. Write-Error "Msbuild exit code indicate test failure."
  34. Write-Host "##vso[task.logissue type=error]Msbuild exit code indicate test failure."
  35. exit(1)
  36. }

the code

  1. if ($false -eq $?)
  2. {
  3. $result = 1
  4. }

is usefull to make the pipeline fail on test error without escaping the other tests.

Terratests

  1. # this file lists all the modules to be tested in the "Tests_Unit_ConfigHelpers" repository.
  2. # it us used by the "run_terratest.ps1" powershell script to trigger terratest for each test.
  3. #
  4. # Any line that doesn't begin with a '#' character and isn't empty is treated
  5. # as a path relative to the top of the repository that has a module in it.
  6. # The 'tobetested' field specifies whether this is a module that have to be tested.
  7. #
  8. # this file is based on https://github.com/google/go-cloud/blob/master/allmodules
  9. # module-directory tobetested
  10. azure_constants yes
  11. configure_app_srv_etc yes
  12. configure_frontdoor_etc yes
  13. configure_hostnames yes
  14. constants yes
  15. FrontEnd_AppService_slots/_main yes
  16. FrontEnd_AppService_slots/settings yes
  17. merge_maps_of_strings yes
  18. name yes
  19. name_template yes
  20. network/hostname_generator yes
  21. network/hostnames_generator yes
  22. replace_2vars_into_string_etc yes
  23. replace_var_into_string_etc yes
  24. sorting_map_with_an_other_map yes

And the change in each terratest folder is that I will add the go.mod and go.sum files :

  1. $ go mod init mytest
  2. go: creating new go.mod: module mytest
  3. go: to add module requirements and sums:
  4. go mod tidy

and

  1. $ go mod tidy
  2. # link each of the go modules needed for your terratest module

so, with that, the go test ./... from the powershell script will downlaod the needed go modules and run the test for that particulary test.

Thanks for reading and vote if you think that can help Issue installing Terratest using Go Task's Yaml Azure pipeline – issue triggering terratest tests in sub-folder

huangapple
  • 本文由 发表于 2022年3月23日 21:13:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/71587871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定