Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit 0aad645

Browse files
shalev007shalev avhar
and
shalev avhar
authored
feat: testing support multiple unit tests per default rule (#952)
* feat: change yaml test files locations * to support multiple fail and pass test cases, fail and pass actions were converted to directories * fix: validate by directory and not file name * feat: print file path instead of file name on error --------- Co-authored-by: shalev avhar <[email protected]>
1 parent 00d82f2 commit 0aad645

File tree

224 files changed

+29
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+29
-23
lines changed

pkg/policy/policy_test.go

+29-23
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"reflect"
89
"strconv"
9-
"strings"
1010
"testing"
1111

1212
"github.com/datreeio/datree/pkg/defaultPolicies"
@@ -21,12 +21,12 @@ import (
2121
type TestFilesByRuleId = map[int]*FailAndPassTests
2222

2323
type FailAndPassTests struct {
24-
fails []*FileWithName
25-
passes []*FileWithName
24+
fails []*FileWithPath
25+
passes []*FileWithPath
2626
}
2727

28-
type FileWithName struct {
29-
name string
28+
type FileWithPath struct {
29+
path string
3030
content string
3131
}
3232

@@ -61,7 +61,7 @@ func TestDefaultRulesValidation(t *testing.T) {
6161
}
6262
}
6363

64-
func validatePassing(t *testing.T, validator *jsonSchemaValidator.JSONSchemaValidator, schemaContent map[string]interface{}, ruleId int, files []*FileWithName, expectPass bool) {
64+
func validatePassing(t *testing.T, validator *jsonSchemaValidator.JSONSchemaValidator, schemaContent map[string]interface{}, ruleId int, files []*FileWithPath, expectPass bool) {
6565
for _, file := range files {
6666
schemaBytes, err := yaml.Marshal(schemaContent)
6767
if err != nil {
@@ -74,10 +74,10 @@ func validatePassing(t *testing.T, validator *jsonSchemaValidator.JSONSchemaVali
7474
}
7575

7676
if len(errorsResult) > 0 && expectPass {
77-
t.Errorf("Expected validation for rule with id %d to pass, but it failed for file %s\n", ruleId, file.name)
77+
t.Errorf("Expected validation for rule with id %d to pass, but it failed for file %s\n", ruleId, file.path)
7878
}
7979
if len(errorsResult) == 0 && !expectPass {
80-
t.Errorf("Expected validation for rule with id %d to fail, but it passed for file %s\n", ruleId, file.name)
80+
t.Errorf("Expected validation for rule with id %d to fail, but it passed for file %s\n", ruleId, file.path)
8181
}
8282
}
8383
}
@@ -91,42 +91,48 @@ func getTestFilesByRuleId(t *testing.T) TestFilesByRuleId {
9191
}
9292

9393
testFilesByRuleId := make(TestFilesByRuleId)
94-
for _, file := range files {
95-
filename, err := fileReader.GetFilename(file)
94+
for _, filePath := range files {
95+
// skip directories
96+
if !fileExists(filePath) {
97+
continue
98+
}
99+
100+
path, _ := filepath.Split(filePath)
101+
passOrFail := filepath.Base(path)
102+
ruleId := filepath.Base(filepath.Dir(filepath.Dir(path)))
103+
id, err := strconv.Atoi(ruleId)
96104
if err != nil {
97105
panic(err)
98106
}
99107

100-
fileContent, err := fileReader.ReadFileContent(file)
108+
isPass := passOrFail == "pass"
109+
110+
fileContent, err := fileReader.ReadFileContent(filePath)
101111
if err != nil {
102112
panic(err)
103113
}
104114

105-
id, isPass := getFileData(filename)
106115
if testFilesByRuleId[id] == nil {
107116
testFilesByRuleId[id] = &FailAndPassTests{}
108117
}
109118

110-
fileWithName := &FileWithName{name: filename, content: fileContent}
119+
fileWithPath := &FileWithPath{path: filePath, content: fileContent}
111120
if isPass {
112-
testFilesByRuleId[id].passes = append(testFilesByRuleId[id].passes, fileWithName)
121+
testFilesByRuleId[id].passes = append(testFilesByRuleId[id].passes, fileWithPath)
113122
} else {
114-
testFilesByRuleId[id].fails = append(testFilesByRuleId[id].fails, fileWithName)
123+
testFilesByRuleId[id].fails = append(testFilesByRuleId[id].fails, fileWithPath)
115124
}
116125
}
117126

118127
return testFilesByRuleId
119128
}
120129

121-
func getFileData(filename string) (int, bool) {
122-
parts := strings.Split(filename, "-")
123-
id, err := strconv.Atoi(parts[0])
124-
if err != nil {
125-
panic(err)
130+
func fileExists(path string) bool {
131+
info, err := os.Stat(path)
132+
if os.IsNotExist(err) {
133+
return false
126134
}
127-
128-
isPass := strings.Contains(parts[1], "pass")
129-
return id, isPass
135+
return !info.IsDir()
130136
}
131137

132138
func expectedPoliciesContent(t *testing.T, path string) *defaultPolicies.EvaluationPrerunPolicies {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)