Skip to content

Commit

Permalink
chore: add unit tests (#2224)
Browse files Browse the repository at this point in the history
* chore: add unit tests

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

* report tests

Signed-off-by: Charles-Edouard Brétéché <[email protected]>

---------

Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Dec 12, 2024
1 parent 41b1966 commit 595060e
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 19 deletions.
29 changes: 15 additions & 14 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ import (
"github.com/kyverno/chainsaw/pkg/model"
)

func Save(report *model.Report, format v1alpha2.ReportFormatType, path, name string) error {
getFile := func(extension string) string {
if filepath.Ext(name) == "" {
name += "." + strings.ToLower(extension)
}
filePath := name
if path != "" {
filePath = filepath.Join(path, name)
}
return filePath
func getFile(path, name, extension string) string {
if filepath.Ext(name) == "" {
name += "." + strings.ToLower(extension)
}
filePath := name
if path != "" {
filePath = filepath.Join(path, name)
}
return filePath
}

func Save(report *model.Report, format v1alpha2.ReportFormatType, path, name string) error {
switch format {
case v1alpha2.XMLFormat, v1alpha2.JUnitTestFormat:
return saveJUnitTest(report, getFile("xml"))
return saveJUnitTest(report, getFile(path, name, "xml"))
case v1alpha2.JUnitStepFormat:
return saveJUnitStep(report, getFile("xml"))
return saveJUnitStep(report, getFile(path, name, "xml"))
case v1alpha2.JUnitOperationFormat:
return saveJUnitOperation(report, getFile("xml"))
return saveJUnitOperation(report, getFile(path, name, "xml"))
case v1alpha2.JSONFormat:
return saveJson(report, getFile("json"))
return saveJson(report, getFile(path, name, "json"))
default:
return fmt.Errorf("unknown report format: %s", format)
}
Expand Down
152 changes: 152 additions & 0 deletions pkg/report/report_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package report

import (
"errors"
"os"
"path/filepath"
"testing"
"time"

"github.com/kyverno/chainsaw/pkg/apis/v1alpha2"
"github.com/kyverno/chainsaw/pkg/model"
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"
)

func Test_getFile(t *testing.T) {
tests := []struct {
path string
name string
extension string
want string
}{{
path: "foo",
name: "bar",
extension: "xml",
want: "foo/bar.xml",
}, {
path: "foo",
name: "bar.json",
extension: "xml",
want: "foo/bar.json",
}, {
path: "foo",
name: "bar.xml",
extension: "xml",
want: "foo/bar.xml",
}, {
path: "",
name: "bar",
extension: "xml",
want: "bar.xml",
}, {
path: "",
name: "bar.json",
extension: "xml",
want: "bar.json",
}, {
path: "",
name: "bar.xml",
extension: "xml",
want: "bar.xml",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := getFile(tt.path, tt.name, tt.extension)
assert.Equal(t, tt.want, got)
})
}
}

func TestSave(t *testing.T) {
report := &model.Report{
Name: "report",
StartTime: time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 44, 58, 651387237, time.UTC),
Tests: []*model.TestReport{{
BasePath: "base-path",
Name: "test-report",
Concurrent: nil,
StartTime: time.Date(2009, 11, 17, 20, 35, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 38, 58, 651387237, time.UTC),
Namespace: "demo",
Skipped: false,
Steps: []*model.StepReport{{
Name: "step-report",
StartTime: time.Date(2009, 11, 17, 20, 36, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 37, 58, 651387237, time.UTC),
Operations: []*model.OperationReport{{
Name: "operation-report",
Type: model.OperationTypeApply,
StartTime: time.Date(2009, 11, 17, 20, 36, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 37, 58, 651387237, time.UTC),
Err: nil,
}, {
Name: "operation-report-err",
Type: model.OperationTypeAssert,
StartTime: time.Date(2009, 11, 17, 20, 36, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 37, 58, 651387237, time.UTC),
Err: errors.New("dummy"),
}},
}},
}, {
BasePath: "base-path",
Name: "test-report-skipped",
Concurrent: ptr.To(true),
StartTime: time.Date(2009, 11, 17, 20, 35, 58, 651387237, time.UTC),
EndTime: time.Date(2009, 11, 17, 20, 38, 58, 651387237, time.UTC),
Namespace: "skipped",
Skipped: true,
}},
}
tests := []struct {
name string
report *model.Report
format v1alpha2.ReportFormatType
wantErr bool
out string
}{{
report: report,
format: v1alpha2.JSONFormat,
out: "JSON.json",
}, {
report: report,
format: v1alpha2.XMLFormat,
out: "XML.xml",
}, {
report: report,
format: v1alpha2.JUnitTestFormat,
out: "JUNIT-TEST.xml",
}, {
report: report,
format: v1alpha2.JUnitStepFormat,
out: "JUNIT-STEP.xml",
}, {
report: report,
format: v1alpha2.JUnitOperationFormat,
out: "JUNIT-OPERATION.xml",
}, {
report: report,
format: v1alpha2.ReportFormatType("xyz"),
wantErr: true,
}}
path, err := os.MkdirTemp("", "")
assert.NoError(t, err)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Save(tt.report, tt.format, path, string(tt.format))
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
if tt.out != "" {
expected, err := os.ReadFile(filepath.Join(path, tt.out))
assert.NoError(t, err)
actual, err := os.ReadFile(filepath.Join(path, tt.out))
assert.NoError(t, err)
assert.Equal(t, string(expected), string(actual))
}
}
})
}
}
9 changes: 4 additions & 5 deletions pkg/runner/internal/test_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"reflect"
"regexp"
"runtime/pprof"
"time"
)

