/
library.go
179 lines (151 loc) · 4.65 KB
/
library.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
177
178
179
/*
Copyright 2021 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi_cases
import (
"fmt"
"os"
"path/filepath"
"github.com/flant/addon-operator/pkg/utils"
"github.com/flant/addon-operator/pkg/values/validation"
"sigs.k8s.io/yaml"
)
const FocusFieldName = "x-test-focus"
type TestCase struct {
ConfigValues []map[string]interface{}
Values []map[string]interface{}
HelmValues []map[string]interface{}
}
func (tc TestCase) HasFocused() bool {
for _, values := range tc.ConfigValues {
if _, hasFocus := values[FocusFieldName]; hasFocus {
return true
}
}
for _, values := range tc.Values {
if _, hasFocus := values[FocusFieldName]; hasFocus {
return true
}
}
for _, values := range tc.HelmValues {
if _, hasFocus := values[FocusFieldName]; hasFocus {
return true
}
}
return false
}
type TestCases struct {
Positive TestCase
Negative TestCase
dir string
hasFocused bool
}
func (t *TestCases) HaveConfigValuesCases() bool {
return len(t.Positive.ConfigValues) > 0 || len(t.Negative.ConfigValues) > 0
}
func (t *TestCases) HaveValuesCases() bool {
return len(t.Positive.Values) > 0 || len(t.Negative.Values) > 0
}
func (t *TestCases) HaveHelmValuesCases() bool {
return len(t.Positive.HelmValues) > 0 || len(t.Negative.HelmValues) > 0
}
type edition struct {
Name string `yaml:"name,omitempty"`
ModulesDir string `yaml:"modulesDir,omitempty"`
}
type editions struct {
Editions []edition `yaml:"editions,omitempty"`
}
func getPossiblePathToModules() []string {
content, err := os.ReadFile("/deckhouse/editions.yaml")
if err != nil {
panic(fmt.Sprintf("cannot read editions file: %v", err))
}
e := editions{}
err = yaml.Unmarshal(content, &e)
if err != nil {
panic(fmt.Errorf("cannot unmarshal editions file: %v", err))
}
modulesDir := make([]string, 0)
for i, ed := range e.Editions {
if ed.Name == "" {
panic(fmt.Sprintf("name for %d index is empty", i))
}
modulesDir = append(modulesDir, fmt.Sprintf("/deckhouse/%s/*/openapi", ed.ModulesDir))
}
return modulesDir
}
func GetAllOpenAPIDirs() ([]string, error) {
var (
dirs []string
openAPIDirs []string
)
for _, possibleDir := range getPossiblePathToModules() {
globDirs, err := filepath.Glob(possibleDir)
if err != nil {
return nil, err
}
openAPIDirs = append(openAPIDirs, globDirs...)
}
openAPIDirs = append(openAPIDirs, "/deckhouse/global-hooks/openapi")
for _, openAPIDir := range openAPIDirs {
info, err := os.Stat(openAPIDir)
if err != nil {
continue
}
if !info.IsDir() {
continue
}
dirs = append(dirs, openAPIDir)
}
return dirs, nil
}
func TestCasesFromFile(filename string) (*TestCases, error) {
var testCases TestCases
yamlFile, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(yamlFile, &testCases)
if err != nil {
return nil, err
}
testCases.hasFocused = testCases.Positive.HasFocused() || testCases.Negative.HasFocused()
return &testCases, nil
}
func ValidatePositiveCase(validator *validation.ValuesValidator, moduleName string, schema validation.SchemaType, testValues map[string]interface{}, runFocused bool) error {
if _, hasFocus := testValues[FocusFieldName]; !hasFocus && runFocused {
return nil
}
delete(testValues, FocusFieldName)
return validator.ValidateValues(validation.ModuleSchema, schema, moduleName, utils.Values{moduleName: testValues})
}
func ValidateNegativeCase(validator *validation.ValuesValidator, moduleName string, schema validation.SchemaType, testValues map[string]interface{}, runFocused bool) error {
_, hasFocus := testValues[FocusFieldName]
if !hasFocus && runFocused {
return nil
}
delete(testValues, FocusFieldName)
err := validator.ValidateValues(validation.ModuleSchema, schema, moduleName, utils.Values{moduleName: testValues})
if err == nil {
return fmt.Errorf("negative case error for %s values: test case should not pass validation: %+v", schema, ValuesToString(testValues))
}
// Focusing is a debugging tool, so print hidden error.
if hasFocus {
fmt.Printf("Debug: expected error for negative case: %v\n", err)
}
return nil
}
func ValuesToString(v map[string]interface{}) string {
b, _ := yaml.Marshal(v)
return string(b)
}