-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathschemas_test.go
165 lines (149 loc) · 5.42 KB
/
schemas_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
package schema
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/stretchr/testify/assert"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"
)
const (
maxRetriesToDownloadSchema = 5
durationBetweenSchemaDownloadRetries = 10 * time.Second
)
func TestFrogbotSchema(t *testing.T) {
// Load frogbot schema
schema, err := os.ReadFile("frogbot-schema.json")
assert.NoError(t, err)
schemaLoader := gojsonschema.NewBytesLoader(schema)
// Validate config in this repository
validateYamlSchema(t, schemaLoader, filepath.Join("..", ".frogbot", "frogbot-config.yml"), "")
// Validate all frogbot configs in commands/testdata/config
validateYamlsInDirectory(t, filepath.Join("..", "testdata", "config"), schemaLoader)
}
func TestBadFrogbotSchemas(t *testing.T) {
// Load frogbot schema
schema, err := os.ReadFile("frogbot-schema.json")
assert.NoError(t, err)
schemaLoader := gojsonschema.NewBytesLoader(schema)
// Validate all bad frogbot configs in schema/testdata/
testCases := []struct {
testName string
errorString string
}{
{"additional-prop", "Additional property additionalProp is not allowed"},
{"no-array", "Expected: array, given: object"},
{"no-git", "git is required"},
{"no-repo", "repoName is required"},
{"empty-repo", "Expected: string, given: null"},
}
for _, testCase := range testCases {
validateYamlSchema(t, schemaLoader, filepath.Join("testdata", testCase.testName+".yml"), testCase.errorString)
}
}
func TestJFrogPipelinesTemplates(t *testing.T) {
schemaLoader := downloadFromSchemaStore(t, "jfrog-pipelines.json")
validateYamlsInDirectory(t, filepath.Join("..", "docs", "templates", "jfrog-pipelines"), schemaLoader)
}
// Download a Yaml schema from https://json.schemastore.org.
// t - Testing object
// schema - The schema file to download
func downloadFromSchemaStore(t *testing.T, schema string) gojsonschema.JSONLoader {
var response *http.Response
var err error
retryExecutor := clientutils.RetryExecutor{
MaxRetries: maxRetriesToDownloadSchema,
RetriesIntervalMilliSecs: int(durationBetweenSchemaDownloadRetries.Milliseconds()),
ErrorMessage: "Failed to download schema.",
ExecutionHandler: func() (bool, error) {
response, err = http.Get("https://json.schemastore.org/" + schema)
if err != nil {
return true, err
}
if response.StatusCode != http.StatusOK {
return true, fmt.Errorf("failed to download schema. Response status: %s", response.Status)
}
return false, nil
},
}
assert.NoError(t, retryExecutor.Execute())
assert.Equal(t, http.StatusOK, response.StatusCode, response.Status)
// Check server response and read schema bytes
defer func() {
assert.NoError(t, response.Body.Close())
}()
schemaBytes, err := io.ReadAll(response.Body)
assert.NoError(t, err)
return gojsonschema.NewBytesLoader(schemaBytes)
}
// Validate all yml files in the given directory against the input schema
// t - Testing object
// schemaLoader - Frogbot config schema
// path - Yaml directory path
func validateYamlsInDirectory(t *testing.T, path string, schemaLoader gojsonschema.JSONLoader) {
err := filepath.Walk(path, func(frogbotConfigFilePath string, info os.FileInfo, err error) error {
assert.NoError(t, err)
if strings.HasSuffix(info.Name(), "yml") {
validateYamlSchema(t, schemaLoader, frogbotConfigFilePath, "")
}
return nil
})
assert.NoError(t, err)
}
// Validate a Yaml file against the input Yaml schema
// t - Testing object
// schemaLoader - Frogbot config schema
// yamlFilePath - Yaml file path
// expectError - Expected error or an empty string if error is not expected
func validateYamlSchema(t *testing.T, schemaLoader gojsonschema.JSONLoader, yamlFilePath, expectError string) {
t.Run(filepath.Base(yamlFilePath), func(t *testing.T) {
// Read frogbot config
yamlFile, err := os.ReadFile(yamlFilePath)
assert.NoError(t, err)
// Unmarshal frogbot config
var frogbotConfigYaml interface{}
err = yaml.Unmarshal(yamlFile, &frogbotConfigYaml)
assert.NoError(t, err)
// Convert the Yaml config to JSON config to help the json parser validate it.
// The reason we don't do the convert by as follows:
// YAML -> Unmarshall -> Go Struct -> Marshal -> JSON
// is because the config's struct includes only YAML annotations.
frogbotConfigJson := convertYamlToJson(frogbotConfigYaml)
// Load and validate frogbot config
documentLoader := gojsonschema.NewGoLoader(frogbotConfigJson)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
assert.NoError(t, err)
if expectError != "" {
assert.False(t, result.Valid())
assert.Contains(t, result.Errors()[0].String(), expectError)
} else {
assert.True(t, result.Valid(), result.Errors())
}
})
}
// Recursively convert yaml interface to JSON interface
func convertYamlToJson(yamlValue interface{}) interface{} {
switch yamlMapping := yamlValue.(type) {
case map[interface{}]interface{}:
jsonMapping := map[string]interface{}{}
for key, value := range yamlMapping {
if key == true {
// "on" is considered a true value for the Yaml Unmarshaler. To work around it, we set the true to be "on".
key = "on"
}
jsonMapping[fmt.Sprint(key)] = convertYamlToJson(value)
}
return jsonMapping
case []interface{}:
for i, value := range yamlMapping {
yamlMapping[i] = convertYamlToJson(value)
}
}
return yamlValue
}