Skip to content

Commit

Permalink
addressing comment from Juan
Browse files Browse the repository at this point in the history
  • Loading branch information
edcdavid committed Dec 14, 2023
1 parent cecfefc commit 1bdbf4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
14 changes: 7 additions & 7 deletions cmd/tnf/claim/show/failures/failures.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ func parseOutputFormatFlag() (string, error) {
return "", fmt.Errorf("invalid output format flag %q - available formats: %v", outputFormatFlag, availableOutputFormats)
}

// Parses the claim's test case's skipReason field and creates a list
// Parses the claim's test case's checkDetails field and creates a list
// of NonCompliantObject's.
func getNonCompliantObjectsFromFailureReason(skipReason string) ([]NonCompliantObject, error) {
func getNonCompliantObjectsFromFailureReason(checkDetails string) ([]NonCompliantObject, error) {
objects := struct {
Compliant []testhelper.ReportObject `json:"CompliantObjectsOut"`
NonCompliant []testhelper.ReportObject `json:"NonCompliantObjectsOut"`
}{}

err := json.Unmarshal([]byte(skipReason), &objects)
err := json.Unmarshal([]byte(checkDetails), &objects)
if err != nil {
return nil, fmt.Errorf("failed to decode skipReason %s: %v", skipReason, err)
return nil, fmt.Errorf("failed to decode checkDetails %s: %v", checkDetails, err)
}

// Now let's create a list of our NonCompliantObject-type items.
Expand All @@ -194,7 +194,7 @@ func printFailuresText(testSuites []FailedTestSuite) {

// In case this tc was not using report objects, just print the failure reason string.
if len(tc.NonCompliantObjects) == 0 {
fmt.Printf(" Failure reason: %s\n", tc.SkipReason)
fmt.Printf(" Failure reason: %s\n", tc.CheckDetails)
continue
}

Expand Down Expand Up @@ -253,12 +253,12 @@ func getFailedTestCasesByTestSuite(claimResultsByTestSuite map[string][]*claim.T
TestCaseDescription: tc.CatalogInfo.Description,
}

nonCompliantObjects, err := getNonCompliantObjectsFromFailureReason(tc.SkipReason)
nonCompliantObjects, err := getNonCompliantObjectsFromFailureReason(tc.CheckDetails)
if err != nil {
// This means the test case doesn't use the report objects yet. Just use the raw failure reason instead.
// Also, send the error into stderr, so it can be filtered out with "2>/errors.txt" or "2>/dev/null".
fmt.Fprintf(os.Stderr, "Failed to parse non compliant objects from test case %s (test suite %s): %v", tc.TestID.ID, testSuite, err)
failingTc.SkipReason = tc.SkipReason
failingTc.CheckDetails = tc.CheckDetails
} else {
failingTc.NonCompliantObjects = nonCompliantObjects
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/tnf/claim/show/failures/failures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ func TestParseOutputFormatFlag(t *testing.T) {

func TestGetNonCompliantObjectsFromFailureReason(t *testing.T) {
testCases := []struct {
skipReason string
checkDetails string

Check failure on line 93 in cmd/tnf/claim/show/failures/failures_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

File is not `gofmt`-ed with `-s` (gofmt)
expectedNonCompliantObjects []NonCompliantObject
expectedError string
}{
{
skipReason: "",
checkDetails: "",
expectedNonCompliantObjects: nil,
expectedError: `failed to decode skipReason : unexpected end of JSON input`,
expectedError: `failed to decode checkDetails : unexpected end of JSON input`,
},
{
skipReason: `{"CompliantObjectsOut": null, "NonCompliantObjectsOut": null}`,
checkDetails: `{"CompliantObjectsOut": null, "NonCompliantObjectsOut": null}`,
expectedNonCompliantObjects: []NonCompliantObject{},
},
// One container failed the SYS_ADMIN check.
{
skipReason: `{
checkDetails: `{
"CompliantObjectsOut": null,
"NonCompliantObjectsOut": [
{
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestGetNonCompliantObjectsFromFailureReason(t *testing.T) {
},
// Two containers failed the SYS_ADMIN check.
{
skipReason: `{
checkDetails: `{
"CompliantObjectsOut": null,
"NonCompliantObjectsOut": [
{
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestGetNonCompliantObjectsFromFailureReason(t *testing.T) {
}

for _, tc := range testCases {
nonCompliantObjects, err := getNonCompliantObjectsFromFailureReason(tc.skipReason)
nonCompliantObjects, err := getNonCompliantObjectsFromFailureReason(tc.checkDetails)
if err != nil {
assert.Equal(t, tc.expectedError, err.Error())
}
Expand All @@ -271,7 +271,7 @@ func TestGetNonCompliantObjectsFromFailureReason(t *testing.T) {

// Uses claim files in testdata folder:
// claim1.json -> Two test suites, access-control & platform-alteration. One failed test case in the access-control ts.
// claim2.json -> Same as clam1.json, but the skipReason is a simple string, not using report objects yet.
// claim2.json -> Same as clam1.json, but the checkDetails is a simple string, not using report objects yet.
func TestGetFailedTestCasesByTestSuite(t *testing.T) {
testCases := []struct {
claimFilePath string
Expand All @@ -295,7 +295,7 @@ func TestGetFailedTestCasesByTestSuite(t *testing.T) {
{
TestCaseName: "access-control-sys-admin-capability-check",
TestCaseDescription: "Ensures that containers do not use SYS_ADMIN capability",
SkipReason: "pod xxx ns yyy container zzz uses SYS_ADMIN",
CheckDetails: "pod xxx ns yyy container zzz uses SYS_ADMIN",
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/tnf/claim/show/failures/testdata/claim1.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"endTime": "2023-07-18 03:37:42.095508375 -0500 CDT m=+23.133713410",
"failureLineContent": "\t\tfail(string(bytes))",
"failureLocation": "/home/greyerof/github/tnf/pkg/testhelper/testhelper.go:352",
"skipReason": "{\"CompliantObjectsOut\":null,\"NonCompliantObjectsOut\":[{\"ObjectType\":\"Container\",\"ObjectFieldsKeys\":[\"Reason For Non Compliance\",\"Namespace\",\"Pod Name\",\"Container Name\",\"SCC Capability\"],\"ObjectFieldsValues\":[\"Non compliant capability detected in container\",\"tnf\",\"test-887998557-8gwwm\",\"test\",\"SYS_ADMIN\"]},{\"ObjectType\":\"Container\",\"ObjectFieldsKeys\":[\"Reason For Non Compliance\",\"Namespace\",\"Pod Name\",\"Container Name\",\"SCC Capability\"],\"ObjectFieldsValues\":[\"Non compliant capability detected in container\",\"tnf\",\"test-887998557-pr2w5\",\"test\",\"SYS_ADMIN\"]}]}",
"checkDetails": "{\"CompliantObjectsOut\":null,\"NonCompliantObjectsOut\":[{\"ObjectType\":\"Container\",\"ObjectFieldsKeys\":[\"Reason For Non Compliance\",\"Namespace\",\"Pod Name\",\"Container Name\",\"SCC Capability\"],\"ObjectFieldsValues\":[\"Non compliant capability detected in container\",\"tnf\",\"test-887998557-8gwwm\",\"test\",\"SYS_ADMIN\"]},{\"ObjectType\":\"Container\",\"ObjectFieldsKeys\":[\"Reason For Non Compliance\",\"Namespace\",\"Pod Name\",\"Container Name\",\"SCC Capability\"],\"ObjectFieldsValues\":[\"Non compliant capability detected in container\",\"tnf\",\"test-887998557-pr2w5\",\"test\",\"SYS_ADMIN\"]}]}",
"startTime": "2023-07-18 03:37:42.095225914 -0500 CDT m=+23.133430956",
"state": "failed",
"testID": {
Expand All @@ -34,7 +34,7 @@
"endTime": "2023-07-18 03:37:44.324268378 -0500 CDT m=+25.362473413",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:44.324023044 -0500 CDT m=+25.362228078",
"state": "passed",
"testID": {
Expand All @@ -61,7 +61,7 @@
"endTime": "0001-01-01 00:00:00 +0000 UTC",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:46.483797163 -0500 CDT m=+27.522002219",
"state": "skipped",
"testID": {
Expand All @@ -88,7 +88,7 @@
"endTime": "0001-01-01 00:00:00 +0000 UTC",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:46.483566421 -0500 CDT m=+27.521771494",
"state": "skipped",
"testID": {
Expand Down
8 changes: 4 additions & 4 deletions cmd/tnf/claim/show/failures/testdata/claim2.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"endTime": "2023-07-18 03:37:42.095508375 -0500 CDT m=+23.133713410",
"failureLineContent": "\t\tfail(string(bytes))",
"failureLocation": "/home/greyerof/github/tnf/pkg/testhelper/testhelper.go:352",
"skipReason": "pod xxx ns yyy container zzz uses SYS_ADMIN",
"checkDetails": "pod xxx ns yyy container zzz uses SYS_ADMIN",
"startTime": "2023-07-18 03:37:42.095225914 -0500 CDT m=+23.133430956",
"state": "failed",
"testID": {
Expand All @@ -34,7 +34,7 @@
"endTime": "2023-07-18 03:37:44.324268378 -0500 CDT m=+25.362473413",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:44.324023044 -0500 CDT m=+25.362228078",
"state": "passed",
"testID": {
Expand All @@ -61,7 +61,7 @@
"endTime": "0001-01-01 00:00:00 +0000 UTC",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:46.483797163 -0500 CDT m=+27.522002219",
"state": "skipped",
"testID": {
Expand All @@ -88,7 +88,7 @@
"endTime": "0001-01-01 00:00:00 +0000 UTC",
"failureLineContent": "",
"failureLocation": ":0",
"skipReason": "",
"checkDetails": "",
"startTime": "2023-07-18 03:37:46.483566421 -0500 CDT m=+27.521771494",
"state": "skipped",
"testID": {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tnf/claim/show/failures/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (spec *ObjectSpec) MarshalJSON() ([]byte, error) {
type FailedTestCase struct {
TestCaseName string `json:"name"`
TestCaseDescription string `json:"description"`
SkipReason string `json:"skipReason,omitempty"`
CheckDetails string `json:"checkDetails,omitempty"`
NonCompliantObjects []NonCompliantObject `json:"nonCompliantObjects,omitempty"`
}

Expand Down

0 comments on commit 1bdbf4c

Please sign in to comment.