From 6505b11122c523d1496cb70fd98b56f478c64be7 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 21 Aug 2024 14:02:20 -0700 Subject: [PATCH 1/9] Blocked non-list operations on adapters --- dsc/src/resource_command.rs | 34 +++++++++++++++++++++++++++++++++- dsc/tests/dsc_args.tests.ps1 | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index 41c1890df..b0c4eba86 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -5,7 +5,7 @@ use crate::args::OutputFormat; use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output}; use dsc_lib::configure::config_doc::{Configuration, ExecutionKind}; use dsc_lib::configure::add_resource_export_results_to_configuration; -use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse}; +use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}}; use dsc_lib::dscerror::DscError; use tracing::{error, debug}; @@ -22,6 +22,11 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op }; debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as); + if resource.kind == Kind::Adapter { + error!("Can not perform this operation on the adapter {} itself", resource.type_name); + exit(EXIT_DSC_ERROR); + } + if let Some(requires) = &resource.require_adapter { input = add_type_name_to_json(input, resource.type_name.clone()); if let Some(pr) = get_resource(dsc, requires) { @@ -59,6 +64,11 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option { // verify is json @@ -236,6 +263,11 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option = None; if let Some(requires) = &dsc_resource.require_adapter { input = add_type_name_to_json(input, dsc_resource.type_name.clone()); diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index e4ed2c510..d1158aa9d 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -272,4 +272,40 @@ resources: $stderr = dsc config get -d $configFile 2>&1 $stderr | Should -Match '.*?--path.*?' } + + It 'Get operation on the adapter itself should fail' -Tag 'z1' { + dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Get-all operation on the adapter itself should fail' -Tag 'z1' { + dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Set operation on the adapter itself should fail' -Tag 'z1' { + 'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Test operation on the adapter itself should fail' -Tag 'z1' { + dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Export operation on the adapter itself should fail' -Tag 'z1' { + dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Delete operation on the adapter itself should fail' -Tag 'z1' { + dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } } From ede4aceaef4ca593e6eca55012e524d67688035f Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:02:07 -0700 Subject: [PATCH 2/9] In PSAdapter cache code compare DateTime upto seconds --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 11 +++++++++-- powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index b1fb56a50..610503876 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -264,12 +264,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTime + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index c842a450c..2194ca238 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -86,12 +86,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTimeUtc + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true From 19c940ea02478db0fa82903b38843d74dcb5484f Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:03:56 -0700 Subject: [PATCH 3/9] Revert "Blocked non-list operations on adapters" This reverts commit 6505b11122c523d1496cb70fd98b56f478c64be7. --- dsc/src/resource_command.rs | 34 +--------------------------------- dsc/tests/dsc_args.tests.ps1 | 36 ------------------------------------ 2 files changed, 1 insertion(+), 69 deletions(-) diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index b0c4eba86..41c1890df 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -5,7 +5,7 @@ use crate::args::OutputFormat; use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output}; use dsc_lib::configure::config_doc::{Configuration, ExecutionKind}; use dsc_lib::configure::add_resource_export_results_to_configuration; -use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}}; +use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse}; use dsc_lib::dscerror::DscError; use tracing::{error, debug}; @@ -22,11 +22,6 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op }; debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as); - if resource.kind == Kind::Adapter { - error!("Can not perform this operation on the adapter {} itself", resource.type_name); - exit(EXIT_DSC_ERROR); - } - if let Some(requires) = &resource.require_adapter { input = add_type_name_to_json(input, resource.type_name.clone()); if let Some(pr) = get_resource(dsc, requires) { @@ -64,11 +59,6 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option { // verify is json @@ -263,11 +236,6 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option = None; if let Some(requires) = &dsc_resource.require_adapter { input = add_type_name_to_json(input, dsc_resource.type_name.clone()); diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index d1158aa9d..e4ed2c510 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -272,40 +272,4 @@ resources: $stderr = dsc config get -d $configFile 2>&1 $stderr | Should -Match '.*?--path.*?' } - - It 'Get operation on the adapter itself should fail' -Tag 'z1' { - dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Get-all operation on the adapter itself should fail' -Tag 'z1' { - dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Set operation on the adapter itself should fail' -Tag 'z1' { - 'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Test operation on the adapter itself should fail' -Tag 'z1' { - dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Export operation on the adapter itself should fail' -Tag 'z1' { - dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Delete operation on the adapter itself should fail' -Tag 'z1' { - dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } } From 68fd1bdbcb94df4daeed453d4148a93f6b0ab386 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 21 Aug 2024 14:02:20 -0700 Subject: [PATCH 4/9] Blocked non-list operations on adapters --- dsc/src/resource_command.rs | 34 +++++++++++++++++++++++++++++++++- dsc/tests/dsc_args.tests.ps1 | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index 41c1890df..b0c4eba86 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -5,7 +5,7 @@ use crate::args::OutputFormat; use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output}; use dsc_lib::configure::config_doc::{Configuration, ExecutionKind}; use dsc_lib::configure::add_resource_export_results_to_configuration; -use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse}; +use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}}; use dsc_lib::dscerror::DscError; use tracing::{error, debug}; @@ -22,6 +22,11 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op }; debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as); + if resource.kind == Kind::Adapter { + error!("Can not perform this operation on the adapter {} itself", resource.type_name); + exit(EXIT_DSC_ERROR); + } + if let Some(requires) = &resource.require_adapter { input = add_type_name_to_json(input, resource.type_name.clone()); if let Some(pr) = get_resource(dsc, requires) { @@ -59,6 +64,11 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option { // verify is json @@ -236,6 +263,11 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option = None; if let Some(requires) = &dsc_resource.require_adapter { input = add_type_name_to_json(input, dsc_resource.type_name.clone()); diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index e4ed2c510..d1158aa9d 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -272,4 +272,40 @@ resources: $stderr = dsc config get -d $configFile 2>&1 $stderr | Should -Match '.*?--path.*?' } + + It 'Get operation on the adapter itself should fail' -Tag 'z1' { + dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Get-all operation on the adapter itself should fail' -Tag 'z1' { + dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Set operation on the adapter itself should fail' -Tag 'z1' { + 'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Test operation on the adapter itself should fail' -Tag 'z1' { + dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Export operation on the adapter itself should fail' -Tag 'z1' { + dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } + + It 'Delete operation on the adapter itself should fail' -Tag 'z1' { + dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + $LASTEXITCODE | Should -Be 2 + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' + } } From fee740218685fc499b784c48fbe0d9fee83dd155 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:02:07 -0700 Subject: [PATCH 5/9] In PSAdapter cache code compare DateTime upto seconds --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 11 +++++++++-- powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index b1fb56a50..610503876 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -264,12 +264,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTime + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index c842a450c..2194ca238 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -86,12 +86,19 @@ function Invoke-DscCacheRefresh { "Checking cache for stale entries" | Write-DscTrace foreach ($cacheEntry in $dscResourceCacheEntries) { - #"Checking cache entry '$($cacheEntry.Type) $($cacheEntry.LastWriteTimes)'" | Write-DscTrace -Operation Trace $cacheEntry.LastWriteTimes.PSObject.Properties | ForEach-Object { if (Test-Path $_.Name) { - if (-not ((Get-Item $_.Name).LastWriteTime.Equals([DateTime]$_.Value))) + $file_LastWriteTime = (Get-Item $_.Name).LastWriteTimeUtc + # Truncate DateTime to seconds + $file_LastWriteTime = $file_LastWriteTime.AddTicks( - ($file_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + $cache_LastWriteTime = [DateTime]$_.Value + # Truncate DateTime to seconds + $cache_LastWriteTime = $cache_LastWriteTime.AddTicks( - ($cache_LastWriteTime.Ticks % [TimeSpan]::TicksPerSecond)); + + if (-not ($file_LastWriteTime.Equals($cache_LastWriteTime))) { "Detected stale cache entry '$($_.Name)'" | Write-DscTrace $refreshCache = $true From b8002da2fefcc8e1ac1594a8d594ec20eb910967 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:03:56 -0700 Subject: [PATCH 6/9] Revert "Blocked non-list operations on adapters" This reverts commit 6505b11122c523d1496cb70fd98b56f478c64be7. --- dsc/src/resource_command.rs | 34 +--------------------------------- dsc/tests/dsc_args.tests.ps1 | 36 ------------------------------------ 2 files changed, 1 insertion(+), 69 deletions(-) diff --git a/dsc/src/resource_command.rs b/dsc/src/resource_command.rs index b0c4eba86..41c1890df 100644 --- a/dsc/src/resource_command.rs +++ b/dsc/src/resource_command.rs @@ -5,7 +5,7 @@ use crate::args::OutputFormat; use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, add_type_name_to_json, write_output}; use dsc_lib::configure::config_doc::{Configuration, ExecutionKind}; use dsc_lib::configure::add_resource_export_results_to_configuration; -use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}}; +use dsc_lib::dscresources::invoke_result::{GetResult, ResourceGetResponse}; use dsc_lib::dscerror::DscError; use tracing::{error, debug}; @@ -22,11 +22,6 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: &Op }; debug!("resource.type_name - {} implemented_as - {:?}", resource.type_name, resource.implemented_as); - if resource.kind == Kind::Adapter { - error!("Can not perform this operation on the adapter {} itself", resource.type_name); - exit(EXIT_DSC_ERROR); - } - if let Some(requires) = &resource.require_adapter { input = add_type_name_to_json(input, resource.type_name.clone()); if let Some(pr) = get_resource(dsc, requires) { @@ -64,11 +59,6 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: &Option { // verify is json @@ -263,11 +236,6 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, format: &Option = None; if let Some(requires) = &dsc_resource.require_adapter { input = add_type_name_to_json(input, dsc_resource.type_name.clone()); diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index d1158aa9d..e4ed2c510 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -272,40 +272,4 @@ resources: $stderr = dsc config get -d $configFile 2>&1 $stderr | Should -Match '.*?--path.*?' } - - It 'Get operation on the adapter itself should fail' -Tag 'z1' { - dsc resource get -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Get-all operation on the adapter itself should fail' -Tag 'z1' { - dsc resource get --all -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Set operation on the adapter itself should fail' -Tag 'z1' { - 'abc' | dsc resource set -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Test operation on the adapter itself should fail' -Tag 'z1' { - dsc resource test -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Export operation on the adapter itself should fail' -Tag 'z1' { - dsc resource export -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } - - It 'Delete operation on the adapter itself should fail' -Tag 'z1' { - dsc resource delete -r Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt - $LASTEXITCODE | Should -Be 2 - "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Can not perform this operation on the adapter' - } } From 87ecb7ad2cdeb768ab7df85424492dc9d91dc32c Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:44:52 -0700 Subject: [PATCH 7/9] Added tests --- .../Tests/powershellgroup.resource.tests.ps1 | 27 +++++++++++++++++++ .../Tests/win_powershellgroup.tests.ps1 | 17 ++++++++++++ 2 files changed, 44 insertions(+) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 4d635133b..905188f8c 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -280,4 +280,31 @@ Describe 'PowerShell adapter resource tests' { $env:TestClassResourceResultCount = $null } + + It 'Verify that there are no cache rebuids for several sequential executions' { + + # remove cache file + $cacheFilePath = if ($IsWindows) { + # PS 6+ on Windows + Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" + } else { + # either WinPS or PS 6+ on Linux/Mac + if ($PSVersionTable.PSVersion.Major -le 5) { + Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" + } else { + Join-Path $env:HOME ".dsc" "PSAdapterCache.json" + } + } + Remove-Item -Force -Path $cacheFilePath -ErrorAction SilentlyContinue + + # first execution should build the cache + dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' + + # next executions following shortly after should Not rebuild the cache + 1..3 | %{ + dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' + } + } } diff --git a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 index d1aee97db..f0f2c47ae 100644 --- a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 +++ b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 @@ -70,4 +70,21 @@ Describe 'WindowsPowerShell adapter resource tests' { $res = $r | ConvertFrom-Json $res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile" } + + It 'Verify that there are no cache rebuids for several sequential executions' { + + # remove cache file + $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" + Remove-Item -Force -Path $cacheFilePath -ErrorAction SilentlyContinue + + # first execution should build the cache + dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' + + # next executions following shortly after should Not rebuild the cache + 1..3 | %{ + dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt + "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' + } + } } From 319dc12d289b6312438e3525ea3cd79017fa8d95 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 28 Aug 2024 15:53:30 -0700 Subject: [PATCH 8/9] Added tests 2 --- powershell-adapter/Tests/win_powershellgroup.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 index f0f2c47ae..aaaf486a9 100644 --- a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 +++ b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 @@ -71,7 +71,7 @@ Describe 'WindowsPowerShell adapter resource tests' { $res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile" } - It 'Verify that there are no cache rebuids for several sequential executions' { + It 'Verify that there are no cache rebuids for several sequential executions' -Skip:(!$IsWindows) { # remove cache file $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" From 1d661e5f39abbaa2230a72bbe72033713ff3cc5b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 4 Sep 2024 13:14:45 -0700 Subject: [PATCH 9/9] Apply suggestions from code review --- powershell-adapter/Tests/powershellgroup.resource.tests.ps1 | 4 ++-- powershell-adapter/Tests/win_powershellgroup.tests.ps1 | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 905188f8c..4e07f0390 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -281,7 +281,7 @@ Describe 'PowerShell adapter resource tests' { $env:TestClassResourceResultCount = $null } - It 'Verify that there are no cache rebuids for several sequential executions' { + It 'Verify that there are no cache rebuilds for several sequential executions' { # remove cache file $cacheFilePath = if ($IsWindows) { @@ -295,7 +295,7 @@ Describe 'PowerShell adapter resource tests' { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } - Remove-Item -Force -Path $cacheFilePath -ErrorAction SilentlyContinue + Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore # first execution should build the cache dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt diff --git a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 index aaaf486a9..d5e72204d 100644 --- a/powershell-adapter/Tests/win_powershellgroup.tests.ps1 +++ b/powershell-adapter/Tests/win_powershellgroup.tests.ps1 @@ -71,11 +71,11 @@ Describe 'WindowsPowerShell adapter resource tests' { $res.results[0].result.actualState.result[0].properties.DestinationPath | Should -Be "$testFile" } - It 'Verify that there are no cache rebuids for several sequential executions' -Skip:(!$IsWindows) { + It 'Verify that there are no cache rebuilds for several sequential executions' -Skip:(!$IsWindows) { # remove cache file $cacheFilePath = Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - Remove-Item -Force -Path $cacheFilePath -ErrorAction SilentlyContinue + Remove-Item -Force -Path $cacheFilePath -ErrorAction Ignore # first execution should build the cache dsc -l trace resource list -a Microsoft.Windows/WindowsPowerShell 2> $TestDrive/tracing.txt