summaryrefslogtreecommitdiff
path: root/scripts/acctest.ps1
blob: 5148e20a37ff1bd9ea7ba7008058b6e030e726c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[CmdletBinding()]
param (
    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string]
    $TestFilter = '^TestAcc',

    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string[]]
    $Tag = 'all',

    [Parameter()]
    [ValidateSet('', 'readonly', 'vendor')]
    [string]
    $GoMod = 'vendor'
)

$script:PSDefaultParameterValues = @{
    '*:Confirm'           = $false
    '*:ErrorAction'       = 'Stop'
}

. (Join-Path -Path $PSScriptRoot -ChildPath 'commons.ps1' -Resolve)

Write-Host "Executing acceptance tests"
Push-Location -Path $SOURCE_DIR
try {
    # This is similar to the unit test command aside from the following:
    #   - TF_ACC=1 is a flag that will enable the acceptance tests. This flag is
    #     documented here:
    #       https://www.terraform.io/docs/extend/testing/acceptance-tests/index.html#running-acceptance-tests
    #
    #   - A `-run` parameter is used to target *only* tests starting with `TestAcc`. This prefix is
    #     recommended by Hashicorp and is documented here:
    #       https://www.terraform.io/docs/extend/testing/acceptance-tests/index.html#test-files
    #
    # Using build tags as test filter: https://stackoverflow.com/a/24036237
    $env:TF_ACC=1
    $env:TF_SCHEMA_PANIC_ON_ERROR=1
    $env:GO111MODULE='on'
  
    $argv = @(
        'test',
        "-mod=$(if ('' -ne $GoMod) { $GoMod } else { $null })",
        '-v'
    )
    if ($TestFilter) {
        $argv += @('-run', $TestFilter)
    }
    if ($Tag -and 0 -lt $Tag.Length) {
        $argv += @('-tags', [string]::Join(' ', $Tag))
    }
    go @argv ./...
    if ($LASTEXITCODE) {
        throw "Build finished in error due to failed tests"
    }
}
finally {
    'TF_ACC', 'TF_SCHEMA_PANIC_ON_ERROR', 'GO111MODULE' `
    | ForEach-Object -Process {Remove-Item -Path "Env:$_" }
    Pop-Location
}