-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDeploy-FinOpsHub.Tests.ps1
137 lines (116 loc) · 6.01 KB
/
Deploy-FinOpsHub.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
& "$PSScriptRoot/../Initialize-Tests.ps1"
InModuleScope 'FinOpsToolkit' {
Describe 'Deploy-FinOpsHub' {
BeforeAll {
function Get-AzResourceGroup {}
function New-AzResourceGroup {}
function New-AzResourceGroupDeployment {}
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$hubName = 'ftk-test-Deploy-FinOpsHub'
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$rgName = 'ftk-test'
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
$location = 'eastus'
}
Context "WhatIf" {
It 'Should run without error' {
# Arrange
Mock -CommandName 'Test-ShouldProcess' { return $false }
Mock -CommandName 'Get-AzResourceGroup' { return $null }
Mock -CommandName 'Initialize-FinOpsHubDeployment' { }
# Act
Deploy-FinOpsHub -WhatIf -Name $hubName -ResourceGroupName $rgName -Location $location
# Assert
Assert-MockCalled -CommandName 'Initialize-FinOpsHubDeployment' -Times 1 -ParameterFilter { $WhatIf -eq $true }
@('CreateResourceGroup', 'CreateTempDirectory', 'DownloadTemplate', 'DeployFinOpsHub') | ForEach-Object {
Assert-MockCalled -CommandName 'Test-ShouldProcess' -Times 1 -ParameterFilter { $Action -eq $_ }
}
}
}
Context 'Initialize' {
It 'Should call Initialize-FinOpsHubDeployment' {
# Arrange
Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $rgName }
Mock -CommandName 'Initialize-FinOpsHubDeployment'
Mock -CommandName 'Test-ShouldProcess' -MockWith { return $false }
# Act
Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location
# Assert
Assert-MockCalled -CommandName 'Initialize-FinOpsHubDeployment' -Times 1
}
}
Context 'Download template' {
It 'Should save the template from GitHub' -Skip {
}
It 'Should clean up template after deployment' -Skip {
}
}
Context 'Deploy' {
It 'Should deploy the template' {
# Arrange
Mock -CommandName 'Get-AzResourceGroup' -MockWith { return $rgName }
Mock -CommandName 'New-AzResourceGroupDeployment'
Mock -CommandName 'Test-ShouldProcess' -MockWith { return $Action -eq 'DeployFinOpsHub' }
# Act
Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location
# Assert
Assert-MockCalled -CommandName 'New-AzResourceGroupDeployment' -Times 1
}
It 'Should add tags to the deployment' -Skip {
}
It 'Should deploy' -Skip {
}
}
Context 'Old tests' {
BeforeAll {
Mock -CommandName 'Get-AzResourceGroup' -MockWith { return @{ ResourceGroupName = $rgName } }
Mock -CommandName 'New-AzResourceGroup'
Mock -CommandName 'Save-FinOpsHubTemplate'
}
It 'Should throw if template file is not found' {
Mock -CommandName 'Get-ChildItem'
{ Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location -Version 'latest' } | Should -Throw
Assert-MockCalled -CommandName 'Get-ChildItem' -Times 1
}
Context 'More' {
BeforeAll {
$templateFile = Join-Path -Path $env:temp -ChildPath 'FinOps\finops-hub-v1.0.0\main.bicep'
Mock -CommandName 'Get-ChildItem' -MockWith { return @{ FullName = $templateFile } }
Mock -CommandName 'New-AzResourceGroupDeployment'
}
It 'Should deploy the template without throwing' {
{ Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location -Version 'latest' } | Should -Not -Throw
Assert-MockCalled -CommandName 'Get-ChildItem' -Times 1
Assert-MockCalled -CommandName 'New-AzResourceGroupDeployment' -ParameterFilter { @{ TemplateFile = $templateFile } } -Times 1
}
It 'Should deploy the template with tags' {
{ Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location -Tags @{ Test = 'Tag' } -Version 'latest' } | Should -Not -Throw
Assert-MockCalled -CommandName 'Get-ChildItem' -Times 1
Assert-MockCalled -CommandName 'New-AzResourceGroupDeployment' -ParameterFilter {
@{
TemplateParameterObject = @{
tags = @{
Test = 'Tag'
}
}
}
} -Times 1
}
It 'Should deploy the template with StorageSku' {
$storageSku = 'Premium_ZRS'
{ Deploy-FinOpsHub -Name $hubName -ResourceGroup $rgName -Location $location -StorageSku $storageSku -Version 'latest' } | Should -Not -Throw
Assert-MockCalled -CommandName 'Get-ChildItem' -Times 1
Assert-MockCalled -CommandName 'New-AzResourceGroupDeployment' -ParameterFilter {
@{
TemplateParameterObject = @{
storageSku = $storageSku
}
}
} -Times 1
}
}
}
}
}