-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.yml
302 lines (265 loc) · 15.3 KB
/
test.yml
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
parameters:
- name: appComponents
displayName: "Component name"
type: object
- name: unitTestRunsettings
displayName: |
Usage: unitTestRunsettings,unitTestDll
Example:
- nano.runsettings,bin/Release/NFUnitTest.dll
- Repo/nano.runsettings,Repo/bin/Release/NFUnitTest.dll
Unit test runsettings file. As default, nano.runsettings which will be represent $(System.DefaultWorkingDirectory)/Artifacts/nano.runsettings.
Unit test DLL. As default, bin/release/NFUnitTest.dll which will be represent $(System.DefaultWorkingDirectory)/Artifacts/bin/release/NFUnitTest.dll.
type: object
default:
- 'nano.runsettings,bin/Release/NFUnitTest.dll'
- name: MaxRetries
displayName: "Maximum number of retries"
type: number
default: 5
stages:
- stage: Test_${{ replace(parameters.appComponents, '-', '_') }}
dependsOn: Build
displayName: Test for ${{ parameters.appComponents }}
pool:
name: TestStream
demands:
- ${{ parameters.appComponents }}
jobs:
- job:
displayName: 'Test job for ${{ parameters.appComponents }}'
steps:
- task: DownloadPipelineArtifact@2
displayName: 'Download Pipeline Artifact'
inputs:
path: '$(System.DefaultWorkingDirectory)'
- task: DotNetCoreCLI@2
displayName: 'Linux Install nanoff'
condition: eq(variables['Agent.OS'], 'Linux')
inputs:
command: custom
custom: tool
arguments: 'install nanoff --tool-path $(Agent.HomeDirectory)/tools'
- task: DotNetCoreCLI@2
displayName: 'Windows Install nanoff'
condition: eq(variables['Agent.OS'], 'Windows_NT')
inputs:
command: custom
custom: tool
arguments: 'install nanoff --tool-path $(Agent.HomeDirectory)\tools'
- ${{ each tests in parameters.unitTestRunsettings }}:
- bash: |
# flash the device with nanoff
SerialPort=$(cat $(Agent.HomeDirectory)/config/configuration.json | jq -r '.capabilities.${{ parameters.appComponents }}')
# Maximum number of retries
MAX_RETRIES=${{ parameters.MaxRetries }}
# Delay between retries in seconds
DELAY=2
# Initialize the counter
attempt=0
# Baud rates to try
BAUD_RATES=(1500000 1000000 500000 250000 150000)
# Initialize baud rate index
baud_index=0
# Loop to retry the function call
while (( attempt < MAX_RETRIES )); do
nanoff --target ${{ parameters.appComponents }} --update --masserase --serialport $(echo $SerialPort) --baud ${BAUD_RATES[$baud_index]} --verbosity m
status=$?
if [ $status -eq 0 ]; then
break
else
((attempt++))
((baud_index++))
if (( baud_index >= ${#BAUD_RATES[@]} )); then
baud_index=${#BAUD_RATES[@]} # reset index to the minimum speed
fi
sleep $DELAY
fi
done
if [ $attempt -eq $MAX_RETRIES ]; then
echo "Flashing failed after $MAX_RETRIES attempts."
exit 1
fi
displayName: 'Linux Flash device'
condition: eq(variables['Agent.OS'], 'Linux')
retryCountOnTaskFailure: 3
- powershell: |
# Read the configuration.json file
$configPath = "$(Agent.HomeDirectory)\configuration.json"
$configJson = Get-Content -Raw -Path $configPath | ConvertFrom-Json
# Get the SerialPort value for the specified app component
$appComponent = '${{ parameters.appComponents }}'
$SerialPort = $configJson.capabilities.$appComponent
# Maximum number of retries
$MAX_RETRIES = ${{ parameters.MaxRetries }}
# Delay between retries in seconds
$DELAY = 2
# Initialize the counter
$attempt = 0
# Baud rates to try
$BAUD_RATES = @(1500000, 1000000, 500000, 250000, 150000)
# Initialize baud rate index
$baud_index = 0
# Loop to retry the function call
while ($attempt -lt $MAX_RETRIES) {
$(Agent.HomeDirectory)\tools\nanoff --target $appComponent --update --masserase --serialport $SerialPort --baud $BAUD_RATES[$baud_index] --verbosity m
$status = $LASTEXITCODE
if ($status -eq 0) {
break
} else {
$attempt++
$baud_index++
if ($baud_index -ge $BAUD_RATES.Length) {
$baud_index = 0 # reset index to the minimum speed
}
Start-Sleep -Seconds $DELAY
}
}
if ($attempt -eq $MAX_RETRIES) {
Write-Host "Flashing failed after $MAX_RETRIES attempts."
exit 1
}
displayName: 'Windows Flash device'
condition: eq(variables['Agent.OS'], 'Windows_NT')
retryCountOnTaskFailure: 3
- bash: |
# replace <IsRealHardware>False</IsRealHardware> by <IsRealHardware>True</IsRealHardware> in nano.runsettings
sed -i "s/<IsRealHardware>False<\/IsRealHardware>/<IsRealHardware>True<\/IsRealHardware>/g" $(System.DefaultWorkingDirectory)/Artifacts/${{ split(tests, ',')[0]}}
sed -i "s/<ResultsDirectory>.*<\/ResultsDirectory>/<ResultsDirectory>\.\/TestResults<\/ResultsDirectory>/g" $(System.DefaultWorkingDirectory)/Artifacts/${{ split(tests, ',')[0]}}
# replace the serial port by the one in the configuration for parameters.appComponents
SerialPort=$(cat $(Agent.HomeDirectory)/config/configuration.json | jq -r '.capabilities.${{ parameters.appComponents }}')
sed -i "s|<RealHardwarePort>.*<\/RealHardwarePort>|<RealHardwarePort>$(echo $SerialPort)<\/RealHardwarePort>|g" $(System.DefaultWorkingDirectory)/Artifacts/${{ split(tests, ',')[0]}}
cat $(System.DefaultWorkingDirectory)/Artifacts/${{ split(tests, ',')[0]}}
exit $?
displayName: 'Linux Update nano.runsettings'
condition: eq(variables['Agent.OS'], 'Linux')
- powershell: |
# Replace <IsRealHardware>False</IsRealHardware> by <IsRealHardware>True</IsRealHardware> in nano.runsettings
$runsettingsPath = "$(System.DefaultWorkingDirectory)\Artifacts\${{ split(tests, ',')[0]}}"
(Get-Content -Path $runsettingsPath) -replace '<IsRealHardware>False</IsRealHardware>', '<IsRealHardware>True</IsRealHardware>' | Set-Content -Path $runsettingsPath
(Get-Content -Path $runsettingsPath) -replace '<ResultsDirectory>.*</ResultsDirectory>', '<ResultsDirectory>./TestResults</ResultsDirectory>' | Set-Content -Path $runsettingsPath
# Read the configuration.json file
$configPath = "$(Agent.HomeDirectory)\configuration.json"
$configJson = Get-Content -Raw -Path $configPath | ConvertFrom-Json
# Get the SerialPort value for the specified app component
$appComponent = '${{ parameters.appComponents }}'
$SerialPort = $configJson.capabilities.$appComponent
# Replace the serial port by the one in the configuration for parameters.appComponents
(Get-Content -Path $runsettingsPath) -replace '<RealHardwarePort>.*</RealHardwarePort>', "<RealHardwarePort>$SerialPort</RealHardwarePort>" | Set-Content -Path $runsettingsPath
# Output the modified file
Get-Content -Path $runsettingsPath
exit $LASTEXITCODE
displayName: 'Windows Update nano.runsettings'
condition: eq(variables['Agent.OS'], 'Windows_NT')
- bash: |
# Maximum number of retries
MAX_RETRIES=${{ parameters.MaxRetries }}
# Delay between retries in seconds
DELAY=2
# Initialize the counter
attempt=0
# Define the base directory to search in
BASE_DIR=$(System.DefaultWorkingDirectory)/Artifacts
# Use the find command to search for paths starting with packages/nanoFramework.TestFramework
TESTADAPTER_PATH=$(find "$BASE_DIR" -type d -path "*/packages/nanoFramework.TestFramework*.*" | head -n 1)
# Extract the directory path from the unitTestRunsettings parameter
UNIT_TEST_RUNSETTINGS_DIR=$(dirname "$BASE_DIR/${{ split(tests, ',')[0] }}")
# Loop to retry the function call
while (( attempt < MAX_RETRIES )); do
RESULT_TEST=$(mono $(Agent.HomeDirectory)/TestPlatform/vstest.console.exe $BASE_DIR/${{ split(tests, ',')[1] }} /Settings:$BASE_DIR/${{ split(tests, ',')[0]}} /TestAdapterPath:$TESTADAPTER_PATH /Logger:trx)
echo "$RESULT_TEST"
if [[ "$RESULT_TEST" == *"Couldn't connect to the device"* || "$RESULT_TEST" == *"Couldn't find any valid nanoDevice"* ]]; then
echo "Test failed. Retrying in $DELAY seconds..."
# we need to delete the trx file to avoid confusion but we keep the last one
if [ $attempt -eq $MAX_RETRIES ]; then
echo "Test failed after $MAX_RETRIES attempts."
exit 1
else
find "$UNIT_TEST_RUNSETTINGS_DIR"/TestResults -type f -name "*.trx" -exec rm -f {} \;
((attempt++))
sleep $DELAY
fi
elif [[ "$RESULT_TEST" =~ "The argument .* is invalid." ]]; then
echo "Test failed as arguments are not valid."
exit 1
elif tail -n 5 <<< "$RESULT_TEST" | grep -qE "Failed: [1-9][0-9]*"; then
echo "Test failed with non-zero failures."
exit 1
else
break
fi
done
displayName: 'Linux Run tests'
timeoutInMinutes: 120
retryCountOnTaskFailure: 3
condition: eq(variables['Agent.OS'], 'Linux')
- task: VisualStudioTestPlatformInstaller@1
condition: eq(variables['Agent.OS'], 'Windows_NT')
displayName: 'Windows Visual Studio Test Platform Installer'
inputs:
versionSelector: latestStable
- powershell: |
# Maximum number of retries
$MAX_RETRIES = ${{ parameters.MaxRetries }}
# Delay between retries in seconds
$DELAY = 2
# Initialize the counter
$attempt = 0
# Define the base directory to search in
$BASE_DIR = "$(System.DefaultWorkingDirectory)\Artifacts"
# Use the Get-ChildItem command to search for paths starting with packages/nanoFramework.TestFramework
$TESTADAPTER_PATH = (Get-ChildItem -Path $BASE_DIR -Recurse -Directory | Where-Object { $_.FullName -match "packages\\nanoFramework.TestFramework.*" } | Select-Object -First 1).FullName
# Extract the directory path from the unitTestRunsettings parameter
$UNIT_TEST_RUNSETTINGS_DIR = Split-Path -Path "$BASE_DIR\${{ split(tests, ',')[0] }}"
# Loop to retry the function call
while ($attempt -lt $MAX_RETRIES) {
$RESULT_TEST = & "$(Agent.HomeDirectory)\_work\_tool\VsTest\17.11.1\x64\tools\net462\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" "$BASE_DIR\${{ split(tests, ',')[1] }}" /Settings:"$BASE_DIR\${{ split(tests, ',')[0]}}" /TestAdapterPath:"$TESTADAPTER_PATH" /Logger:trx
$RESULT_TEST
if ($RESULT_TEST -match "Couldn't connect to the device" -or $RESULT_TEST -match "Couldn't find any valid nanoDevice") {
Write-Output "Test failed. Retrying in $DELAY seconds..."
# we need to delete the trx file to avoid confusion but we keep the last one
if ($attempt -eq $MAX_RETRIES) {
Write-Output "Test failed after $MAX_RETRIES attempts."
exit 1
} else {
Get-ChildItem -Path "$UNIT_TEST_RUNSETTINGS_DIR\TestResults" -Filter *.trx | Remove-Item -Force
$attempt++
Start-Sleep -Seconds $DELAY
}
} elseif ($RESULT_TEST -match "The argument .* is invalid.") {
Write-Output "Test failed as arguments are not valid."
exit 1
} else {
break
}
}
displayName: 'Windows Run tests'
timeoutInMinutes: 120
retryCountOnTaskFailure: 3
condition: eq(variables['Agent.OS'], 'Windows_NT')
- task: PublishTestResults@2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: '**/*.trx'
- bash: |
# Define the base directory to search in
BASE_DIR=$(System.DefaultWorkingDirectory)/Artifacts
# Extract the directory path from the unitTestRunsettings parameter
UNIT_TEST_RUNSETTINGS_DIR=$(dirname "$BASE_DIR/${{ split(tests, ',')[0] }}")
# Find and remove all .trx files in the subdirectory
find "$UNIT_TEST_RUNSETTINGS_DIR"/TestResults -type f -name "*.trx" -exec rm -f {} \;
# Optional: Print a message indicating completion
echo "All .trx files have been removed from $unitTestRunsettings"
displayName: Linux Cleaning tests results
condition: eq(variables['Agent.OS'], 'Linux')
- powershell: |
# Define the base directory to search in
$BASE_DIR = "$(System.DefaultWorkingDirectory)\Artifacts"
# Extract the directory path from the unitTestRunsettings parameter
$UNIT_TEST_RUNSETTINGS_DIR = Split-Path -Path "$BASE_DIR\${{ split(tests, ',')[0] }}"
# Find and remove all .trx files in the subdirectory
Get-ChildItem -Path "$UNIT_TEST_RUNSETTINGS_DIR\TestResults" -Filter *.trx | Remove-Item -Force
# Optional: Print a message indicating completion
Write-Output "All .trx files have been removed from $UNIT_TEST_RUNSETTINGS_DIR\TestResults"
displayName: 'Windows Cleaning tests results'
condition: eq(variables['Agent.OS'], 'Windows_NT')