Tests/Unit/MSFT_IPAddressOption.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
$script:DSCModuleName = 'NetworkingDsc'
$script:DSCResourceName = 'MSFT_IPAddressOption'

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 {
        Describe 'MSFT_IPAddressOption\Get-TargetResource' -Tag 'Get' {
            Context 'Invoked with an existing IP address' {
                Mock -CommandName Get-NetIPAddress -MockWith {
                    [PSCustomObject] @{
                        IPAddress      = '192.168.0.1'
                        InterfaceAlias = 'Ethernet'
                        InterfaceIndex = 1
                        PrefixLength   = [System.Byte] 24
                        AddressFamily  = 'IPv4'
                        SkipAsSource   = $true
                    }
                }

                It 'Should return existing IP options' {
                    $getTargetResourceParameters = @{
                        IPAddress = '192.168.0.1'
                    }
                    $result = Get-TargetResource @getTargetResourceParameters
                    $result.IPAddress    | Should -Be $getTargetResourceParameters.IPAddress
                    $result.SkipAsSource | Should -Be $true
                }
            }
        }

        Describe 'MSFT_IPAddressOption\Set-TargetResource' -Tag 'Set' {
            Context 'Invoked with an existing IP address, SkipAsSource = $false' {
                BeforeEach {
                    Mock -CommandName Get-NetIPAddress -MockWith {
                        [PSCustomObject] @{
                            IPAddress      = '192.168.0.1'
                            InterfaceAlias = 'Ethernet'
                            InterfaceIndex = 1
                            PrefixLength   = [System.Byte] 24
                            AddressFamily  = 'IPv4'
                            SkipAsSource   = $false
                        }
                    }

                    Mock -CommandName Set-NetIPAddress
                }

                Context 'Invoked with valid IP address' {
                    It 'Should return $null' {
                        $setTargetResourceParameters = @{
                            IPAddress    = '192.168.0.1'
                            SkipAsSource = $true
                        }
                        { $result = Set-TargetResource @setTargetResourceParameters } | Should -Not -Throw
                        $result | Should -BeNullOrEmpty
                    }

                    It 'Should call all the mock' {
                        Assert-MockCalled -CommandName Set-NetIPAddress -Exactly -Times 1
                    }
                }
            }
        }

        Describe 'MSFT_IPAddressOption\Test-TargetResource' -Tag 'Test' {
            Context 'Invoked with an existing IP address, SkipAsSource = $true' {
                BeforeEach {
                    Mock -CommandName Get-NetIPAddress -MockWith {
                        [PSCustomObject] @{
                            IPAddress      = '192.168.0.1'
                            InterfaceAlias = 'Ethernet'
                            InterfaceIndex = 1
                            PrefixLength   = [System.Byte] 24
                            AddressFamily  = 'IPv4'
                            SkipAsSource   = $true
                        }
                    }
                }

                Context 'Invoked with valid IP address' {
                    It 'Should return $true' {
                        $testGetResourceParameters = @{
                            IPAddress    = '192.168.0.1'
                            SkipAsSource = $true
                        }

                        $result = Test-TargetResource @testGetResourceParameters
                        $result | Should -Be $true
                    }
                }
            }
        }
    } #end InModuleScope $DSCResourceName
}
finally
{
    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}