Tests/xDSCSEPVIE.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
Import-Module -Name .\DSCResources\xDSCSEPVIE\xDSCSEPVIE.psm1

$Global:DSCModuleName      = 'xDSCSEPVIE' 
$Global:DSCResourceName    = 'xDSCSEPVIE'

InModuleScope -ModuleName xDSCSEPVIE -ScriptBlock {
  $VIELocation = 'C:\Temp\doesntmatter.exe'

  $global:mockedVolume = [pscustomobject] @{
    FileSystemLabel = 'myLabel'
    DriveLetter     = 'C'
    DriveType       = 'Fixed'
  }
  $global:mockedCSV = @()
  $global:mockedCSV += [pscustomobject] @{
    DriveLetter = 'C'
    DateScanned = (Get-Date -Format dd/MM/yyyy)
  }
  $global:mockedCSV += [pscustomobject] @{
    DriveLetter = 'GG'
    DateScanned = (Get-Date -Format dd/MM/yyyy)
  }

  Describe -Name 'Testing if functions return correct objects' -Fixture {
    Mock -CommandName Import-CSV -MockWith {
      $global:mockedCSV
    }
    Mock -CommandName Get-Volume -MockWith {
      $global:mockedVolume
    }
    Mock -CommandName Test-Path -MockWith {
      return $true
    }
    It -name 'Get-TargetResource returns a hashtable' -test {
      Get-TargetResource -VIELocation $VIELocation | Should Be 'System.Collections.Hashtable'
    }

    It -name 'Test-TargetResource returns true or false' -test {
      (Test-TargetResource -VIELocation $VIELocation -Ensure Present).GetType() -as [string] | Should Be 'bool'
    }
  }

  Describe -Name "Testing $($Global:DSCResourceName)\Get-TargetResource present/absent logic" -Fixture {
    foreach ($drivetest in $global:mockedCSV ) 
    {
      if ($drivetest.driveLetter -in $global:mockedVolume.driveLetter) 
      {
        Mock -CommandName Import-CSV -MockWith {
          $drivetest
        }
        Mock -CommandName Get-Volume -MockWith {
          $global:mockedVolume
        }
        Mock -CommandName Test-Path -MockWith {
          return $true
        }
        It -name "Get-TargetResource should return present for drive letter ($($drivetest.driveletter))" -test {
          (Get-TargetResource -VIELocation $VIELocation).Values | Should Be 'Present'
        }    
      }
      else 
      {
        Mock -CommandName Import-CSV -MockWith {
          $drivetest
        }
        Mock -CommandName Get-Volume -MockWith {
          $global:mockedVolume
        }
        Mock -CommandName Test-Path -MockWith {
          return $true
        }
        It -name "Get-TargetResource should return absent for drive letter ($($drivetest.driveletter))" -test {
          (Get-TargetResource -VIELocation $VIELocation).Values | Should Be 'absent'
        }  
      }
    }
  }
  
  Describe -Name "Testing $($Global:DSCResourceName)\Get-TargetResource present/absent logic" -Fixture {
    foreach ($drivetest in $global:mockedCSV ) 
    {
      if ($drivetest.driveLetter -in $global:mockedVolume.driveLetter) 
      {
        Mock -CommandName Import-CSV -MockWith {
          $drivetest
        }
        Mock -CommandName Get-Volume -MockWith {
          $global:mockedVolume
        }
        Mock -CommandName Test-Path -MockWith {
          return $true
        }
        It -name "Test-TargetResource should return true for drive letter ($($drivetest.driveletter))" -test {
          Test-TargetResource -VIELocation $VIELocation -Ensure Present | Should Be 'True'
        }     
      }
      else 
      {
        Mock -CommandName Import-CSV -MockWith {
          $drivetest
        }
        Mock -CommandName Get-Volume -MockWith {
          $global:mockedVolume
        }
        Mock -CommandName Test-Path -MockWith {
          return $true
        }
        It -name "Test-TargetResource should return false for drive letter ($($drivetest.driveletter))" -test {
          Test-TargetResource -VIELocation $VIELocation -Ensure Absent | Should Be 'True'
        }
      }
    }
  } 
}