mirror of
https://github.com/PowerShell/PSDscResources
synced 2026-06-21 13:45:29 +00:00
Add Archive resource
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
||||
[ClassVersion("1.0.0.0"),FriendlyName("Archive")]
|
||||
class MSFT_Archive : OMI_BaseResource
|
||||
{
|
||||
[Key, Description("The path to the archive file that should be expanded to or removed from the specified destination.")] String Path;
|
||||
[Key, Description("The path where the specified archive file should be expanded to or removed from.")] String Destination;
|
||||
[Write, Description("Specifies whether or not the expanded content of the archive file at the specified path should exist at the specified destination. To update the specified destination to have the expanded content of the archive file at the specified path, specify this property as Present. To remove the expanded content of the archive file at the specified path from the specified destination, specify this property as Absent. The default value is Present."), ValueMap{"Present", "Absent"}, Values{"Present", "Absent"}] String Ensure;
|
||||
[Write, Description("Specifies whether or not to validate that a file at the destination with the same name as a file in the archive actually matches that corresponding file in the archive by the specified checksum method. If the file does not match and Ensure is specified as Present and Force is not specified, the resource will throw an error that the file at the desintation cannot be overwritten. If the file does not match and Ensure is specified as Present and Force is specified, the file at the desintation will be overwritten. If the file does not match and Ensure is specified as Absent, the file at the desintation will not be removed. The default value is false.")] Boolean Validate;
|
||||
[Write, Description("The Checksum method to use to validate whether or not a file at the destination with the same name as a file in the archive actually matches that corresponding file in the archive. An invalid argument exception will be thrown if Checksum is specified while Validate is specified as false. ModifiedDate will check that the LastWriteTime property of the file at the destination matches the LastWriteTime property of the file in the archive. CreatedDate will check that the CreationTime property of the file at the destination matches the CreationTime property of the file in the archive. SHA-1, SHA-256, and SHA-512 will check that the hash of the file at the destination by the specified SHA method matches the hash of the file in the archive by the specified SHA method. The default value is ModifiedDate.") ,ValueMap{"SHA-1", "SHA-256", "SHA-512", "CreatedDate", "ModifiedDate"}, Values{"SHA-1", "SHA-256", "SHA-512", "CreatedDate", "ModifiedDate"}] String Checksum;
|
||||
[Write, Description("The credential of a user account with permissions to access the specified archive path and destination if needed.") ,EmbeddedInstance("MSFT_Credential")] String Credential;
|
||||
[Write, Description("Specifies whether or not any existing files or directories at the destination with the same name as a file or directory in the archive should be overwritten to match the file or directory in the archive. When this property is false, an error will be thrown if an item at the destination needs to be overwritten. The default value is false.")] Boolean Force;
|
||||
};
|
||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
# Localized MSFT_Archive.strings.psd1
|
||||
|
||||
ConvertFrom-StringData @'
|
||||
RetrievingArchiveState = Retrieving the state of the archive with path {0} and destination {1}...
|
||||
SettingArchiveState = Setting the state of the archive with path {0} and destination {1}...
|
||||
ArchiveStateSet = The state of the archive with path {0} and destination {1} has been set.
|
||||
TestingArchiveState = Testing whether or not the state of the archive with path {0} and destination {1} matches the desired state...
|
||||
|
||||
PathAccessiblePSDriveNotNeeded = The path {0} is accessible. A new PSDrive is not needed.
|
||||
CreatingPSDrive = Creating a PSDrive to access the path {0} with permissions from the user {1}...
|
||||
RemovingPSDrive = Removing the mounted PSDrive {0}...
|
||||
|
||||
DestinationExists = A directory already exists at the destination path {0}.
|
||||
DestinationDoesNotExist = A directory does not exist at the destination path {0}.
|
||||
CreatingDirectoryAtDestination = Creating the root directory at the destination path {0}...
|
||||
|
||||
TestingIfArchiveExistsAtDestination = Testing if the archive at the destination path {0} exists...
|
||||
ArchiveExistsAtDestination = The archive at path {0} exists at the destination {1}.
|
||||
ArchiveDoesNotExistAtDestination = The archive at path {0} does not exist at the destination {1}.
|
||||
|
||||
OpeningArchive = Opening the archive at path {0}...
|
||||
ClosingArchive = Closing the archive at path {0}...
|
||||
|
||||
OpeningArchiveEntry = Opening the archive entry {0}...
|
||||
|
||||
ItemWithArchiveEntryNameExists = An item with the same name as the archive entry exists at the destination path {0}.
|
||||
ItemWithArchiveEntryNameDoesNotExist = An item with the same name as the archive entry does not exist at the destination path {0}.
|
||||
ItemWithArchiveEntryNameIsNotDirectory = The item at the destination path {0} has the same name as a directory archive entry but is not a directory.
|
||||
ItemWithArchiveEntryNameIsNotFile = The item at the destination path {0} has the same name as a file archive entry but is not a file.
|
||||
|
||||
TestingIfFileMatchesArchiveEntryByChecksum = Testing if the file at {0} matches the archive entry at {1} by the checksum method {2}...
|
||||
ComparingHashes = Comparing the hash of the file at {0} to the hash of the archive entry at {1} with the hash algorithm {2}...
|
||||
FileMatchesArchiveEntryByChecksum = The file at {0} matches the archive entry at {1} by the checksum method {2}.
|
||||
FileDoesNotMatchArchiveEntryByChecksum = The file at {0} does not match the archive entry at {1} by the checksum method {2}.
|
||||
|
||||
ExpandingArchiveToDestination = Expanding the archive at {0} to the destination {1}...
|
||||
CreatingParentDirectory = Creating an archive entry parent directory at the path {0}...
|
||||
OverwritingItem = Overwriting the item at the path {0}...
|
||||
CopyingArchiveEntryToDestination = Copying the corresponding archive entry to the path {0}...
|
||||
|
||||
RemovingArchiveFromDestination = Removing archive from the destination path {0}...
|
||||
RemovingDirectory = Removing the directory at path {0}...
|
||||
RemovingFile = Removing the file at path {0}...
|
||||
CouldNotRemoveItemOfIncorrectType = The file at {0} does not match the item type (file, directory, or other) or the archive entry at {1}, so it will not be removed.
|
||||
ArchiveRemovedFromDestination = Archive removed from the destination path {0}.
|
||||
|
||||
ChecksumSpecifiedAndValidateFalse = The Checksum parameter was specified as {0} but the Validate parameter is set to false for the archive with path {1} and destination {2}. Please specify the Validate parameter as true to use the Checksum parameter.
|
||||
PathDoesNotContainValidPSDriveRoot = The path {0} cannot be accessed because it does not contain any directories to use as the root of a PSDrive.
|
||||
ErrorCreatingPSDrive = An error occurred while attempting to create a PSDrive to access the path {0} under the user {1}.
|
||||
PathDoesNotExistAsLeaf = The path {0} does not exist or is not a path leaf.
|
||||
DestinationExistsAsFile = A file exists at the desintation path {0}.
|
||||
ErrorOpeningArchive = An error occurred while attempting to open the archive at path {0}.
|
||||
ErrorCopyingFromArchiveToDestination = An error occurred while attempting copy from the archive to the destination path {0}.
|
||||
DirectoryIsNotEmpty = The directory at path {0} is not empty, so it will not be removed.
|
||||
ErrorComparingHashes = An error occurred while comparing the hash of the file at {0} to the archive entry {1} with the hash algorithm {2}.
|
||||
ForceNotSpecifiedToOverwriteItem = An item already exists at {0} that does not match the item in the archive at {1}, but the Force parameter has not been specified to overwrite this item.
|
||||
'@
|
||||
@@ -0,0 +1,30 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Expands the archive located at 'C:\ExampleArchivePath\Archive.zip' to the destination path
|
||||
'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
Since Validate is specified as $true and the Checksum parameter is specified as SHA-256, the
|
||||
resource will check if the SHA-256 hash of the file in the archive matches the SHA-256 hash
|
||||
of the corresponding file at the destination and replace any files that do not match.
|
||||
|
||||
Since Force is specified as $true, the resource will overwrite any mismatching files at the
|
||||
destination. If Force is specified as $false, the resource will throw an error instead of
|
||||
overwrite any files at the destination.
|
||||
#>
|
||||
Configuration Sample_Archive_ExpandArchiveChecksumAndForce
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive4
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $true
|
||||
Ensure = 'Present'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Expands the archive located at 'C:\ExampleArchivePath\Archive.zip' to the destination path
|
||||
'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
Since Validate is specified as $true and the Checksum parameter is not provided, the
|
||||
resource will check if the last write time of the archive file matches the last write time
|
||||
of the corresponding file at the destination and replace any files that do not match.
|
||||
|
||||
Since Force is specified as $true, the resource will overwrite any mismatching files at the
|
||||
destination. If Force is specified as $false, the resource will throw an error instead of
|
||||
overwrite any files at the destination.
|
||||
#>
|
||||
Configuration Sample_Archive_ExpandArchiveDefaultValidationAndForce
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive3
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Validate = $true
|
||||
Force = $true
|
||||
Ensure = 'Present'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Expands the archive located at 'C:\ExampleArchivePath\Archive.zip' to the destination path
|
||||
'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
The resource will only check if the expanded archive files exist at the destination.
|
||||
No validation is performed on any existing files at the destination to ensure that they
|
||||
match the files in the archive.
|
||||
#>
|
||||
Configuration Sample_Archive_ExpandArchiveNoValidation
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive1
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Ensure = 'Present'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Expands the archive located at 'C:\ExampleArchivePath\Archive.zip' to the destination path
|
||||
'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
The added specification of a Credential here allows you to provide the credential of a user
|
||||
to provide the resource access to the archive and destination paths.
|
||||
|
||||
The resource will only check if the expanded archive files exist at the destination.
|
||||
No validation is performed on any existing files at the destination to ensure that they
|
||||
match the files in the archive.
|
||||
#>
|
||||
Configuration Sample_Archive_ExpandArchiveNoValidationCredential
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[System.Management.Automation.PSCredential]
|
||||
[System.Management.Automation.Credential()]
|
||||
$Credential
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive2
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Credential = $Credential
|
||||
Ensure = 'Present'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Remove the expansion of the archive located at 'C:\ExampleArchivePath\Archive.zip' from the
|
||||
destination path 'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
Since Validate is specified as $true and the Checksum parameter is specified as SHA-256, the
|
||||
resource will check if the SHA-256 hash of the file in the archive matches the SHA-256 hash
|
||||
of the corresponding file at the destination and will not remove any files that do not match.
|
||||
#>
|
||||
Configuration Sample_Archive_RemoveArchiveChecksum
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive6
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Removes the expansion of the archive located at 'C:\ExampleArchivePath\Archive.zip' from the
|
||||
destination path 'C:\ExampleDestinationPath\Destination'.
|
||||
|
||||
The resource will only check if the expanded archive files exist at the destination.
|
||||
No validation is performed on any existing files at the destination to ensure that they
|
||||
match the files in the archive before removing them.
|
||||
#>
|
||||
Configuration Sample_Archive_RemoveArchiveNoValidation
|
||||
{
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive5
|
||||
{
|
||||
Path = 'C:\ExampleArchivePath\Archive.zip'
|
||||
Destination = 'C:\ExampleDestinationPath\Destination'
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -73,7 +73,7 @@ VariablesToExport = '*'
|
||||
AliasesToExport = @()
|
||||
|
||||
# DSC resources to export from this module
|
||||
DscResourcesToExport = 'Environment', 'Group', 'GroupSet', 'Registry', 'Script', 'Service', 'ServiceSet', 'User', 'WindowsFeature', 'WindowsFeatureSet', 'WindowsOptionalFeature', 'WindowsOptionalFeatureSet', 'WindowsPackageCab', 'WindowsProcess', 'ProcessSet'
|
||||
DscResourcesToExport = @( 'Archive', 'Environment', 'Group', 'GroupSet', 'Registry', 'Script', 'Service', 'ServiceSet', 'User', 'WindowsFeature', 'WindowsFeatureSet', 'WindowsOptionalFeature', 'WindowsOptionalFeatureSet', 'WindowsPackageCab', 'WindowsProcess', 'ProcessSet' )
|
||||
|
||||
# List of all modules packaged with this module
|
||||
# ModuleList = @()
|
||||
|
||||
@@ -37,6 +37,7 @@ Please check out the common DSC Resources [contributing guidelines](https://gith
|
||||
|
||||
## Resources
|
||||
|
||||
* [Archive](#archive): Provides a mechanism to expand an archive (.zip) file to a specific path or remove an expanded archive (.zip) file from a specific path on a target node.
|
||||
* [Environment](#environment): Provides a mechanism to configure and manage environment variables for a machine or process.
|
||||
* [Group](#group): Provides a mechanism to manage local groups on a target node.
|
||||
* [GroupSet](#groupset): Provides a mechanism to configure and manage multiple Group resources with common settings but different names.
|
||||
@@ -63,6 +64,38 @@ Please check out the common DSC Resources [contributing guidelines](https://gith
|
||||
* [WindowsOptionalFeatureSet](#windowsoptionalfeatureset)
|
||||
* [WindowsPackageCab](#windowspackagecab)
|
||||
|
||||
### Archive
|
||||
|
||||
Provides a mechanism to expand an archive (.zip) file to a specific path or remove an expanded archive (.zip) file from a specific path on a target node.
|
||||
|
||||
#### Requirements
|
||||
|
||||
* The System.IO.Compression type assembly must be available on the machine.
|
||||
* The System.IO.Compression.FileSystem type assembly must be available on the machine.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* **[String] Path** _(Key)_: The path to the archive file that should be expanded to or removed from the specified destination.
|
||||
* **[String] Destination** _(Key)_: The path where the specified archive file should be expanded to or removed from.
|
||||
* **[String] Ensure** _(Write)_: Specifies whether or not the expanded content of the archive file at the specified path should exist at the specified destination. To update the specified destination to have the expanded content of the archive file at the specified path, specify this property as Present. To remove the expanded content of the archive file at the specified path from the specified destination, specify this property as Absent. The default value is Present. { *Present* | Absent }.
|
||||
* **[Boolean] Validate** _(Write)_: Specifies whether or not to validate that a file at the destination with the same name as a file in the archive actually matches that corresponding file in the archive by the specified checksum method. If the file does not match and Ensure is specified as Present and Force is not specified, the resource will throw an error that the file at the desintation cannot be overwritten. If the file does not match and Ensure is specified as Present and Force is specified, the file at the desintation will be overwritten. If the file does not match and Ensure is specified as Absent, the file at the desintation will not be removed. The default value is false.
|
||||
* **[String] Checksum** _(Write)_: The Checksum method to use to validate whether or not a file at the destination with the same name as a file in the archive actually matches that corresponding file in the archive. An invalid argument exception will be thrown if Checksum is specified while Validate is specified as false. ModifiedDate will check that the LastWriteTime property of the file at the destination matches the LastWriteTime property of the file in the archive. CreatedDate will check that the CreationTime property of the file at the destination matches the CreationTime property of the file in the archive. SHA-1, SHA-256, and SHA-512 will check that the hash of the file at the destination by the specified SHA method matches the hash of the file in the archive by the specified SHA method. The default value is ModifiedDate. { *ModifiedDate* | CreatedDate | SHA-1 | SHA-256 | SHA-512 }
|
||||
* **[System.Management.Automation.PSCredential] Credential** _(Write)_: The credential of a user account with permissions to access the specified archive path and destination if needed.
|
||||
* **[Boolean] Force** _(Write)_: Specifies whether or not any existing files or directories at the destination with the same name as a file or directory in the archive should be overwritten to match the file or directory in the archive. When this property is false, an error will be thrown if an item at the destination needs to be overwritten. The default value is false.
|
||||
|
||||
#### Read-Only Properties from Get-TargetResource
|
||||
|
||||
None
|
||||
|
||||
#### Examples
|
||||
|
||||
* [Expand an archive without file validation](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_ExpandArchiveNoValidation.ps1)
|
||||
* [Expand an archive under a credential without file validation](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_ExpandArchiveNoValidationCredential.ps1)
|
||||
* [Expand an archive with default file validation and file overwrite allowed](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_ExpandArchiveDefaultValidationAndForce.ps1)
|
||||
* [Expand an archive with SHA-256 file validation and file overwrite allowed](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_ExpandArchiveChecksumAndForce.ps1)
|
||||
* [Remove an archive without file validation](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_RemoveArchiveNoValidation.ps1)
|
||||
* [Remove an archive with SHA-256 file validation](https://github.com/PowerShell/PSDscResources/blob/dev/Examples/Sample_Archive_RemoveArchiveChecksum.ps1)
|
||||
|
||||
### Environment
|
||||
|
||||
Provides a mechanism to configure and manage environment variables for a machine or process.
|
||||
@@ -504,8 +537,9 @@ The following parameters will be the same for each process in the set:
|
||||
### Unreleased
|
||||
|
||||
* Enable codecov.io code coverage reporting
|
||||
- Group
|
||||
- Added support for domain based group members on Nano server.
|
||||
* Group
|
||||
* Added support for domain based group members on Nano server.
|
||||
* Added the Archive resource
|
||||
|
||||
### 2.4.0.0
|
||||
|
||||
|
||||
@@ -0,0 +1,852 @@
|
||||
$errorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version 'Latest'
|
||||
|
||||
if ($PSVersionTable.PSVersion.Major -lt 5 -or $PSVersionTable.PSVersion.Minor -lt 1)
|
||||
{
|
||||
Write-Warning -Message 'Cannot run PSDscResources integration tests on PowerShell versions lower than 5.1'
|
||||
return
|
||||
}
|
||||
|
||||
Describe 'Archive End to End Tests' {
|
||||
BeforeAll {
|
||||
# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
|
||||
$testsFolderFilePath = Split-Path $PSScriptRoot -Parent
|
||||
$testHelperFolderFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'TestHelpers'
|
||||
$commonTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'CommonTestHelper.psm1'
|
||||
Import-Module -Name $commonTestHelperFilePath
|
||||
|
||||
$script:testEnvironment = Enter-DscResourceTestEnvironment `
|
||||
-DscResourceModuleName 'PSDscResources' `
|
||||
-DscResourceName 'MSFT_Archive' `
|
||||
-TestType 'Integration'
|
||||
|
||||
# Import Archive resource module for Test-TargetResource
|
||||
$moduleRootFilePath = Split-Path -Path $testsFolderFilePath -Parent
|
||||
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'
|
||||
$archiveResourceFolderFilePath = Join-Path -Path $dscResourcesFolderFilePath -ChildPath 'MSFT_Archive'
|
||||
$archiveResourceModuleFilePath = Join-Path -Path $archiveResourceFolderFilePath -ChildPath 'MSFT_Archive.psm1'
|
||||
Import-Module -Name $archiveResourceModuleFilePath -Force
|
||||
|
||||
# Import the Archive test helper for New-ZipFileFromHashtable and Test-FileStructuresMatch
|
||||
$archiveTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'MSFT_Archive.TestHelper.psm1'
|
||||
Import-Module -Name $archiveTestHelperFilePath -Force
|
||||
|
||||
# Set up the paths to the test configurations
|
||||
$script:confgurationFilePathValidateOnly = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_Archive_ValidateOnly.config.ps1'
|
||||
$script:confgurationFilePathValidateAndChecksum = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_Archive_ValidateAndChecksum.config.ps1'
|
||||
$script:confgurationFilePathCredentialOnly = Join-Path -Path $PSScriptRoot -ChildPath 'MSFT_Archive_CredentialOnly.config.ps1'
|
||||
|
||||
# Create the test archive
|
||||
$script:testArchiveName = 'TestArchive1'
|
||||
|
||||
$testArchiveFileStructure = @{
|
||||
Folder1 = @{}
|
||||
Folder2 = @{
|
||||
Folder21 = @{
|
||||
Folder22 = @{
|
||||
Folder23 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder3 = @{
|
||||
Folder31 = @{
|
||||
Folder31 = @{
|
||||
Folder33 = @{
|
||||
Folder34 = @{
|
||||
File31 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder4 = @{
|
||||
Folder41 = @{
|
||||
Folder42 = @{
|
||||
Folder43 = @{
|
||||
Folder44 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File1 = 'Fake file contents'
|
||||
File2 = 'Fake file contents'
|
||||
}
|
||||
|
||||
$newZipFileFromHashtableParameters = @{
|
||||
Name = $script:testArchiveName
|
||||
ParentPath = $TestDrive
|
||||
ZipFileStructure = $testArchiveFileStructure
|
||||
}
|
||||
|
||||
$script:testArchiveFilePath = New-ZipFileFromHashtable @newZipFileFromHashtableParameters
|
||||
$script:testArchiveFilePathWithoutExtension = $script:testArchiveFilePath.Replace('.zip', '')
|
||||
|
||||
# Create another test archive with the same name and file structure but different file content
|
||||
$script:testArchiveWithDifferentFileContentName = $script:testArchiveName
|
||||
|
||||
$testArchiveFileWithDifferentFileContentStructure = @{
|
||||
Folder1 = @{}
|
||||
Folder2 = @{
|
||||
Folder21 = @{
|
||||
Folder22 = @{
|
||||
Folder23 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder3 = @{
|
||||
Folder31 = @{
|
||||
Folder31 = @{
|
||||
Folder33 = @{
|
||||
Folder34 = @{
|
||||
File31 = 'Different fake file contents'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder4 = @{
|
||||
Folder41 = @{
|
||||
Folder42 = @{
|
||||
Folder43 = @{
|
||||
Folder44 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File1 = 'Different fake file contents'
|
||||
File2 = 'Different fake file contents'
|
||||
}
|
||||
|
||||
$testArchiveFileWithDifferentFileContentParentPath = Join-Path -Path $TestDrive -ChildPath 'MismatchingArchive'
|
||||
$null = New-Item -Path $testArchiveFileWithDifferentFileContentParentPath -ItemType 'Directory'
|
||||
|
||||
$newZipFileFromHashtableParameters = @{
|
||||
Name = $script:testArchiveWithDifferentFileContentName
|
||||
ParentPath = $testArchiveFileWithDifferentFileContentParentPath
|
||||
ZipFileStructure = $testArchiveFileWithDifferentFileContentStructure
|
||||
}
|
||||
|
||||
$script:testArchiveFileWithDifferentFileContentPath = New-ZipFileFromHashtable @newZipFileFromHashtableParameters
|
||||
$script:testArchiveFileWithDifferentFileContentPathWithoutExtension = $script:testArchiveFileWithDifferentFileContentPath.Replace('.zip', '')
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
|
||||
}
|
||||
|
||||
Context 'Expand an archive to a destination that does not yet exist' {
|
||||
$configurationName = 'ExpandArchiveToNonExistentDestination'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'NonExistentDestinationForExpand'
|
||||
|
||||
It 'Destination should not exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive to an existing destination with items that are not in the archive' {
|
||||
$configurationName = 'ExpandArchiveToDestinationWithOtherItems'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithOtherItemsForExpand'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
# Keys are the paths to the items. Values are the items' contents with an empty string denoting a directory
|
||||
$otherItems = @{}
|
||||
|
||||
$otherDirectoryPath = Join-Path -Path $destination -ChildPath 'OtherDirectory'
|
||||
$null = New-Item -Path $otherDirectoryPath -ItemType 'Directory'
|
||||
$otherItems[$otherDirectoryPath] = ''
|
||||
|
||||
$otherSubDirectoryPath = Join-Path -Path $otherDirectoryPath -ChildPath 'OtherSubDirectory'
|
||||
$null = New-Item -Path $otherSubDirectoryPath -ItemType 'Directory'
|
||||
$otherItems[$otherSubDirectoryPath] = ''
|
||||
|
||||
$otherFilePath = Join-Path $otherSubDirectoryPath -ChildPath 'OtherFile'
|
||||
$otherFileContent = 'Other file content'
|
||||
$null = New-Item -Path $otherFilePath -ItemType 'File'
|
||||
$null = Set-Content -Path $otherFilePath -Value $otherFileContent
|
||||
$otherItems[$otherFilePath] = $otherFileContent
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
foreach ($otherItemPath in $otherItems.Keys)
|
||||
{
|
||||
$otherItemName = Split-Path -Path $otherItemPath -Leaf
|
||||
$otherItemExpectedContent = $otherItems[$otherItemPath]
|
||||
$otherItemIsDirectory = [String]::IsNullOrEmpty($otherItemExpectedContent)
|
||||
|
||||
if ($otherItemIsDirectory)
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a directory before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Container' | Should Be $true
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a file before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Leaf' | Should Be $true
|
||||
}
|
||||
|
||||
It "Other file under destination $otherItemName should have the expected content before configuration" {
|
||||
Get-Content -Path $otherItemPath -Raw -ErrorAction 'SilentlyContinue' | Should Be ($otherItemExpectedContent + "`r`n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
foreach ($otherItemPath in $otherItems.Keys)
|
||||
{
|
||||
$otherItemName = Split-Path -Path $otherItemPath -Leaf
|
||||
$otherItemExpectedContent = $otherItems[$otherItemPath]
|
||||
$otherItemIsDirectory = [String]::IsNullOrEmpty($otherItemExpectedContent)
|
||||
|
||||
if ($otherItemIsDirectory)
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a directory after configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Container' | Should Be $true
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a file after configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Leaf' | Should Be $true
|
||||
}
|
||||
|
||||
It "Other file under destination $otherItemName should have the expected after before configuration" {
|
||||
Get-Content -Path $otherItemPath -Raw -ErrorAction 'SilentlyContinue' | Should Be ($otherItemExpectedContent + "`r`n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive to an existing destination that already contains the same archive files' {
|
||||
$configurationName = 'ExpandArchiveToDestinationWithArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMatchingArchiveForExpand'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFilePath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive to a destination that contains archive files that do not match by the specified SHA Checksum without Force specified' {
|
||||
$configurationName = 'ExpandArchiveToDestinationWithMismatchingArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMismatchingArchiveWithSHANoForceForExpand'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFileWithDifferentFileContentPath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should not match the file contents of the archive' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $false
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration and throw an error for attempting to overwrite files without Force specified' {
|
||||
# We don't know which file will throw the error, so we will only check that an error was thrown rather than checking the specific error message
|
||||
{
|
||||
. $script:confgurationFilePathValidateAndChecksum -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should not match the file contents of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive to a destination that contains archive files that do not match by the specified SHA Checksum with Force specified' {
|
||||
$configurationName = 'ExpandArchiveToDestinationWithMismatchingArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMismatchingArchiveWithSHAAndForceForExpand'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFileWithDifferentFileContentPath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should not match the file contents of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $true
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateAndChecksum -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should match the file contents of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive to a destination that contains archive files that match by the specified SHA Checksum' {
|
||||
$configurationName = 'ExpandArchiveToDestinationWithMatchingArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMatchingArchiveWithSHAForExpand'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFilePath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should match the file contents of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Present'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateAndChecksum -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should match the file contents of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an expanded archive from an existing destination that contains only the expanded archive' {
|
||||
$configurationName = 'RemoveArchiveAtDestinationWithArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMatchingArchiveForRemove'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFilePath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should not match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an expanded archive from an existing destination that contains the expanded archive and other files' {
|
||||
$configurationName = 'RemoveArchiveAtDestinationWithArchiveAndOtherFiles'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMatchingArchiveAndOtherFilesForRemove'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFilePath -DestinationPath $destination -Force
|
||||
|
||||
# Keys are the paths to the items. Values are the items' contents with an empty string denoting a directory
|
||||
$otherItems = @{}
|
||||
|
||||
$otherDirectoryPath = Join-Path -Path $destination -ChildPath 'OtherDirectory'
|
||||
$null = New-Item -Path $otherDirectoryPath -ItemType 'Directory'
|
||||
$otherItems[$otherDirectoryPath] = ''
|
||||
|
||||
$otherSubDirectoryPath = Join-Path -Path $otherDirectoryPath -ChildPath 'OtherSubDirectory'
|
||||
$null = New-Item -Path $otherSubDirectoryPath -ItemType 'Directory'
|
||||
$otherItems[$otherSubDirectoryPath] = ''
|
||||
|
||||
$otherFilePath = Join-Path $otherSubDirectoryPath -ChildPath 'OtherFile'
|
||||
$otherFileContent = 'Other file content'
|
||||
$null = New-Item -Path $otherFilePath -ItemType 'File'
|
||||
$null = Set-Content -Path $otherFilePath -Value $otherFileContent
|
||||
$otherItems[$otherFilePath] = $otherFileContent
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
foreach ($otherItemPath in $otherItems.Keys)
|
||||
{
|
||||
$otherItemName = Split-Path -Path $otherItemPath -Leaf
|
||||
$otherItemExpectedContent = $otherItems[$otherItemPath]
|
||||
$otherItemIsDirectory = [String]::IsNullOrEmpty($otherItemExpectedContent)
|
||||
|
||||
if ($otherItemIsDirectory)
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a directory before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Container' | Should Be $true
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a file before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Leaf' | Should Be $true
|
||||
}
|
||||
|
||||
It "Other file under destination $otherItemName should have the expected content before configuration" {
|
||||
Get-Content -Path $otherItemPath -Raw -ErrorAction 'SilentlyContinue' | Should Be ($otherItemExpectedContent + "`r`n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
foreach ($otherItemPath in $otherItems.Keys)
|
||||
{
|
||||
$otherItemName = Split-Path -Path $otherItemPath -Leaf
|
||||
$otherItemExpectedContent = $otherItems[$otherItemPath]
|
||||
$otherItemIsDirectory = [String]::IsNullOrEmpty($otherItemExpectedContent)
|
||||
|
||||
if ($otherItemIsDirectory)
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a directory before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Container' | Should Be $true
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
It "Other item under destination $otherItemName should exist as a file before configuration" {
|
||||
Test-Path -Path $otherItemPath -PathType 'Leaf' | Should Be $true
|
||||
}
|
||||
|
||||
It "Other file under destination $otherItemName should have the expected content before configuration" {
|
||||
Get-Content -Path $otherItemPath -Raw -ErrorAction 'SilentlyContinue' | Should Be ($otherItemExpectedContent + "`r`n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
It 'File structure of destination should not match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an expanded archive from an existing destination that does not contain any archive files' {
|
||||
$configurationName = 'RemoveArchiveAtDestinationWithoutArchive'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'EmptyDestinationForRemove'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should not match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should not match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an expanded archive from a destination that does not exist' {
|
||||
$configurationName = 'RemoveArchiveFromMissingDestination'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'NonexistentDestinationForRemove'
|
||||
|
||||
It 'Destination should not exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateOnly -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should not exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an archive from a destination that contains archive files that do not match by the specified SHA Checksum' {
|
||||
$configurationName = 'RemoveArchiveFromDestinationWithMismatchingArchiveWithSHA'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMismatchingArchiveWithSHAForRemove'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFileWithDifferentFileContentPath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should not match the file contents of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $true
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateAndChecksum -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File contents of destination should not match the file contents of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return true from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an archive from a destination that contains archive files that match by the specified SHA Checksum' {
|
||||
$configurationName = 'RemoveArchiveFromDestinationWithMatchingArchiveWithSHA'
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'DestinationWithMatchingArchiveWithSHAForRemove'
|
||||
$null = New-Item -Path $destination -ItemType 'Directory'
|
||||
|
||||
$null = Expand-Archive -Path $script:testArchiveFilePath -DestinationPath $destination -Force
|
||||
|
||||
It 'Destination should exist before configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure and contents of destination should match the file contents of the archive before configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
$archiveParameters = @{
|
||||
Path = $script:testArchiveFilePath
|
||||
Destination = $destination
|
||||
Ensure = 'Absent'
|
||||
Validate = $true
|
||||
Checksum = 'SHA-256'
|
||||
Force = $true
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters before configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should compile and run configuration' {
|
||||
{
|
||||
. $script:confgurationFilePathValidateAndChecksum -ConfigurationName $configurationName
|
||||
& $configurationName -OutputPath $TestDrive @archiveParameters
|
||||
Start-DscConfiguration -Path $TestDrive -ErrorAction 'Stop' -Wait -Force
|
||||
} | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Destination should exist after configuration' {
|
||||
Test-Path -Path $destination | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure of destination should not match the file structure of the archive after configuration' {
|
||||
Test-FileStructuresMatch -SourcePath $script:testArchiveFilePathWithoutExtension -DestinationPath $destination | Should Be $false
|
||||
}
|
||||
|
||||
It 'Should return false from Test-TargetResource with the same parameters after configuration' {
|
||||
MSFT_Archive\Test-TargetResource @archiveParameters | Should Be $true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,743 @@
|
||||
$errorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version 'Latest'
|
||||
|
||||
if ($PSVersionTable.PSVersion.Major -lt 5 -or $PSVersionTable.PSVersion.Minor -lt 1)
|
||||
{
|
||||
Write-Warning -Message 'Cannot run PSDscResources integration tests on PowerShell versions lower than 5.1'
|
||||
return
|
||||
}
|
||||
|
||||
Describe 'Archive Integration Tests' {
|
||||
BeforeAll {
|
||||
# Import CommonTestHelper for Enter-DscResourceTestEnvironment, Exit-DscResourceTestEnvironment
|
||||
$testsFolderFilePath = Split-Path $PSScriptRoot -Parent
|
||||
$testHelperFolderFilePath = Join-Path -Path $testsFolderFilePath -ChildPath 'TestHelpers'
|
||||
$commonTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'CommonTestHelper.psm1'
|
||||
Import-Module -Name $commonTestHelperFilePath
|
||||
|
||||
$script:testEnvironment = Enter-DscResourceTestEnvironment `
|
||||
-DscResourceModuleName 'PSDscResources' `
|
||||
-DscResourceName 'MSFT_Archive' `
|
||||
-TestType 'Unit'
|
||||
|
||||
# Import archive test helper for New-ZipFileFromHashtable, Test-FileStructuresMatch
|
||||
$archiveTestHelperFilePath = Join-Path -Path $testHelperFolderFilePath -ChildPath 'MSFT_Archive.TestHelper.psm1'
|
||||
Import-Module -Name $archiveTestHelperFilePath -Force
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
Exit-DscResourceTestEnvironment -TestEnvironment $script:testEnvironment
|
||||
}
|
||||
|
||||
Context 'Expand a basic archive' {
|
||||
$zipFileName = 'BasicArchive1'
|
||||
$subfolderName = 'Folder1'
|
||||
|
||||
$zipFileStructure = @{
|
||||
$subfolderName = @{
|
||||
File1 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destinationDirectoryName = 'ExpandBasicArchive'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove a basic archive' {
|
||||
$zipFileName = 'BasicArchive2'
|
||||
$subfolderName = 'Folder1'
|
||||
|
||||
$zipFileStructure = @{
|
||||
$subfolderName = @{
|
||||
File1 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destinationDirectoryName = 'RemoveBasicArchive'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive with nested directories' {
|
||||
$zipFileName = 'NestedArchive1'
|
||||
|
||||
$zipFileStructure = @{
|
||||
Folder1 = @{}
|
||||
Folder2 = @{
|
||||
Folder21 = @{
|
||||
Folder22 = @{
|
||||
Folder23 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder3 = @{
|
||||
Folder31 = @{
|
||||
Folder31 = @{
|
||||
Folder33 = @{
|
||||
Folder34 = @{
|
||||
File31 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder4 = @{
|
||||
Folder41 = @{
|
||||
Folder42 = @{
|
||||
Folder43 = @{
|
||||
Folder44 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File1 = 'Fake file contents'
|
||||
File2 = 'Fake file contents'
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destinationDirectoryName = 'ExpandNestedArchive'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an archive with nested directories' {
|
||||
$zipFileName = 'NestedArchive2'
|
||||
|
||||
$zipFileStructure = @{
|
||||
Folder1 = @{}
|
||||
Folder2 = @{
|
||||
Folder21 = @{
|
||||
Folder22 = @{
|
||||
Folder23 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder3 = @{
|
||||
Folder31 = @{
|
||||
Folder31 = @{
|
||||
Folder33 = @{
|
||||
Folder34 = @{
|
||||
File31 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Folder4 = @{
|
||||
Folder41 = @{
|
||||
Folder42 = @{
|
||||
Folder43 = @{
|
||||
Folder44 = @{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File1 = 'Fake file contents'
|
||||
File2 = 'Fake file contents'
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destinationDirectoryName = 'RemoveNestedArchive'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Expand an archive when another archive with the same timestamp exists in the same folder' {
|
||||
$zipFileName1 = 'SameTimestamp1'
|
||||
|
||||
$zipFileStructure1 = @{
|
||||
Folder1 = @{
|
||||
File1 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
|
||||
$zipFilePath1 = New-ZipFileFromHashtable -Name $zipFileName1 -ParentPath $TestDrive -ZipFileStructure $zipFileStructure1
|
||||
|
||||
$zipFileName2 = 'SameTimestamp2'
|
||||
|
||||
$zipFileStructure2 = @{
|
||||
Folder2 = @{
|
||||
File2 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
|
||||
$zipFilePath2 = New-ZipFileFromHashtable -Name $zipFileName2 -ParentPath $TestDrive -ZipFileStructure $zipFileStructure2
|
||||
|
||||
$currentTimestamp = Get-Date
|
||||
$null = Set-ItemProperty -Path $zipFilePath1 -Name 'LastWriteTime' -Value $currentTimestamp
|
||||
$null = Set-ItemProperty -Path $zipFilePath2 -Name 'LastWriteTime' -Value $currentTimestamp
|
||||
|
||||
$destinationDirectoryName = 'ArchiveWithMatchingTimeStampDestination'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath1.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false for specified archive before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath1 -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true for specified archive before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath1 -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false for other archive with same timestamp with before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath2 -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true for other archive with same timestamp before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath2 -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath1 -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath1.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true for specified archive after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath1 -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false for specified archive after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath1 -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false for other archive with same timestamp with before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath2 -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true for other archive with same timestamp before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath2 -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Remove an archive with an extra file in a nested directory' {
|
||||
$zipFileName = 'NestedArchiveWithAdd'
|
||||
|
||||
$zipFileStructure = @{
|
||||
Folder1 = @{
|
||||
Folder11 = @{
|
||||
Folder12 = @{
|
||||
Folder13 = @{
|
||||
Folder14 = @{
|
||||
File11 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
File1 = 'Fake file contents'
|
||||
File2 = 'Fake file contents'
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destinationDirectoryName = 'RemoveArchiveWithExtra'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
$newFilePath = "$destinationDirectoryPath\Folder1\Folder11\Folder12\AddedFile"
|
||||
$null = New-Item -Path $newFilePath -ItemType 'File'
|
||||
$null = Set-Content -Path $newFilePath -Value 'Fake text'
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Extra file should be present before Set-TargetResource' {
|
||||
Test-Path -Path $newFilePath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Extra file should be present after Set-TargetResource' {
|
||||
Test-Path -Path $newFilePath | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
$possibleChecksumValues = @( 'SHA-1', 'SHA-256', 'SHA-512', 'CreatedDate', 'ModifiedDate' )
|
||||
|
||||
$zipFileName = 'ChecksumWithModifiedFile'
|
||||
$fileToEditName = 'File1'
|
||||
$fileNotToEditName = 'File2'
|
||||
|
||||
$zipFileStructure = @{
|
||||
$fileToEditName = 'Fake file contents'
|
||||
$fileNotToEditName = 'Fake file contents'
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
foreach ($possibleChecksumValue in $possibleChecksumValues)
|
||||
{
|
||||
Context "Expand an archive with an edited file, Validate and Force specified, and Checksum specified as $possibleChecksumValue" {
|
||||
$destinationDirectoryName = 'ExpandModifiedArchiveWithCheck'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
# This foreach loop with the Open-Archive call is needed to set the timestamps of the files at the destination correctly
|
||||
$destinationChildItems = Get-ChildItem -Path $destinationDirectoryPath -Recurse -File
|
||||
foreach ($destinationChildItem in $destinationChildItems)
|
||||
{
|
||||
$correspondingZipItemPath = $destinationChildItem.FullName.Replace($destinationDirectoryPath + '\', '')
|
||||
$archive = Open-Archive -Path $zipFilePath
|
||||
|
||||
try
|
||||
{
|
||||
$matchingArchiveEntry = $archive.Entries | Where-Object -FilterScript {$_.FullName -eq $correspondingZipItemPath }
|
||||
$archiveEntryLastWriteTime = Get-Date -Date $matchingArchiveEntry.LastWriteTime.DateTime -Format 'G'
|
||||
}
|
||||
finally
|
||||
{
|
||||
$null = $archive.Dispose()
|
||||
}
|
||||
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'CreationTime' -Value $archiveEntryLastWriteTime
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'LastWriteTime' -Value $archiveEntryLastWriteTime
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before file edit' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before file edit' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
$fileToEditPath = Join-Path -Path $destinationDirectoryPath -ChildPath $fileToEditName
|
||||
|
||||
It 'File to edit should exist at the destination before Set-TargetResource' {
|
||||
Test-Path -Path $fileToEditPath | Should Be $true
|
||||
}
|
||||
|
||||
$fileBeforeEdit = Get-Item -Path $fileToEditPath
|
||||
$lastWriteTimeBeforeEdit = $fileBeforeEdit.LastWriteTime
|
||||
$creationTimeBeforeEdit = $fileBeforeEdit.CreationTime
|
||||
|
||||
$null = Set-Content -Path $fileToEditPath -Value 'Different false text' -Force
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'LastWriteTime' -Value ([DateTime]::MaxValue)
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'CreationTime' -Value ([DateTime]::MaxValue)
|
||||
|
||||
$fileAfterEdit = Get-Item -Path $fileToEditPath
|
||||
|
||||
It 'Edited file at the destination should have different content than the same file in the archive before Set-TargetResource' {
|
||||
Get-Content -Path $fileToEditPath -Raw | Should Not Be ($zipFileStructure[$fileToEditName] + "`r`n")
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different last write time than the same file in the archive before Set-TargetResource' {
|
||||
$fileAfterEdit.LastWriteTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different creation time than the last write time of the the same file in the archive before Set-TargetResource' {
|
||||
$fileAfterEdit.CreationTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue -Force $true } | Should Not Throw
|
||||
}
|
||||
|
||||
$fileAfterSetTargetResource = Get-Item -Path $fileToEditPath
|
||||
|
||||
It 'Edited file should exist at the destination after Set-TargetResource' {
|
||||
Test-Path -Path $fileToEditPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have the same content as the same file in the archive after Set-TargetResource' {
|
||||
Get-Content -Path $fileToEditPath -Raw | Should Be ($zipFileStructure[$fileToEditName] + "`r`n")
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have the same last write time as the same file in the archive after Set-TargetResource' {
|
||||
$fileAfterSetTargetResource.LastWriteTime | Should Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have the same creation time as last write time of the the same file in the archive after Set-TargetResource' {
|
||||
$fileAfterSetTargetResource.CreationTime | Should Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $true
|
||||
}
|
||||
}
|
||||
|
||||
Context "Expand an archive with an edited file, Validate specfied, Force not specified, and Checksum specified as $possibleChecksumValue" {
|
||||
$destinationDirectoryName = 'ExpandModifiedArchiveWithCheckFail'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
# This foreach loop with the Open-Archive call is needed to set the timestamps of the files at the destination correctly
|
||||
$destinationChildItems = Get-ChildItem -Path $destinationDirectoryPath -Recurse -File
|
||||
foreach ($destinationChildItem in $destinationChildItems)
|
||||
{
|
||||
$correspondingZipItemPath = $destinationChildItem.FullName.Replace($destinationDirectoryPath + '\', '')
|
||||
$archive = Open-Archive -Path $zipFilePath
|
||||
|
||||
try
|
||||
{
|
||||
$matchingArchiveEntry = $archive.Entries | Where-Object -FilterScript {$_.FullName -eq $correspondingZipItemPath }
|
||||
$archiveEntryLastWriteTime = Get-Date -Date $matchingArchiveEntry.LastWriteTime.DateTime -Format 'G'
|
||||
}
|
||||
finally
|
||||
{
|
||||
$null = $archive.Dispose()
|
||||
}
|
||||
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'CreationTime' -Value $archiveEntryLastWriteTime
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'LastWriteTime' -Value $archiveEntryLastWriteTime
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before file edit' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before file edit' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
$fileToEditPath = Join-Path -Path $destinationDirectoryPath -ChildPath $fileToEditName
|
||||
|
||||
It 'File to edit should exist at the destination before Set-TargetResource' {
|
||||
Test-Path -Path $fileToEditPath | Should Be $true
|
||||
}
|
||||
|
||||
$fileBeforeEdit = Get-Item -Path $fileToEditPath
|
||||
$lastWriteTimeBeforeEdit = $fileBeforeEdit.LastWriteTime
|
||||
$creationTimeBeforeEdit = $fileBeforeEdit.CreationTime
|
||||
|
||||
$null = Set-Content -Path $fileToEditPath -Value 'Different false text' -Force
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'LastWriteTime' -Value ([DateTime]::MaxValue)
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'CreationTime' -Value ([DateTime]::MaxValue)
|
||||
|
||||
$fileAfterEdit = Get-Item -Path $fileToEditPath
|
||||
|
||||
It 'Edited file at the destination should have different content than the same file in the archive before Set-TargetResource' {
|
||||
Get-Content -Path $fileToEditPath -Raw | Should Not Be ($zipFileStructure[$fileToEditName] + "`r`n")
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different last write time than the same file in the archive before Set-TargetResource' {
|
||||
$fileAfterEdit.LastWriteTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different creation time than the last write time of the the same file in the archive before Set-TargetResource' {
|
||||
$fileAfterEdit.CreationTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue } | Should Throw
|
||||
}
|
||||
|
||||
Context "Remove an archive with an edited file, Validate specified, and Checksum specified as $possibleChecksumValue" {
|
||||
$destinationDirectoryName = 'RemoveModifiedArchiveWithCheck'
|
||||
$destinationDirectoryPath = Join-Path -Path $TestDrive -ChildPath $destinationDirectoryName
|
||||
|
||||
$null = Expand-Archive -Path $zipFilePath -DestinationPath $destinationDirectoryPath -Force
|
||||
|
||||
# This foreach loop with the Open-Archive call is needed to set the timestamps of the files at the destination correctly
|
||||
$destinationChildItems = Get-ChildItem -Path $destinationDirectoryPath -Recurse -File
|
||||
foreach ($destinationChildItem in $destinationChildItems)
|
||||
{
|
||||
$correspondingZipItemPath = $destinationChildItem.FullName.Replace($destinationDirectoryPath + '\', '')
|
||||
$archive = Open-Archive -Path $zipFilePath
|
||||
|
||||
try
|
||||
{
|
||||
$matchingArchiveEntry = $archive.Entries | Where-Object -FilterScript {$_.FullName -eq $correspondingZipItemPath }
|
||||
$archiveEntryLastWriteTime = Get-Date -Date $matchingArchiveEntry.LastWriteTime.DateTime -Format 'G'
|
||||
}
|
||||
finally
|
||||
{
|
||||
$null = $archive.Dispose()
|
||||
}
|
||||
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'CreationTime' -Value $archiveEntryLastWriteTime
|
||||
$null = Set-ItemProperty -Path $destinationChildItem.FullName -Name 'LastWriteTime' -Value $archiveEntryLastWriteTime
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return true before file edit' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return false before file edit' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
$fileToEditPath = Join-Path -Path $destinationDirectoryPath -ChildPath $fileToEditName
|
||||
|
||||
It 'File to edit should exist at the destination before Set-TargetResource' {
|
||||
Test-Path -Path $fileToEditPath | Should Be $true
|
||||
}
|
||||
|
||||
$fileBeforeEdit = Get-Item -Path $fileToEditPath
|
||||
$lastWriteTimeBeforeEdit = $fileBeforeEdit.LastWriteTime
|
||||
$creationTimeBeforeEdit = $fileBeforeEdit.CreationTime
|
||||
|
||||
$null = Set-Content -Path $fileToEditPath -Value 'Different false text' -Force
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'LastWriteTime' -Value ([DateTime]::MaxValue)
|
||||
Set-ItemProperty -Path $fileToEditPath -Name 'CreationTime' -Value ([DateTime]::MaxValue)
|
||||
|
||||
$fileAfterEdit = Get-Item -Path $fileToEditPath
|
||||
|
||||
It 'Edited file at the destination should have the edited content' {
|
||||
Get-Content -Path $fileToEditPath -Raw | Should Be ('Different false text' + "`r`n")
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different last write time than the same file in the archive after file edit and before Set-TargetResource' {
|
||||
$fileAfterEdit.LastWriteTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different creation time than the last write time of the the same file in the archive after file edit before Set-TargetResource' {
|
||||
$fileAfterEdit.CreationTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true before Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive before Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue } | Should Not Throw
|
||||
}
|
||||
|
||||
$fileAfterSetTargetResource = Get-Item -Path $fileToEditPath
|
||||
|
||||
It 'Edited file should exist at the destination after Set-TargetResource' {
|
||||
Test-Path -Path $fileToEditPath | Should Be $true
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have the edited content' {
|
||||
Get-Content -Path $fileToEditPath -Raw | Should Be ('Different false text' + "`r`n")
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different last write time than the same file in the archive after Set-TargetResource' {
|
||||
$fileAfterEdit.LastWriteTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Edited file at the destination should have different creation time than the last write time of the the same file in the archive after Set-TargetResource' {
|
||||
$fileAfterEdit.CreationTime | Should Not Be $lastWriteTimeBeforeEdit
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Present should return false after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $false
|
||||
}
|
||||
|
||||
It 'Test-TargetResource with Ensure as Absent should return true after Set-TargetResource' {
|
||||
Test-TargetResource -Ensure 'Absent' -Path $zipFilePath -Destination $destinationDirectoryPath -Validate $true -Checksum $possibleChecksumValue | Should Be $true
|
||||
}
|
||||
|
||||
It 'File structure and contents of the destination should not match the file structure and contents of the archive after Set-TargetResource' {
|
||||
Test-FileStructuresMatch -SourcePath $zipFilePath.Replace('.zip', '') -DestinationPath $destinationDirectoryPath -CheckContents | Should Be $false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Context 'Archive name contains a bracket' {
|
||||
$zipFileName = 'ReturnCorrectValue['
|
||||
|
||||
$zipFileStructure = @{
|
||||
Folder1 = @{
|
||||
File1 = 'Fake file contents'
|
||||
}
|
||||
}
|
||||
|
||||
$zipFilePath = New-ZipFileFromHashtable -Name $zipFileName -ParentPath $TestDrive -ZipFileStructure $zipFileStructure
|
||||
|
||||
$destination = Join-Path -Path $TestDrive -ChildPath 'ArchiveNameWithBracket'
|
||||
|
||||
It 'Set-TargetResource should not throw' {
|
||||
{ Set-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destination } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Get-TargetResource should not throw' {
|
||||
{ $null = Get-TargetResource -Path $zipFilePath -Destination $destination } | Should Not Throw
|
||||
}
|
||||
|
||||
It 'Test-TargetResource should not throw' {
|
||||
{ $null = Test-TargetResource -Ensure 'Present' -Path $zipFilePath -Destination $destination } | Should Not Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$ConfigurationName
|
||||
)
|
||||
|
||||
Configuration $ConfigurationName
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Path,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Destination,
|
||||
|
||||
[Parameter()]
|
||||
[ValidateSet('Present', 'Absent')]
|
||||
[String]
|
||||
$Ensure = 'Present',
|
||||
|
||||
[Parameter()]
|
||||
[System.Management.Automation.PSCredential]
|
||||
[System.Management.Automation.Credential()]
|
||||
$Credential
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive1
|
||||
{
|
||||
Path = $Path
|
||||
Destination = $Destination
|
||||
Ensure = $Ensure
|
||||
Credential = $Credential
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$ConfigurationName
|
||||
)
|
||||
|
||||
Configuration $ConfigurationName
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Path,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Destination,
|
||||
|
||||
[Parameter()]
|
||||
[ValidateSet('Present', 'Absent')]
|
||||
[String]
|
||||
$Ensure = 'Present',
|
||||
|
||||
[Parameter()]
|
||||
[Boolean]
|
||||
$Validate = $false,
|
||||
|
||||
[Parameter()]
|
||||
[String]
|
||||
$Checksum = 'SHA-256',
|
||||
|
||||
[Parameter()]
|
||||
[Boolean]
|
||||
$Force = $false
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive1
|
||||
{
|
||||
Path = $Path
|
||||
Destination = $Destination
|
||||
Ensure = $Ensure
|
||||
Validate = $Validate
|
||||
Checksum = $Checksum
|
||||
Force = $Force
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[String]
|
||||
$ConfigurationName
|
||||
)
|
||||
|
||||
Configuration $ConfigurationName
|
||||
{
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Path,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Destination,
|
||||
|
||||
[Parameter()]
|
||||
[ValidateSet('Present', 'Absent')]
|
||||
[String]
|
||||
$Ensure = 'Present',
|
||||
|
||||
[Parameter()]
|
||||
[Boolean]
|
||||
$Validate = $false
|
||||
)
|
||||
|
||||
Import-DscResource -ModuleName 'PSDscResources'
|
||||
|
||||
Node localhost
|
||||
{
|
||||
Archive Archive1
|
||||
{
|
||||
Path = $Path
|
||||
Destination = $Destination
|
||||
Ensure = $Ensure
|
||||
Validate = $Validate
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
$errorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version 'Latest'
|
||||
|
||||
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Converts the specified hashtable to a file structure under the specified parent path.
|
||||
|
||||
.PARAMETER ParentPath
|
||||
The path to the directory that should contain the specified file structure.
|
||||
|
||||
.PARAMETER FileStructure
|
||||
The hashtable defining the file structure.
|
||||
Nested hashtable values denote directories with the key as the name of the directory and the
|
||||
hashtable value as the contents.
|
||||
String values denote files with the key as the name of the file and the value as the contents.
|
||||
|
||||
.EXAMPLE
|
||||
$fileStructure = @{
|
||||
DirectoryName = @{
|
||||
FileName = 'File contents'
|
||||
}
|
||||
}
|
||||
|
||||
ConvertTo-FileStructure -ParentPath $env:temp -FileStructure $fileStructure
|
||||
#>
|
||||
function ConvertTo-FileStructure
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$ParentPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[Hashtable]
|
||||
$FileStructure
|
||||
)
|
||||
|
||||
foreach ($itemName in $FileStructure.Keys)
|
||||
{
|
||||
if ($FileStructure[$itemName] -is [Hashtable])
|
||||
{
|
||||
$newDirectoryPath = Join-Path -Path $ParentPath -ChildPath $itemName
|
||||
$null = New-Item -Path $newDirectoryPath -ItemType 'Directory'
|
||||
ConvertTo-FileStructure -ParentPath $newDirectoryPath -FileStructure $FileStructure[$itemName]
|
||||
}
|
||||
elseif ($FileStructure[$itemName] -is [String])
|
||||
{
|
||||
$newFilePath = Join-Path -Path $ParentPath -ChildPath $itemName
|
||||
$null = New-Item -Path $newFilePath -ItemType 'File'
|
||||
Set-Content -LiteralPath $newFilePath -Value $FileStructure[$itemName]
|
||||
}
|
||||
else
|
||||
{
|
||||
throw 'Zip file structure must be made of strings and hashtable values. Found a different type.'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Creates a new zip file with the specified name and file structure under the specified parent path.
|
||||
Returns the file path to the compressed zip file.
|
||||
|
||||
.PARAMETER ParentPath
|
||||
The path under which the new zip file should be created.
|
||||
|
||||
.PARAMETER Name
|
||||
The name of the zip file to create.
|
||||
|
||||
.PARAMETER ZipFileStructure
|
||||
The hashtable defining the file structure.
|
||||
Nested hashtable values denote directories with the key as the name of the directory and the
|
||||
hashtable value as the contents.
|
||||
String values denote files with the key as the name of the file and the value as the contents.
|
||||
|
||||
.EXAMPLE
|
||||
$zipFileStructure = @{
|
||||
DirectoryName = @{
|
||||
FileName = 'File contents'
|
||||
}
|
||||
}
|
||||
|
||||
New-ZipFileFromHashtable -ParentPath $env:temp -Name 'ArchiveName' -ZipFileStructure $zipFileStructure
|
||||
|
||||
.NOTES
|
||||
The expanded file structure that the zip file is created from is not removed as part of this function.
|
||||
This is to allow tests to compare the structure of the 'zip file' against another file structure.
|
||||
#>
|
||||
function New-ZipFileFromHashtable
|
||||
{
|
||||
[OutputType([String])]
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$ParentPath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String]
|
||||
$Name,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNull()]
|
||||
[Hashtable]
|
||||
$ZipFileStructure
|
||||
)
|
||||
|
||||
$expandedZipPath = Join-Path -Path $ParentPath -ChildPath $Name
|
||||
$compressedZipPath = Join-Path -Path $ParentPath -ChildPath "$Name.zip"
|
||||
|
||||
$null = New-Item -Path $expandedZipPath -ItemType 'Directory'
|
||||
ConvertTo-FileStructure -ParentPath $expandedZipPath -FileStructure $ZipFileStructure
|
||||
|
||||
$null = [System.IO.Compression.ZipFile]::CreateFromDirectory($expandedZipPath, $compressedZipPath, 'NoCompression', $false)
|
||||
|
||||
return $compressedZipPath
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Tests if the two specified file structures match.
|
||||
|
||||
.PARAMETER SourcePath
|
||||
The path to the root of the file structure to test against.
|
||||
|
||||
.PARAMETER DestinationPath
|
||||
The path to the root of the file structure to test
|
||||
|
||||
.PARAMETER CheckLastWriteTime
|
||||
Indicates whether or not to test that the last write times of the files in the structure match.
|
||||
|
||||
.PARAMETER CheckCreationTime
|
||||
Indicates whether or not to test that the creation times of the files in the structure match.
|
||||
|
||||
.PARAMETER CheckContents
|
||||
Indicates whether or not to test that the contents of the files in the structure match.
|
||||
#>
|
||||
function Test-FileStructuresMatch
|
||||
{
|
||||
[CmdletBinding()]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String] $SourcePath,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[String] $DestinationPath,
|
||||
|
||||
[Parameter()]
|
||||
[Switch] $CheckLastWriteTime,
|
||||
|
||||
[Parameter()]
|
||||
[Switch] $CheckCreationTime,
|
||||
|
||||
[Parameter()]
|
||||
[Switch] $CheckContents
|
||||
)
|
||||
|
||||
$sourcePathLength = $SourcePath.Length
|
||||
$destinationPathLength = $DestinationPath.Length
|
||||
|
||||
$destinationContents = @{}
|
||||
$destinationChildItems = Get-ChildItem -Path $DestinationPath -Recurse
|
||||
|
||||
foreach ($destinationChildItem in $destinationChildItems)
|
||||
{
|
||||
$destinationChildItemName = Split-Path -Path $destinationChildItem.FullName -Leaf
|
||||
$destinationContents[$destinationChildItemName] = $destinationChildItem
|
||||
}
|
||||
|
||||
$sourceChildItems = Get-ChildItem -Path $SourcePath -Recurse
|
||||
|
||||
foreach ($sourceChildItem in $sourceChildItems)
|
||||
{
|
||||
$sourceChildItemName = Split-Path -Path $sourceChildItem.FullName -Leaf
|
||||
$destinationChildItem = $destinationContents[$sourceChildItemName]
|
||||
|
||||
if ($destinationChildItem -eq $null)
|
||||
{
|
||||
return $false
|
||||
}
|
||||
else
|
||||
{
|
||||
if (-not $destinationChildItem.GetType() -eq $sourceChildItem.GetType())
|
||||
{
|
||||
return $false
|
||||
}
|
||||
|
||||
if ($destinationChildItem.GetType() -eq [System.IO.FileInfo])
|
||||
{
|
||||
if ($CheckLastWriteTime)
|
||||
{
|
||||
if ($sourceChildItem.LastWriteTime -ne $destinationChildItem.LastWriteTime)
|
||||
{
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
if ($CheckCreationTime)
|
||||
{
|
||||
if ($sourceChildItem.CreationTime -ne $destinationChildItem.CreationTime)
|
||||
{
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
if ($CheckContents)
|
||||
{
|
||||
$sourceFileContents = Get-Content -Path $sourceChildItem.FullName -Raw
|
||||
$destinationFileContents = Get-Content -Path $destinationChildItem.FullName -Raw
|
||||
|
||||
if ($sourceFileContents -ne $destinationFileContents)
|
||||
{
|
||||
return $false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $true
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function @( 'New-ZipFileFromHashtable', 'Test-FileStructuresMatch' )
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user