Skip to content

Commit a85823b

Browse files
committed
fixup! Edit Pipeline Trigger
1 parent 7a51610 commit a85823b

File tree

1 file changed

+105
-108
lines changed

1 file changed

+105
-108
lines changed

.pipelines/templates/run-unit-tests.yaml

Lines changed: 105 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ stages:
5050
ls -la "$REPORT_DIR"
5151
mv coverage-all.out "$COVERAGE_OUT"
5252
ls -la "$REPORT_DIR"
53-
echo "##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]$(cat $REPORT_DIR/linux-coverage.xml)"
54-
name: test_results
5553
retryCountOnTaskFailure: 3
5654
displayName: "Run Unit Tests - Linux"
5755
#workingDirectory: $(ACN_DIR)
@@ -64,25 +62,13 @@ stages:
6462
GOCOV_BIN="$BIN_INSTALL_DIR/gocov"
6563
GOCOV_XML_BIN="$BIN_INSTALL_DIR/gocov-xml"
6664
67-
mkdir -p "$REPORT_DIR"
68-
touch "$REPORT_XML"
69-
7065
$GOCOV_BIN convert "$COVERAGE_OUT" > "$REPORT_DIR"/linux-coverage.json
7166
$GOCOV_XML_BIN < "$REPORT_DIR"/linux-coverage.json > "$REPORT_DIR"/linux-coverage.gocov.xml
7267
68+
echo "##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]$(cat $REPORT_DIR/linux-coverage.xml)"
7369
echo "##vso[task.setvariable variable=GOCOV_OUT_XML;isOutput=true]$(cat $REPORT_DIR/linux-coverage.gocov.xml)"
7470
name: report
7571
displayName: "Generate Test Reporting"
76-
#workingDirectory: $(ACN_DIR)
77-
78-
# - task: PublishPipelineArtifact@1
79-
# displayName: "\U0001F512 Publish to Pipeline Artifact"
80-
# target:
81-
# container: host
82-
# condition: eq('True', 'True')
83-
# inputs:
84-
# artifactName: drop_unittest_linux
85-
# targetPath: $(Build.ArtifactStagingDirectory)/linux-unittest
8672
8773
8874
- job: windows
@@ -104,100 +90,111 @@ stages:
10490
inputs:
10591
version: '$(GOVERSION)'
10692

