Tests/Integration/MSFT_xDSCWebService.xxx.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
######################################################################################
# Integration Tests for DSC Resource xDSCWebService
#
# There tests will make changes to your system, we are tyring to roll them back,
# but you never know. Best to run this on a throwaway VM.
# Run as an elevated administrator
######################################################################################

$here = Split-Path -Parent $MyInvocation.MyCommand.Path

# create a unique name that we use for our temp files and folders
[string]$tempName = "xDSCWebServiceTests_" + (Get-Date).ToString("yyyyMMdd_HHmmss")

Describe "xDSCWebService" {

    function Verify-DSCPullServer ($protocol,$hostname,$port) {
        ([xml](invoke-webrequest "$($protocol)://$($hostname):$($port)/psdscpullserver.svc" | % Content)).service.workspace.collection.href
    }

    function Remove-WebRoot([string]$filePath)
    {
       if (Test-Path $filePath)
       {
           Get-ChildItem $filePath -Recurse | Remove-Item -Recurse
           Remove-Item $filePath
       }
    }

    try
    {


        # before doing our changes, create a backup of the current config
        Backup-WebConfiguration -Name $tempName


        It 'Installing Service' -test {
        {

            # define the configuration
            configuration InstallingService
            {
                WindowsFeature DSCServiceFeature
                {
                    Ensure = �Present�
                    Name   = �DSC-Service�
                }
            }

            # execute the configuration into a temp location
            InstallingService -OutputPath $env:temp\$($tempName)_InstallingService
            # run the configuration, it should not throw any errors
            Start-DscConfiguration -Path $env:temp\$($tempName)_InstallingService -Wait -Verbose -ErrorAction Stop -Force}  | should not throw

            (Get-WindowsFeature -name DSC-Service | Where Installed).count | should be 1
        }

        It 'Creating Sites' -test {
        {
            # define the configuration
            configuration CreatingSites
            {
                Import-DSCResource -ModuleName xPSDesiredStateConfiguration
                
                xDscWebService PSDSCPullServer
                {
                    EndpointName            = �TestPSDSCPullServer�
                    Port                    = 21001
                    CertificateThumbPrint   = �AllowUnencryptedTraffic�
                }

                xDscWebService PSDSCComplianceServer
                {
                    EndpointName            = �TestPSDSCComplianceServer�
                    Port                    = 21002
                    CertificateThumbPrint   = �AllowUnencryptedTraffic�
                    IsComplianceServer      = $true
                }
            }

            # execute the configuration into a temp location
            CreatingSites -OutputPath $env:temp\$($tempName)_CreatingSites
            # run the configuration, it should not throw any errors
            Start-DscConfiguration -Path $env:temp\$($tempName)_CreatingSites -Wait -Verbose -ErrorAction Stop -Force}  | should not throw

            # we now expect two sites starting with our prefix
            (Get-ChildItem iis:\sites | Where-Object Name -match "^TestPSDSC").count | should be 2

            # we expect some files in the web root, using the defaults
            (Test-Path "$env:SystemDrive\inetpub\TestPSDSCPullServer\web.config") | should be $true

            $FireWallRuleDisplayName = "Desired State Configuration - Pull Server Port:{0}"
            $ruleName = ($($FireWallRuleDisplayName) -f "21001")
            (Get-NetFirewallRule | Where-Object DisplayName -eq "$ruleName" | Measure-Object).count | should be 1

            # we also expect an XML document with certain strings at a certain URI
            (Verify-DSCPullServer "http" "localhost" "21001") | should match "Action|Module"

        }

        It 'Removing Sites' -test {
        {

            # define the configuration
            configuration RemovingSites
            {
                Import-DSCResource -ModuleName xPSDesiredStateConfiguration

                xDscWebService PSDSCPullServer
                {
                    Ensure                  = �Absent�
                    EndpointName            = �TestPSDSCPullServer�
                    CertificateThumbPrint   = �NotUsed�
                }

                xDscWebService PSDSCComplianceServer
                {
                    Ensure                  = �Absent�
                    EndpointName            = �TestPSDSCComplianceServer�
                    CertificateThumbPrint   = �NotUsed�
                }
            }

            # execute the configuration into a temp location
            RemovingSites -OutputPath $env:temp\$($tempName)_RemovingSites
            # run the configuration, it should not throw any errors
            Start-DscConfiguration -Path $env:temp\$($tempName)_RemovingSites -Wait -Verbose -ErrorAction Stop -Force}  | should not throw

            # we now expect two sites starting with our prefix
            (Get-ChildItem iis:\sites | Where-Object Name -match "^TestPSDSC").count | should be 0

            (Test-Path "$env:SystemDrive\inetpub\TestPSDSCPullServer\web.config") | should be $false

            $FireWallRuleDisplayName = "Desired State Configuration - Pull Server Port:{0}"
            $ruleName = ($($FireWallRuleDisplayName) -f "8081")
            (Get-NetFirewallRule | Where-Object DisplayName -eq "$ruleName" | Measure-Object).count | should be 0

        }

        It 'CreatingSitesWithFTP' -test {
        {
            # create a new FTP site on IIS
            If (!(Test-Path IIS:\Sites\DummyFTPSite))
            {
                New-WebFtpSite -Name "DummyFTPSite" -Port "21000"
                # stop the site, we don't want it, it is just here to check whether setup works
                (get-Website -Name "DummyFTPSite").ftpserver.stop()
            }

            # define the configuration
            configuration CreatingSitesWithFTP
            {
                Import-DSCResource -ModuleName xPSDesiredStateConfiguration

                xDscWebService PSDSCPullServer2
                {
                    EndpointName            = �TestPSDSCPullServer2�
                    Port                    = 21003
                    CertificateThumbPrint   = �AllowUnencryptedTraffic�
                }
            }

            # execute the configuration into a temp location
            CreatingSitesWithFTP -OutputPath $env:temp\$($tempName)_CreatingSitesWithFTP
            # run the configuration, it should not throw any errors
            Start-DscConfiguration -Path $env:temp\$($tempName)_CreatingSitesWithFTP -Wait -Verbose -ErrorAction Stop -Force}  | should not throw

        }

    }
    finally
    {
        # roll back our changes
        Restore-WebConfiguration -Name $tempName
        Remove-WebConfigurationBackup -Name $tempName

        # remove possible web files
        Remove-WebRoot -filePath "$env:SystemDrive\inetpub\TestPSDSCPullServer"
        Remove-WebRoot -filePath "$env:SystemDrive\inetpub\TestPSDSCComplianceServer"
        Remove-WebRoot -filePath "$env:SystemDrive\inetpub\TestPSDSCPullServer2"

        # remove the generated MoF files
        Get-ChildItem $env:temp -Filter $tempName* | Remove-item -Recurse

        # remove all firewall rules starting with port 21*
        Get-NetFirewallRule | Where-Object DisplayName -match "^Desired State Configuration - Pull Server Port:21" | Remove-NetFirewallRule

    } 
}