Skip to content

Commit

Permalink
Set reportFormat to lower before generating report
Browse files Browse the repository at this point in the history
Fixes issue #449

When reportFormat in `kuttl-test.yaml` is specified in uppercase,
it is passed as it is to report.Report func in harness.go.
It needs to be passed as lowercase so that
the `switch` compares against valid report format types (`ftype`)

Also added unit tests

List of tests added:

- should_create_an_XML_report_when_format_is_XML
- should_create_an_XML_report_when_format_is_xml
- should_create_an_JSON_report_when_format_is_JSON
- should_create_an_JSON_report_when_format_is_json
- should_not_create_any_report_when_format_is_empty

Signed-off-by: Rishikesh Nair <[email protected]>
  • Loading branch information
Rishi authored and rishinair11 committed Mar 11, 2023
1 parent f6d64c9 commit 23ea060
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/test/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,9 @@ func (h *Harness) Report() {
if len(h.TestSuite.ReportFormat) == 0 {
return
}
if err := h.report.Report(h.TestSuite.ArtifactsDir, h.reportName(), report.Type(h.TestSuite.ReportFormat)); err != nil {

reportType := report.Type(strings.ToLower(h.TestSuite.ReportFormat))
if err := h.report.Report(h.TestSuite.ArtifactsDir, h.reportName(), reportType); err != nil {
h.fatal(fmt.Errorf("fatal error writing report: %v", err))
}
}
Expand Down
75 changes: 75 additions & 0 deletions pkg/test/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
volumetypes "github.com/docker/docker/api/types/volume"
"github.com/stretchr/testify/assert"
kindConfig "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"

harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/report"
)

func TestGetTimeout(t *testing.T) {
Expand All @@ -28,6 +31,78 @@ func TestGetReportName(t *testing.T) {
assert.Equal(t, "special-kuttl-report", h.reportName())
}

func TestHarnessReport(t *testing.T) {
type HarnessTest struct {
name string
expectedFormat string
h *Harness
}

tests := []HarnessTest{
{
name: "should create an XML report when format is XML",
expectedFormat: "xml",
h: &Harness{
TestSuite: harness.TestSuite{
ReportFormat: "XML",
},
report: &report.Testsuites{},
},
}, {
name: "should create an XML report when format is xml",
expectedFormat: "xml",
h: &Harness{
TestSuite: harness.TestSuite{
ReportFormat: "xml",
},
report: &report.Testsuites{},
},
}, {
name: "should create an JSON report when format is JSON",
expectedFormat: "json",
h: &Harness{
TestSuite: harness.TestSuite{
ReportFormat: "JSON",
},
report: &report.Testsuites{},
},
}, {
name: "should create an JSON report when format is json",
expectedFormat: "json",
h: &Harness{
TestSuite: harness.TestSuite{
ReportFormat: "json",
},
report: &report.Testsuites{},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set the artifacts dir for current test run
tt.h.TestSuite.ArtifactsDir = t.TempDir()
tt.h.Report()
assert.FileExists(t, fmt.Sprintf("%s/%s.%s", tt.h.TestSuite.ArtifactsDir, "kuttl-report", tt.expectedFormat))
})
}

// unit test for not passing any report format
emptyTest := HarnessTest{
name: "should not create any report when format is empty",
expectedFormat: "json",
h: &Harness{
TestSuite: harness.TestSuite{},
report: &report.Testsuites{},
},
}
t.Run(emptyTest.name, func(t *testing.T) {
emptyTest.h.TestSuite.ArtifactsDir = t.TempDir()
emptyTest.h.Report()
assert.NoFileExists(t, fmt.Sprintf("%s/%s.%s", emptyTest.h.TestSuite.ArtifactsDir, "kuttl-report", emptyTest.expectedFormat))
})
}

type dockerMock struct {
ImageWriter *io.PipeWriter
imageReader *io.PipeReader
Expand Down

0 comments on commit 23ea060

Please sign in to comment.