Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/container-structure-test/app/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func run(out io.Writer) error {
channel := make(chan interface{}, 1)
go runTests(out, channel, args, driverImpl)
// TODO(nkubala): put a sync.WaitGroup here
return test.ProcessResults(out, opts.Output, channel)
return test.ProcessResults(out, opts.Output, opts.JunitSuiteName, channel)
}

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

cmd.Flags().StringArrayVarP(&opts.ConfigFiles, "config", "c", []string{}, "test config files")
cmd.MarkFlagRequired("config")
Expand Down
4 changes: 2 additions & 2 deletions cmd/container-structure-test/app/cmd/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Parse(fp string, args *drivers.DriverConfig, driverImpl func(drivers.Driver
return tests, nil
}

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

return err
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ type StructureTestOptions struct {
TestReport string
ConfigFiles []string

JSON bool
Output unversioned.OutputValue
Pull bool
Save bool
Quiet bool
Force bool
NoColor bool
JSON bool
Output unversioned.OutputValue
JunitSuiteName string
Pull bool
Save bool
Quiet bool
Force bool
NoColor bool
}
13 changes: 11 additions & 2 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ import (

var bannerLength = 27 // default banner length

const DefaultJunitSuiteName = "container-structure-test.test"

func getJunitSuiteName(junitSuiteName string) string {
if junitSuiteName != "" {
return junitSuiteName
}
return DefaultJunitSuiteName
}

func OutputResult(out io.Writer, result *types.TestResult) {
color.Default.Fprintf(out, "=== RUN: %s\n", result.Name)
if result.Pass {
Expand Down Expand Up @@ -58,7 +67,7 @@ func Banner(out io.Writer, filename string) {
color.Purple.Fprintln(out, strings.Repeat("=", bannerLength))
}

func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryObject) error {
func FinalResults(out io.Writer, format types.OutputValue, junitSuiteName string, result types.SummaryObject) error {
if format == types.Json {
res, err := json.Marshal(result)
if err != nil {
Expand Down Expand Up @@ -95,7 +104,7 @@ func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryO
Total: result.Total,
Duration: time.Duration.Seconds(result.Duration), // JUnit expects durations as float of seconds
TestSuite: types.JUnitTestSuite{
Name: "container-structure-test.test",
Name: getJunitSuiteName(junitSuiteName),
Results: junit_cases,
},
}
Expand Down
20 changes: 18 additions & 2 deletions pkg/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,46 @@ func TestFinalResults(t *testing.T) {
},
}

var customSuiteName = "custom-suite-name"

var finalResultsTests = []struct {
actual *bytes.Buffer
format unversioned.OutputValue
name string
expected string
}{
{
actual: bytes.NewBuffer([]byte{}),
format: unversioned.Junit,
name: "junit-default-suite-name",
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>`,
},
{
actual: bytes.NewBuffer([]byte{}),
format: unversioned.Junit,
name: "junit-custom-suite-name",
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>`,
},
{
actual: bytes.NewBuffer([]byte{}),
format: unversioned.Json,
name: "json",
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}]}`,
},
}

for _, test := range finalResultsTests {
test := test

t.Run(test.format.String(), func(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

FinalResults(test.actual, test.format, result)
junitSuite := ""
if strings.Contains(test.name, customSuiteName) {
junitSuite = customSuiteName
}

FinalResults(test.actual, test.format, junitSuite, result)

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