forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm_suite_test.go
228 lines (191 loc) · 7.89 KB
/
helm_suite_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//go:build ignore
package test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"
"text/template"
"github.com/ghodss/yaml"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
soloHelm "github.com/solo-io/go-utils/helmutils"
"github.com/solo-io/go-utils/testutils"
"github.com/solo-io/k8s-utils/installutils/kuberesource"
. "github.com/solo-io/k8s-utils/manifesttestutils"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/release"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/kgateway-dev/kgateway/pkg/cliutil/helm"
"github.com/kgateway-dev/kgateway/projects/gloo/cli/pkg/cmd/install"
"github.com/kgateway-dev/kgateway/projects/gloo/pkg/defaults"
"github.com/kgateway-dev/kgateway/test/makefile"
glootestutils "github.com/kgateway-dev/kgateway/test/testutils"
)
const (
namespace = defaults.GlooSystem
releaseName = "gloo"
chartDir = "../helm/gloo"
debugOutputDir = "../../_output/helm/charts"
// the Gateway CR helm templates are stored as yaml in a configmap with this name
customResourceConfigMapName = "gloo-custom-resource-config"
)
var (
version string
pullPolicy corev1.PullPolicy
)
func TestHelm(t *testing.T) {
RegisterFailHandler(Fail)
testutils.RegisterCommonFailHandlers()
RunSpecs(t, "Helm Suite")
}
var _ = BeforeSuite(func() {
version = makefile.MustGetVersion(".", "-C", "../../")
pullPolicy = corev1.PullIfNotPresent
// generate the values.yaml and Chart.yaml files
makefile.MustMake(".", "-C", "../../", "generate-helm-files", "-B")
})
type renderTestCase struct {
rendererName string
renderer ChartRenderer
}
var renderers = []renderTestCase{
{"Helm 3", helm3Renderer{
chartDir: chartDir,
manifestOutputDir: "", // set to the value of the const debugOutputDir when debugging locally
}},
}
func runTests(callback func(testCase renderTestCase)) {
for _, r := range renderers {
callback(r)
}
}
type ChartRenderer interface {
// returns a TestManifest containing all resources
RenderManifest(namespace string, values glootestutils.HelmValues) (TestManifest, error)
}
var _ ChartRenderer = &helm3Renderer{}
type helm3Renderer struct {
chartDir string
// manifestOutputDir is a useful field to set when running tests locally
// it will output the generated manifest to a directory that you can easily
// inspect. If this value is an empty string, it will print the manifest to a temporary
// file and automatically clean it up.
manifestOutputDir string
}
func (h3 helm3Renderer) RenderManifest(namespace string, values glootestutils.HelmValues) (TestManifest, error) {
rel, err := buildHelm3Release(h3.chartDir, namespace, values)
if err != nil {
return nil, errors.Errorf("failure in buildHelm3Release: %s", err.Error())
}
// the test manifest utils can only read from a file
var testManifestFile *os.File
if h3.manifestOutputDir == "" {
testManifestFile, err = os.CreateTemp("", "*.yaml")
Expect(err).NotTo(HaveOccurred(), "Should be able to write a temp file for the helm unit test manifest")
defer func() {
_ = os.Remove(testManifestFile.Name())
}()
} else {
// Create a new file, with the version name, or truncate the file if one already exists
testManifestFile, err = os.Create(fmt.Sprintf("%s.yaml", filepath.Join(h3.manifestOutputDir, version)))
Expect(err).NotTo(HaveOccurred(), "Should be able to write a file to the manifestOutputDir for the helm unit test manifest")
}
_, err = testManifestFile.Write([]byte(rel.Manifest))
Expect(err).NotTo(HaveOccurred(), "Should be able to write the release manifest to the manifest file for the helm unit tests")
hooks, err := helm.GetHooks(rel.Hooks)
Expect(err).NotTo(HaveOccurred(), "Should be able to get the hooks in the helm unit test setup")
for _, hook := range hooks {
manifest := hook.Manifest
_, err = testManifestFile.Write([]byte("\n---\n" + manifest))
Expect(err).NotTo(HaveOccurred(), "Should be able to write the hook manifest to the manifest file for the helm unit tests")
}
err = testManifestFile.Close()
Expect(err).NotTo(HaveOccurred(), "Should be able to close the manifest file")
// check the manifest for lines that are not correctly parsed
manifestData, err := os.ReadFile(testManifestFile.Name())
Expect(err).ToNot(HaveOccurred())
windowsFound := soloHelm.FindHelmChartWhiteSpaces(string(manifestData), soloHelm.HelmDetectOptions{})
Expect(windowsFound).To(BeEmpty(), "Helm chart has parsing, white spacing, or formatting issues present")
return NewTestManifest(testManifestFile.Name()), nil
}
func buildHelm3Release(chartDir, namespace string, values glootestutils.HelmValues) (*release.Release, error) {
chartRequested, err := loader.Load(chartDir)
if err != nil {
return nil, errors.Errorf("failed to load chart directory: %s", err.Error())
}
helmValues, err := glootestutils.BuildHelmValues(values)
if err != nil {
return nil, errors.Errorf("failure in buildHelmValues: %s", err.Error())
}
// Validate that the provided values match the Go types used to construct out docs
err = glootestutils.ValidateHelmValues(helmValues)
if err != nil {
return nil, errors.Errorf("failure in ValidateHelmValues: %s", err.Error())
}
// Install the chart
installAction, err := createInstallAction(namespace)
if err != nil {
return nil, errors.Errorf("failure in createInstallAction: %s", err.Error())
}
release, err := installAction.Run(chartRequested, helmValues)
if err != nil {
return nil, errors.Errorf("failure in installAction.run: %s", err.Error())
}
return release, err
}
func createInstallAction(namespace string) (*action.Install, error) {
settings := install.NewCLISettings(namespace, "")
actionConfig := new(action.Configuration)
noOpDebugLog := func(format string, v ...interface{}) {}
if err := actionConfig.Init(
settings.RESTClientGetter(),
namespace,
os.Getenv("HELM_DRIVER"),
noOpDebugLog,
); err != nil {
return nil, err
}
renderer := action.NewInstall(actionConfig)
renderer.DryRun = true
renderer.Namespace = namespace
renderer.ReleaseName = releaseName
renderer.ClientOnly = true
return renderer, nil
}
func makeUnstructured(yam string) *unstructured.Unstructured {
jsn, err := yaml.YAMLToJSON([]byte(yam))
ExpectWithOffset(1, err).NotTo(HaveOccurred())
runtimeObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, jsn)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return runtimeObj.(*unstructured.Unstructured)
}
//nolint:unparam // values always receives "gloo-system"
func makeUnstructureFromTemplateFile(fixtureName string, values interface{}) *unstructured.Unstructured {
tmpl, err := template.ParseFiles(fixtureName)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
var b bytes.Buffer
err = tmpl.Execute(&b, values)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return makeUnstructured(b.String())
}
func makeRoleBindingFromUnstructured(resource *unstructured.Unstructured) *rbacv1.RoleBinding {
bindingObject, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("RoleBinding %+v should be able to convert from unstructured", resource))
structuredRoleBinding, ok := bindingObject.(*rbacv1.RoleBinding)
Expect(ok).To(BeTrue(), fmt.Sprintf("RoleBinding %+v should be able to cast to a structured role binding", resource))
return structuredRoleBinding
}
func makeClusterRoleBindingFromUnstructured(resource *unstructured.Unstructured) *rbacv1.ClusterRoleBinding {
bindingObject, err := kuberesource.ConvertUnstructured(resource)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("ClusterRoleBinding %+v should be able to convert from unstructured", resource))
structuredClusterRoleBinding, ok := bindingObject.(*rbacv1.ClusterRoleBinding)
Expect(ok).To(BeTrue(), fmt.Sprintf("ClusterRoleBinding %+v should be able to cast to a structured cluster role binding", resource))
return structuredClusterRoleBinding
}