tests/Get-DbaDiskSpace.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
$commandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$commandName Integration Tests" -Tags "IntegrationTests" {
    Context "Disks are properly retrieved" {
        $results = Get-DbaDiskSpace -ComputerName $env:COMPUTERNAME
        It "returns at least the system drive" {
            $results.Name -contains "$env:SystemDrive\" | Should Be $true
        }

        $results = Get-DbaDiskSpace -ComputerName $env:COMPUTERNAME | Where-Object Name -eq "$env:SystemDrive\"
        It "has some valid properties" {
            $results.BlockSize -gt 0 | Should Be $true
            $results.SizeInGB -gt 0 | Should Be $true
        }
    }

    Context "CheckForSql works with mount points" {
        Mock -ModuleName 'dbatools' -CommandName 'Connect-SqlInstance' -ParameterFilter { $SqlInstance -in ('MadeUpServer', 'MadeUpServer\MadeUpInstance') } -MockWith {
            $object = [PSCustomObject] @{
                Version = @{
                    Major = 13
                }
            }
            $object | Add-Member -Name 'Query' -MemberType ScriptMethod -Value {
                return @{
                    SqlDisk = @('D:\Data\')
                }
            }
            return $object
        }

        Mock -ModuleName 'dbatools' -CommandName 'Get-DbaService' -ParameterFilter { $ComputerName.ComputerName -eq 'MadeUpServer' } -MockWith {
            return @(
                @{
                    ComputerName = 'MadeUpServer'
                    ServiceName = 'MSSQLSERVER'
                    ServiceType = 'Engine'
                    InstanceName = 'MSSQLSERVER'
                    DisplayName = 'SQL Server (MSSQLSERVER)'
                    StartName = 'FAKEDOMAIN\FAKEUSER'
                    State = 'Running'
                    StartMode = 'Automatic'
                },
                @{
                    ComputerName = 'MadeUpServer'
                    ServiceName = 'MSSQLSERVER$MADEUPINSTANCE'
                    ServiceType = 'Engine'
                    InstanceName = 'MadeUpInstance'
                    DisplayName = 'SQL Server (MSSQLSERVER)'
                    StartName = 'FAKEDOMAIN\FAKEUSER'
                    State = 'Running'
                    StartMode = 'Automatic'
                }
            )
        }

        Mock -ModuleName 'dbatools' -CommandName 'Get-DbaCmObject' -ParameterFilter { $ComputerName::InputObject -eq 'MadeUpServer' -and $Query -like '*Win32_Volume*' } -MockWith {
            return @(
                @{
                    Name = 'D:\Data\'
                    Label = 'Log'
                    Capacity = 32209043456
                    Freespace = 11653545984
                    BlockSize = 65536
                    FileSystem = 'NTFS'
                    DriveType = 3
                    DriveLetter = ''
                },
                @{
                    Name = 'C:\'
                    Label = 'OS'
                    Capacity = 32209043456
                    Freespace = 11653545984
                    BlockSize = 4096
                    FileSystem = 'NTFS'
                    DriveType = 2
                    DriveLetter = 'C:'
                }
            )
        }

        It -Skip "SQL Server drive is found in there somewhere" {
            $results = Get-DbaDiskSpace -ComputerName 'MadeUpServer' -CheckForSql -EnableException
            $true | Should -BeIn $results.IsSqlDisk
        }

        Assert-MockCalled -ModuleName 'dbatools' -CommandName 'Get-DbaCmObject' -Times 0
        Assert-MockCalled -ModuleName 'dbatools' -CommandName 'Get-DbaService' -Times 0
        Assert-MockCalled -ModuleName 'dbatools' -CommandName 'Connect-SqlInstance' -Times 0
    }

    Context "CheckForSql returns IsSqlDisk property with a value (likely false)" {
        Mock -ModuleName 'dbatools' -CommandName 'Get-DbaCmObject' -ParameterFilter { $Query -like '*Win32_Volume*' } -MockWith {
            return @(
                @{
                    Name = 'D:\Data\'
                    Label = 'Log'
                    Capacity = 32209043456
                    Freespace = 11653545984
                    BlockSize = 65536
                    FileSystem = 'NTFS'
                    DriveType = 3
                    DriveLetter = ''
                },
                @{
                    Name = 'C:\'
                    Label = 'OS'
                    Capacity = 32209043456
                    Freespace = 11653545984
                    BlockSize = 4096
                    FileSystem = 'NTFS'
                    DriveType = 2
                    DriveLetter = 'C:'
                },
                @{
                    Name = 'T:\'
                    Label = 'Data'
                    Capacity = 32209043456
                    Freespace = 11653545984
                    BlockSize = 4096
                    FileSystem = 'NTFS'
                    DriveType = 2
                    DriveLetter = 'T:'
                }
            )
        }

        It "SQL Server drive is not found in there somewhere" {
            $results = Get-DbaDiskSpace -ComputerName $env:COMPUTERNAME -CheckForSql -WarningAction SilentlyContinue
            $false | Should BeIn $results.IsSqlDisk
        }
    }
}