Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PiyushDub committed Sep 5, 2024
1 parent 652017f commit a29baef
Show file tree
Hide file tree
Showing 8 changed files with 730 additions and 0 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[ClassVersion("1.0.0.0"), FriendlyName("ResourceName")]
class MSFT_ResourceName : OMI_BaseResource
{
[Key, Description("")] String PrimaryKey;
[Write, Description("")] String OtherProperties;

[Write, Description("Credentials of the workload's Admin"), EmbeddedInstance("MSFT_Credential")] string Credential;
[Write, Description("Id of the Azure Active Directory application to authenticate with.")] String ApplicationId;
[Write, Description("Id of the Azure Active Directory tenant used for authentication.")] String TenantId;
[Write, Description("Thumbprint of the Azure Active Directory application's authentication certificate to use for authentication.")] String CertificateThumbprint;
[Write, Description("Managed ID being used for authentication.")] Boolean ManagedIdentity;
[Write, Description("Access token used for authentication.")] String AccessTokens[];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# ResourceName

## Description

##TODO - Provide a short description of what the resource is set to configure.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"resourceName": "ResourceName",
"description": "Description of what the resource is about.",
"roles": {
"read": [
"Role"
],
"update": [
"Role"
]
},
"permissions": {
"graph": {
"delegated": {
"read": [],
"update": []
},
"application": {
"read": [
{
"name": "Permission for Monitoring and Export"
}
],
"update": [
{
"name": "Permission for deploying"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
This example is used to test new resources and showcase the usage of new resources being worked on.
It is not meant to use as a production baseline.
#>

Configuration Example
{
param(
[Parameter()]
[System.String]
$ApplicationId,

[Parameter()]
[System.String]
$TenantId,

[Parameter()]
[System.String]
$CertificateThumbprint
)
Import-DscResource -ModuleName Microsoft365DSC
node localhost
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
This example is used to test new resources and showcase the usage of new resources being worked on.
It is not meant to use as a production baseline.
#>

Configuration Example
{
param(
[Parameter()]
[System.String]
$ApplicationId,

[Parameter()]
[System.String]
$TenantId,

[Parameter()]
[System.String]
$CertificateThumbprint
)
Import-DscResource -ModuleName Microsoft365DSC
node localhost
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<#
This example is used to test new resources and showcase the usage of new resources being worked on.
It is not meant to use as a production baseline.
#>

Configuration Example
{
param(
[Parameter()]
[System.String]
$ApplicationId,

[Parameter()]
[System.String]
$TenantId,

[Parameter()]
[System.String]
$CertificateThumbprint
)
Import-DscResource -ModuleName Microsoft365DSC
node localhost
{

}
}
176 changes: 176 additions & 0 deletions Tests/Unit/Microsoft365DSC/Microsoft365DSC.ResourceName.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
[CmdletBinding()]
param(
)
$M365DSCTestFolder = Join-Path -Path $PSScriptRoot `
-ChildPath '..\..\Unit' `
-Resolve
$CmdletModule = (Join-Path -Path $M365DSCTestFolder `
-ChildPath '\Stubs\Microsoft365.psm1' `
-Resolve)
$GenericStubPath = (Join-Path -Path $M365DSCTestFolder `
-ChildPath '\Stubs\Generic.psm1' `
-Resolve)
Import-Module -Name (Join-Path -Path $M365DSCTestFolder `
-ChildPath '\UnitTestHelper.psm1' `
-Resolve)

$CurrentScriptPath = $PSCommandPath.Split('\')
$CurrentScriptName = $CurrentScriptPath[$CurrentScriptPath.Length -1]
$ResourceName = $CurrentScriptName.Split('.')[1]
$Global:DscHelper = New-M365DscUnitTestHelper -StubModule $CmdletModule `
-DscResource $ResourceName -GenericStubModule $GenericStubPath

Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
InModuleScope -ModuleName $Global:DscHelper.ModuleName -ScriptBlock {
Invoke-Command -ScriptBlock $Global:DscHelper.InitializeScript -NoNewScope
BeforeAll {

$secpasswd = ConvertTo-SecureString (New-Guid | Out-String) -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ('[email protected]', $secpasswd)

Mock -CommandName Confirm-M365DSCDependencies -MockWith {
}

Mock -CommandName New-M365DSCConnection -MockWith {
return "Credentials"
}

##TODO - Mock any Remove/Set/New cmdlets

# Mock Write-Host to hide output during the tests
Mock -CommandName Write-Host -MockWith {
}
$Script:exportedInstances =$null
$Script:ExportMode = $false
}
# Test contexts
Context -Name "The instance should exist but it DOES NOT" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return $null
Mock -CommandName Get-Cmdlet -MockWith {
return $null
}
}
It 'Should return Values from the Get method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Absent'
}
It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should create a new instance from the Set method' {
##TODO - Replace the New-Cmdlet by the appropriate one
Should -Invoke -CommandName New-Cmdlet -Exactly 1
}
}

Context -Name "The instance exists but it SHOULD NOT" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Ensure = 'Absent'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return an instance
Mock -CommandName Get-Cmdlet -MockWith {
return @{

}
}
}
It 'Should return Values from the Get method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
}
It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should remove the instance from the Set method' {
##TODO - Replace the Remove-Cmdlet by the appropriate one
Should -Invoke -CommandName Remove-Cmdlet -Exactly 1
}
}

Context -Name "The instance exists and values are already in the desired state" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return the desired values
Mock -CommandName Get-Cmdlet -MockWith {
return @{

}
}
}

It 'Should return true from the Test method' {
Test-TargetResource @testParams | Should -Be $true
}
}

Context -Name "The instance exists and values are NOT in the desired state" -Fixture {
BeforeAll {
$testParams = @{
##TODO - Add Parameters
Ensure = 'Present'
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return a drift
Mock -CommandName Get-Cmdlet -MockWith {
return @{

}
}
}

It 'Should return Values from the Get method' {
(Get-TargetResource @testParams).Ensure | Should -Be 'Present'
}

It 'Should return false from the Test method' {
Test-TargetResource @testParams | Should -Be $false
}

It 'Should call the Set method' {
Set-TargetResource @testParams
##TODO - Replace the Update-Cmdlet by the appropriate one
Should -Invoke -CommandName Update-Cmdlet -Exactly 1
}
}

Context -Name 'ReverseDSC Tests' -Fixture {
BeforeAll {
$Global:CurrentModeIsExport = $true
$Global:PartialExportFileName = "$(New-Guid).partial.ps1"
$testParams = @{
Credential = $Credential;
}

##TODO - Mock the Get-Cmdlet to return an instance
Mock -CommandName Get-Cmdlet -MockWith {
return @{

}
}
}
It 'Should Reverse Engineer resource from the Export method' {
$result = Export-TargetResource @testParams
$result | Should -Not -BeNullOrEmpty
}
}
}
}

Invoke-Command -ScriptBlock $Global:DscHelper.CleanupScript -NoNewScope

0 comments on commit a29baef

Please sign in to comment.