Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic test coverage report format for SonarQube #1612

Merged
merged 35 commits into from
Jan 18, 2024

Conversation

mrodm
Copy link
Contributor

@mrodm mrodm commented Dec 22, 2023

Relates #1595

  • Update Cobertura coverage reports
  • Add support for Generic test coverage report format for SonarQube
  • Add new flag to create Generic or Cobertura coverage reports
    • Added --coverage-format flag
  • Add minimal support for coverage input packages
    • Assets tests in input packages are not working
    • Complete support for input packages still requires more work to do.

PR to test these changes in the integrations repository: elastic/integrations#8464

Example of commands:

# to be backwards compatible, this generates cobertura reports
elastic-package test --test-coverage

# Using the new flag to set the desired format
elastic-package test --test-coverage --coverage-format=cobertura
elastic-package test --test-coverage --coverage-format=generic

IMPORTANT: Introduced a workaround to be able to show coverage for asset, static, system and pipeline (if no ingest pipeline is defined) tests in SonarQube

  • Use different lines per each test type to show if it is covered or not (if tests exist or not).
    • Ensure that it gets represented by SonarQube as a missing or covered line.

Fixes performed in Cobertura format:

  • Use valid file paths for filename attribute in class element
    • Required by SonarQube
  • Add information about lines in class element too
  • Remove hits key from CoberturaMethod (not present in the DTD).

Before:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage line-rate="0" branch-rate="0" version="" timestamp="1705071908664826427" lines-covered="0" lines-valid="0" branches-covered="0" branches-valid="0" complexity="0">
  <sources></sources>
  <packages>
    <package name="mongodb" line-rate="0" branch-rate="0" complexity="0">
      <classes>
        <class name="system" filename="mongodb/status" line-rate="0" branch-rate="0" complexity="0">
          <methods>
            <method name="OK" signature="" line-rate="0" branch-rate="0" complexity="0" hits="0">
              <lines>
                <line number="1" hits="1"></line>
              </lines>
            </method>
          </methods>
          <lines></lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

After using Cobertura:

<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage line-rate="0" branch-rate="0" version="" timestamp="1705071445658692562" lines-covered="0" lines-valid="0" branches-covered="0" branches-valid="0" complexity="0">
  <sources></sources>
  <packages>
    <package name="mongodb" line-rate="0" branch-rate="0" complexity="0">
      <classes>
        <class name="system" filename="test/packages/parallel/mongodb/data_stream/status/manifest.yml" line-rate="0" branch-rate="0" complexity="0">
          <methods>
            <method name="OK" signature="" line-rate="0" branch-rate="0" complexity="0">
              <lines>
                <line number="3" hits="1"></line>
              </lines>
            </method>
          </methods>
          <lines>
            <line number="3" hits="1"></line>
          </lines>
        </class>
      </classes>
    </package>
  </packages>
</coverage>

After using Generic test format:

<?xml version="1.0" encoding="UTF-8"?>

<coverage version="1">
  <file path="packages/elastic_package_registry/data_stream/metrics/manifest.yml">
    <lineToCover lineNumber="3" covered="true"></lineToCover>
  </file>
  <!--Coverage for system test-->
</coverage>

@mrodm mrodm force-pushed the update_paths_coverage_reports branch from 018e2d4 to 9cb94d8 Compare January 12, 2024 12:11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has been renamed to be coveragereport.go and part of its logic related to the Cobertura format has been moved to its own file coberturacoverage.go

@mrodm mrodm marked this pull request as ready for review January 17, 2024 15:01
@mrodm mrodm requested a review from a team January 17, 2024 15:02
@mrodm mrodm self-assigned this Jan 17, 2024
@mrodm mrodm changed the title Update paths coverage Add generic test coverage report format for SonarQube Jan 17, 2024
@@ -153,6 +153,8 @@ func loadElasticsearchAssets(pkgRootPath string) ([]Asset, error) {
}
}

// TODO add assets for input packages
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue for this: #1623

Comment on lines +59 to +66
type CoberturaMethod struct {
Name string `xml:"name,attr"`
Signature string `xml:"signature,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Lines []*CoberturaLine `xml:"lines>line"`
}
Copy link
Contributor Author

@mrodm mrodm Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed Hits field from this struct since it is not present in the DTD

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this was used by the jenkins plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked the Jenkins plugin code , looking at the code it does not look like it was used. And their tests do not include that field neither

https://github.com/jenkinsci/cobertura-plugin/blob/ff0973461f60c28dbc0bf5ab4853a5aec4798210/src/test/resources/hudson/plugins/cobertura/coverage-with-data.xml

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +59 to +66
type CoberturaMethod struct {
Name string `xml:"name,attr"`
Signature string `xml:"signature,attr"`
LineRate float32 `xml:"line-rate,attr"`
BranchRate float32 `xml:"branch-rate,attr"`
Complexity float32 `xml:"complexity,attr"`
Lines []*CoberturaLine `xml:"lines>line"`
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if this was used by the jenkins plugin?

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @mrodm

@mrodm mrodm merged commit 282cfe3 into elastic:main Jan 18, 2024
3 checks passed
@mrodm mrodm deleted the update_paths_coverage_reports branch January 18, 2024 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants