Public/Invoke-Terrafun.Tests.ps1

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Import-Module -Force "./Terrafun/Terrafun.psd1"

BeforeAll {
}

Describe 'Invoke-Terrafun.Acceptance' -Tag "Acceptance" {

    InModuleScope "Terrafun" {

        BeforeDiscovery  {
            # run acceptance tests on n-3 major.minor versions
            # include all patch releases within those versions
            # e.g if latest version is 1.5.0 then all of the current versions are in scope
            # 1.5.x (n)
            # 1.4.x (n-1)
            # 1.3.x (n-2)
            # 1.2.x (n-3)
            $AllVersions = Invoke-Terrafun version list
            $NMinus3Versions = [version[]]$AllVersions | Group-Object -property Major, Minor | Select-Object -Last 4
            $TargetVersions = [string[]]$NMinus3Versions.Group
        }

        BeforeEach {
            Push-Location
            Set-Location -Path "TestDrive:\"
            Remove-Item ".terraformversion" -Force -ErrorAction SilentlyContinue
        }

        AfterEach {
            Pop-Location
        }

        It 'Defaults to the latest version' {

            # setup
            $ConfigPath = Join-Path -path $HOME -ChildPath ".terrafun/config.json"
            Remove-item $ConfigPath -ErrorAction SilentlyContinue -Force

            # execute
            $Version = Invoke-Terrafun version -json | ConvertFrom-Json

            # assert
            $Version.terraform_outdated | Should -Be $False
        }

        It 'Lists the available versions' {

            # execute
            $Output = Invoke-Terrafun version list

            # assert
            $Output.count | Should -BeGreaterThan 100
        }

        It 'Sets the correct version inside the current directory' {

            # execute
            Invoke-Terrafun version 1.0.0
            Invoke-Terrafun version global 1.0.1
            $Content = Get-Content ".terraformversion"
            $Version = Invoke-Terrafun version -json | ConvertFrom-Json

            # assert
            $Content | Should -Be "1.0.0"
            $Version.terraform_version | Should -Be "1.0.0"
        }

        It 'Sets the correct version in the user profile' {

            # execute
            Invoke-Terrafun version global 1.0.0
            $Version = Invoke-Terrafun version -json | ConvertFrom-Json

            # assert
            $Version.terraform_version | Should -Be "1.0.0"
        }

        It 'Uses the version set in the current directory when a user profile is present' {

            # setup
            Invoke-Terrafun version 1.0.0
            Invoke-Terrafun version global 1.0.1

            # execute
            $Content = Get-Content ".terraformversion"
            $Version = Invoke-Terrafun version -json | ConvertFrom-Json

            # assert
            $Content | Should -Be "1.0.0"
            $Version.terraform_version | Should -Be "1.0.0"
        }


        It 'Executes the "terraform version" command on version <_>' -ForEach ($TargetVersions) {

            # execute
            Invoke-Terrafun version $_
            $Version = Invoke-Terrafun version -json | ConvertFrom-Json

            # assert
            $Version.terraform_version | Should -Be $_
        }

        It 'Successfully executes the "terraform init" command on version <_>' -ForEach ($TargetVersions) {

            # setup
            $Content = @"
resource "local_file" "test" {
    content = "apply_result_content"
    filename = "apply_result_filename"
}
"@

            $TestDirectory = "version-$_"
            mkdir $TestDirectory
            Set-Location $TestDirectory
            Set-Content -Path "main.tf" -Value $Content

            # execute
            Invoke-Terrafun version $_
            $Output = Invoke-Terrafun init -no-color | Out-String

            # assert
            $Output | Should -BeLike "*Terraform has been successfully initialized!*"
        }

        It 'Successfully executes the "terraform plan" command on version <_>' -ForEach ($TargetVersions) {

            # setup
            $TestDirectory = "version-$_"
            Set-Location $TestDirectory

            # execute
            Invoke-Terrafun version $_
            $PlanOutput = Invoke-Terrafun plan -no-color -out plan.tfstate | Out-String
            $PlanContent = Invoke-Terrafun show -json plan.tfstate | Convertfrom-json

            # assert
            $PlanContent.resource_changes.count | Should -BeExactly 1
            $PlanOutput | Should -BeLike "*Plan: 1 to add, 0 to change, 0 to destroy*"
        }

        It 'Successfully executes the "terraform apply" command on version <_>' -ForEach ($TargetVersions) {

            # setup
            $TestDirectory = "version-$_"
            Set-Location $TestDirectory

            # execute
            Invoke-Terrafun version $_
            $Output = Invoke-Terrafun apply --auto-approve -no-color | Out-String
            $Content = Get-Content -path "apply_result_filename"

            # assert
            $Content | Should -Be "apply_result_content"
            $Output | Should -BeLike "*Apply complete! Resources: 1 added, 0 changed, 0 destroyed*"
        }

    }


}