Skip to content

File Directory and File Content resource #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ if (!$SkipBuild) {
"osinfo",
"powershell-adapter",
"process",
"resources/filesys",
"runcommandonset",
"tools/dsctest",
"tools/test_group_resource",
Expand Down
13 changes: 13 additions & 0 deletions dsc/examples/faultydriver_detect.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: check for CrowdStrike driver
type: Microsoft.Windows/Driver
properties:
name: CSAgent

- name: check if faulty driver of CrowdStrike exists under system32\drivers
type: Microsoft.DSC/FileSystem
properties:
path: "[path(systemRoot(), 'windows', 'system32', 'drivers', 'crowdstrike', 'C-00000291-00000000-00000026.sys')]"
hash: '6454cc4779c1c8387c547099181cfcbecc17401fb22750431c15a3b02f8243ea'
9 changes: 9 additions & 0 deletions dsc/examples/faultydriver_recovery.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: check if faulty driver of CrowdStrike exists under system32\drivers
type: Microsoft.DSC/FileSystem
properties:
path: "[path(systemRoot(), 'windows', 'system32', 'drivers', 'crowdstrike', 'C-00000291-00000000-00000026.sys')]"
_exist: False
hash: '6454cc4779c1c8387c547099181cfcbecc17401fb22750431c15a3b02f8243ea'
8 changes: 8 additions & 0 deletions dsc/examples/filesys_create.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Create empty file
type: Microsoft.DSC/File
properties:
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
_exist: true
8 changes: 8 additions & 0 deletions dsc/examples/filesys_create_parent.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Create empty file
type: Microsoft.DSC/File
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-file-resource.txt')]"
_exist: true
8 changes: 8 additions & 0 deletions dsc/examples/filesys_delete.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Create empty file
type: Microsoft.DSC/File
properties:
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
_exist: false
8 changes: 8 additions & 0 deletions dsc/examples/filesys_dir_create.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Create empty directory
type: Microsoft.DSC/Directory
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
_exist: true
8 changes: 8 additions & 0 deletions dsc/examples/filesys_dir_create_parent.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Create empty directory
type: Microsoft.DSC/Directory
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-sub-dir-resource')]"
_exist: true
8 changes: 8 additions & 0 deletions dsc/examples/filesys_dir_delete.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Delete empty directory
type: Microsoft.DSC/Directory
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
_exist: false
9 changes: 9 additions & 0 deletions dsc/examples/filesys_dir_delete_recurse.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Delete non-empty directory
type: Microsoft.DSC/Directory
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource')]"
recurse: true
_exist: false
9 changes: 9 additions & 0 deletions dsc/examples/filesys_filecontent.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Set file content
type: Microsoft.DSC/FileContent
properties:
path: "[path(envvar('TEMP'), 'test-file-resource.txt')]"
content: "Hello, World!"
_exist: true
9 changes: 9 additions & 0 deletions dsc/examples/filesys_filecontent_parent.dsc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Example configuration mixing native app resources with classic PS resources
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Set file content
type: Microsoft.DSC/FileContent
properties:
path: "[path(envvar('TEMP'), 'test-dir-resource', 'test-file-resource.txt')]"
content: "Hello, World!"
_exist: true
138 changes: 138 additions & 0 deletions dsc/tests/filesys.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'FileSys resoure tests' {
BeforeAll {
$testDir = Join-Path $env:TEMP 'test-dir-resource'
$testFile = Join-Path $testDir 'test-file-resource.txt'
$testFileName = 'test-file-resource.txt'
}

It 'Filesys resource can create file' {
if (Test-Path $testFile) {
Remove-Item -Path $testFile -Force
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$path = $resultJson.results.result.afterState.path
$path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
}

It 'Filesys resource can create directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
}

It 'Filesys resource can create file with content' {
$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse

$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Exist
Get-Content $resultFilePath | Should -Be "Hello, World!"
}

It 'Filesys resource can delete a file' {
if (-not (Test-Path $testFile)) {
New-Item -Path $testFile -ItemType File -Force | Out-Null
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Not -Exist
}

It 'Filesys resource can delete an empty directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

New-Item -Path $testDir -ItemType Directory -Force | Out-Null

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultDirPath = $resultJson.results.result.afterState.path
$resultDirPath | Should -Not -Exist
}

It 'Filesys resource cannot delete a non-empty directory' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

New-Item -Path $testDir -ItemType Directory -Force | Out-Null
New-Item -Path (Join-Path $testDir $testFileName) -ItemType File -Force | Out-Null

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Not -Be 0
$testDir | Should -Exist
}

It 'Filesys resource can delete a directory recursively' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

$dirPath = New-Item -Path $testDir -ItemType Directory -Force
$subDirPath = New-Item -Path (Join-Path $dirPath 'test-subdir') -ItemType Directory -Force
New-Item -Path (Join-Path $subDirPath $testFileName) -ItemType File -Force | Out-Null

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_delete_recurse.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultDirPath = $resultJson.results.result.afterState.path
$resultDirPath | Should -Not -Exist
}

It 'Can create file if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_create_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.FileInfo'
}

It 'Can create file with content if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_filecontent_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse

$resultFilePath = $resultJson.results.result.afterState.path
$resultFilePath | Should -Exist
Get-Content $resultFilePath | Should -Be "Hello, World!"
}

It 'Can create directory if parent directory does not exist' {
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Force -Recurse
}

$resultJson = dsc config set -f "$PSScriptRoot/../examples/filesys_dir_create_parent.dsc.yaml" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resultJson.hadErrors | Should -BeFalse
$resultJson.results.result.afterState.path | Should -Exist
Get-Item $resultJson.results.result.afterState.path | Should -BeOfType 'System.IO.DirectoryInfo'
}
}
2 changes: 1 addition & 1 deletion pal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ opt-level = 2
lto = true

[build-dependencies]
cc = "1.1"
cc = "~1.1"
Loading
Loading