@@ -81,8 +81,9 @@ stages:
8181 ob_outputDirectory : $(Build.ArtifactStagingDirectory)/windows-unittest
8282
8383 REPORT_DIR : $(Build.ArtifactStagingDirectory)/windows-unittest
84- REPORT_XML : $(Build.ArtifactStagingDirectory)/windows-unittest/report.xml
85- COVERAGE_OUT : $(Build.ArtifactStagingDirectory)/windows-unittest/windows-coverage.out
84+ REPORT_XML : report.xml
85+ GOCOV_OUT : windows-gocov.out
86+ COVERAGE_OUT : windows-coverage.out
8687 steps :
8788 - checkout : azure-container-networking
8889
@@ -104,71 +105,59 @@ stages:
104105 script : |
105106 import os
106107 import subprocess
107- import shutil
108108 import sys
109109
110110 # Set environment variables and directories
111111 cwd = sys.argv[1]
112+ cwd = os.path.realpath(cwd)
112113 bin_install_dir = os.path.join(cwd, 'bin')
113114 os.environ['GOBIN'] = bin_install_dir
114- coverage_out = os.environ['COVERAGE_OUT']
115+
115116 report_dir = os.environ['REPORT_DIR']
116- report_xml = os.path.join(report_dir, 'report.xml')
117+ report_dir = os.path.realpath(report_dir)
118+
119+ coverage_file = os.environ['COVERAGE_OUT']
120+ coverage_out = os.path.join(report_dir, coverage_file)
121+
122+ gocover_file = os.environ['GOCOV_OUT']
123+ gocover_out = os.path.join(report_dir, gocover_file)
124+
125+ junit_file = os.environ['REPORT_XML']
126+ junit_xml = os.path.join(report_dir, junit_file)
117127
118128 # Install the go-junit-report tool
119129 subprocess.check_call(['go', 'install', 'github.com/jstemmer/go-junit-report/v2@latest'])
120130 junit_report_bin = os.path.join(bin_install_dir, 'go-junit-report')
121131
122132 # Create report directory and touch report XML file
123133 os.makedirs(report_dir, exist_ok=True)
124- open(report_xml, 'a').close()
125134
126135 # Run make tools
127136 subprocess.run('make tools', shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
128137
129138 # Function to run the test and capture output
130139 def run_test():
131- os.environ['CGO_ENABLED'] = 1
132- command = 'go test -timeout 30m -mod=readonly -buildvcs=false -tags "unit" '
133- command += "--skip 'TestE2E*' -race -covermode atomic -coverprofile=windows-coverage.out ./npm/... ./cni/... ./platform/..."
134- testing_process = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
135- out, err = testing_process.communicate()
136- #test_exit_code = result.returncode
137- print("Out:")
138- print(out)
139- print("Error:")
140- print(err)
141-
142- command = 'go tool cover -func=windows-coverage.out'
143- result = subprocess.Popen(command, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
144- out, err = result.communicate()
145- print("Out:")
146- print(out)
147- print("Error:")
148- print(err)
149- print(result)
150- #print(test_exit_code)
151- print(os.listdir(cwd))
152- test_exit_code = 1 # result.returncode
140+ os.environ['CGO_ENABLED'] = "1"
141+ cmd_gotest = 'go test -timeout 30m -mod=readonly -buildvcs=false -tags "unit" '
142+ cmd_gotest += f"--skip 'TestE2E*' -race -covermode atomic -coverprofile={coverage_out} ./npm/... ./cni/... ./platform/..."
143+ cmd_junitreport = f'{junit_report_bin} -set-exit-code -in {report_dir}/test.log -out {junit_xml} -iocopy'
144+ cmd_gocover = f'go tool cover -func={coverage_out}'
153145
154- cov_out = os.path.join(cwd, 'windows-coverage.out' )
155- # Write output to report XML using go-junit-report
156- with open(report_xml, 'w') as report_file:
157- with open(cov_out, 'r') as cov_out_file:
158- subprocess.run(
159- [junit_report_bin],
160- stdin=cov_out_file,
161- stdout=report_file,
162- check=True
163- )
146+ print(cmd_gotest )
147+ gotest_process = subprocess.run(cmd_gotest, shell=True, stdout=open(f'{report_dir}/test.log', 'w'), text=True, cwd=cwd)
148+
149+ print(cmd_junitreport)
150+ junit_process = subprocess.run(cmd_junitreport, shell=True, stdout=sys.stdout, stderr=sys.stderr, text=True, cwd=cwd)
151+
152+ print(cmd_gocover)
153+ gocover_process = subprocess.run(cmd_gocover, shell=True, text=True, stdout=open(gocover_out, "w"), cwd=cwd)
154+
155+ test_exit_code = gotest_process.returncode
164156 sys.exit(test_exit_code)
165157
166158 # Run the test function
167159 run_test()
168-
169- # Move coverage output file
170- shutil.move('windows-coverage.out', coverage_out)
171-
160+
172161 # List report directory contents again
173162 print(os.listdir(report_dir))
174163
@@ -182,13 +171,25 @@ stages:
182171 import os
183172 import subprocess
184173
185- cwd = sys.argv[1]
186174 # Define the necessary variables
175+ cwd = sys.argv[1]
176+ cwd = os.path.realpath(cwd)
187177 bin_install_dir = os.path.join(cwd, 'bin')
178+ os.environ['GOBIN'] = bin_install_dir
179+
188180 report_dir = os.environ['REPORT_DIR']
189- coverage_out = os.environ['COVERAGE_OUT']
181+ report_dir = os.path.realpath(report_dir)
182+
183+ coverage_file = os.environ['COVERAGE_OUT']
184+ coverage_out = os.path.join(report_dir, coverage_file)
190185 coverage_json = os.path.join(report_dir, 'windows-coverage.json')
191- gocov_xml_file = os.path.join(report_dir, 'windows-coverage.gocov.xml')
186+ coverage_xml = os.path.join(report_dir, 'windows-coverage.xml')
187+
188+ gocover_file = os.environ['GOCOV_OUT']
189+ gocover_out = os.path.join(report_dir, gocover_file)
190+
191+ junit_file = os.environ['REPORT_XML']
192+ junit_xml = os.path.join(report_dir, junit_file)
192193
193194 # Set the GOBIN environment variable
194195 os.environ['GOBIN'] = BIN_INSTALL_DIR
@@ -206,22 +207,28 @@ stages:
206207
207208 # Convert coverage output to JSON
208209 with open(coverage_json, 'w') as json_file:
209- subprocess.run([gocov_bin, 'convert', coverage_out], stdout=json_file, check=True)
210+ subprocess.run([gocov_bin, 'convert', coverage_out], stdout=json_file, check=True)
210211
211- # Convert JSON to XML
212- with open(gocov_xml_file , 'w') as xml_file:
212+ # create coverage xml
213+ with open(coverage_xml , 'w') as xml_file:
213214 with open(coverage_json, 'r') as json_file:
214215 subprocess.run([gocov_xml_bin], stdin=json_file, stdout=xml_file, check=True)
215-
216- # Set the coverage output XML variable
217- with open(coverage_out, 'r') as f:
218- coverage_out_xml_content = f.read()
219- print(f"##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]{coverage_out_xml_content}")
220-
221- # Print the XML content to the console
222- with open(gocov_xml_file, 'r') as xml_file:
223- xml_content = xml_file.read()
224- print(f"##vso[task.setvariable variable=GOCOV_OUT_XML;isOutput=true]{xml_content}")
216+ # coverage json
217+ with open(coverage_json, 'r') as f:
218+ coverage_out_json_content = f.read()
219+ print(f"##vso[task.setvariable variable=COVERAGE_OUT_JSON;isOutput=true]{coverage_out_json_content}")
220+ # coverage xml
221+ with open(coverage_xml, 'r') as xml_file:
222+ xml_content = xml_file.read()
223+ print(f"##vso[task.setvariable variable=COVERAGE_OUT_XML;isOutput=true]{xml_content}")
224+ # go cover
225+ with open(gocover_out, 'r') as f:
226+ gocover_out_content = f.read()
227+ print(f"##vso[task.setvariable variable=GOCOV_OUT;isOutput=true]{gocover_out_content}")
228+ # junit xml
229+ with open(junit_xml, 'r') as f:
230+ junit_xml_content = f.read()
231+ print(f"##vso[task.setvariable variable=JUNIT_XML;isOutput=true]{junit_xml_content}")
225232
226233
227234 - job : coverage
@@ -235,17 +242,19 @@ stages:
235242 variables :
236243 ob_outputDirectory : $(Build.ArtifactStagingDirectory)/out
237244
245+ WINDOWS_JUNIT_XML : $[ dependencies.windows.outputs['report.JUNIT_XML'] ]
238246 WINDOWS_COVERAGE_OUT : $[ dependencies.windows.outputs['report.COVERAGE_OUT_XML'] ]
239- WINDOWS_GOCOV_OUT : $[ dependencies.windows.outputs['report.GOCOV_OUT_XML '] ]
247+ WINDOWS_GOCOV_OUT : $[ dependencies.windows.outputs['report.GOCOV_OUT '] ]
240248 LINUX_COVERAGE_OUT : $[ dependencies.linux.outputs['report.COVERAGE_OUT_XML'] ]
241249 LINUX_GOCOV_OUT : $[ dependencies.linux.outputs['report.GOCOV_OUT_XML'] ]
242250 steps :
243251 - script : |
244252 mkdir "$COV_DIR"
245253 echo "$LINUX_COVERAGE_OUT" > "$COV_DIR"/linux-coverage.xml
246254 echo "$LINUX_GOCOV_OUT" > "$COV_DIR"/linux-coverage.gocov.xml
247- echo "$WIN_COVERAGE_OUT" > "$COV_DIR"/windows-coverage.xml
248- echo "$WIN_GOCOV_OUT" > "$COV_DIR"/windows-coverage.gocov.xml
255+ echo "$WINDOWS_COVERAGE_OUT" > "$COV_DIR"/windows-coverage.xml
256+ echo "$WINDOWS_GOCOV_OUT" > "$COV_DIR"/windows-coverage.gocov.xml
257+ echo "$WINDOWS_JUNIT_XML" > "$COV_DIR"/windows-coverage.junit.xml
249258 env:
250259 COV_DIR: $(Build.ArtifactStagingDirectory)/coverage
251260
@@ -254,11 +263,12 @@ stages:
254263 inputs :
255264 testResultsFormat : ' JUnit'
256265 testResultsFiles : # string. Required. Test results files. Default: **/TEST-*.xml.
257- searchFolder : coverage/**/*.gocov. xml
266+ searchFolder : coverage/**/*.xml
258267 failTaskOnFailedTests : true
259268 failTaskOnMissingResultsFile : false
260269 # testRunTitle: # Name of the test runs
261- # mergeTestResults: false # boolean. Merge test results. Default: false.
270+ # boolean. Merge test results. Default: false.
271+ mergeTestResults : true
262272 # failTaskOnFailureToPublishResults: false # boolean. Fail if there is failure in publishing test results. Default: false.
263273 # Advanced
264274 # buildPlatform: windows/amd64
0 commit comments