-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathvalues_fuzz_test.go
More file actions
185 lines (165 loc) · 4.14 KB
/
Copy pathvalues_fuzz_test.go
File metadata and controls
185 lines (165 loc) · 4.14 KB
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
180
181
182
183
184
185
//go:build gofuzz_libfuzzer
// +build gofuzz_libfuzzer
/*
Copyright 2024 The Flux authors
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 chartutil
import (
"context"
"testing"
"github.com/go-logr/logr"
"helm.sh/helm/v4/pkg/chart/common"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"github.com/fluxcd/pkg/apis/meta"
)
func FuzzChartValuesFromReferences(f *testing.F) {
scheme := testScheme()
tests := []struct {
targetPath string
valuesKey string
hrValues string
createObject bool
secretData []byte
configData string
}{
{
targetPath: "flat",
valuesKey: "custom-values.yaml",
secretData: []byte(`flat:
nested: value
nested: value
`),
configData: `flat: value
nested:
configuration: value
`,
hrValues: `
other: values
`,
createObject: true,
},
{
targetPath: "'flat'",
valuesKey: "custom-values.yaml",
secretData: []byte(`flat:
nested: value
nested: value
`),
configData: `flat: value
nested:
configuration: value
`,
hrValues: `
other: values
`,
createObject: true,
},
{
targetPath: "flat[0]",
secretData: []byte(``),
configData: `flat: value`,
hrValues: `
other: values
`,
createObject: true,
},
{
secretData: []byte(`flat:
nested: value
nested: value
`),
configData: `flat: value
nested:
configuration: value
`,
hrValues: `
other: values
`,
createObject: true,
},
{
targetPath: "some-value",
hrValues: `
other: values
`,
createObject: false,
},
}
for _, tt := range tests {
f.Add(tt.targetPath, tt.valuesKey, tt.hrValues, tt.createObject, tt.secretData, tt.configData)
}
f.Fuzz(func(t *testing.T,
targetPath, valuesKey, hrValues string, createObject bool, secretData []byte, configData string) {
// objectName and objectNamespace represent a name reference to a core
// Kubernetes object upstream (Secret/ConfigMap) which is validated upstream,
// and also validated by us in the OpenAPI-based validation set in
// meta.ValuesReference. Therefore, a static value here suffices, and instead
// we just play with the objects presence/absence.
objectName := "values"
objectNamespace := "default"
var resources []runtime.Object
if createObject {
resources = append(resources,
mockConfigMap(objectName, map[string]string{valuesKey: configData}),
mockSecret(objectName, map[string][]byte{valuesKey: secretData}),
)
}
references := []meta.ValuesReference{
{
Kind: kindConfigMap,
Name: objectName,
ValuesKey: valuesKey,
TargetPath: targetPath,
},
{
Kind: kindSecret,
Name: objectName,
ValuesKey: valuesKey,
TargetPath: targetPath,
},
}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(resources...)
var values common.Values
if hrValues != "" {
values, _ = common.ReadValues([]byte(hrValues))
}
_, _ = ChartValuesFromReferences(context.TODO(), logr.Discard(), c.Build(), objectNamespace, values, references...)
})
}
func mockSecret(name string, data map[string][]byte) *corev1.Secret {
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: kindSecret,
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{Name: name},
Data: data,
}
}
func mockConfigMap(name string, data map[string]string) *corev1.ConfigMap {
return &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: kindConfigMap,
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{Name: name},
Data: data,
}
}
func testScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
return scheme
}