-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
41b1966
commit 595060e
Showing
8 changed files
with
275 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |