Skip to content

Commit

Permalink
sdk generation pipeline script (#16782)
Browse files Browse the repository at this point in the history
* compatible with old SDK generation and new codegen automation
  • Loading branch information
chunyu3 committed Jan 26, 2022
1 parent 4b8c2a1 commit 916bcb2
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 238 deletions.
30 changes: 30 additions & 0 deletions codegen_to_sdk_config.json
@@ -0,0 +1,30 @@
{
"init": {
"initScript": {
"path": "./eng/scripts/automation_init.sh",
"logPrefix": "[GO]",
"stderr":{
"storeAllLog": true
}
}
},
"generateAndBuild": {
"generateAndBuildScript": {
"path": "generator automation-v2",
"logPrefix": "[GO-Generate]",
"stderr":{
"storeLogByFilter": "[error|Error|Exception]"
}
}
},
"mockTest": {
"mockTestScript": {
"path": "./eng/scripts/automation/Invoke-MockTest.ps1",
"script": "pwsh",
"logPrefix": "[GO-MockTest]",
"stderr":{
"storeLogByFilter": "[error|Error|Exception]"
}
}
}
}
151 changes: 1 addition & 150 deletions eng/scripts/Invoke-MgmtMockTest.ps1
Expand Up @@ -2,157 +2,8 @@ Param(
[string] $filter
)

$MOCK_SERVER_NAME = "mock-server"
$MOCK_SERVER_READY = "validator initialized"
$MOCK_SERVER_WAIT_TIME = 600
$AUTOREST_CONFIG_FILE = "autorest.md"

. (Join-Path $PSScriptRoot .. common scripts common.ps1)

function PrepareMockServer()
{
# install mock server
$folder = Join-Path $env:TEMP "mock-service-host"
StopMockServer
try
{
Remove-Item -Recurse -Force -Path $folder
}
catch
{
Write-Host "Mock service host folder: $folder not existed"
}
New-Item -ItemType Directory -Force -Path $folder
Set-Location $folder
npm install @azure-tools/mock-service-host
}

function StartMockServer($specName, $commitID)
{
$folder = Join-Path $env:TEMP "mock-service-host"
Set-Location $folder

# change .env file to use the specific swagger file
$envFile = Join-Path $folder .env
if ([string]::IsNullOrEmpty($specName))
{
$specName = "*"
}
New-Item -Path $envFile -ItemType File -Value '' -Force
Add-Content $envFile "specRetrievalGitUrl=https://github.com/Azure/azure-rest-api-specs
specRetrievalGitBranch=main
specRetrievalGitCommitID=$commitID
validationPathsPattern=specification/$specName/resource-manager/**/*.json"

# start mock server and check status
Start-Job -Name $MOCK_SERVER_NAME -ScriptBlock { node node_modules/@azure-tools/mock-service-host/dist/src/main.js 2>&1 }
$output = Receive-Job $MOCK_SERVER_NAME
Write-Host "Mock sever status: `n $("$output")"
$time = 0
try
{
while ("$output" -notmatch $MOCK_SERVER_READY)
{
if ($time -gt $MOCK_SERVER_WAIT_TIME)
{
Write-Host "##[error] mock server start timeout"
StopMockServer
exit 1
}
Write-Host "Server not ready, wait for annother 10 seconds"
$time += 10
Start-Sleep -Seconds 10
$output = Receive-Job $MOCK_SERVER_NAME
Write-Host "Mock sever status: `n $("$output")"
}
}
catch
{
Write-Host "##[error]wait for mock server start:`n$_"
exit 1
}
}

function StopMockServer()
{
try
{
Stop-Job -Name $MOCK_SERVER_NAME
}
catch
{
Write-Host "##[error]can not stop mock server:`n$_"
}
}

function GetSwaggerInfo($dir)
{
Set-Location $dir
$swaggerInfoRegex = "(?<commitID>[0-9a-f]{40})\/specification\/(?<specName>.*)\/resource-manager\/readme.md"
try
{
$content = Get-Content .\$AUTOREST_CONFIG_FILE -Raw
if ($content -match $swaggerInfoRegex)
{
return $matches["specName"], $matches["commitID"]
}
}
catch
{
Write-Error "Error parsing swagger info"
Write-Error $_
}
Write-Host "Cannot find swagger info"
exit 1
}

function TestAndGenerateReport($dir)
{
Set-Location $dir
# dependencies for go coverage report generation
go get github.com/jstemmer/go-junit-report
go get github.com/axw/gocov/gocov
go get github.com/AlekSi/gocov-xml
go get github.com/matm/gocov-html

# set azidentity env for mock test
$Env:AZURE_TENANT_ID = "mock-test"
$Env:AZURE_CLIENT_ID = "mock-test"
$Env:AZURE_USERNAME = "mock-test"
$Env:AZURE_PASSWORD = "mock-test"

# do test with corage report and convert to cobertura format
Write-Host "go cmd: go test -v -coverprofile coverage.txt | Tee-Object -FilePath output.txt"
go test -v -coverprofile coverage.txt | Tee-Object -FilePath output.txt
Write-Host "report.xml: cat output.txt | go-junit-report > report.xml"
cat output.txt | go-junit-report > report.xml
Write-Host "coverage.json: gocov convert ./coverage.txt > ./coverage.json"
gocov convert ./coverage.txt > ./coverage.json
Write-Host "coverage.xml: Get-Content ./coverage.json | gocov-xml > ./coverage.xml"
Get-Content ./coverage.json | gocov-xml > ./coverage.xml
Write-Host "coverage.html: Get-Content ./coverage.json | gocov-html > ./coverage.html"
Get-Content ./coverage.json | gocov-html > ./coverage.html
}

function JudgeExitCode($errorMsg = "execution error")
{
if (!$?)
{
Write-Host "##[error] $errorMsg"
exit $LASTEXITCODE
}
}

function ExecuteSingleTest($sdk)
{
Write-Host "Start mock server"
$swaggerInfo = GetSwaggerInfo $sdk.DirectoryPath
StartMockServer $swaggerInfo[0] $swaggerInfo[1]
Write-Host "Execute mock test for $($sdk.Name)"
TestAndGenerateReport $sdk.DirectoryPath
Write-Host "Stop mock server"
StopMockServer
}
. (Join-Path $PSScriptRoot MgmtTestLib.ps1)

$env:TEMP = [System.IO.Path]::GetTempPath()
Write-Host "Path tmp: $env:TEMP"
Expand Down
79 changes: 3 additions & 76 deletions eng/scripts/Invoke-MgmtTestgen.ps1
Expand Up @@ -17,80 +17,7 @@ param(
)

. (Join-Path $PSScriptRoot .. common scripts common.ps1)

function Invoke-MgmtTestgen ()
{
$currentDirectory = Get-Location
if ($clean)
{
Write-Host "##[command]Executing go clean -v ./... in " $currentDirectory
go clean -v ./...
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if ($cleanGenerated)
{
Write-Host "##[command]Cleaning auto-generated files in" $currentDirectory
Remove-Item "ze_generated_*"
Remove-Item "zt_generated_*"
}

if ($generateExample -or $generateMockTest)
{
Write-Host "##[command]Executing autorest.gotest in " $currentDirectory
$autorestPath = "./" + $config

if ($outputFolder -eq '')
{
$outputFolder = $currentDirectory
}
$exampleFlag = "false"
if ($generateExample)
{
$exampleFlag = "true"
}
$mockTestFlag = "true"
if (-not $generateMockTest)
{
$mockTestFlag = "false"
}
Write-Host "autorest --version=$autorestVersion --use=$goExtension --use=$testExtension --go --track2 --output-folder=$outputFolder --clear-output-folder=false --go.clear-output-folder=false --generate-sdk=false --testmodeler.generate-mock-test=$mockTestFlag --testmodeler.generate-sdk-example=$exampleFlag $autorestPath"
autorest --version=$autorestVersion --use=$goExtension --use=$testExtension --go --track2 --output-folder=$outputFolder --clear-output-folder=false --go.clear-output-folder=false --generate-sdk=false --testmodeler.generate-mock-test=$mockTestFlag --testmodeler.generate-sdk-example=$exampleFlag $autorestPath
if ($LASTEXITCODE)
{
Write-Host "##[error]Error running autorest.gotest"
exit $LASTEXITCODE
}
}

if ($format)
{
Write-Host "##[command]Executing gofmt -s -w . in " $currentDirectory
gofmt -s -w .
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if ($tidy)
{
Write-Host "##[command]Executing go mod tidy in " $currentDirectory
go mod tidy
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if (!$skipBuild)
{
Write-Host "##[command]Executing go build -v ./... in " $currentDirectory
go build -v ./...
Write-Host "##[command]Build Complete!"
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

if ($vet)
{
Write-Host "##[command]Executing go vet ./... in " $currentDirectory
go vet ./...
}
}
. (Join-Path $PSScriptRoot MgmtTestLib.ps1)

try
{
Expand All @@ -101,11 +28,11 @@ try
foreach ($sdk in $sdks)
{
Push-Location $sdk.DirectoryPath
Invoke-MgmtTestgen
Invoke-MgmtTestgen -sdkDirectory $sdk.DirectoryPath @psBoundParameters
Pop-Location
}
}
finally
{
Set-Location $startingDirectory
}
}

0 comments on commit 916bcb2

Please sign in to comment.