-
Notifications
You must be signed in to change notification settings - Fork 435
/
k8s_gateway_suite.go
157 lines (135 loc) · 5.35 KB
/
k8s_gateway_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
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
package port_routing
import (
"context"
"time"
"github.com/solo-io/gloo/pkg/utils/kubeutils"
"github.com/solo-io/gloo/pkg/utils/requestutils/curl"
"github.com/solo-io/gloo/test/kubernetes/e2e"
"github.com/stretchr/testify/suite"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var _ e2e.NewSuiteFunc = NewK8sGatewayTestingSuite
// portRoutingTestingSuite is the entire Suite of tests for the "PortRouting" cases
type portRoutingTestingSuite 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
// maps test name to a list of manifests to apply before the test
manifests map[string][]string
}
/*
The port routing suite sets up in the following order
SetupSuite:
1. Create the setup apps (curl, nginx, etc.)
2. Create k8s Gateway
3. Proxy provisioned (k8s deployment created and checked)
Each port routing test:
1. Attach HttpRoute with different port/targetport definition per test
2. Remove HttpRoute, proxy still exists without any routes
TearDownSuite:
1. Deletes the setup apps (curl, nginx, etc.)
2. Deletes the k8s Gateway
3. Proxy de-provisioned (k8s deployment deleted)
*/
func NewK8sGatewayTestingSuite(ctx context.Context, testInst *e2e.TestInstallation) suite.TestingSuite {
return &portRoutingTestingSuite{
ctx: ctx,
testInstallation: testInst,
manifests: map[string][]string{
"TestInvalidPortAndValidTargetport": {svcInvalidPortAndValidTargetportManifest, invalidPortAndValidTargetportManifest},
"TestMatchPortAndTargetport": {svcMatchPortandTargetportManifest, matchPortandTargetportManifest},
"TestMatchPodPortWithoutTargetport": {svcMatchPodPortWithoutTargetportManifest, matchPodPortWithoutTargetportManifest},
"TestInvalidPortWithoutTargetport": {svcInvalidPortWithoutTargetportManifest, invalidPortWithoutTargetportManifest},
"TestInvalidPortAndInvalidTargetport": {svcInvalidPortAndInvalidTargetportManifest, invalidPortAndInvalidTargetportManifest},
},
}
}
func (s *portRoutingTestingSuite) SetupSuite() {
err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, setupManifest)
s.NoError(err, "can apply setup manifest")
err = s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, setupK8sManifest)
s.NoError(err, "can apply setup k8s gateway manifest")
s.testInstallation.Assertions.EventuallyObjectsExist(s.ctx, proxyService, proxyDeployment)
// Check that test resources are running
s.testInstallation.Assertions.EventuallyPodsRunning(s.ctx, proxyDeployment.ObjectMeta.GetNamespace(),
metav1.ListOptions{LabelSelector: "app.kubernetes.io/name=gloo-proxy-gw"}, time.Minute*2)
}
func (s *portRoutingTestingSuite) TearDownSuite() {
err := s.testInstallation.Actions.Kubectl().DeleteFile(s.ctx, setupManifest)
s.NoError(err, "can delete setup manifest")
err = s.testInstallation.Actions.Kubectl().DeleteFile(s.ctx, setupK8sManifest)
s.NoError(err, "can delete setup k8s gateway manifest")
s.testInstallation.Assertions.EventuallyObjectsNotExist(s.ctx, proxyService, proxyDeployment)
}
func (s *portRoutingTestingSuite) BeforeTest(suiteName, testName string) {
manifests, ok := s.manifests[testName]
if !ok {
s.FailNow("no manifests found for %s, manifest map contents: %v", testName, s.manifests)
}
for _, manifest := range manifests {
err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, manifest)
s.NoError(err, "can apply "+manifest)
}
}
func (s *portRoutingTestingSuite) AfterTest(suiteName, testName string) {
manifests, ok := s.manifests[testName]
if !ok {
s.FailNow("no manifests found for " + testName)
}
for _, manifest := range manifests {
err := s.testInstallation.Actions.Kubectl().DeleteFile(s.ctx, manifest)
s.NoError(err, "can delete "+manifest)
}
}
func (s *portRoutingTestingSuite) TestInvalidPortAndValidTargetport() {
s.testInstallation.Assertions.AssertEventualCurlResponse(
s.ctx,
curlPodExecOpt,
[]curl.Option{
curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)),
curl.WithHostHeader("example.com"),
},
expectedHealthyResponse)
}
func (s *portRoutingTestingSuite) TestMatchPortAndTargetport() {
s.testInstallation.Assertions.AssertEventualCurlResponse(
s.ctx,
curlPodExecOpt,
[]curl.Option{
curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)),
curl.WithHostHeader("example.com"),
},
expectedHealthyResponse)
}
func (s *portRoutingTestingSuite) TestMatchPodPortWithoutTargetport() {
s.testInstallation.Assertions.AssertEventualCurlResponse(
s.ctx,
curlPodExecOpt,
[]curl.Option{
curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)),
curl.WithHostHeader("example.com"),
},
expectedHealthyResponse)
}
func (s *portRoutingTestingSuite) TestInvalidPortWithoutTargetport() {
s.testInstallation.Assertions.AssertEventualCurlResponse(
s.ctx,
curlPodExecOpt,
[]curl.Option{
curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)),
curl.WithHostHeader("example.com"),
},
expectedServiceUnavailableResponse)
}
func (s *portRoutingTestingSuite) TestInvalidPortAndInvalidTargetport() {
s.testInstallation.Assertions.AssertEventualCurlResponse(
s.ctx,
curlPodExecOpt,
[]curl.Option{
curl.WithHost(kubeutils.ServiceFQDN(proxyService.ObjectMeta)),
curl.WithHostHeader("example.com"),
},
expectedServiceUnavailableResponse)
}