This repository was archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPestertest.tests.ps1
More file actions
150 lines (125 loc) · 5.81 KB
/
Copy pathPestertest.tests.ps1
File metadata and controls
150 lines (125 loc) · 5.81 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
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
Describe Get-DiskInfo {
Mock Get-Date {
return "201808121230"
} -ParameterFilter {$format -eq "yyyyMMddhhmm"}
Mock Get-Volume {
$result = [pscustomobject]@{
DriveLetter = "C"
Size = 512GB
SizeRemaining = 99.12345GB
HealthStatus = "Healthy"
PSComputername = "bttleuswsnp01"
}
return $result
} -ParameterFilter {$CimSession -match "bttleuswsnp01"}
Mock Get-Volume {
$result = [pscustomobject]@{
DriveLetter = "C"
Size = 256GB
SizeRemaining = 128GB
HealthStatus = "Healthy"
PSComputername = "bttleuswsnp02"
}
return $result
} -ParameterFilter {$CimSession -match "bttleuswsnp02"}
Mock Get-Volume {
$result = [pscustomobject]@{
DriveLetter = "D"
Size = 512GB
SizeRemaining = 100GB
HealthStatus = "Healthy"
PSComputername = "bttleuswsnp01"
}
return $result
} -ParameterFilter {$CimSession -match "bttleuswsnp01" -AND $Drive -eq 'D'}
Mock Get-Volume {
#write-host "Offline mock" -ForegroundColor magenta
write-Error "Failed to connect to Offline"
} -parameterfilter {$cimsession -match "Offline"}
Context "Input" {
$cmd = Get-Command -Name Get-DiskInfo
$attributes = $cmd.parameters["computername"].attributes | where-object {$_.typeid.name -eq 'parameterattribute'}
It "Should have a mandatory parameter for the computername" {
$attributes.Mandatory | Should Be $True
}
It "Should accept parameter input for the computername" {
{Get-Diskinfo -Computername bttleuswsnp01} | Should beTrue
}
It "Should accept multiple computernames from the parameter" {
$r = Get-Diskinfo -Computername bttleuswsnp01, bttleuswsnp02
$r.count | Should be 2
}
It "Should accept positional parameter input for the computername" {
$attributes.position | Should be 0
{get-Diskinfo bttleuswsnp01} | Should beTrue
}
It "Should accept pipeline input by property name for the computername" {
$attributes.ValueFromPipelinebyPropertyName | Should Be $True
{[pscustomobject]@{Computername = "bttleuswsnp01"} | Get-Diskinfo } | Should beTrue
$r = [pscustomobject]@{Computername = "bttleuswsnp01"}, [pscustomobject]@{Computername = "bttleuswsnp01"} | Get-Diskinfo
$r.count | Should be 2
}
It "Should accept pipeline input by value for the computername" {
$attributes.ValueFromPipeline | Should Be $True
{"bttleuswsnp01" | Get-DiskInfo} | Should BeTrue
}
It "Should only accept drives between C and G" {
$driveparam = $cmd.parameters["drive"].attributes | where-object {$_.typeid.name -eq 'ValidatePatternAttribute'}
$driveparam.RegexPattern | Should match "c-g"
{Get-DiskInfo -computername bttleuswsnp01 -Drive D} | Should BeTrue
{Get-DiskInfo -computername bttleuswsnp01 -Drive H} | Should Throw
}
It "Should fail with a bad computername" {
$r = Get-Diskinfo -Computername "offline" -WarningAction "silentlyContinue"
$r | Should BeFalse
}
}
Context "Output" {
$test = Get-DiskInfo -computername bttleuswsnp01
It "Should call Get-Volume" {
Assert-MockCalled 'Get-Volume'
}
It "Should write an object to the pipeline with the computername bttleuswsnp01" {
$test.computername | Should Be "bttleuswsnp01"
}
It "Should write an object to the pipeline with a FreeGB value of a [double] for bttleuswsnp01" {
$test.freeGB | Should BeOfType "double"
}
It "Should write an object to the pipeline with a SizeGB value of an [int] for bttleuswsnp01" {
$test.SizeGB | Should BeofType "int"
$test.SizeGB | Should Be 512
}
It "Should write an object to the pipeline with a PctFree value of an [int] for bttleuswsnp01" {
$test.PctFree | Should BeOfType "int"
$test.PctFree | Should Be 19
}
It "should write an object to the pipeline with a HealthStatus value of 'Healthy' for bttleuswsnp01" {
$test.HealthStatus | Should Be "Healthy"
}
It "bttleuswsnp01 should respond to a connection test"{
(test-netconnection -computername bttleuswsnp01 -port 80 | select -ExpandProperty TcpTestSucceeded) | Should Be $True
}
It "bttleuswsnp02 should respond to a connection test"{
(test-netconnection -computername bttleuswsnp02 -port 80 | select -ExpandProperty TcpTestSucceeded) | Should Be $True
}
It "bttleuswsnp01 should have IIS installed"{
Get-WindowsFeature -ComputerName "bttleuswsnp01" -name "Web-Server" | Should Be $True
}
It "bttleuswsnp02 should have IIS installed"{
Get-WindowsFeature -ComputerName "bttleuswsnp02" -name "Web-Server" | Should Be $True
}
}
Context "Error Handling" {
It "Should fail with a bad folder for the log" {
{Get-DiskInfo -Computername foo -LogPath TestDrive:\foo -ErrorAction stop } | Should Throw
}
It "Should create an error log with a name that iudes YearMonthDayHourMinute" {
Get-DiskInfo -Computername Offline -LogPath TestDrive: -WarningVariable w -WarningAction SilentlyContinue
$log = Get-Item TestDrive:\*.txt
$w | Should Be $true
$w | Should Match "201808121230_DiskInfo_Errors"
$log.length | Should BeGreaterThan 0
$log.name | Should Match "201808121230"
}
}
}