Tests/Unit/MSFT_NetAdapterAdvancedProperty.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
$script:DSCModuleName = 'NetworkingDsc'
$script:DSCResourceName = 'MSFT_NetAdapterAdvancedProperty'

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
{
    #region Pester Tests
    InModuleScope $script:DSCResourceName {

        $TestJumboPacket9014 = @{
            NetworkAdapterName    = 'Ethernet'
            RegistryKeyword = "*JumboPacket"
            RegistryValue = 9014
        }

        $TestJumboPacket1514 = @{
            NetworkAdapterName    = 'Ethernet'
            RegistryKeyword = '*JumboPacket'
            RegistryValue = 1514
        }

        $TestAdapterNotFound = @{
            NetworkAdapterName    = 'Ethe'
            RegistryKeyword = "*JumboPacket"
            RegistryValue = 1514
        }

        function Get-NetAdapterAdvancedProperty { }

        Describe 'MSFT_NetAdapterAdvancedProperty\Get-TargetResource' -Tag 'Get' {

            Context 'Adapter exist and JumboPacket is enabled 9014' {
                Mock Get-NetAdapterAdvancedProperty -Verbose -MockWith {
                    @{
                        RegistryValue = $TestJumboPacket9014.RegistryValue
                        RegistryKeyword = $TestJumboPacket9014.RegistryKeyword
                    }
                }

                It 'Should return the JumboPacket size' {
                    $result = Get-TargetResource @TestJumboPacket9014
                    $result.RegistryValue | Should -Be $TestJumboPacket9014.RegistryValue
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                }
            }

            Context 'Adapter exist and JumboPacket is 1514' {
                Mock Get-NetAdapterAdvancedProperty -Verbose -MockWith {
                    @{
                        RegistryValue = $TestJumboPacket1514.RegistryValue
                        RegistryKeyword = $TestJumboPacket1514.RegistryKeyword
                    }
                }

                It 'Should return the JumboPacket size' {
                    $result = Get-TargetResource @TestJumboPacket1514
                    $result.RegistryValue | Should -Be $TestJumboPacket1514.RegistryValue
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                }
            }

            Context 'Adapter does not exist' {

                Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith { throw 'Network adapter not found' }

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

                It 'Should throw an exception' {
                    { Get-TargetResource @TestAdapterNotFound } | Should -Throw $errorRecord
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                }
            }

            Describe 'MSFT_NetAdapterAdvancedProperty\Set-TargetResource' -Tag 'Set' {

                Context 'Adapter exist, JumboPacket is 9014, no action required' {
                    Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
                        @{
                            RegistryValue = $TestJumboPacket9014.RegistryValue
                            RegistryKeyword = $TestJumboPacket9014.RegistryKeyword
                        }
                    }
                    Mock -CommandName Set-NetAdapterAdvancedProperty

                    It 'Should not throw an exception' {
                        { Set-TargetResource @TestJumboPacket9014 } | Should -Not -Throw
                    }

                    It 'Should call all mocks' {
                        Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                        Assert-MockCalled -CommandName Set-NetAdapterAdvancedProperty -Exactly -Time 0
                    }
                }

                Context 'Adapter exist, JumboPacket is 9014, should be 1514' {
                    Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
                        @{
                            RegistryValue = $TestJumboPacket9014.RegistryValue
                            RegistryKeyword = $TestJumboPacket9014.RegistryKeyword
                        }
                    }
                    Mock -CommandName Set-NetAdapterAdvancedProperty

                    It 'Should not throw an exception' {
                        { Set-TargetResource @TestJumboPacket1514 } | Should -Not -Throw
                    }

                    It 'Should call all mocks' {
                        Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                        Assert-MockCalled -CommandName Set-NetAdapterAdvancedProperty -Exactly -Time 1
                    }
                }

                Context 'Adapter exist, JumboPacket is 1514, should be 9014' {
                    Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
                        @{
                            RegistryValue = $TestJumboPacket1514.RegistryValue
                            RegistryKeyword = $TestJumboPacket1514.RegistryKeyword
                        }
                    }
                    Mock -CommandName Set-NetAdapterAdvancedProperty

                    It 'Should not throw an exception' {
                        { Set-TargetResource @TestJumboPacket9014 } | Should -Not -Throw
                    }

                    It 'Should call all mocks' {
                        Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                        Assert-MockCalled -CommandName Set-NetAdapterAdvancedProperty -Exactly -Time 1
                    }
                }

                # Adapter
                Context 'Adapter does not exist' {
                    Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith { throw 'Network adapter not found' }

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

                    It 'Should throw an exception' {
                        { Set-TargetResource @TestAdapterNotFound } | Should -Throw $errorRecord
                    }

                    It 'Should call all mocks' {
                        Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly -Time 1
                    }
                }
            }
        }

        Describe 'MSFT_NetAdapterAdvancedProperty\Test-TargetResource' -Tag 'Test' {

            # JumboPacket
            Context 'Adapter exist, JumboPacket is 9014, no action required' {
                Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
                    @{
                        RegistryValue = $TestJumboPacket9014.RegistryValue
                        RegistryKeyword = $TestJumboPacket9014.RegistryKeyword
                    }
                }

                It 'Should return true' {
                    Test-TargetResource @TestJumboPacket9014 | Should -Be $true
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly 1
                }
            }

            Context 'Adapter exist, JumboPacket is 9014 should be 1514' {
                Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith {
                    @{
                        RegistryValue = $TestJumboPacket9014.RegistryValue
                        RegistryKeyword = $TestJumboPacket9014.RegistryKeyword
                    }
                }

                It 'Should return false' {
                    Test-TargetResource @TestJumboPacket1514 | Should -Be $false
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly 1
                }
            }


            # Adapter
            Context 'Adapter does not exist' {
                Mock -CommandName Get-NetAdapterAdvancedProperty -MockWith { throw 'Network adapter not found' }

                It 'Should throw an exception' {
                    { Test-TargetResource @TestAdapterNotFound } | Should -Throw
                }

                It 'Should call all mocks' {
                    Assert-MockCalled -CommandName Get-NetAdapterAdvancedProperty -Exactly 1
                }
            }
        }
    }
    #endregion
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}