diff --git a/.github/workflows/azure-ipam-testing.yml b/.github/workflows/azure-ipam-testing.yml index 015b153..cc6aa15 100644 --- a/.github/workflows/azure-ipam-testing.yml +++ b/.github/workflows/azure-ipam-testing.yml @@ -20,6 +20,7 @@ jobs: deploy: runs-on: ubuntu-latest outputs: + ipamURL: ${{ steps.pwshScript.outputs.ipamURL }} ipamUIAppId: ${{ steps.pwshScript.outputs.ipamUIAppId }} ipamEngineAppId: ${{ steps.pwshScript.outputs.ipamEngineAppId }} ipamSuffix: ${{ steps.pwshScript.outputs.ipamSuffix }} @@ -31,7 +32,7 @@ jobs: shell: pwsh run: | Set-PSRepository PSGallery -InstallationPolicy Trusted - Install-Module Az, Microsoft.Graph -Force + Install-Module Az, Microsoft.Graph -AllowClobber -Force # - name: "Azure Login" # uses: azure/login@v1 @@ -49,6 +50,9 @@ jobs: - name: Checkout Azure IPAM Code uses: actions/checkout@v3 + with: + sparse-checkout: | + deploy - name: Deploy Azure IPAM working-directory: "deploy" @@ -94,15 +98,47 @@ jobs: sleep 30 echo "Time: $(date +'%T')" - cleanup: + test: runs-on: ubuntu-latest needs: [ deploy, sleep ] + steps: + - name: Install Testing Prerequisites + shell: pwsh + run: | + Set-PSRepository PSGallery -InstallationPolicy Trusted + Install-Module Az, Pester -AllowClobber -Force + + - name: "Azure Login" + uses: azure/login@v1 + with: + creds: ${{ secrets.AZURE_CREDENTIALS }} + enable-AzPSSession: true + + - name: Checkout Azure IPAM Code + uses: actions/checkout@v3 + with: + sparse-checkout: | + tests + + - name: Test Azure IPAM via Pester + working-directory: "tests" + env: + IPAM_URL: ${{ needs.deploy.outputs.ipamURL }} + IPAM_ENGINE_APP_ID: ${{ needs.deploy.outputs.ipamEngineAppId }} + shell: pwsh + run: | + Import-Module Pester -PassThru + Invoke-Pester -Output Detailed ./azureipam.tests.ps1 -PassThru + + cleanup: + runs-on: ubuntu-latest + needs: [ deploy, sleep, test ] steps: - name: Install Deployment Prerequisites shell: pwsh run: | Set-PSRepository PSGallery -InstallationPolicy Trusted - Install-Module Az -Force + Install-Module Az -AllowClobber -Force - name: "Azure Login" uses: azure/login@v1 diff --git a/deploy/deploy.ps1 b/deploy/deploy.ps1 index f6c4a99..c7189f9 100644 --- a/deploy/deploy.ps1 +++ b/deploy/deploy.ps1 @@ -1028,6 +1028,7 @@ process { Write-Host Stop-Transcript | Out-Null + Write-Output "ipamURL=https://$($deployment.Outputs["appServiceHostName"].Value)" >> $Env:GITHUB_OUTPUT Write-Output "ipamUIAppId=$($appDetails.UIAppId)" >> $Env:GITHUB_OUTPUT Write-Output "ipamEngineAppId=$($appDetails.EngineAppId)" >> $Env:GITHUB_OUTPUT Write-Output "ipamSuffix=$($deployment.Outputs["suffix"].Value)" >> $Env:GITHUB_OUTPUT diff --git a/tests/azureipam.tests.ps1 b/tests/azureipam.tests.ps1 new file mode 100644 index 0000000..246a8b9 --- /dev/null +++ b/tests/azureipam.tests.ps1 @@ -0,0 +1,127 @@ +BeforeAll { + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + + Set-StrictMode -Version Latest + + [string]$baseUrl = "$env:IPAM_URL/api" + [System.Security.SecureString]$accessToken = ConvertTo-SecureString (Get-AzAccessToken -ResourceUrl api://$env:IPAM_ENGINE_APP_ID).Token -AsPlainText + [hashtable]$headers = @{ + "Content-Type" = "application/json" + } + + # GET API Request + Function Get-ApiResource { + [CmdletBinding()] + Param ( + [Parameter(Mandatory=$True, Position=0)] + [string]$resource + ) + + $response = Invoke-RestMethod ` + -Method Get ` + -Authentication Bearer ` + -Token $accessToken ` + -Uri "${baseUrl}${resource}" ` + -Headers $headers + + Write-Output $response + } + + # POST API Request + Function New-ApiResource { + [CmdletBinding()] + Param( + [Parameter(Mandatory=$True, Position=0)] + [string]$resource, + + [Parameter(Mandatory=$True, Position=1)] + [hashtable]$body + ) + + $jsonBody = $body | ConvertTo-Json + $response = Invoke-RestMethod ` + -Method Post ` + -Authentication Bearer ` + -Token $accessToken ` + -Uri "${baseUrl}${resource}" ` + -Headers $headers ` + -Body $jsonBody + + Write-Output $response + } + + # PUT API Request + Function Set-ApiResource { + [CmdletBinding()] + Param( + [Parameter(Mandatory=$True, Position=0)] + [string]$resource, + + [Parameter(Mandatory=$True, Position=1)] + [hashtable]$body + ) + + $jsonBody = $body | ConvertTo-Json + $response = Invoke-RestMethod ` + -Method Put ` + -Authentication Bearer ` + -Token $accessToken ` + -Uri "${baseUrl}${resource}" ` + -Headers $headers ` + -Body $jsonBody + + Write-Output $response + } + + # PATCH API Request + Function Update-ApiResource { + [CmdletBinding()] + Param( + [Parameter(Mandatory=$True, Position=0)] + [string]$resource, + + [Parameter(Mandatory=$True, Position=1)] + [hashtable]$body + ) + + $jsonBody = $body | ConvertTo-Json + $response = Invoke-RestMethod ` + -Method Patch ` + -Authentication Bearer ` + -Token $accessToken ` + -Uri "${baseUrl}${resource}" ` + -Headers $headers ` + -Body $jsonBody + + Write-Output $response + } + + # DELETE API Request + Function Remove-ApiResource { + [CmdletBinding()] + Param( + [Parameter(Mandatory=$True, Position=0)] + [string]$resource + ) + + $response = Invoke-RestMethod ` + -Method Delete ` + -Autjentication Bearer ` + -Token $accessToken ` + -Uri "${baseUrl}${resource}" ` + -Headers $headers + + Write-Output $response + } +} + +Describe 'Get-Posts' { + + It 'Spaces is empty' { + + $spaces = Get-ApiResource '/spaces' + + $spaces | Should -Be $null + } + +}