forked from datreeio/datree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultRules_test.go
176 lines (127 loc) · 4.7 KB
/
defaultRules_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package defaultRules
import (
"encoding/json"
"errors"
"fmt"
"os"
"testing"
"github.com/datreeio/datree/pkg/fileReader"
"github.com/datreeio/datree/pkg/jsonSchemaValidator"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/assert"
)
var defaultRulesFileContent string
var defaultRulesSchemaFileContent string
const defaultRulesYamlPath = "./defaultRules.yaml"
const defaultRulesJSONSchemaPath = "./defaultRulesSchema.json"
type DefaultRules struct {
ApiVersion string `json:"apiVersion"`
Rules []DefaultRuleDefinition `json:"rules"`
}
func TestMain(m *testing.M) {
defaultRulesFile, err := getFileFromPath(defaultRulesYamlPath)
if err != nil {
panic(err)
}
defaultRulesFileContent = defaultRulesFile
defaultRulesSchemaFile, err := getFileFromPath(defaultRulesJSONSchemaPath)
if err != nil {
panic(err)
}
defaultRulesSchemaFileContent = defaultRulesSchemaFile
testsRunResult := m.Run()
os.Exit(testsRunResult)
}
func TestDefaultRulesFileExists(t *testing.T) {
_, fileReadError := getFileFromPath(defaultRulesYamlPath)
assert.Nil(t, fileReadError)
}
func TestDefaultRulesFileFitsJSONSchema(t *testing.T) {
validationError := validateYamlUsingJSONSchema(defaultRulesYamlPath, defaultRulesSchemaFileContent)
assert.Nil(t, validationError)
}
func TestDefaultRulesHasUniqueNamesInRules(t *testing.T) {
defaultRulesMap, conversionToMapError := convertYamlFileToMap(defaultRulesFileContent)
assert.Nil(t, conversionToMapError)
uniquenessValidationError := validateUniqueNameUniquenessInRules(defaultRulesMap.Rules)
assert.Nil(t, uniquenessValidationError)
}
func TestDefaultRulesHasUniqueIDsInRules(t *testing.T) {
defaultRulesMap, conversionToMapError := convertYamlFileToMap(defaultRulesFileContent)
assert.Nil(t, conversionToMapError)
uniquenessValidationError := validateIDUniquenessInRules(defaultRulesMap.Rules)
assert.Nil(t, uniquenessValidationError)
}
func TestDefaultRulesHasUniqueDocumentationUrlsInRules(t *testing.T) {
defaultRulesMap, conversionToMapError := convertYamlFileToMap(defaultRulesFileContent)
assert.Nil(t, conversionToMapError)
uniquenessValidationError := validateDocumentationUrlUniquenessInRules(defaultRulesMap.Rules)
assert.Nil(t, uniquenessValidationError)
}
func getFileFromPath(path string) (string, error) {
fileReader := fileReader.CreateFileReader(nil)
fileContent, readFileError := fileReader.ReadFileContent(path)
if readFileError != nil {
return "", readFileError
}
return fileContent, nil
}
func validateYamlUsingJSONSchema(yamlFilePath string, schema string) error {
fileContent, _ := getFileFromPath(yamlFilePath)
jsonSchemaValidator := jsonSchemaValidator.New()
schemaValidationResult, schemaValidationError := jsonSchemaValidator.ValidateYamlSchema(schema, fileContent)
if schemaValidationError != nil {
panic(errors.New("error occurred while validating yaml file against schema"))
}
if schemaValidationResult != nil {
validationErrors := fmt.Errorf("Received validation errors for %s:\n", yamlFilePath)
for _, validationError := range schemaValidationResult {
validationErrors = fmt.Errorf("%s\n%s", validationErrors, validationError.Error)
}
return validationErrors
}
return nil
}
func convertYamlFileToMap(yamlFileContent string) (DefaultRules, error) {
yamlFileContentRawJSON, yamlParseError := yaml.YAMLToJSON([]byte(yamlFileContent))
if yamlParseError != nil {
return DefaultRules{}, yamlParseError
}
var yamlFileContentJSON DefaultRules
jsonUnmarshallingError := json.Unmarshal(yamlFileContentRawJSON, &yamlFileContentJSON)
if jsonUnmarshallingError != nil {
return DefaultRules{}, jsonUnmarshallingError
}
return yamlFileContentJSON, nil
}
func validateUniqueNameUniquenessInRules(rules []DefaultRuleDefinition) error {
propertyValuesExistenceMap := make(map[string]bool)
for _, item := range rules {
propertyValue := item.UniqueName
if propertyValuesExistenceMap[propertyValue] {
return fmt.Errorf("duplicate unique name found: %s", propertyValue)
}
propertyValuesExistenceMap[propertyValue] = true
}
return nil
}
func validateIDUniquenessInRules(rules []DefaultRuleDefinition) error {
propertyValuesExistenceMap := make(map[int]bool)
for _, item := range rules {
if propertyValuesExistenceMap[item.ID] {
return fmt.Errorf("duplicate id found: %d", item.ID)
}
propertyValuesExistenceMap[item.ID] = true
}
return nil
}
func validateDocumentationUrlUniquenessInRules(rules []DefaultRuleDefinition) error {
propertyValuesExistenceMap := make(map[string]bool)
for _, item := range rules {
if propertyValuesExistenceMap[item.DocumentationUrl] {
return fmt.Errorf("duplicate id found: %d", item.ID)
}
propertyValuesExistenceMap[item.DocumentationUrl] = true
}
return nil
}