-
Notifications
You must be signed in to change notification settings - Fork 382
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
Instead of using the first cimClass and then having no superClass, use the first cimClass that has a non-null superClass #1200
Conversation
…e the first cimClass that has a non-null superClass
@ykuijs This would be a general improvement and would fix your issue because the first cimClass has no Superclass, therefore, it will pick the 2nd cimClass, which has a Superclass. What do you think? I still have to look into whether we can do something better rather than selecting the first superClass and rather try to get the expected name so that we can match against it or do you think we don't need it. It would be great if you could maybe come up with a more minimal example that we could use as a regression test, which is a minimum requirement. |
I agree that this should work. xWebsite is using three sub-CimClasses, all without SuperClass. So I guess this should skip all three and take the xWebSite class. |
To get back to your question about a test scenario:
|
Any update on the progress of this PR? |
I think the PR is OK, it just needs a test, I'll add reviewers to speedup the process for the moment |
If you add this test to Tests\Rules\UseIdenticalMandatoryParametersForDSC.tests.ps1, this should test the change: Context "When a CIM class has no parent, but does contain a subclass which should not be processed" {
# regression test for #1192 - just check no uncaught exception
It "Should find no violations, and throw no exceptions" -skip:($IsLinux -or $IsMacOS) {
# Arrange test content in testdrive
$dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources"
$noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent"
# need a fake module
$fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1"
Set-Content -Path $fakeModulePath -Value @"
@{
ModuleVersion = '1.0'
GUID = 'f5e6cc2a-5500-4592-bbe2-ef033754b56f'
Author = 'test'
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
} # End of PSData hashtable
} # End of PrivateData hashtable
}
"@
# and under it a directory called dscresources\something
New-Item -ItemType Directory -Path $noParentClassDir
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.psm1'
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'ClassWithNoParent.schema.mof'
# containing a .psm1 file and a .schema.mof file with same base name
Set-Content -Path $noParentClassFilepath -Value "#requires -Version 4.0 -Modules CimCmdlets" # the file content doesn't much matter
Set-Content -Path $noParentClassMofFilePath -Value @"
[ClassVersion("1.0.0.0")]
Class MSFT_SubClass
{
[Key, Description("Key of the subclass")] String Name;
[Required, Description("Required parameter of the subclass")] String Description;
[Write, Description("Additional non-required parameter")] Boolean Enabled;
};
[ClassVersion("1.0.0"), FriendlyName("WaitForAny")]
class MSFT_WaitForAnyNoIdenticalMandatoryParameter : OMI_BaseResource
{
[key, Description("Name of Resource on remote machine")]
string ResourceName;
[key, Description("dummy variable")]
string Dummy;
[required, Description("List of remote machines")]
string NodeName[];
[required, EmbeddedInstance("MSFT_Credential"), Description("Credential to access all remote machines")]
String Credential;
[write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")]
string Subclass;
};
"@
# Act - run scriptanalyzer
$violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop
$violations.Count | Should -Be 0
}
} |
@ykuijs Thanks for all your efforts.. :-) |
DSC requires a specific folder structure, so if you take the resource out of that folder structure I believe PSSA no longer sees it as a DSC module and therefore does not run the test. Let me dive into the test a little deeper, I have some cycles available this Thursday. |
A little later than planned, but I just managed to test the new test with v1.18.0 and there it fails as expected:
So the test is good for testing this scenario. |
@ykuijs When I paste your test into the end of |
No sure what you mean here. The Test passes for me as well with the fixes from this PR. However if I generate the example resource using the code in the test and run the v1.18.0 from the PSGallery, I get the above errors. |
Sorry, now see I did something wrong.....continuing testing! |
[UPDATED the test because of a copy/paste error] @bergmeister That makes perfect sense. I tried to reproduce the error using v1.18.0, but indeed was unable to. It took me a while, but I finally managed to figure it out :-) I copied the test code from several places, but made a small mistake while doing this. DSC requires the filename to match the classname, which it didn't. Therefore these tests were not processed correctly. I tested the following test which fails on v1.18.0 and passes with the updated code: Context "When a CIM class has no parent, but does contain a subclass which should not be processed" {
# regression test for #1192 - just check no uncaught exception
It "Should find no violations, and throw no exceptions" -skip:($IsLinux -or $IsMacOS) {
# Arrange test content in testdrive
$dscResources = Join-Path -Path "TestDrive:" -ChildPath "DSCResources"
$noparentClassDir = Join-Path -Path $dscResources "ClassWithNoParent"
# need a fake module
$fakeModulePath = Join-Path -Path "TestDrive:" -ChildPath "test.psd1"
Set-Content -Path $fakeModulePath -Value @"
@{
ModuleVersion = '1.0'
GUID = 'f5e6cc2a-5500-4592-bbe2-ef033754b56f'
Author = 'test'
FunctionsToExport = @()
CmdletsToExport = @()
VariablesToExport = '*'
AliasesToExport = @()
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
PSData = @{
} # End of PSData hashtable
} # End of PrivateData hashtable
}
"@
# and under it a directory called dscresources\something
New-Item -ItemType Directory -Path $noParentClassDir
$noparentClassFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.psm1'
$noparentClassMofFilepath = Join-Path -Path $noParentClassDir -ChildPath 'MSFT_ClassWithNoParent.schema.mof'
# containing a .psm1 file and a .schema.mof file with same base name
Set-Content -Path $noParentClassFilepath -Value @"
#requires -Version 4.0 -Modules CimCmdlets
function Get-TargetResource
{
param()
}
function Set-TargetResource
{
param()
}
function Test-TargetResource
{
param()
}
Export-ModuleMember -Function *-TargetResource
"@
Set-Content -Path $noParentClassMofFilePath -Value @"
[ClassVersion("1.0.0.0")]
Class MSFT_SubClass
{
[Key, Description("Key of the subclass")] String Name;
[Required, Description("Required parameter of the subclass")] String Description;
[Write, Description("Additional non-required parameter")] Boolean Enabled;
};
[ClassVersion("1.0.0"), FriendlyName("ClassWithNoParent")]
class MSFT_ClassWithNoParent : OMI_BaseResource
{
[key, Description("Name of Resource on remote machine")]
string ResourceName;
[key, Description("dummy variable")]
string Dummy;
[required, Description("List of remote machines")]
string NodeName[];
[required, EmbeddedInstance("MSFT_Credential"), Description("Credential to access all remote machines")]
String Credential;
[write, Description("dummy subclass variable"), EmbeddedInstance("MSFT_SubClass")]
string Subclass;
};
"@
# Act - run scriptanalyzer
$violations = Invoke-ScriptAnalyzer -Path $noParentClassFilepath -IncludeRule $ruleName -ErrorAction Stop
$violations.Count | Should -Be 0
}
} |
@ykuijs Thanks for coming back. I can confirm that the new regression test would now actually fail as proven in PR #1231 :-) |
@bergmeister Any clue when this will be released to the PS Gallery? |
@ykuijs Please ask @JamesWTruher |
PR Summary
Fixes #1192
Instead of using the first cimClass and then having no superClass, use the first cimClass that has a non-null superClass.
Implementation is ready but we need to write a test for it I suppose.
PR Checklist
.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
to the beginning of the title and remove the prefix when the PR is ready.