-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathbuild-image.Tests.ps1
More file actions
103 lines (83 loc) · 4.29 KB
/
Copy pathbuild-image.Tests.ps1
File metadata and controls
103 lines (83 loc) · 4.29 KB
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
Describe "build-image.ps1 parameter sets" {
BeforeAll {
$scriptPath = (Resolve-Path (Join-Path $PSScriptRoot "..\build-image.ps1")).Path
$command = Get-Command -Name $scriptPath
}
It "defines exactly two parameter sets" {
$command.ParameterSets | Should -HaveCount 2
}
It "requires temp resource group parameters in TempResourceGroup set" {
$tempSet = $command.ParameterSets | Where-Object Name -eq "TempResourceGroup"
$tempSet | Should -Not -BeNullOrEmpty
($tempSet.Parameters | Where-Object Name -eq "Location") | Should -Not -BeNullOrEmpty
($tempSet.Parameters | Where-Object Name -eq "Location").IsMandatory | Should -BeTrue
($tempSet.Parameters | Where-Object Name -eq "TempResourceGroupName") | Should -Not -BeNullOrEmpty
($tempSet.Parameters | Where-Object Name -eq "TempResourceGroupName").IsMandatory | Should -BeTrue
$tempSet.Parameters.Name | Should -Not -Contain "ExistingResourceGroupName"
}
It "requires existing resource group parameter in ExistingResourceGroup set" {
$existingSet = $command.ParameterSets | Where-Object Name -eq "ExistingResourceGroup"
$existingSet | Should -Not -BeNullOrEmpty
($existingSet.Parameters | Where-Object Name -eq "ExistingResourceGroupName") | Should -Not -BeNullOrEmpty
($existingSet.Parameters | Where-Object Name -eq "ExistingResourceGroupName").IsMandatory | Should -BeTrue
$existingSet.Parameters.Name | Should -Not -Contain "Location"
$existingSet.Parameters.Name | Should -Not -Contain "TempResourceGroupName"
}
}
Describe "build-image.ps1 switch" {
BeforeAll {
$scriptPath = (Resolve-Path (Join-Path $PSScriptRoot "..\build-image.ps1")).Path
Mock -CommandName Write-Host
$global:packerInvocations = @()
Mock -CommandName packer -MockWith {
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]] $arguments
)
$global:packerInvocations += ,$arguments
"mocked packer output"
}
}
Context "TempResourceGroup" {
It "calls packer build with temp resource group parameters" {
& $scriptPath -TemplatePath ".\" `
-BuildTemplateName "template.TestBuild" `
-TempResourceGroupName "TestTempRG" `
-ClientId "TestClientId" `
-Location "TestLocation" `
-ImageName "TestImage" `
-ImageResourceGroupName "TestImageRG" `
-SubscriptionId "TestSubId" `
-TenantId "TestTenantId" `
-ImageOS "TestOS" | Out-Null
Should -Invoke -CommandName Write-Host -Times 1 -Exactly -ParameterFilter {
$Object -eq "Use temporary resource group TestTempRG"
}
Should -Invoke -CommandName packer -Times 1 -Exactly -ParameterFilter {
$arguments -contains "temp_resource_group_name=TestTempRG" -and
$arguments -contains "location=TestLocation" -and
$arguments -contains "-var"
}
}
}
Context "ExistingResourceGroup" {
It "calls packer build with existing resource group parameters" {
& $scriptPath -TemplatePath ".\" `
-BuildTemplateName "template.TestBuild" `
-ExistingResourceGroupName "TestExistingRG" `
-ClientId "TestClientId" `
-ImageName "TestImage" `
-ImageResourceGroupName "TestImageRG" `
-SubscriptionId "TestSubId" `
-TenantId "TestTenantId" `
-ImageOS "TestOS" | Out-Null
Should -Invoke -CommandName Write-Host -Times 1 -Exactly -ParameterFilter {
$Object -eq "Use existing resource group TestExistingRG"
}
Should -Invoke -CommandName packer -Times 1 -Exactly -ParameterFilter {
$arguments -contains "build_resource_group_name=TestExistingRG" -and
$arguments -contains "-var"
}
}
}
}