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

Adds support for junit xml output #527

Merged
merged 1 commit into from
Feb 16, 2021

Conversation

patilpankaj212
Copy link
Contributor

-added support for junit xml output
-unit tests for junit xml output
-for now we will have one test suite as part of output
-added few attributes (File, Severity, Line, Category) as test case attribute which is non standard junitXML
-added violation details as part of test case failure message

@codecov
Copy link

codecov bot commented Feb 3, 2021

Codecov Report

Merging #527 (237c04e) into master (7b3593f) will increase coverage by 0.65%.
The diff coverage is 98.38%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #527      +/-   ##
==========================================
+ Coverage   76.33%   76.98%   +0.65%     
==========================================
  Files          97       98       +1     
  Lines        2303     2364      +61     
==========================================
+ Hits         1758     1820      +62     
+ Misses        405      404       -1     
  Partials      140      140              
Impacted Files Coverage Δ
pkg/cli/register.go 5.55% <0.00%> (ø)
pkg/results/types.go 100.00% <ø> (ø)
pkg/policy/opa/engine.go 63.11% <100.00%> (+0.16%) ⬆️
pkg/writer/junit_xml.go 100.00% <100.00%> (ø)
pkg/version/version.go 100.00% <0.00%> (+100.00%) ⬆️

@cesar-rodriguez cesar-rodriguez changed the title support junit xml Adds support for junit xml output Feb 4, 2021
@mikaelkrief
Copy link

+1 interested by this feature

@jlk
Copy link
Contributor

jlk commented Feb 10, 2021

code review looks good. I want to test a little in the morning before approving.

pkg/writer/junit_xml.go Show resolved Hide resolved
pkg/writer/junit_xml.go Show resolved Hide resolved
pkg/writer/junit_xml.go Show resolved Hide resolved
func JUnitXMLWriter(data interface{}, writer io.Writer) error {
output, ok := data.(policy.EngineOutput)
if !ok {
return fmt.Errorf("incorrect input for JunitXML writer, supportted type is policy.EngineOutput")
Copy link
Contributor

Choose a reason for hiding this comment

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

typo => supportted.

Also, the error message seems good for a debug level log but not for an error level log as the user should not be bothered to understand what is policy.EngineOutput.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error is only for the developers, so that developers would know what type to pass

Copy link
Contributor

Choose a reason for hiding this comment

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

alrighty

Copy link
Contributor

Choose a reason for hiding this comment

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

can you correct the type though? @patilpankaj212

testCase = JUnitTestCase{Failure: new(JUnitFailure)}
}
testCase.Classname = v.File
testCase.Name = "[ERROR] resource: " + fmt.Sprintf(`"%s"`, v.ResourceName) + " at line: " + fmt.Sprintf("%d", v.LineNumber) + " violates: " + v.RuleID
Copy link
Contributor

Choose a reason for hiding this comment

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

We can wrap this whole string under fmt.Sprintf

Copy link
Contributor

Choose a reason for hiding this comment

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

skipMsg := new(JUnitSkipMessage)
skipMsg.Message = v.Comment

testCase = JUnitTestCase{Failure: new(JUnitFailure), SkipMessage: skipMsg}

Summary: results.ScanSummary{
ResourcePath: "test_resource_path",
IacType: "k8s",
Timestamp: "2020-12-12 11:21:29.902796 +0000 UTC",
Copy link
Contributor

Choose a reason for hiding this comment

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

@patilpankaj212 you sure about hardcoding the timestamp?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there anything wrong with it?

@@ -55,6 +55,7 @@ type ScanSummary struct {
LowCount int `json:"low" yaml:"low" xml:"low,attr"`
MediumCount int `json:"medium" yaml:"medium" xml:"medium,attr"`
HighCount int `json:"high" yaml:"high" xml:"high,attr"`
TotalTime int64 `json:"-" yaml:"-" xml:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be left blank for encoding?

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 is done on purpose, it is not required for other output types.

pkg/cli/register.go Show resolved Hide resolved
return
}
if gotWriter := writer.String(); !strings.EqualFold(strings.TrimSpace(gotWriter), strings.TrimSpace(tt.wantWriter)) {
fmt.Println(gotWriter)
Copy link
Contributor

Choose a reason for hiding this comment

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

@patilpankaj212 you forgot to remove these console log lines

testCase = JUnitTestCase{Failure: new(JUnitFailure)}
}
testCase.Classname = v.File
testCase.Name = "[ERROR] resource: " + fmt.Sprintf(`"%s"`, v.ResourceName) + " at line: " + fmt.Sprintf("%d", v.LineNumber) + " violates: " + v.RuleID
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
testCase.Name = "[ERROR] resource: " + fmt.Sprintf(`"%s"`, v.ResourceName) + " at line: " + fmt.Sprintf("%d", v.LineNumber) + " violates: " + v.RuleID
testCase.Name = "[ERROR] resource: " + fmt.Sprintf(`"%s,"`, v.ResourceName) + " at line: " + fmt.Sprintf("%d", v.LineNumber) + " violates: rule " + v.RuleID

@sonarcloud
Copy link

sonarcloud bot commented Feb 16, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

</testcase>
</testsuite>
</testsuites>
`, version.Get())
Copy link
Contributor

Choose a reason for hiding this comment

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

good one

Copy link
Contributor

@devang-gaur devang-gaur left a comment

Choose a reason for hiding this comment

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

LGTM

@devang-gaur devang-gaur merged commit efeed62 into tenable:master Feb 16, 2021
@patilpankaj212 patilpankaj212 deleted the support-junitXML branch May 5, 2022 11:36
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.

4 participants