-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfIntegration.tests.ps1
103 lines (81 loc) · 3.45 KB
/
tfIntegration.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
function Get-ResourceState($address) {
# Get state and resources after `terraform apply`
$tfState = terraform show -json | ConvertFrom-Json
$resources = $tfState.values.root_module.resources
return $resources | Where-Object { $_.address -eq $address }
}
function Get-Project {
return Get-ResourceState "azuredevops_project.test"
}
function Get-Repository {
return Get-ResourceState "azuredevops_git_repository.test"
}
function Get-VariableGroup {
return Get-ResourceState "azuredevops_variable_group.test"
}
Describe "Terraform Deployment" -Tag 'Deploy' {
Context "clean tfstate" {
It "remove tfstate" {
$tfStatePath = "./terraform.tfstate"
if (Test-Path $tfStatePath -PathType leaf) {
Remove-Item $tfStatePath -Force
}
}
It "terraform initialize" {
terraform init
}
It "terraform apply" {
$tfResult = terraform apply -auto-approve
$LASTEXITCODE | Should -Be 0
Write-Host $tfResult
}
It "returns state for all resources" {
Get-Project | Should -Not -BeNullOrEmpty
Get-Repository | Should -Not -BeNullOrEmpty
Get-VariableGroup | Should -Not -BeNullOrEmpty
}
It "returns a valid repostiory for the repository ID" {
$repository = Get-Repository
$repository.values.id | Should -Not -BeNullOrEmpty
az repos show -r $repository.values.id -p $repository.values.project_id --org ${env:AZDO_ORG_SERVICE_URL}
$LASTEXITCODE | Should -Be 0
}
It "returns a valid variableGroup for the variableGroup ID" {
$variableGroup = Get-VariableGroup
$variableGroup.values.id | Should -Not -BeNullOrEmpty
az pipelines variable-group show --id $variableGroup.values.id -p $variableGroup.values.project_id --org ${env:AZDO_ORG_SERVICE_URL}
$LASTEXITCODE | Should -Be 0
}
It "returns an empty plan when re-run" {
# Run a terraform plan and check no changes are detected
# `-detailed-exitcode` will cause the command to exit with 0 exit code
# only if there are no diffs in the plan
# https://www.terraform.io/docs/commands/plan.html#detailed-exitcode
#
# If this test fails it shows an issue with the `read` command returning different data between calls.
terraform plan -out plan.tfstate -detailed-exitcode
if ($LASTEXITCODE -ne 0) {
Write-Host "Detected terraform changes:"
terraform show plan.tfstate
}
$LASTEXITCODE | Should -Be 0 -Because "plan should show no changes"
}
}
}
Describe "Terraform Destroy" -Tag 'Destroy' {
Context "existing tfstate" {
It "ensure we have an existing terraform deployment" {
"./terraform.tfstate" | Should -Exist
}
It "terraform destroy" {
$tfResult = terraform destroy -auto-approve
$LASTEXITCODE | Should -Be 0
Write-Host $tfResult
}
It "clean up terraform files" {
Remove-Item ./terraform.tfstate -Force
Remove-Item ./terraform.tfstate.backup -Force
Remove-Item ./plan.tfstate -Force
}
}
}