Skip to content
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

Added cmdlet Test-VSTeamYamlPipelines for testing YAML pipeline code #272

Merged
merged 21 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions .docs/Test-VSTeamYamlPipeline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!-- #include "./common/header.md" -->

# Test-VSTeamYamlPipeline

## SYNOPSIS

<!-- #include "./synopsis/Test-VSTeamYamlPipeline.md" -->

## SYNTAX

## DESCRIPTION

<!-- #include "./synopsis/Test-VSTeamYamlPipeline.md" -->

## EXAMPLES

### -------------------------- EXAMPLE 1 --------------------------

```PowerShell
PS C:\> Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 -FilePath './azure-pipelines.yml'

Name Id url state
---- -- --- -----
-1 https://dev.azure.com/devsdb/3428bdd7-9fed-4c30-a6c9-fcb52f084ab9/_apis/pipelines/24/runs/-1 unknown
```

This example checks the YAML pipeline with ID 24 and the file './azure-pipelines.yml' for consistency on Azure DevOps to see if the changes still work.

### -------------------------- EXAMPLE 2 --------------------------

```PowerShell
PS C:\> $yamlOverride = [string](Get-Content -raw $FilePath)
PS C:\> Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24 -YamlOverride $yamlOverride
```

This example checks the YAML pipeline with ID 24 and the content of a yaml file in the variable $yamlOverride for consistency on Azure DevOps to see if the changes still work.

### -------------------------- EXAMPLE 3 --------------------------

```PowerShell
PS C:\> $yamlOverride = [string](Get-Content -raw $FilePath)
SebastianSchuetze marked this conversation as resolved.
Show resolved Hide resolved
PS C:\> Test-VSTeamYamlPipeline -Project DemoProject -PipelineId 24
```

This example checks the YAML pipeline with ID 24 for consistency on Azure DevOps to see if the existing YAML of the pipeline works.

## PARAMETERS

<!-- #include "./params/projectName.md" -->

### -PipelineId

Id of the YAML pipeline to be checked

```yaml
Type: Int32
Position: 1
```

### -FilePath

Path to the file that should be checked

```yaml
Type: String
```

## INPUTS

### System.String

FilePath

### System.Int32

PipelineId

## OUTPUTS

### Team.YamlPipelineResult

## NOTES

If you do not set the default project by called Set-VSTeamDefaultProject before calling Add-VSTeamBuild you will have to type in the names.

## RELATED LINKS

[Get-VSTeamBuildDefinition](Get-VSTeamBuildDefinition.md)
1 change: 1 addition & 0 deletions .docs/synopsis/Test-VSTeamYamlPipeline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests the commited YAML pipeline files to check for inconsitencies.
6 changes: 6 additions & 0 deletions Source/Private/applyTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,9 @@ function _applyTypesToVariableGroup {
}
$item.variables.PSObject.TypeNames.Insert(0, 'Team.Variables')
}

