/
Run-PQTests.ps1
136 lines (109 loc) · 3.96 KB
/
Run-PQTests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<#
Author: John Kerski
.SYNOPSIS
This script runs the proof-of-concept Continuous Integration of a custom connector.
.DESCRIPTION
This script runs the proof-of-concept Continuous Integration of a custom connector.
Dependencies: Premium Per User license purchased and assigned to UserName and UserName has admin right to workspace.
.PARAMETER Compile
Default is True, and makes sure Compile step should happen.
Use Compile set to False when you just want to run tests.
Example:
-Compile $False
.EXAMPLE
./Run-PBITests.ps1
./Run-PBITests.ps1 -Compile $False
#>
param([Boolean]$Compile)
# Set default compile settings
if(!$Compile){
$Compile = $True
}
# Install Powershell Module if Needed
if (Get-Module -ListAvailable -Name "MicrosoftPowerBIMgmt") {
Write-Host "MicrosoftPowerBIMgmt already installed"
} else {
Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser -AllowClobber -Force
}
# Check if we are running locally or in a pipeline
if(${env:BUILD_SOURCEVERSION}) # assumes this only exists in Azure Pipelines
{
# Get from environment
$UserName = "${env:PPU_USERNAME}"
$Password = "${env:PPU_PASSWORD}"
#Set Password as Secure String
$Secret = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = [System.Management.Automation.PSCredential]::new($UserName,$Secret)
#Connect to Power BI
Connect-PowerBIServiceAccount -Credential $Credentials
}
else { # Runs Local so will ask to sign in
Connect-PowerBIServiceAccount
}
# Setup Test File Path
$RelTestFilePath = ".\\CI\\Scripts\\variables.test.json"
$TestFilePath = (Resolve-Path -Path $RelTestFilePath).Path
# Clear Credentials
$Result = $null
$Result = .\CI\PQTest\PQTest.exe delete-credential --ALL
# Generate Token
$AccessToken = Get-PowerBIAccessToken
# Remove 'Bearer' from token
$AccessToken = $AccessToken.Values.Substring(7)
# Relative Extension File Path
$RelExtFilePath = ".\\bin\\AnyCPU\\Debug\\powerquery-connector-pbi-rest-api-commercial.mez"
$RelQueryCredFilePath = ".\\PBIRESTAPICommCredTemplate.query.pq"
$RelQueryFilePath = ".\\PBIRESTAPIComm.query.pq"
# Get full path because PQTest expects that
$ExtensionFilePath = (Resolve-Path -Path $RelExtFilePath).Path
$QueryCredFilePath = (Resolve-Path -Path $RelQueryCredFilePath).Path
$QueryFilePath = (Resolve-Path -Path $RelQueryFilePath).Path
# Compile Check
if($Compile -eq $True){
# Setup target compile
$Target = $($ExtensionFilePath -replace ".mez", "")
Write-Host "Compile Connector: $($Target)"
# Run compile
.\CI\PQTest\MakePQX.exe compile --target $Target
}
# Setup credentials
$Template = $null
$Template = .\CI\PQTest\PQTest.exe credential-template --extension $ExtensionFilePath `
--queryFile $QueryCredFilePath
# Output Template to Console for monitoring
Write-Host $Template
# Update Template
$Template = $Template.Replace('OAuth2','AAD')
$Template = $Template.Replace('$$ACCESS_TOKEN$$',$AccessToken)
$X = $Template | ConvertFrom-Json | ConvertTo-Json -Compress
$Result = $null
$Result = $X | .\CI\PQTest\PQTest.exe set-credential `
--extension $ExtensionFilePath `
--queryFile $QueryCredFilePath `
--prettyPrint
$TestSetCredential = $Result | ConvertFrom-Json
if(!$TestSetCredential -and !($TestSetCredential.Status -like 'Success')){
Write-Error "Passed"
return 0
}
else{
Write-Host "Credential Successfully Created"
}
# Now Run The Tests
$Result = $null
$Result = .\CI\PQTest\PQTest.exe run-test --extension $ExtensionFilePath `
--queryFile $QueryFilePath `
--prettyPrint `
-ecf $TestFilePath
$TestResults = $Result | ConvertFrom-Json
if(!$TestResults){
Write-Error "No Expected Test Results"
#return 0
}
elseif(!($TestResults.Status -like 'Passed') -or $TestResults.RowCount -gt 0){
Write-Error $TestResults.Error.Message
return 0
}
else {
Write-Host "Test Results Passed"
}