Examples/PullServerDeploymentVerificationTest/PullServerSetupTests.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

<#
 *
 * Once you setup your pullserver(V2), run the following set of tests on the pullserver machine to verify if the pullserver is setup properly and ready to go.
 * This test assumes default values are used during deployment for the location of web.config and pull server URL.
 * If default values are not used during deployment , please update these values in the 'BeforeAll' block accordingly.
 *
 #>



Describe PullServerInstallationTests{

BeforeAll{
 
 # UPDATE THE PULLSERVER URL, If it is different from the default value.
 $pullserverURL = "http://$(hostname):8080/PSDSCPullServer/PSDSCPullserver.svc"

 # UPDATE THE LOCATION OF WEB.CONFIG, if it is differnet from the default path.
 $script:defaultPullServerConfigFile = "$($env:SystemDrive)\inetpub\wwwroot\psdscpullserver\web.config"

 $skip = $false
 if (-not (Test-Path $defaultPullServerConfigFile))
 {
    $skip = $true
    Write-Error "No pullserver web.config found." -ErrorAction Stop
 }
 $script:webConfigXml = [xml](cat $script:defaultPullServerConfigFile)

 # RegKey info.
 $regFileName = "RegistrationKeys.txt"
 $regKeyElement = $webConfigXml.SelectNodes("//appSettings/add[@key = 'RegistrationKeyPath']")
 $fullRegKeyPath = Join-Path $regKeyElement.value  $regFileName
 $regKeyContent = cat $fullRegKeyPath

 # Configuration repository info.
 $configurationPathElement  = $webConfigXml.SelectNodes("//appSettings/add[@key = 'ConfigurationPath']")
 $script:configurationRepository =  $configurationPathElement.value
 
}

It "Verify RegistrationKeyFile exist" -skip:$skip{

    $regKey = $webConfigXml.SelectNodes("//appSettings/add[@key = 'RegistrationKeyPath']")
    
    if (Test-path (Join-Path $regKey.value $regFileName)){
        Write-Verbose "Registration key file found."
    }
    else
    {   
        throw "RegistrationKeyFile NOT found. Make sure registration key file is placed on the location specified on the web.config of the pullserver."
    }
 }

<#
 # Verify module and configuration repository referenced on the web.config file exist.
 #>

 It "Verify configuration and module repository folders exist" -skip:$skip{
 
    $modulePathElement = $webConfigXml.SelectNodes("//appSettings/add[@key = 'ModulePath']")
    Test-Path ($modulePathElement.value) | Should Be $true "Module repository path referenced on web.config does not exist on the disk"
    Test-Path $script:configurationRepository | Should Be $true "Module repository path referenced on web.config does not exist on the disk"
}

<#
#
# Verify the server URL is up and running.
#>

 It "Verify server is up and running" -skip:$skip{
    $response = Invoke-WebRequest -Uri $pullserverURL -UseBasicParsing
    $response.StatusCode | Should Be 200 "Server response should be ok"
 }

 <#
 # Verify pull works on the current evironement by pulling a (No-OP) configuration from the pullserver.
 #
 #>

 It "Verify pull end to end works" -skip:$skip{
 
# Sample test meta-configuration
$configName = "PullServerSetUpTest"
[DscLocalConfigurationManager()]
Configuration PullServerSetUpTestMetaConfig
{
        Settings
        {
            RefreshMode = "PULL"             

        }
        ConfigurationRepositoryWeb ConfigurationManager
        {
            ServerURL =  $pullserverURL
            AllowUnsecureConnection = $true     # Remove this if you are using https
            RegistrationKey = $regKeyContent     
            ConfigurationNames = @($configName)
        }

}

PullServerSetUpTestMetaConfig -OutputPath .\PullServerSetUpTestMetaConfig
slcm -path .\PullServerSetUpTestMetaConfig -Verbose -force


$name = glcm |% ConfigurationDownloadManagers|% ConfigurationNames
$name | Should Be $configName


# Sample test configuration
Configuration NoOpConfig
{
    Import-DscResource –ModuleName 'PSDesiredStateConfiguration'
    Node ($configName)
    {
        Script script
        {
            GetScript = "@{}"
            SetScript = "{}"            
            TestScript =  ‘if ($false) { return $true } else {return $false}‘
        }
    }
}

# Create a mof file copy it to
NoOpConfig -OutputPath $configurationRepository -Verbose

# Create checksum
New-DscChecksum $configurationRepository -Verbose -Force

# pull configuration from the server.
Update-DscConfiguration -Wait -Verbose 

$confignameSet = Get-DscConfiguration | % ConfigurationName
$confignameSet | Should Be "NoOpConfig" "Configuration is not set properly"
 }

}