function _applyTypesToYamlPipelineResultType {
param($item)

$item.PSObject.TypeNames.Insert(0, 'Team.YamlPipelineResult')
}
2 changes: 1 addition & 1 deletion Source/Public/Set-VSTeamAPIVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function Set-VSTeamAPIVersion {
[VSTeamVersions]::Version = $Target
[VSTeamVersions]::Git = '5.1-preview'
[VSTeamVersions]::Core = '5.0'
[VSTeamVersions]::Build = '5.0-preview'
[VSTeamVersions]::Build = '5.1-preview'
[VSTeamVersions]::Release = '5.1-preview'
[VSTeamVersions]::DistributedTask = '5.0-preview'
[VSTeamVersions]::VariableGroups = '5.0-preview.1'
Expand Down
51 changes: 51 additions & 0 deletions Source/Public/Test-VSTeamYamlPipeline.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Test-VSTeamYamlPipeline {
[CmdletBinding(DefaultParameterSetName = 'WithFilePath')]
param(
[Parameter(ParameterSetName = 'WithFilePath', Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[Parameter(ParameterSetName = 'WithYamlOverride', Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[Int32] $PipelineId,
[Parameter(ParameterSetName = 'WithFilePath', Mandatory = $false)]
[string] $FilePath,
[Parameter(ParameterSetName = 'WithYamlOverride', Mandatory = $false)]
[string] $YamlOverride
)
DynamicParam {
_buildProjectNameDynamicParam
}

process {
# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]

$body = @{
PreviewRun = $true
}

if ($FilePath) {
$body.YamlOverride = [string](Get-Content -raw $FilePath)
}
elseif ($YamlOverride) {
$body.YamlOverride = $YamlOverride
}

try {
# Call the REST API
$resp = _callAPI -ProjectName $ProjectName -Area 'pipelines' -Resource "$PipelineId" -id "runs" `
-Method Post -ContentType 'application/json; charset=utf-8' -Body ($body | ConvertTo-Json) `
-Version $([VSTeamVersions]::Build)
}
catch {
if ($PSItem -match 'PipelineValidationException') {
Write-Error (($PSItem | ConvertFrom-Json).message -replace '/azure-pipelines.yml( ?: ?)? ?', '')
return
}
else {
throw
}
}

_applyTypesToYamlPipelineResultType -item $resp

return $resp
}
}
45 changes: 45 additions & 0 deletions Source/formats/Team.YamlPipelineResult.TableView.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Team.YamlPipelineResult.TableView</Name>
<ViewSelectedBy>
<TypeName>Team.YamlPipelineResult</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>Id</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>url</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>state</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>name</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>id</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>url</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>state</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
3 changes: 2 additions & 1 deletion Source/formats/_formats.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"Team.WorkItemDeleted.ListView.ps1xml",
"Team.JobRequest.TableView.ps1xml",
"Team.GitCommitRef.TableView.ps1xml",
"Team.GitUserDate.TableView.ps1xml"
"Team.GitUserDate.TableView.ps1xml",
"Team.YamlPipelineResult.TableView.ps1xml"
]
}
51 changes: 51 additions & 0 deletions Source/types/Team.YamlPipelineResult.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8" ?>
<Types>
<Type>
<Name>Team.YamlPipelineResult</Name>
<Members>
<ScriptProperty>
<Name>links</Name>
<GetScriptBlock>$this._links</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>pipeline</Name>
<GetScriptBlock>$this.pipeline</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>state</Name>
<GetScriptBlock>$this.state</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>url</Name>
<GetScriptBlock>$this.url</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>finalYaml</Name>
<GetScriptBlock>$this.finalYaml</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>id</Name>
<GetScriptBlock>$this.id</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>name</Name>
<GetScriptBlock>$this.name</GetScriptBlock>
</ScriptProperty>
<MemberSet>
<Name>PSStandardMembers</Name>
<Members>
<PropertySet>
<Name>DefaultDisplayPropertySet</Name>
<ReferencedProperties>
<Name>name</Name>
<Name>id</Name>
<Name>url</Name>
<Name>state</Name>
<Name>finalYaml</Name>
</ReferencedProperties>
</PropertySet>
</Members>
</MemberSet>
</Members>
</Type>
</Types>
80 changes: 80 additions & 0 deletions unit/test/Test-VSTeamYamlPipeline.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Set-StrictMode -Version Latest

# Loading System.Web avoids issues finding System.Web.HttpUtility
Add-Type -AssemblyName 'System.Web'

InModuleScope VSTeam {
[VSTeamVersions]::Account = 'https://dev.azure.com/test'

$resultsAzD = Get-Content "$PSScriptRoot\sampleFiles\pipelineDefYamlResult.json" -Raw | ConvertFrom-Json

Describe 'Yaml Pipeline Checks AzD Services' {
# Mock the call to Get-Projects by the dynamic parameter for ProjectName
Mock Invoke-RestMethod { return @() } -ParameterFilter {
$Uri -like "*_apis/projects*"
}

. "$PSScriptRoot\mocks\mockProjectNameDynamicParamNoPSet.ps1"
$testYamlPath = "$PSScriptRoot\sampleFiles\azure-pipelines.test.yml"

Context 'Test-VSTeamYamlPipeline' {
Mock Invoke-RestMethod {
# If this test fails uncomment the line below to see how the mock was called.
Write-Host $args
Write-Host $([VSTeamVersions]::Build)
Write-Host $([VSTeamVersions]::Account)

return $resultsAzD
}

It 'With Pipeline with PipelineID and without extra YAML' {
Test-VSTeamYamlPipeline -projectName project -PipelineId 24

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {

$Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and
$Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and
$Body -like '*"PreviewRun":*true*' -and
$Body -notlike '*YamlOverride*'
}
}

It 'With Pipeline with PipelineID and YAML file path' {

Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {

$Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and
$Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and
$Body -like '*"PreviewRun":*true*' -and
$Body -like '*YamlOverride*'
}
}

It 'With Pipeline with PipelineID and YAML code' {

$yamlOverride = [string](Get-Content -raw $testYamlPath)
Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -YamlOverride $yamlOverride

Assert-MockCalled Invoke-RestMethod -Exactly -Scope It -Times 1 -ParameterFilter {

$Uri -like "*https://dev.azure.com/test/project/_apis/pipelines/24/runs*" -and
$Uri -like "*api-version=$([VSTeamVersions]::Build)*" -and
$Body -like '*"PreviewRun":*true*' -and
$Body -like '*YamlOverride*'
}
}



$yamlResult = Test-VSTeamYamlPipeline -projectName project -PipelineId 24 -FilePath $testYamlPath

It 'Should create Yaml result' {
$yamlResult | Should Not be $null
}

}

}
}
32 changes: 32 additions & 0 deletions unit/test/sampleFiles/azure-pipelines.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test-Yaml-Pipeline

trigger:
- master

resources:
- repo: self

stages:
- stage: Build
displayName: Build Stage

jobs:

- job: Windows_Phase
displayName: Build Windows
pool:
vmImage: 'windows-latest'

steps:
- task: CopyFiles@2
displayName: 'Copy Module to Artifacts Folder'
inputs:
Contents: '**'
TargetFolder: '$(build.artifactstagingdirectory)'
flattenFolders: false

- task: PublishPipelineArtifact@1
displayName: 'Publish Module'
inputs:
targetPath: '$(Build.ArtifactStagingDirectory)'
artifact: 'module'
Loading