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

making service connection optional #3008

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
191 changes: 191 additions & 0 deletions Tasks/QuickPerfTest/CltTasksUtility.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
function InvokeRestMethod($headers, $contentType, $uri , $method= "Get", $body)
{
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($uri)
$result = Invoke-RestMethod -ContentType "application/json" -UserAgent $global:userAgent -TimeoutSec $global:RestTimeout -Uri $uri -Method $method -Headers $headers -Body $body
$ServicePoint.CloseConnectionGroup("")
return $result
}

function ComposeTestDropJson($name, $duration, $homepage, $vu, $geoLocation)
{
$tdjson = @"
{
"dropType": "InplaceDrop",
"loadTestDefinition":{
"loadTestName":"$name",
"runDuration":$duration,
"urls":["$homepage"],
"browserMixs":[
{"browserName":"Internet Explorer 11.0","browserPercentage":60.0},
{"browserName":"Chrome 2","browserPercentage":40.0}
],
"loadPatternName":"Constant",
"maxVusers":$vu,
"loadGenerationGeoLocations":[
{"Location":"$geoLocation","Percentage":100}
]
}
}
"@

return $tdjson
}

function CreateTestDrop($headers, $dropJson, $CltAccountUrl)
{
$uri = [String]::Format("{0}/_apis/clt/testdrops?api-version=1.0", $CltAccountUrl)
$drop = InvokeRestMethod -contentType "application/json" -uri $uri -method Post -headers $headers -body $dropJson

return $drop
}

function GetTestDrop($headers, $drop, $CltAccountUrl)
{
$uri = [String]::Format("{0}/_apis/clt/testdrops/{1}?api-version=1.0", $CltAccountUrl, $drop.id)
$testdrop = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers

return $testdrop
}

function UploadTestDrop($testdrop)
{
$uri = New-Object System.Uri($testdrop.accessData.dropContainerUrl)
$sas = New-Object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($testdrop.accessData.sasKey)
$container = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer($uri, $sas)

return $container
}

#function GetTestRuns($headers, $CltAccountUrl)
#{
# $uri = [String]::Format("{0}/_apis/clt/testruns?api-version=1.0", $CltAccountUrl)
# $runs = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers

# return $runs
#}

function GetTestRunUri($testRunId, $headers, $CltAccountUrl)
{
$uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl,$testRunId)
$run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers

return $run.WebResultUrl
}

function RunInProgress($run)
{
return $run.state -eq "queued" -or $run.state -eq "inProgress"
}

function MonitorTestRun($headers, $run, $CltAccountUrl)
{
$uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl, $run.id)
$prevState = $run.state
$prevSubState = $run.subState
Write-Output ("Load test '{0}' is in state '{1}|{2}'." -f $run.name, $run.state, $run.subState)

do
{
Start-Sleep -s 15
$run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers
if ($prevState -ne $run.state -or $prevSubState -ne $run.subState)
{
$prevState = $run.state
$prevSubState = $run.subState
Write-Output ("Load test '{0}' is in state '{1}|{2}'." -f $run.name, $run.state, $run.subState)
}
}
while (RunInProgress $run)

$run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers
Write-Output "------------------------------------"
$uri = [String]::Format("{0}/_apis/clt/testruns/{1}/messages?api-version=1.0", $CltAccountUrl, $run.id)
$messages = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers

if ($messages)
{
$timeSorted = $messages.value | Sort-Object loggedDate
foreach ($message in $timeSorted)
{
switch ($message.messageType)
{
"info" { Write-Host -NoNewline ("[Message]{0}" -f $message.message) }
"output" { Write-Host -NoNewline ("[Output]{0}" -f $message.message) }
"warning" { Write-Warning $message.message }
"error" { Write-Error $message.message }
"critical" { Write-Error $message.message }
}
}
}

Write-Output "------------------------------------"
}

function ComposeTestRunJson($name, $tdid, $machineType)
{
$trjson = @"
{
"name":"$name",
"description":"Quick perf test from automation task",
"testSettings":{"cleanupCommand":"", "hostProcessPlatform":"x64", "setupCommand":""},
"superSedeRunSettings":{"loadGeneratorMachinesType":"$machineType"},
"testDrop":{"id":"$tdid"},
"runSourceIdentifier":"build/$env:SYSTEM_DEFINITIONID/$env:BUILD_BUILDID"
}
"@

return $trjson
}

function QueueTestRun($headers, $runJson, $CltAccountUrl)
{
$uri = [String]::Format("{0}/_apis/clt/testruns?api-version=1.0", $CltAccountUrl)
$run = InvokeRestMethod -contentType "application/json" -uri $uri -method Post -headers $headers -body $runJson

$start = @"
{
"state": "queued"
}
"@

$uri = [String]::Format("{0}/_apis/clt/testruns/{1}?api-version=1.0", $CltAccountUrl, $run.id)
InvokeRestMethod -contentType "application/json" -uri $uri -method Patch -headers $headers -body $start
$run = InvokeRestMethod -contentType "application/json" -uri $uri -headers $headers

return $run
}

function ComposeAccountUrl($connectedServiceUrl, $headers)
{
#Load all dependent files for execution
. $PSScriptRoot/VssConnectionHelper.ps1
$connectedServiceUrl = $connectedServiceUrl.TrimEnd('/')
Write-Host "Getting Clt Endpoint:"
$elsUrl = Get-CltEndpoint $connectedServiceUrl $headers

return $elsUrl
}

function ValidateInputs($websiteUrl, $tfsCollectionUrl, $connectedServiceName)
{
if (![System.Uri]::IsWellFormedUriString($websiteUrl, [System.UriKind]::Absolute))
{
throw "Website Url is not well formed."
}

if([string]::IsNullOrWhiteSpace($connectedServiceName) -and $tfsCollectionUrl -notlike "*VISUALSTUDIO.COM*" -and $tfsCollectionUrl -notlike "*TFSALLIN.NET*")
{
throw "VS Team Services Connection is mandatory for non hosted TFS builds "
}
}

function UploadSummaryMdReport($summaryMdPath)
{
Write-Verbose "Summary Markdown Path = $summaryMdPath"

if (($env:SYSTEM_HOSTTYPE -eq "build") -and (Test-Path($summaryMdPath)))
{
Write-Host "##vso[task.addattachment type=Distributedtask.Core.Summary;name=Load test results;]$summaryMdPath"
}
}

Loading