4
4
"context"
5
5
"fmt"
6
6
"net/http"
7
- "time"
8
-
9
7
"strings"
8
+ "time"
10
9
11
10
"github.com/onsi/gomega"
12
11
"github.com/stretchr/testify/suite"
@@ -15,13 +14,14 @@ import (
15
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
15
"sigs.k8s.io/controller-runtime/pkg/client"
17
16
17
+ envoyadmincli "github.com/kgateway-dev/kgateway/v2/pkg/utils/envoyutils/admincli"
18
18
"github.com/kgateway-dev/kgateway/v2/pkg/utils/kubeutils"
19
19
"github.com/kgateway-dev/kgateway/v2/pkg/utils/requestutils/curl"
20
20
testmatchers "github.com/kgateway-dev/kgateway/v2/test/gomega/matchers"
21
+ "github.com/kgateway-dev/kgateway/v2/test/helpers"
21
22
"github.com/kgateway-dev/kgateway/v2/test/kubernetes/e2e"
23
+ "github.com/kgateway-dev/kgateway/v2/test/kubernetes/e2e/defaults"
22
24
testdefaults "github.com/kgateway-dev/kgateway/v2/test/kubernetes/e2e/defaults"
23
-
24
- envoyadmincli "github.com/kgateway-dev/kgateway/v2/pkg/utils/envoyutils/admincli"
25
25
)
26
26
27
27
var _ e2e.NewSuiteFunc = NewTestingSuite
@@ -35,6 +35,11 @@ type testingSuite struct {
35
35
// testInstallation contains all the metadata/utilities necessary to execute a series of tests
36
36
// against an installation of kgateway
37
37
testInstallation * e2e.TestInstallation
38
+
39
+ // manifests shared by all tests
40
+ commonManifests []string
41
+ // resources from manifests shared by all tests
42
+ commonResources []client.Object
38
43
}
39
44
40
45
func NewTestingSuite (ctx context.Context , testInst * e2e.TestInstallation ) suite.TestingSuite {
@@ -44,41 +49,60 @@ func NewTestingSuite(ctx context.Context, testInst *e2e.TestInstallation) suite.
44
49
}
45
50
}
46
51
47
- func (s * testingSuite ) TestGatewayWithTransformedRoute () {
48
- manifests : = []string {
52
+ func (s * testingSuite ) SetupSuite () {
53
+ s . commonManifests = []string {
49
54
testdefaults .CurlPodManifest ,
50
55
simpleServiceManifest ,
51
56
gatewayWithRouteManifest ,
52
57
}
53
- manifestObjects := []client.Object {
54
- testdefaults .CurlPod , // curl
55
- simpleSvc , // echo service
56
- proxyService , proxyServiceAccount , proxyDeployment , // proxy
58
+ s .commonResources = []client.Object {
59
+ // resources from curl manifest
60
+ testdefaults .CurlPod ,
61
+ // resources from service manifest
62
+ simpleSvc , simpleDeployment ,
63
+ // resources from gateway manifest
64
+ gateway , route , routePolicy ,
65
+ // deployer-generated resources
66
+ proxyDeployment , proxyService , proxyServiceAccount ,
57
67
}
58
68
59
- s .T ().Cleanup (func () {
60
- for _ , manifest := range manifests {
61
- err := s .testInstallation .Actions .Kubectl ().DeleteFileSafe (s .ctx , manifest )
62
- s .Require ().NoError (err )
63
- }
64
- s .testInstallation .Assertions .EventuallyObjectsNotExist (s .ctx , manifestObjects ... )
65
- })
66
-
67
- for _ , manifest := range manifests {
69
+ // set up common resources once
70
+ for _ , manifest := range s .commonManifests {
68
71
err := s .testInstallation .Actions .Kubectl ().ApplyFile (s .ctx , manifest )
69
- s .Require ().NoError (err )
72
+ s .Require ().NoError (err , "can apply " + manifest )
70
73
}
71
- s .testInstallation .Assertions .EventuallyObjectsExist (s .ctx , manifestObjects ... )
74
+ s .testInstallation .Assertions .EventuallyObjectsExist (s .ctx , s . commonResources ... )
72
75
73
76
// make sure pods are running
74
- s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , testdefaults .CurlPod .GetNamespace (), metav1.ListOptions {
75
- LabelSelector : "app.kubernetes.io/name=curl" ,
77
+ s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , defaults .CurlPod .GetNamespace (), metav1.ListOptions {
78
+ LabelSelector : defaults .CurlPodLabelSelector ,
79
+ })
80
+ s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , simpleDeployment .GetNamespace (), metav1.ListOptions {
81
+ LabelSelector : "app=backend-0,version=v1" ,
76
82
})
77
-
78
83
s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , proxyObjectMeta .GetNamespace (), metav1.ListOptions {
79
- LabelSelector : "app.kubernetes.io/name=gw" ,
84
+ LabelSelector : fmt .Sprintf ("app.kubernetes.io/name=%s" , proxyObjectMeta .GetName ()),
85
+ })
86
+ }
87
+
88
+ func (s * testingSuite ) TearDownSuite () {
89
+ // clean up common resources
90
+ for _ , manifest := range s .commonManifests {
91
+ err := s .testInstallation .Actions .Kubectl ().DeleteFileSafe (s .ctx , manifest )
92
+ s .Require ().NoError (err , "can delete " + manifest )
93
+ }
94
+ s .testInstallation .Assertions .EventuallyObjectsNotExist (s .ctx , s .commonResources ... )
95
+
96
+ // make sure pods are gone
97
+ s .testInstallation .Assertions .EventuallyPodsNotExist (s .ctx , simpleDeployment .GetNamespace (), metav1.ListOptions {
98
+ LabelSelector : "app=backend-0,version=v1" ,
99
+ })
100
+ s .testInstallation .Assertions .EventuallyPodsNotExist (s .ctx , proxyObjectMeta .GetNamespace (), metav1.ListOptions {
101
+ LabelSelector : fmt .Sprintf ("app.kubernetes.io/name=%s" , proxyObjectMeta .GetName ()),
80
102
})
103
+ }
81
104
105
+ func (s * testingSuite ) TestGatewayWithTransformedRoute () {
82
106
testCasess := []struct {
83
107
name string
84
108
opts []curl.Option
@@ -124,67 +148,66 @@ func (s *testingSuite) TestGatewayWithTransformedRoute() {
124
148
}
125
149
126
150
func (s * testingSuite ) TestGatewayRustformationsWithTransformedRoute () {
127
- manifests := []string {
128
- testdefaults .CurlPodManifest ,
129
- simpleServiceManifest ,
130
- gatewayWithRouteManifest ,
131
- }
132
- manifestObjects := []client.Object {
133
- testdefaults .CurlPod , // curl
134
- simpleSvc , // echo service
135
- proxyService , proxyServiceAccount , proxyDeployment , // proxy
136
- }
137
-
151
+ // make a copy of the original controller deployment
138
152
controllerDeploymentOriginal := & appsv1.Deployment {}
139
153
err := s .testInstallation .ClusterContext .Client .Get (s .ctx , client.ObjectKey {
140
154
Namespace : s .testInstallation .Metadata .InstallNamespace ,
141
- Name : "kgateway" ,
155
+ Name : helpers . DefaultKgatewayDeploymentName ,
142
156
}, controllerDeploymentOriginal )
143
157
s .Assert ().NoError (err , "has controller deploymnet" )
144
158
145
- controllerDeploy := controllerDeploymentOriginal .DeepCopy ()
146
- // add the environment variable RUSTFORMATIONS to the controller deployment
147
-
148
- env := append (controllerDeploy .Spec .Template .Spec .Containers [0 ].Env , corev1.EnvVar {
159
+ // add the environment variable RUSTFORMATIONS to the modified controller deployment
160
+ rustFormationsEnvVar := corev1.EnvVar {
149
161
Name : "KGW_USE_RUST_FORMATIONS" ,
150
162
Value : "true" ,
151
- })
152
- containers := controllerDeploy .Spec .Template .Spec .Containers
153
- containers [0 ].Env = env
154
- controllerDeploy .Spec .Template .Spec .Containers = containers
163
+ }
164
+ controllerDeployModified := controllerDeploymentOriginal .DeepCopy ()
165
+ controllerDeployModified .Spec .Template .Spec .Containers [0 ].Env = append (
166
+ controllerDeployModified .Spec .Template .Spec .Containers [0 ].Env ,
167
+ rustFormationsEnvVar ,
168
+ )
155
169
156
- // patch the actual deployment with the new environment variable
157
- err = s .testInstallation .ClusterContext .Client .Patch (s .ctx , controllerDeploy , client .MergeFrom (controllerDeploymentOriginal ))
170
+ // patch the deployment
171
+ controllerDeployModified .ResourceVersion = ""
172
+ err = s .testInstallation .ClusterContext .Client .Patch (s .ctx , controllerDeployModified , client .MergeFrom (controllerDeploymentOriginal ))
158
173
s .Assert ().NoError (err , "patching controller deployment" )
159
174
160
- s .T ().Cleanup (func () {
161
- for _ , manifest := range manifests {
162
- err := s .testInstallation .Actions .Kubectl ().DeleteFileSafe (s .ctx , manifest )
163
- s .Require ().NoError (err )
164
- }
165
- s .testInstallation .Assertions .EventuallyObjectsNotExist (s .ctx , manifestObjects ... )
166
- err = s .testInstallation .ClusterContext .Client .Patch (s .ctx , controllerDeploy , client .MergeFrom (controllerDeploy ))
167
- s .Require ().NoError (err )
168
- })
175
+ // wait for the changes to be reflected in pod
176
+ s .testInstallation .Assertions .EventuallyPodContainerContainsEnvVar (
177
+ s .ctx ,
178
+ s .testInstallation .Metadata .InstallNamespace ,
179
+ metav1.ListOptions {
180
+ LabelSelector : "app.kubernetes.io/name=kgateway" ,
181
+ },
182
+ helpers .KgatewayContainerName ,
183
+ rustFormationsEnvVar ,
184
+ )
169
185
170
- for _ , manifest := range manifests {
171
- err := s .testInstallation .Actions .Kubectl ().ApplyFile (s .ctx , manifest )
186
+ s .T ().Cleanup (func () {
187
+ // revert to original version of deployment
188
+ controllerDeploymentOriginal .ResourceVersion = ""
189
+ err = s .testInstallation .ClusterContext .Client .Patch (s .ctx , controllerDeploymentOriginal , client .MergeFrom (controllerDeployModified ))
172
190
s .Require ().NoError (err )
173
- }
174
- s .testInstallation .Assertions .EventuallyObjectsExist (s .ctx , manifestObjects ... )
175
-
176
- // make sure pods are running
177
- s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , testdefaults .CurlPod .GetNamespace (), metav1.ListOptions {
178
- LabelSelector : "app.kubernetes.io/name=curl" ,
179
- })
180
191
181
- s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , proxyObjectMeta .GetNamespace (), metav1.ListOptions {
182
- LabelSelector : "app.kubernetes.io/name=gw" ,
192
+ // make sure the env var is removed
193
+ s .testInstallation .Assertions .EventuallyPodContainerDoesNotContainEnvVar (
194
+ s .ctx ,
195
+ s .testInstallation .Metadata .InstallNamespace ,
196
+ metav1.ListOptions {
197
+ LabelSelector : "app.kubernetes.io/name=kgateway" ,
198
+ },
199
+ helpers .KgatewayContainerName ,
200
+ rustFormationsEnvVar .Name ,
201
+ )
183
202
})
184
203
204
+ // wait for pods to be running again, since controller deployment was patched
185
205
s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , s .testInstallation .Metadata .InstallNamespace , metav1.ListOptions {
186
206
LabelSelector : "app.kubernetes.io/name=kgateway" ,
187
207
})
208
+ s .testInstallation .Assertions .EventuallyPodsRunning (s .ctx , proxyObjectMeta .GetNamespace (), metav1.ListOptions {
209
+ LabelSelector : "app.kubernetes.io/name=gw" ,
210
+ })
188
211
189
212
adminClient , closeFwd , err := envoyadmincli .NewPortForwardedClient (s .ctx , "deploy/" + proxyObjectMeta .Name , proxyObjectMeta .Namespace )
190
213
s .Assert ().NoError (err , "get admin cli for envoy" )
0 commit comments