为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Go Task 的 Yaml Azure 管道安装 Terratest 的问题

使用 Go Task 的 Yaml Azure 管道安装 Terratest 的问题

Go
长风秋雁 2022-11-23 19:53:04

我在通过 azure yaml 管道安装 terratest 时遇到了这个问题:


C:\hostedtoolcache\windows\go\1.17.1\x64\bin\go.exe install -v github.com/gruntwork-io/terratest@v0.40.6

go: downloading github.com/gruntwork-io/terratest v0.40.6

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

##[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

Finishing: Install Go Terratest module - v0.40.6


我的安装代码如下:


            - task: Go@0

              displayName: Install Go Terratest module - v$(TERRATEST_VERSION)

              inputs:

                command: custom

                customCommand: install

                arguments: $(TF_LOG) github.com/gruntwork-io/terratest@v$(TERRATEST_VERSION)

                workingDirectory: $(pipeline_artefact_folder_extract)/$(pathToTerraformRootModule)

但是很明显我在使用 terratest 时犯了错误。


查看完整描述

1 回答

?
慕沐林林

TA贡献1749条经验 获得超9个赞

我将再次回答我自己的问题。:D

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

  • -- 治理:1.17.1

  • -- 地形版本:1.1.7

  • -- TERRATEST_VERSION:0.40.6

文件夹层次结构已更改以下内容,关于 terratest 测试:

//img2.sycdn.imooc.com/637e09c20001b26802890373.jpg

我不再尝试导入我的 Terratest 模块。(所以上面的第 1 点很明显)

我现在只需要:

  1. 修改我的每个 terratest 模块

  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 :D

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

    }  

对于使管道因测试错误而失败而不逃避其他测试很有用。


地测


# 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

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


感谢阅读,如果您认为有帮助,请投票:)


查看完整回答
反对 回复 2022-11-23

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信