MSFT_xPfxImport.Integration.Tests.ps1 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')]
  2. param ()
  3. $script:DSCModuleName = 'xCertificate'
  4. $script:DSCResourceName = 'MSFT_xPfxImport'
  5. #region HEADER
  6. # Integration Test Template Version: 1.1.0
  7. [String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
  8. if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
  9. (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
  10. {
  11. & git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
  12. }
  13. Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
  14. $TestEnvironment = Initialize-TestEnvironment `
  15. -DSCModuleName $script:DSCModuleName `
  16. -DSCResourceName $script:DSCResourceName `
  17. -TestType Integration
  18. #endregion
  19. # Using try/finally to always cleanup even if something awful happens.
  20. try
  21. {
  22. # Generate a self-signed certificate, export it and remove it from the store
  23. # to use for testing.
  24. # Don't use CurrentUser certificates for this test because they won't be found because
  25. # DSC LCM runs under a different context (Local System).
  26. $Certificate = New-SelfSignedCertificate `
  27. -DnsName $ENV:ComputerName `
  28. -CertStoreLocation Cert:\LocalMachine\My
  29. $CertificatePath = Join-Path `
  30. -Path $ENV:Temp `
  31. -ChildPath "xPfxImport-$($Certificate.Thumbprint).pfx"
  32. $testUsername = 'DummyUsername'
  33. $testPassword = 'DummyPassword'
  34. $testCredential = New-Object System.Management.Automation.PSCredential $testUsername, (ConvertTo-SecureString $testPassword -AsPlainText -Force)
  35. $null = Export-PfxCertificate `
  36. -Cert $Certificate `
  37. -FilePath $CertificatePath `
  38. -Password $testCredential.Password
  39. $null = Remove-Item `
  40. -Path $Certificate.PSPath `
  41. -Force
  42. #region Integration Tests
  43. $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName)_Add.config.ps1"
  44. . $ConfigFile
  45. Describe "$($script:DSCResourceName)_Add_Integration" {
  46. #region DEFAULT TESTS
  47. It 'Should compile without throwing' {
  48. {
  49. $configData = @{
  50. AllNodes = @(
  51. @{
  52. NodeName = 'localhost';
  53. PSDscAllowPlainTextPassword = $true
  54. }
  55. )
  56. }
  57. & "$($script:DSCResourceName)_Add_Config" `
  58. -OutputPath $TestDrive `
  59. -ConfigurationData $configData `
  60. -Path $CertificatePath `
  61. -Thumbprint $Certificate.Thumbprint `
  62. -Credential $testCredential
  63. Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
  64. } | Should not throw
  65. }
  66. It 'should be able to call Get-DscConfiguration without throwing' {
  67. { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw
  68. }
  69. #endregion
  70. It 'Should have set the resource and all the parameters should match' {
  71. # Get the Certificate details
  72. $CertificateNew = Get-Item `
  73. -Path "Cert:\LocalMachine\My\$($Certificate.Thumbprint)"
  74. $CertificateNew | Should BeOfType System.Security.Cryptography.X509Certificates.X509Certificate2
  75. $CertificateNew.Thumbprint | Should Be $Certificate.Thumbprint
  76. $CertificateNew.Subject | Should Be $Certificate.Subject
  77. }
  78. }
  79. #endregion
  80. #region Integration Tests
  81. $ConfigFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName)_Remove.config.ps1"
  82. . $ConfigFile
  83. Describe "$($script:DSCResourceName)_Remove_Integration" {
  84. #region DEFAULT TESTS
  85. It 'Should compile without throwing' {
  86. {
  87. & "$($script:DSCResourceName)_Remove_Config" `
  88. -OutputPath $TestDrive `
  89. -Path $CertificatePath `
  90. -Thumbprint $Certificate.Thumbprint
  91. Start-DscConfiguration -Path $TestDrive -ComputerName localhost -Wait -Verbose -Force
  92. } | Should not throw
  93. }
  94. It 'should be able to call Get-DscConfiguration without throwing' {
  95. { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should Not throw
  96. }
  97. #endregion
  98. It 'Should have set the resource and all the parameters should match' {
  99. # Get the Certificate details
  100. $CertificateNew = Get-Item `
  101. -Path "Cert:\LocalMachine\My\$($Certificate.Thumbprint)" `
  102. -ErrorAction SilentlyContinue
  103. $CertificateNew | Should BeNullOrEmpty
  104. }
  105. }
  106. #endregion
  107. }
  108. finally
  109. {
  110. # Clean up
  111. $null = Remove-Item `
  112. -Path $CertificatePath `
  113. -Force `
  114. -ErrorAction SilentlyContinue
  115. $null = Remove-Item `
  116. -Path $Certificate.PSPath `
  117. -Force `
  118. -ErrorAction SilentlyContinue
  119. #region FOOTER
  120. Restore-TestEnvironment -TestEnvironment $TestEnvironment
  121. #endregion
  122. }