forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_json_transcoder_test.go
176 lines (160 loc) · 6.39 KB
/
grpc_json_transcoder_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
//go:build ignore
package test
import (
"encoding/base64"
"fmt"
"os"
"strconv"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
. "github.com/solo-io/k8s-utils/manifesttestutils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/kgateway-dev/kgateway/projects/gateway/pkg/defaults"
glootestutils "github.com/kgateway-dev/kgateway/test/testutils"
)
var _ = Describe("GrpcJsonTranscoder helm test", func() {
var allTests = func(rendererTestCase renderTestCase) {
var (
testManifest TestManifest
protoDescriptor = getExampleProtoDescriptor()
// Create a TestManifest out of the custom resource yaml from the configmap,
// and then perform assertions on the resources (which are assumed to all be Gateways).
// `matchers` is a map of Gateway name to the assertion to be made on that Gateway.
// There must be a matcher for every Gateway that's expected to be in the manifest.
assertCustomResourceManifest = func(matchers map[string]types.GomegaMatcher) {
configMap := getConfigMap(testManifest, namespace, customResourceConfigMapName)
ExpectWithOffset(1, configMap.Data).NotTo(BeNil())
customResourceYaml := configMap.Data["custom-resources"]
customResourceManifest := NewTestManifestFromYaml(customResourceYaml)
// make sure that the number of resources found in the manifest equals the number of
// matchers passed in, so we can ensure that every resource has an associated matcher
ExpectWithOffset(1, customResourceManifest.NumResources()).To(Equal(len(matchers)))
for gwName, matcher := range matchers {
customResourceManifest.ExpectUnstructured("Gateway", namespace, gwName).To(matcher)
}
}
)
prepareManifest := func(namespace string, values glootestutils.HelmValues) {
GinkgoHelper()
tm, err := rendererTestCase.renderer.RenderManifest(namespace, values)
Expect(err).NotTo(HaveOccurred(), "Failed to render manifest")
testManifest = tm
}
Context("protoDescriptorBin field", func() {
BeforeEach(func() {
prepareManifest(namespace, glootestutils.HelmValues{
ValuesArgs: []string{
fmt.Sprintf("gatewayProxies.gatewayProxy.gatewaySettings.customHttpGateway.options.grpcJsonTranscoder.protoDescriptorBin=%s", protoDescriptor),
fmt.Sprintf("gatewayProxies.gatewayProxy.gatewaySettings.customHttpsGateway.options.grpcJsonTranscoder.protoDescriptorBin=%s", protoDescriptor),
},
})
})
It("renders with the proto descriptor", func() {
gw := makeUnstructuredGatewayWithProtoDescriptorBin(namespace, "gateway-proxy", false)
gwSsl := makeUnstructuredGatewayWithProtoDescriptorBin(namespace, "gateway-proxy", true)
assertCustomResourceManifest(map[string]types.GomegaMatcher{
defaults.GatewayProxyName: BeEquivalentTo(gw),
getSslGatewayName(defaults.GatewayProxyName): BeEquivalentTo(gwSsl),
})
})
})
Context("protoDescriptorConfigMap field", func() {
BeforeEach(func() {
prepareManifest(namespace, glootestutils.HelmValues{
ValuesArgs: []string{
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.configMapRef.name=my-config-map",
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.configMapRef.namespace=gloo-system",
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.key=my-key",
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpsGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.configMapRef.name=my-config-map",
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpsGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.configMapRef.namespace=gloo-system",
"gatewayProxies.gatewayProxy.gatewaySettings.customHttpsGateway.options.grpcJsonTranscoder.protoDescriptorConfigMap.key=my-key",
"global.configMaps[0].name=my-config-map",
"global.configMaps[0].namespace=gloo-system",
"global.configMaps[0].data.my-key=" + protoDescriptor,
},
})
})
It("renders with the proto descriptor", func() {
gw := makeUnstructuredGatewayWithProtoDescriptorConfigMap(namespace, "gateway-proxy", false)
gwSsl := makeUnstructuredGatewayWithProtoDescriptorConfigMap(namespace, "gateway-proxy", true)
assertCustomResourceManifest(map[string]types.GomegaMatcher{
defaults.GatewayProxyName: BeEquivalentTo(gw),
getSslGatewayName(defaults.GatewayProxyName): BeEquivalentTo(gwSsl),
})
})
})
}
runTests(allTests)
})
//nolint:unparam // namespace always receives "gloo-system"
func makeUnstructuredGatewayWithProtoDescriptorBin(namespace string, name string, ssl bool) *unstructured.Unstructured {
GinkgoHelper()
port := "8080"
gwName := name
if ssl {
port = "8443"
gwName = getSslGatewayName(name)
}
return makeUnstructured(`
apiVersion: gateway.solo.io/v1
kind: Gateway
metadata:
labels:
app: gloo
name: ` + gwName + `
namespace: ` + namespace + `
spec:
bindAddress: '::'
bindPort: ` + port + `
httpGateway:
options:
grpcJsonTranscoder:
protoDescriptorBin: '` + getExampleProtoDescriptor() + `'
proxyNames:
- ` + name + `
ssl: ` + strconv.FormatBool(ssl) + `
useProxyProto: false
`)
}
//nolint:unparam // namespace always receives "gloo-system"
func makeUnstructuredGatewayWithProtoDescriptorConfigMap(namespace string, name string, ssl bool) *unstructured.Unstructured {
GinkgoHelper()
port := "8080"
gwName := name
if ssl {
port = "8443"
gwName = getSslGatewayName(name)
}
return makeUnstructured(`
apiVersion: gateway.solo.io/v1
kind: Gateway
metadata:
labels:
app: gloo
name: ` + gwName + `
namespace: ` + namespace + `
spec:
bindAddress: '::'
bindPort: ` + port + `
httpGateway:
options:
grpcJsonTranscoder:
protoDescriptorConfigMap:
configMapRef:
name: my-config-map
namespace: gloo-system
key: my-key
proxyNames:
- ` + name + `
ssl: ` + strconv.FormatBool(ssl) + `
useProxyProto: false
`)
}
// return a base64-encoded proto descriptor to use for testing
func getExampleProtoDescriptor() string {
pathToDescriptors := "../../test/v1helpers/test_grpc_service/descriptors/proto.pb"
bytes, err := os.ReadFile(pathToDescriptors)
Expect(err).NotTo(HaveOccurred())
return base64.StdEncoding.EncodeToString(bytes)
}