Skip to content

Commit 23ea060

Browse files
Rishirishinair11
Rishi
authored andcommitted
Set reportFormat to lower before generating report
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]>
1 parent f6d64c9 commit 23ea060

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

pkg/test/harness.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,9 @@ func (h *Harness) Report() {
605605
if len(h.TestSuite.ReportFormat) == 0 {
606606
return
607607
}
608-
if err := h.report.Report(h.TestSuite.ArtifactsDir, h.reportName(), report.Type(h.TestSuite.ReportFormat)); err != nil {
608+
609+
reportType := report.Type(strings.ToLower(h.TestSuite.ReportFormat))
610+
if err := h.report.Report(h.TestSuite.ArtifactsDir, h.reportName(), reportType); err != nil {
609611
h.fatal(fmt.Errorf("fatal error writing report: %v", err))
610612
}
611613
}

pkg/test/harness_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
volumetypes "github.com/docker/docker/api/types/volume"
1111
"github.com/stretchr/testify/assert"
1212
kindConfig "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
13+
14+
harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
15+
"github.com/kudobuilder/kuttl/pkg/report"
1316
)
1417

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

34+
func TestHarnessReport(t *testing.T) {
35+
type HarnessTest struct {
36+
name string
37+
expectedFormat string
38+
h *Harness
39+
}
40+
41+
tests := []HarnessTest{
42+
{
43+
name: "should create an XML report when format is XML",
44+
expectedFormat: "xml",
45+
h: &Harness{
46+
TestSuite: harness.TestSuite{
47+
ReportFormat: "XML",
48+
},
49+
report: &report.Testsuites{},
50+
},
51+
}, {
52+
name: "should create an XML report when format is xml",
53+
expectedFormat: "xml",
54+
h: &Harness{
55+
TestSuite: harness.TestSuite{
56+
ReportFormat: "xml",
57+
},
58+
report: &report.Testsuites{},
59+
},
60+
}, {
61+
name: "should create an JSON report when format is JSON",
62+
expectedFormat: "json",
63+
h: &Harness{
64+
TestSuite: harness.TestSuite{
65+
ReportFormat: "JSON",
66+
},
67+
report: &report.Testsuites{},
68+
},
69+
}, {
70+
name: "should create an JSON report when format is json",
71+
expectedFormat: "json",
72+
h: &Harness{
73+
TestSuite: harness.TestSuite{
74+
ReportFormat: "json",
75+
},
76+
report: &report.Testsuites{},
77+
},
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
// set the artifacts dir for current test run
84+
tt.h.TestSuite.ArtifactsDir = t.TempDir()
85+
tt.h.Report()
86+
assert.FileExists(t, fmt.Sprintf("%s/%s.%s", tt.h.TestSuite.ArtifactsDir, "kuttl-report", tt.expectedFormat))
87+
})
88+
}
89+
90+
// unit test for not passing any report format
91+
emptyTest := HarnessTest{
92+
name: "should not create any report when format is empty",
93+
expectedFormat: "json",
94+
h: &Harness{
95+
TestSuite: harness.TestSuite{},
96+
report: &report.Testsuites{},
97+
},
98+
}
99+
t.Run(emptyTest.name, func(t *testing.T) {
100+
emptyTest.h.TestSuite.ArtifactsDir = t.TempDir()
101+
emptyTest.h.Report()
102+
assert.NoFileExists(t, fmt.Sprintf("%s/%s.%s", emptyTest.h.TestSuite.ArtifactsDir, "kuttl-report", emptyTest.expectedFormat))
103+
})
104+
}
105+
31106
type dockerMock struct {
32107
ImageWriter *io.PipeWriter
33108
imageReader *io.PipeReader

0 commit comments

Comments
 (0)