Azure / pytest-azurepipelines

Plugin for pytest that makes it simple to work with Azure Pipelines
MIT License
112 stars 35 forks source link

Problem with coverage on docker containers #84

Open interesaaat opened 1 year ago

interesaaat commented 1 year ago

We recently moved our pipeline from running on a VM to running on a docker container. After the move we started to have the below error.

Failed to parse result files: System.IO.DirectoryNotFoundException: Could not find a part of the path '/home/dockeragent/persistent_space/_work/1/s/test-output.xml'.
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
   at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
   at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   at System.Xml.XmlTextReaderImpl.FinishInitUriString()
   at System.Xml.XmlTextReaderImpl..ctor(String uriStr, XmlReaderSettings settings, XmlParserContext context, XmlResolver uriResolver)
   at System.Xml.XmlReaderSettings.CreateReader(String inputUri, XmlParserContext inputContext)
   at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
   at Microsoft.TeamFoundation.TestClient.PublishTestResults.NUnitResultParser.ParseTestResultFile(TestRunContext runContext, String filePath)
   at Microsoft.TeamFoundation.TestClient.PublishTestResults.NUnitResultParser.<>c__DisplayClass1_0.<ParseTestResultFiles>b__0(String file)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at Microsoft.TeamFoundation.TestClient.PublishTestResults.NUnitResultParser.ParseTestResultFiles(TestRunContext runContext, IList`1 resultFilePaths)
   at Microsoft.VisualStudio.Services.Agent.Worker.TestResults.Parser.ParseFiles(IExecutionContext executionContext, TestRunContext testRunContext, List`1 testResultsFiles, ITestResultParser testResultParser) in /home/vsts/work/1/s/src/Agent.Worker/TestResults/Parser.cs:line 57
##[warning]Failed to inline CSS styles in coverage reports. Error: [Errno 2] No such file or directory: '//home/dockeragent/persistent_space/_work/1/s/htmlcov'
Reading code coverage summary from '//home/dockeragent/persistent_space/_work/1/s/coverage.xml'
##[error]Unable to process command '##vso[codecoverage.publish codecoveragetool=Cobertura;summaryfile=//home/dockeragent/persistent_space/_work/1/s/coverage.xml;reportdirectory=//home/dockeragent/persistent_space/_work/1/s/htmlcov;]' successfully. Please reference documentation (http://go.microsoft.com/fwlink/?LinkId=817296)
##[error]File '//home/dockeragent/persistent_space/_work/1/s/coverage.xml' does not exist or is not accessible.

- generated Nunit xml file: /host/home/dockeragent/persistent_space/_work/1/s/test-output.xml -
- generated xml file: /host/home/dockeragent/persistent_space/_work/1/s/junit/test-results.xml -

---------- coverage: platform linux, python 3.8.10-final-0 -----------
Coverage HTML written to dir htmlcov
Coverage XML written to file /host/home/dockeragent/persistent_space/_work/1/s/coverage.xml

================ 834 passed, 208 skipped in 1681.84s (0:28:01) =================
Async Command Start: Publish test results
Async Command End: Publish test results
Finishing: Run tests with coverage

Needless to say, the files are there but it looks they are generate afterwards? We were able to solve this by adding the flag --no-docker-discovery but now no coverage is uploaded (for some reason) and this is the output we are getting.

Skipping uploading of test results because --no-docker-discovery set.
Skipping uploading of coverage data.

- generated Nunit xml file: /host/home/dockeragent/persistent_space/_work/1/s/test-output.xml -
- generated xml file: /host/home/dockeragent/persistent_space/_work/1/s/junit/test-results.xml -

---------- coverage: platform linux, python 3.8.10-final-0 -----------
Coverage HTML written to dir htmlcov
Coverage XML written to file /host/home/dockeragent/persistent_space/_work/1/s/coverage.xml

================ 834 passed, 208 skipped in 1662.15s (0:27:42) =================
Finishing: Run tests with coverage

Any idea on what is going on and how we can fix it?

Kosaaaaa commented 1 year ago

I encountered the same problem. The output file generates correctly, but PyTest-AzurePipelines cannot recognise it.

Docker container command: pip install pytest pytest-azurepipelines && pytest xyz/ --junitxml=test-output.xml

Nantero1 commented 1 year ago

+1

my current dirty workaround, in the job:

    container:
      image: devops-image:latest
      endpoint: "sc-docker"
      options: "-v /agent/_work/1/:/agent/_work/1/"

in the test step:

      - bash: |
          # https://github.com/Azure/pytest-azurepipelines/issues/84
          ln -s "/__w/1/s/" "/agent/_work/1/s/" || true
          python3 -m pytest -vv --cov --cov-config=.coveragerc --cov-report=xml --cov-report html tests
        name: run_tests