From 23ea060d1ecfb8371c641b9945e845711fa0d57d Mon Sep 17 00:00:00 2001 From: Rishi Date: Sat, 11 Mar 2023 14:19:21 +0530 Subject: [PATCH] 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 --- pkg/test/harness.go | 4 ++- pkg/test/harness_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/pkg/test/harness.go b/pkg/test/harness.go index b221a5bf..4edcf957 100644 --- a/pkg/test/harness.go +++ b/pkg/test/harness.go @@ -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)) } } diff --git a/pkg/test/harness_test.go b/pkg/test/harness_test.go index 6eed8d93..a995f871 100644 --- a/pkg/test/harness_test.go +++ b/pkg/test/harness_test.go @@ -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) { @@ -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