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

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

#region HEADER
# Integration Test Template Version: 1.1.1
[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 Integration
#endregion

# Backup the existing settings
$CurrentDnsClientGlobalSetting = Get-DnsClientGlobalSetting

# Using try/finally to always cleanup even if something awful happens.
try
{
    # Import the Common Networking functions
    Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\NetworkingDsc.Common\NetworkingDsc.Common.psm1') -Force

    # Load the ParameterList from the data file.
    $resourceDataPath = Join-Path `
        -Path $script:moduleRoot `
        -ChildPath (Join-Path -Path 'DSCResources' -ChildPath $script:DSCResourceName)
    $resourceData = Import-LocalizedData `
        -BaseDirectory $resourceDataPath `
        -FileName "$($script:DSCResourceName).data.psd1"
    $parameterList = $resourceData.ParameterList

    # Set the DNS Client Global settings to known values
    Set-DnsClientGlobalSetting `
        -SuffixSearchList 'fabrikam.com' `
        -UseDevolution $False `
        -DevolutionLevel 4

    $configData = @{
        AllNodes = @(
            @{
                NodeName         = 'localhost'
                SuffixSearchList = 'contoso.com'
                UseDevolution    = $True
                DevolutionLevel  = 2
            }
        )
    }

    #region Integration Tests
    $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1"
    . $ConfigFile

    Describe "$($script:DSCResourceName)_Integration" {
        #region DEFAULT TESTS
        It 'Should compile and apply the MOF without throwing' {
            {
                & "$($script:DSCResourceName)_Config" `
                    -OutputPath $TestDrive `
                    -ConfigurationData $configData
                Start-DscConfiguration `
                    -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
            } | Should -Not -Throw
        }

        It 'Should be able to call Get-DscConfiguration without throwing' {
            { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
        }
        #endregion

        # Get the DNS Client Global Settings details
        $dnsClientGlobalSettingNew = Get-DnsClientGlobalSetting

        # Use the Parameters List to perform these tests
        foreach ($parameter in $parameterList)
        {
            $parameterCurrentValue = (Get-Variable -Name 'dnsClientGlobalSettingNew').value.$($parameter.name)
            $parameterNewValue = (Get-Variable -Name configData).Value.AllNodes[0].$($parameter.Name)

            It "Should have set the '$parameterName' to '$parameterNewValue'" {
                $parameterCurrentValue | Should -Be $parameterNewValue
            }
        }
    }
    #endregion
}
finally
{
    # Clean up
    Set-DnsClientGlobalSetting `
        -SuffixSearchList $CurrentDnsClientGlobalSetting.SuffixSearchList `
        -UseDevolution $CurrentDnsClientGlobalSetting.UseDevolution `
        -DevolutionLevel $CurrentDnsClientGlobalSetting.DevolutionLevel

    #region FOOTER
    Restore-TestEnvironment -TestEnvironment $TestEnvironment
    #endregion
}