Expand Down Expand Up @@ -32,17 +33,15 @@ func (d *TestDeps) MatchString(pat, str string) (bool, error) {
func (*TestDeps) SetPanicOnExit0(bool) {}

func (*TestDeps) StartCPUProfile(w io.Writer) error {
return nil
// return pprof.StartCPUProfile(w)
return pprof.StartCPUProfile(w)
}

func (*TestDeps) StopCPUProfile() {
// pprof.StopCPUProfile()
pprof.StopCPUProfile()
}

func (*TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
return nil
// return pprof.Lookup(name).WriteTo(w, debug)
return pprof.Lookup(name).WriteTo(w, debug)
}

func (*TestDeps) ImportPath() string {
Expand Down
47 changes: 47 additions & 0 deletions testdata/report/JSON.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "report",
"startTime": "2009-11-17T20:34:58.651387237Z",
"endTime": "2009-11-17T20:44:58.651387237Z",
"tests": [
{
"basePath": "base-path",
"name": "test-report",
"startTime": "2009-11-17T20:35:58.651387237Z",
"endTime": "2009-11-17T20:38:58.651387237Z",
"namespace": "demo",
"steps": [
{
"name": "step-report",
"startTime": "2009-11-17T20:36:58.651387237Z",
"endTime": "2009-11-17T20:37:58.651387237Z",
"operations": [
{
"name": "operation-report",
"type": "apply",
"startTime": "2009-11-17T20:36:58.651387237Z",
"endTime": "2009-11-17T20:37:58.651387237Z"
},
{
"name": "operation-report-err",
"type": "assert",
"startTime": "2009-11-17T20:36:58.651387237Z",
"endTime": "2009-11-17T20:37:58.651387237Z",
"failure": {
"error": "dummy"
}
}
]
}
]
},
{
"basePath": "base-path",
"name": "test-report-skipped",
"concurrent": true,
"startTime": "2009-11-17T20:35:58.651387237Z",
"endTime": "2009-11-17T20:38:58.651387237Z",
"namespace": "skipped",
"skipped": true
}
]
}
19 changes: 19 additions & 0 deletions testdata/report/JUNIT-OPERATION.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<testsuites name="report" time="600.000000" tests="3" failures="1" skipped="1">
<testsuite name="test-report" tests="2" failures="1" errors="0" id="0" package="base-path" time="180.000000" timestamp="2009-11-17T20:34:58Z">
<properties>
<property name="namespace" value="demo"></property>
</properties>
<testcase name="step-report / operation-report" classname="apply" time="60.000000"></testcase>
<testcase name="step-report / operation-report-err" classname="assert" time="60.000000">
<failure message="dummy"></failure>
</testcase>
</testsuite>
<testsuite name="test-report-skipped" tests="1" failures="0" errors="0" id="0" package="base-path" skipped="1" time="180.000000" timestamp="2009-11-17T20:34:58Z">
<properties>
<property name="namespace" value="skipped"></property>
</properties>
<testcase name="test-report-skipped" classname="" time="180.000000">
<skipped message=""></skipped>
</testcase>
</testsuite>
</testsuites>
18 changes: 18 additions & 0 deletions testdata/report/JUNIT-STEP.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<testsuites name="report" time="600.000000" tests="2" failures="1" skipped="1">
<testsuite name="test-report" tests="1" failures="1" errors="0" id="0" package="base-path" time="180.000000" timestamp="2009-11-17T20:34:58Z">
<properties>
<property name="namespace" value="demo"></property>
</properties>
<testcase name="step-report" classname="" time="60.000000">
<failure message="dummy"></failure>
</testcase>
</testsuite>
<testsuite name="test-report-skipped" tests="1" failures="0" errors="0" id="0" package="base-path" skipped="1" time="180.000000" timestamp="2009-11-17T20:34:58Z">
<properties>
<property name="namespace" value="skipped"></property>
</properties>
<testcase name="test-report-skipped" classname="" time="180.000000">
<skipped message=""></skipped>
</testcase>
</testsuite>
</testsuites>
10 changes: 10 additions & 0 deletions testdata/report/JUNIT-TEST.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<testsuites name="report" time="600.000000" tests="2" failures="1" skipped="1">
<testsuite name="base-path" tests="2" failures="1" errors="0" id="0" skipped="1" time="">
<testcase name="test-report" classname="" time="180.000000">
<failure message="dummy"></failure>
</testcase>
<testcase name="test-report-skipped" classname="" time="180.000000">
<skipped message=""></skipped>
</testcase>
</testsuite>
</testsuites>
10 changes: 10 additions & 0 deletions testdata/report/XML.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<testsuites name="report" time="600.000000" tests="2" failures="1" skipped="1">
<testsuite name="base-path" tests="2" failures="1" errors="0" id="0" skipped="1" time="">
<testcase name="test-report" classname="" time="180.000000">
<failure message="dummy"></failure>
</testcase>
<testcase name="test-report-skipped" classname="" time="180.000000">
<skipped message=""></skipped>
</testcase>
</testsuite>
</testsuites>

0 comments on commit 595060e

Please sign in to comment.