Tests/Unit/MSFT_NetconnectionProfile.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
$script:DSCModuleName      = 'NetworkingDsc'
$script:DSCResourceName    = 'MSFT_NetConnectionProfile'

Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global

#region HEADER
# Unit Test Template Version: 1.1.0
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
    (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
    & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
}

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
$TestEnvironment = Initialize-TestEnvironment `
    -DSCModuleName $script:DSCModuleName `
    -DSCResourceName $script:DSCResourceName `
    -TestType Unit
#endregion HEADER

# Begin Testing
try
{
    InModuleScope $script:DSCResourceName {
        # Create the Mock Objects that will be used for running tests
        $mockNetAdapter = [PSCustomObject] @{
            Name = 'TestAdapter'
        }

        $mockNetConnnectionProfileAll = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            NetworkCategory  = 'Public'
            IPv4Connectivity = 'Internet'
            IPv6Connectivity = 'Internet'
        }

        $testValidInterfaceAliasOnlyPassed = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
        }

        $testNetworkCategoryMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            NetworkCategory  = 'Public'
        }

        $testNetworkCategoryNoMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            NetworkCategory  = 'Private'
        }

        $testIPv4ConnectivityMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            IPv4Connectivity = 'Internet'
        }

        $testIPv4ConnectivityNoMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            IPv4Connectivity = 'Disconnected'
        }

        $testIPv6ConnectivityMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            IPv6Connectivity = 'Internet'
        }

        $testIPv6ConnectivityNoMatches = [PSObject] @{
            InterfaceAlias   = $mockNetAdapter.Name
            IPv6Connectivity = 'Disconnected'
        }

        Describe 'MSFT_NetConnectionProfile\Get-TargetResource' -Tag 'Get' {
            Mock -CommandName Get-NetConnectionProfile {
                return $mockNetConnnectionProfileAll
            }

            $result = Get-TargetResource -InterfaceAlias $mockNetAdapter.Name

            It 'Should return the correct values' {
                $result.InterfaceAlias   | Should -Be $mockNetConnnectionProfileAll.InterfaceAlias
                $result.NetworkCategory  | Should -Be $mockNetConnnectionProfileAll.NetworkCategory
                $result.IPv4Connectivity | Should -Be $mockNetConnnectionProfileAll.IPv4Connectivity
                $result.IPv6Connectivity | Should -Be $mockNetConnnectionProfileAll.IPv6Connectivity
            }
        }

        Describe 'MSFT_NetConnectionProfile\Test-TargetResource' -Tag 'Test' {
            BeforeEach {
                Mock -CommandName Get-TargetResource -MockWith {
                    return $mockNetConnnectionProfileAll
                }

                Mock -CommandName Assert-ResourceProperty
            }

            Context 'NetworkCategory matches' {
                It 'Should return false' {
                    Test-TargetResource @testNetworkCategoryMatches | should -be $true
                }
            }

            Context 'NetworkCategory does not match' {
                It 'Should return false' {
                    Test-TargetResource @testNetworkCategoryNoMatches | should -be $false
                }
            }

            Context 'IPv4Connectivity matches' {
                It 'Should return false' {
                    Test-TargetResource @testIPv4ConnectivityMatches | should -be $true
                }
            }

            Context 'IPv4Connectivity does not match' {
                It 'Should return false' {
                    Test-TargetResource @testIPv4ConnectivityNoMatches | should -be $false
                }
            }

            Context 'IPv6Connectivity matches' {
                It 'Should return false' {
                    Test-TargetResource @testIPv6ConnectivityMatches | should -be $true
                }
            }

            Context 'IPv6Connectivity does not match' {
                It 'Should return false' {
                    Test-TargetResource @testIPv6ConnectivityNoMatches | should -be $false
                }
            }
        }

        Describe 'MSFT_NetConnectionProfile\Set-TargetResource' -Tag 'Set' {
            It 'Should call all the mocks' {
                Mock -CommandName Set-NetConnectionProfile
                Mock -CommandName Assert-ResourceProperty

                Set-TargetResource @testNetworkCategoryMatches

                Assert-MockCalled -CommandName Set-NetConnectionProfile
            }
        }

        Describe 'MSFT_NetConnectionProfile\Assert-ResourceProperty' {
            Context 'Invoking with bad interface alias' {
                Mock -CommandName Get-NetAdapter

                It 'Should throw testValidInterfaceAliasOnlyPassed exception' {
                    $errorRecord = Get-InvalidOperationRecord `
                        -Message ($script:localizedData.InterfaceNotAvailableError -f $testValidInterfaceAliasOnlyPassed.InterfaceAlias)

                    { Assert-ResourceProperty @testValidInterfaceAliasOnlyPassed } | Should -Throw $errorRecord
                }
            }

            Context 'Invoking with valid interface alias but all empty parameters' {
                Mock -CommandName Get-NetAdapter -MockWith { return $mockNetAdapter }

                $errorRecord = Get-InvalidOperationRecord `
                    -Message ($script:localizedData.ParameterCombinationError)

                It 'Should not ParameterCombinationError exception' {
                    { Assert-ResourceProperty @testValidInterfaceAliasOnlyPassed } | Should -Throw $errorRecord
                }
            }

            Context 'Invoking with valid interface alias and one NetworkCategory' {
                Mock -CommandName Get-NetAdapter -MockWith { return $mockNetAdapter }

                It 'Should not throw an exception' {
                    { Assert-ResourceProperty @testNetworkCategoryMatches } | Should -Not -Throw
                }
            }

            Context 'Invoking with valid interface alias and one IPv4Connectivity' {
                Mock -CommandName Get-NetAdapter -MockWith { return $mockNetAdapter }

                It 'Should not throw an exception' {
                    { Assert-ResourceProperty @testIPv4ConnectivityMatches } | Should -Not -Throw
                }
            }

            Context 'Invoking with valid interface alias and one IPv6Connectivity' {
                Mock -CommandName Get-NetAdapter -MockWith { return $mockNetAdapter }

                It 'Should not throw an exception' {
                    { Assert-ResourceProperty @testIPv6ConnectivityMatches } | Should -Not -Throw
                }
            }
        }
    } #end InModuleScope $DSCResourceName
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}