107-
- pwsh: |
108-
# Define variables
109-
$env:BIN_INSTALL_DIR = (Resolve-Path -Path "bin").Path
110-
$env:PRE_BIN = $env:GOBIN
111-
# Install go-junit-report
112-
$env:GOBIN = $env:BIN_INSTALL_DIR
113-
go install github.com/jstemmer/go-junit-report/v2@latest
114-
115-
$env:JUNIT_REPORT_BIN = Join-Path -Path $env:BIN_INSTALL_DIR -ChildPath "go-junit-report"
116-
117-
# Create report directory and report.xml file
118-
if (!(Test-Path -Path $env:REPORT_DIR)) {
119-
New-Item -Path $env:REPORT_DIR -ItemType Directory
120-
}
121-
if (!(Test-Path -Path $env:REPORT_XML)) {
122-
New-Item -Path $env:REPORT_XML -ItemType File
123-
}
124-
125-
$env:GOBIN = $env:PRE_BIN
126-
# Run make tools
127-
make tools
128-
129-
# Run tests and generate report
130-
try {
131-
# Run test, capture output and exit code
132-
$env:testOutput = @(
133-
go test -timeout 30m -mod=readonly -buildvcs=false -tags "unit" --skip 'TestE2E*' -race -covermode atomic -coverprofile=windows-coverage.out ./npm/... ./cni/... ./platform/...
134-
go tool cover -func=windows-coverage.out
135-
)
136-
$env:exitCode = $LASTEXITCODE
137-
138-
# Write test output to report.xml using go-junit-report
139-
$env:testOutput | & Invoke-Expression $env:JUNIT_REPORT_BIN | Out-File -FilePath $env:REPORT_XML -Encoding utf8
140-
141-
# Exit with test exit code
142-
if ($exitCode -ne 0) {
143-
exit $exitCode
144-
}
145-
} catch {
146-
Write-Error $_
147-
exit 1
148-
}
149-
150-
# List files in report directory
151-
Get-ChildItem -Path $env:REPORT_DIR -Force | Select-Object -ExpandProperty FullName | ForEach-Object { Write-Host $_ }
152-
153-
# Move coverage.out to COVERAGE_OUT
154-
Move-Item -Path "windows-coverage.out" -Destination $env:COVERAGE_OUT -Force
155-
156-
# List files in report directory again
157-
Get-ChildItem -Path $env:REPORT_DIR -Force | Select-Object -ExpandProperty FullName | ForEach-Object { Write-Host $_ }
158-
159-
echo "##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]$(cat $env:REPORT_DIR/windows-coverage.xml)"
160-
retryCountOnTaskFailure: 3
161-
name: test_results
93+
- task: PythonScript@0
16294
displayName: "Run Unit Tests - Windows"
163-
#workingDirectory: $(ACN_DIR)
164-
165-
- pwsh: |
166-
$env:BIN_INSTALL_DIR = (Resolve-Path -Path "bin").Path
167-
$env:PRE_BIN = $env:GOBIN
168-
169-
$env:GOBIN = $env:BIN_INSTALL_DIR
170-
go install github.com/axw/gocov/gocov@latest
171-
go install github.com/AlekSi/gocov-xml@latest
172-
173-
$env:GOCOV_BIN = Join-Path -Path $env:BIN_INSTALL_DIR -ChildPath "gocov"
174-
$env:GOCOV_XML_BIN = Join-Path -Path $env:BIN_INSTALL_DIR -ChildPath "gocov-xml"
175-
176-
if (!(Test-Path -Path $env:REPORT_DIR)) {
177-
New-Item -Path $env:REPORT_DIR -ItemType Directory
178-
}
179-
if (!(Test-Path -Path $env:REPORT_XML)) {
180-
New-Item -Path $env:REPORT_XML -ItemType File
181-
}
182-
183-
$env:GOBIN = $env:PRE_BIN
95+
retryCountOnTaskFailure: 3
96+
inputs:
97+
scriptSource: 'inline'
98+
script: |
99+
import os
100+
import subprocess
101+
import shutil
102+
import sys
103+
104+
# Set environment variables and directories
105+
bin_install_dir = os.path.realpath('bin')
106+
os.environ['GOBIN'] = bin_install_dir
107+
108+
# Install the go-junit-report tool
109+
subprocess.check_call(['go', 'install', 'github.com/jstemmer/go-junit-report/v2@latest'])
110+
junit_report_bin = os.path.join(bin_install_dir, 'go-junit-report')
111+
112+
# Create report directory and touch report XML file
113+
report_dir = os.environ['REPORT_DIR']
114+
os.makedirs(report_dir, exist_ok=True)
115+
report_xml = os.path.join(report_dir, 'report.xml')
116+
open(report_xml, 'a').close()
117+
118+
# Run make tools
119+
subprocess.check_call(['make', 'tools'])
120+
121+
# Function to run the test and capture output
122+
def run_test():
123+
test_str = 'go test -timeout 30m -mod=readonly -buildvcs=false -tags "unit" ' +
124+
"--skip 'TestE2E*' -race -covermode atomic -coverprofile=windows-coverage.out ./npm/... ./cni/... ./platform/...;" +
125+
'go tool cover -func=windows-coverage.out'
126+
result = subprocess.run(command, capture_output=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
127+
test_exit_code = result.returncode
128+
129+
# Write output to report XML using go-junit-report
130+
with open(report_xml, 'w') as report_file:
131+
junit_process = subprocess.Popen(
132+
[junit_report_bin],
133+
stdin=subprocess.PIPE,
134+
stdout=report_file
135+
)
136+
junit_process.communicate(input=stdout)
137+
138+
sys.exit(test_exit_code)
139+
140+
# Run the test function
141+
run_test()
142+
143+
# Move coverage output file
144+
coverage_out = os.environ['COVERAGE_OUT']
145+
shutil.move('windows-coverage.out', coverage_out)
146+
147+
# List report directory contents again
148+
print(os.listdir(report_dir))
184149
185-
& Invoke-Expression $env:GOCOV_BIN convert $env:COVERAGE_OUT | Out-File -FilePath (Join-Path -Path $env:REPORT_DIR -ChildPath "windows-coverage.json")
186-
& Invoke-Expression $env:GOCOV_XML_BIN < (Join-Path -Path $env:REPORT_DIR -ChildPath "windows-coverage.json") | Out-File -FilePath (Join-Path -Path $env:REPORT_DIR -ChildPath "windows-coverage.gocov.xml")
187-
188-
echo "##vso[task.setvariable variable=GOCOV_OUT_XML;isOutput=true]$(cat $env:REPORT_DIR/windows-coverage.gocov.xml)"
189-
name: report
150+
- task: PythonScript@0
190151
displayName: "Generate Test Reporting"
191-
#workingDirectory: $(ACN_DIR)
192-
193-
# - task: PublishPipelineArtifact@1
194-
# displayName: "\U0001F512 Publish to Pipeline Artifact"
195-
# target:
196-
# container: host
197-
# condition: eq('True', 'True')
198-
# inputs:
199-
# artifactName: drop_unittest_windows
200-
# targetPath: $(Build.ArtifactStagingDirectory)/linux-unittest
152+
name: report
153+
inputs:
154+
scriptSource: 'inline'
155+
script: |
156+
import os
157+
import subprocess
158+
159+
# Define the necessary variables
160+
bin_install_dir = os.path.realpath('bin')
161+
report_dir = os.environ['REPORT_DIR']
162+
coverage_out = os.environ['COVERAGE_OUT']
163+
coverage_json = os.path.join(report_dir, 'windows-coverage.json')
164+
gocov_xml_file = os.path.join(report_dir, 'windows-coverage.gocov.xml')
165+
166+
# Set the GOBIN environment variable
167+
os.environ['GOBIN'] = BIN_INSTALL_DIR
168+
169+
# Install gocov and gocov-xml
170+
subprocess.run(['go', 'install', 'github.com/axw/gocov/gocov@latest'], check=True)
171+
subprocess.run(['go', 'install', 'github.com/AlekSi/gocov-xml@latest'], check=True)
172+
173+
# Define the paths to the installed binaries
174+
GOCOV_BIN = os.path.join(bin_install_dir, 'gocov')
175+
GOCOV_XML_BIN = os.path.join(bin_install_dir, 'gocov-xml')
176+
177+
# Create the report directory if it doesn't exist
178+
os.makedirs(report_dir, exist_ok=True)
179+
180+
# Convert coverage output to JSON
181+
with open(coverage_json, 'w') as json_file:
182+
subprocess.run([gocov_bin, 'convert', coverage_out], stdout=json_file, check=True)
183+
184+
# Convert JSON to XML
185+
with open(gocov_xml_file, 'w') as xml_file:
186+
with open(coverage_json, 'r') as json_file:
187+
subprocess.run([gocov_xml_bin], stdin=json_file, stdout=xml_file, check=True)
188+
189+
# Set the coverage output XML variable
190+
with open(coverage_out, 'r') as f:
191+
coverage_out_xml_content = f.read()
192+
print(f"##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]{coverage_out_xml_content}")
193+
194+
# Print the XML content to the console
195+
with open(gocov_xml_file, 'r') as xml_file:
196+
xml_content = xml_file.read()
197+
print(f"##vso[task.setvariable variable=GOCOV_OUT_XML;isOutput=true]{xml_content}")
201198
202199
203200
- job: coverage
@@ -211,9 +208,9 @@ stages:
211208
variables:
212209
ob_outputDirectory: $(Build.ArtifactStagingDirectory)/out
213210

214-
WINDOWS_COVERAGE_OUT: $[ dependencies.windows.outputs['test_results.COVERAGE_OUT_XML'] ]
211+
WINDOWS_COVERAGE_OUT: $[ dependencies.windows.outputs['report.COVERAGE_OUT_XML'] ]
215212
WINDOWS_GOCOV_OUT: $[ dependencies.windows.outputs['report.GOCOV_OUT_XML'] ]
216-
LINUX_COVERAGE_OUT: $[ dependencies.linux.outputs['test_results.COVERAGE_OUT_XML'] ]
213+
LINUX_COVERAGE_OUT: $[ dependencies.linux.outputs['report.COVERAGE_OUT_XML'] ]
217214
LINUX_GOCOV_OUT: $[ dependencies.linux.outputs['report.GOCOV_OUT_XML'] ]
218215
steps:
219216
- script: |

0 commit comments

Comments
 (0)