Skip to content

Commit 018b3cf

Browse files
authored
Allow custom junit suite name via command line argument (#519)
* Allow custom junit suite name via command line argument * address gemini comments
1 parent 033edc5 commit 018b3cf

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

cmd/container-structure-test/app/cmd/test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func run(out io.Writer) error {
205205
channel := make(chan interface{}, 1)
206206
go runTests(out, channel, args, driverImpl)
207207
// TODO(nkubala): put a sync.WaitGroup here
208-
return test.ProcessResults(out, opts.Output, channel)
208+
return test.ProcessResults(out, opts.Output, opts.JunitSuiteName, channel)
209209
}
210210

211211
func runTests(out io.Writer, channel chan interface{}, args *drivers.DriverConfig, driverImpl func(drivers.DriverConfig) (drivers.Driver, error)) {
@@ -246,6 +246,7 @@ func AddTestFlags(cmd *cobra.Command) {
246246
cmd.Flags().MarkDeprecated("json", "please use --output instead")
247247
cmd.Flags().VarP(&opts.Output, "output", "o", "output format for the test report (available format: text, json, junit)")
248248
cmd.Flags().BoolVar(&opts.NoColor, "no-color", false, "no color in the output")
249+
cmd.Flags().StringVar(&opts.JunitSuiteName, "junit-suite-name", "", fmt.Sprintf("name to use for the junit test suite (defaults to '%s')", output.DefaultJunitSuiteName))
249250

250251
cmd.Flags().StringArrayVarP(&opts.ConfigFiles, "config", "c", []string{}, "test config files")
251252
cmd.MarkFlagRequired("config")

cmd/container-structure-test/app/cmd/test/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Parse(fp string, args *drivers.DriverConfig, driverImpl func(drivers.Driver
105105
return tests, nil
106106
}
107107

108-
func ProcessResults(out io.Writer, format unversioned.OutputValue, c chan interface{}) error {
108+
func ProcessResults(out io.Writer, format unversioned.OutputValue, junitSuiteName string, c chan interface{}) error {
109109
totalPass := 0
110110
totalFail := 0
111111
totalDuration := time.Duration(0)
@@ -143,7 +143,7 @@ func ProcessResults(out io.Writer, format unversioned.OutputValue, c chan interf
143143
// only output results here if we're in json mode
144144
summary.Results = results
145145
}
146-
output.FinalResults(out, format, summary)
146+
output.FinalResults(out, format, junitSuiteName, summary)
147147

148148
return err
149149
}

pkg/config/options.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ type StructureTestOptions struct {
2828
TestReport string
2929
ConfigFiles []string
3030

31-
JSON bool
32-
Output unversioned.OutputValue
33-
Pull bool
34-
Save bool
35-
Quiet bool
36-
Force bool
37-
NoColor bool
31+
JSON bool
32+
Output unversioned.OutputValue
33+
JunitSuiteName string
34+
Pull bool
35+
Save bool
36+
Quiet bool
37+
Force bool
38+
NoColor bool
3839
}

pkg/output/output.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ import (
3131

3232
var bannerLength = 27 // default banner length
3333

34+
const DefaultJunitSuiteName = "container-structure-test.test"
35+
36+
func getJunitSuiteName(junitSuiteName string) string {
37+
if junitSuiteName != "" {
38+
return junitSuiteName
39+
}
40+
return DefaultJunitSuiteName
41+
}
42+
3443
func OutputResult(out io.Writer, result *types.TestResult) {
3544
color.Default.Fprintf(out, "=== RUN: %s\n", result.Name)
3645
if result.Pass {
@@ -58,7 +67,7 @@ func Banner(out io.Writer, filename string) {
5867
color.Purple.Fprintln(out, strings.Repeat("=", bannerLength))
5968
}
6069

61-
func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryObject) error {
70+
func FinalResults(out io.Writer, format types.OutputValue, junitSuiteName string, result types.SummaryObject) error {
6271
if format == types.Json {
6372
res, err := json.Marshal(result)
6473
if err != nil {
@@ -95,7 +104,7 @@ func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryO
95104
Total: result.Total,
96105
Duration: time.Duration.Seconds(result.Duration), // JUnit expects durations as float of seconds
97106
TestSuite: types.JUnitTestSuite{
98-
Name: "container-structure-test.test",
107+
Name: getJunitSuiteName(junitSuiteName),
99108
Results: junit_cases,
100109
},
101110
}

pkg/output/output_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,46 @@ func TestFinalResults(t *testing.T) {
3636
},
3737
}
3838

39+
var customSuiteName = "custom-suite-name"
40+
3941
var finalResultsTests = []struct {
4042
actual *bytes.Buffer
4143
format unversioned.OutputValue
44+
name string
4245
expected string
4346
}{
4447
{
4548
actual: bytes.NewBuffer([]byte{}),
4649
format: unversioned.Junit,
50+
name: "junit-default-suite-name",
4751
expected: `<?xml version="1.0" encoding="UTF-8"?><testsuites failures="1" tests="2" time="2e-09"><testsuite name="container-structure-test.test"><testcase name="my first test" time="1e-09"><system-out>it works!</system-out><system-err></system-err></testcase><testcase name="my fail" time="1e-09"><failure>this failed because of that</failure><system-out></system-out><system-err>this failed</system-err></testcase></testsuite></testsuites>`,
4852
},
53+
{
54+
actual: bytes.NewBuffer([]byte{}),
55+
format: unversioned.Junit,
56+
name: "junit-custom-suite-name",
57+
expected: `<?xml version="1.0" encoding="UTF-8"?><testsuites failures="1" tests="2" time="2e-09"><testsuite name="` + customSuiteName + `"><testcase name="my first test" time="1e-09"><system-out>it works!</system-out><system-err></system-err></testcase><testcase name="my fail" time="1e-09"><failure>this failed because of that</failure><system-out></system-out><system-err>this failed</system-err></testcase></testsuite></testsuites>`,
58+
},
4959
{
5060
actual: bytes.NewBuffer([]byte{}),
5161
format: unversioned.Json,
62+
name: "json",
5263
expected: `{"Pass":1,"Fail":1,"Total":2,"Duration":2,"Results":[{"Name":"my first test","Pass":true,"Stdout":"it works!","Duration":1},{"Name":"my fail","Pass":false,"Stderr":"this failed","Errors":["this failed because of that"],"Duration":1}]}`,
5364
},
5465
}
5566

5667
for _, test := range finalResultsTests {
5768
test := test
5869

59-
t.Run(test.format.String(), func(t *testing.T) {
70+
t.Run(test.name, func(t *testing.T) {
6071
t.Parallel()
6172

62-
FinalResults(test.actual, test.format, result)
73+
junitSuite := ""
74+
if strings.Contains(test.name, customSuiteName) {
75+
junitSuite = customSuiteName
76+
}
77+
78+
FinalResults(test.actual, test.format, junitSuite, result)
6379

6480
if strings.TrimSpace(test.actual.String()) != test.expected {
6581
t.Errorf("expected %s but got %s", test.expected, test.actual)

0 commit comments

Comments
 (0)