forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.go
116 lines (95 loc) · 3.25 KB
/
suite.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
//go:build ignore
package helm_settings
import (
"bytes"
"context"
"os"
"path/filepath"
"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"text/template"
"github.com/kgateway-dev/kgateway/pkg/utils/fsutils"
"github.com/kgateway-dev/kgateway/test/kubernetes/e2e"
"github.com/stretchr/testify/suite"
)
var _ e2e.NewSuiteFunc = NewTestingSuite
// testingSuite is a Suite of tests that is used to validate that the manifests
// used in our Helm unit tests are valid
//
// These tests are defined separately from our `features/helm` tests, to ensure they are run against
// a standalone installation, and do not impact those other tests.
// The helm unit tests involve templating settings with various values set
// and then validating that the templated data matches fixture data.
// The tests assume that the fixture data we have defined is valid yaml that
// will be accepted by a cluster. However, this has not always been the case,
// and it's important that we validate the settings end to end
//
// This solution may not be the best way to validate settings, but it
// attempts to avoid re-running all the helm template tests against a live cluster
// Reference PRs:
// - https://github.com/kgateway-dev/kgateway/pull/5957 (introduced)
// - https://github.com/kgateway-dev/kgateway/pull/9732 (migrated)
type testingSuite struct {
suite.Suite
ctx context.Context
// testInstallation contains all the metadata/utilities necessary to execute a series of tests
// against an installation of Gloo Gateway
testInstallation *e2e.TestInstallation
}
func NewTestingSuite(ctx context.Context, testInst *e2e.TestInstallation) suite.TestingSuite {
return &testingSuite{
ctx: ctx,
testInstallation: testInst,
}
}
func (s *testingSuite) TestApplySettingsManifestsFromUnitTests() {
settingsFixturesFolder := filepath.Join(fsutils.GetModuleRoot(), "install", "test", "fixtures", "settings")
err := filepath.Walk(settingsFixturesFolder, func(settingsFixtureFile string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
templatedSettings, err := makeUnstructuredFromTemplateFile(settingsFixtureFile, s.testInstallation.Metadata.InstallNamespace)
if err != nil {
// stop traversing, error
return err
}
settingsBytes, err := templatedSettings.MarshalJSON()
if err != nil {
// stop traversing, error
return err
}
// Apply the fixture
err = s.testInstallation.Actions.Kubectl().Apply(s.ctx, settingsBytes)
if err != nil {
// stop traversing, error
return err
}
// continue traversing
return nil
})
s.Assert().NoError(err, "can apply all settings manifests without an error")
}
func makeUnstructuredFromTemplateFile(fixtureName string, values interface{}) (*unstructured.Unstructured, error) {
tmpl, err := template.ParseFiles(fixtureName)
if err != nil {
return nil, err
}
var b bytes.Buffer
err = tmpl.Execute(&b, values)
if err != nil {
return nil, err
}
jsn, err := yaml.YAMLToJSON(b.Bytes())
if err != nil {
return nil, err
}
runtimeObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, jsn)
if err != nil {
return nil, err
}
return runtimeObj.(*unstructured.Unstructured), nil
}