Functions/Invoke-NotebookTests.ps1

Function Invoke-NotebookTests {
    param(
        $NotebookTest
    )
    $DatabricksJobRun = $null
    $notebookTestResults = $null
    $noteBookOutput = $null
    $testCaseTemplate = $null
    $ClusterId = Get-CachedDatabricksClusterId -ClusterName $NotebookTest.testClusterName
    $DatabricksNotebookJob = @{
        JobName      = $NotebookTest.jobName
        ClusterId    = $ClusterId
        NotebookPath = $NotebookTest.notebookPath
        RunImmediate = $true
    }
    if ($NotebookTest.notebookparamsjson) {                        
        $DatabricksNotebookJob.add("NotebookParametersJson", $NotebookTest.notebookparamsjson)            
    } 
    $runId = Add-DatabricksNotebookJob @DatabricksNotebookJob
    Write-Host "Run ID - "$runId
    Write-Host "Running tests. This is the info for the job running the tests..."
    Get-DatabricksRun -RunId $runId
    $DatabricksJobRunState = $null
    $wait = 1
    $currentStatus = "INITIATED"
    Write-Host "There is a 20 minute timeout for test jobs."
    do {
        Start-Sleep -Seconds 10
        $DatabricksJobRunState = Get-DatabricksRun -RunId $runId     
        $DatabricksJobRunLifeCycleState = $DatabricksJobRunState.state.life_cycle_state
        if ($currentStatus -ne $DatabricksJobRunLifeCycleState) {
            Write-Host "Status has altered from $currentStatus to $DatabricksJobRunLifeCycleState."
            $currentStatus = $DatabricksJobRunLifeCycleState
        }
        if (($wait % 5) -eq 0) {
            Write-Host "Job has not completed. Status is $DatabricksJobRunLifeCycleState."
        }
        $wait ++
    } until (($DatabricksJobRunLifeCycleState -notin "PENDING", "RUNNING", "TERMINATING", "SKIPPED", "INTERNAL_ERROR") -or ($wait -eq 120))
    Write-Host "Test Completed. Check here for the status of the job."
    $databricksRun = Get-DatabricksRun -RunId $runId     
    if ($databricksRun.state.result_state -ne "SUCCESS") {
        $databricksRun
        Write-Error "Job failed. Use url to open job and investigate"
        throw
    }
    $DatabricksJobRun = Get-DatabricksJobRun -RunId $runId -includeNoteBookOutput
    if ($null -eq $DatabricksJobRun.notebook_output.result) {
        Write-Host "Job succeeded, but no result, so notebook most likely failed. Use url to open job and investigate"
        Throw
    }
    $DatabricksJobRun = Get-DatabricksJobRun -RunId $runId -includeNoteBookOutput
    $DatabricksJobRun | ConvertTo-Json | Out-File .\"NotebooksTest_$($NotebookTest.jobName)_$($runId).json"
    Write-Verbose $DatabricksJobRun
    $noteBookOutput = $DatabricksJobRun.notebook_output.result
    Write-Verbose $noteBookOutput
    $notebookTestResults = ConvertFrom-Json($noteBookOutput)

    $xmlHeader = '<test-results name="##notebookPath##" total="##total##" date="##getdate##" time="##gettime##">
<environment nunit-version="2.6.0.12035" clr-version="2.0.50727.4963" os-version="Microsoft Windows NT 6.1.7600.0" platform="Win32NT" cwd="C:\Program Files\NUnit 2.6\bin" machine-name="dummymachine" user="dummyuser" user-domain="dummy"/>
<culture-info current-culture="en-US" current-uiculture="en-US"/>'

    $xmlFooter = '</results>
</test-suite>
</test-results>'


    $xmlHeader = $xmlHeader.Replace("##notebookPath##", $NotebookTest.notebookPath).Replace('##total##', $notebookTestResults.Count).Replace('##getdate##', (Get-Date -Format yyyy-MM-dd)).Replace('##gettime##', (Get-Date -Format hh:mm:ss))

    if ($notebookTestResults.result -match "failure") {
        $testSuiteResult = "failure"
        $testSuiteSuccess = "False"
    }
    else {
        $testSuiteResult = "success"
        $testSuiteSuccess = "True"
    }
    $testSuite = '<test-suite type="TestFixture" name="' + $NotebookTest.notebookPath + '" executed="True" result="' + $testSuiteResult + '" success="' + $testSuiteSuccess + '" time="0.000" asserts="0">
<results>'

    foreach ($notebookTestResult in $notebookTestResults) {
        if ($notebookTestResult.result -eq "failure") {
            $testCaseTemplate += '<test-case name="' + $notebookTestResult.test + '" description="" executed="True" result="' + $notebookTestResult.result + '" success="' + $notebookTestResult.issuccess + '" time="0.000" asserts="1">
        <failure>
        </failure>
        </test-case>'

        }
        elseif ($notebookTestResult.result -eq "success") {
            $testCaseTemplate += '<test-case name="' + $notebookTestResult.test + '" description="" executed="True" result="' + $notebookTestResult.result + '" success="' + $notebookTestResult.issuccess + '" time="0.000" asserts="1"/>
    '

        }
    }
    
    $nunitXml = $xmlHeader + $testSuite + $testCaseTemplate + $xmlFooter | Out-File .\"NotebooksTest_$($NotebookTest.jobName)_$($runId).xml" -Force
}