forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestserver.go
192 lines (175 loc) · 5.82 KB
/
testserver.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
package helper
import (
"fmt"
"os"
"time"
"github.com/pkg/errors"
"github.com/solo-io/go-utils/log"
)
const (
defaultTestServerImage = "quay.io/solo-io/testrunner:v1.7.0-beta17"
TestServerName = "testserver"
TestServerPort = 1234
// This response is given by the testserver when the SimpleServer is started
SimpleHttpResponse = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_name">product_name</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`
SimpleHttpResponseArm = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
<li><a href="bin/">bin/</a>
<li><a href="boot/">boot/</a>
<li><a href="dev/">dev/</a>
<li><a href="etc/">etc/</a>
<li><a href="home/">home/</a>
<li><a href="lib/">lib/</a>
<li><a href="lib64/">lib64/</a>
<li><a href="media/">media/</a>
<li><a href="mnt/">mnt/</a>
<li><a href="opt/">opt/</a>
<li><a href="proc/">proc/</a>
<li><a href="product_uuid">product_uuid</a>
<li><a href="root/">root/</a>
<li><a href="root.crt">root.crt</a>
<li><a href="run/">run/</a>
<li><a href="sbin/">sbin/</a>
<li><a href="srv/">srv/</a>
<li><a href="sys/">sys/</a>
<li><a href="tmp/">tmp/</a>
<li><a href="usr/">usr/</a>
<li><a href="var/">var/</a>
</ul>
<hr>
</body>
</html>`
)
// tests relying on the test server should be ported using the default nginx deployment located at
// test/kubernetes/e2e/defaults/testdata/nginx_pod.yaml
func NewTestServer(namespace string) (TestUpstreamServer, error) {
testContainer, err := newTestContainer(namespace, defaultTestServerImage, TestServerName, TestServerPort)
if err != nil {
return nil, err
}
return &testServer{
testContainer: testContainer,
}, nil
}
// This object represents a container that gets deployed to the cluster to support testing.
type testServer struct {
*testContainer
deployed bool
}
// DeployServer deletes any running server pod and its service if it exists, then
// deploys a server which serves HTTP traffic.
func (t *testServer) DeployServer(timeout time.Duration) error {
if t.deployed {
if err := t.TerminatePodAndDeleteService(); err != nil {
return errors.Wrap(err, "terminating pod and deleting service")
}
t.deployed = false
}
if err := t.DeployResources(timeout); err != nil {
return err
}
t.deployed = true
go func() {
start := time.Now()
log.Debugf("starting http server listening on port %v", TestServerPort)
// This command start an http SimpleHttpServer and blocks until the server terminates
if _, err := t.Exec("python", "-m", "SimpleHTTPServer", fmt.Sprintf("%v", TestServerPort)); err != nil {
// if an error happened after 5 seconds, it's probably not an error.. just the pod terminating.
if time.Now().Sub(start).Seconds() < 5.0 {
log.Warnf("failed to start HTTP Server in Test Server: %v", err)
}
}
}()
return nil
}
// DeployServerTls deletes any running server pod and its service if it exists, then
// deploys a server which serves HTTPS traffic with the cert and key provided.
func (t *testServer) DeployServerTls(timeout time.Duration, crt, key []byte) error {
if t.deployed {
if err := t.TerminatePodAndDeleteService(); err != nil {
return errors.Wrap(err, "terminating pod and deleting service")
}
t.deployed = false
}
if err := t.testContainer.DeployResources(timeout); err != nil {
return errors.Wrap(err, "deploying pod")
}
t.deployed = true
certFname := "/tmp/testserver_tls/cert.pem"
keyFname := "/tmp/testserver_tls/key.pem"
scriptFname := "/tmp/testserver_tls/server.py"
os.MkdirAll("/tmp/testserver_tls", os.ModePerm)
defer os.RemoveAll("/tmp/testserver_tls")
if err := os.WriteFile(certFname, crt, os.ModePerm); err != nil {
return errors.Wrap(err, "writing cert")
}
if err := os.WriteFile(keyFname, key, os.ModePerm); err != nil {
return errors.Wrap(err, "writing key")
}
// Our default image for the old testrunner (now testserver) uses python 2.
// Therefore until this old image is no longer used, we use this script to
// set up a python 2 https server.
if err := os.WriteFile(scriptFname, []byte(fmt.Sprintf(`
import BaseHTTPServer, SimpleHTTPServer
import ssl
httpd = BaseHTTPServer.HTTPServer(('0.0.0.0', %d), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile='/tmp/key.pem', certfile='/tmp/cert.pem', server_side=True)
httpd.serve_forever()
`, TestServerPort)), os.ModePerm); err != nil {
return errors.Wrap(err, "writing server")
}
if err := t.Cp(map[string]string{
certFname: "/tmp/cert.pem",
keyFname: "/tmp/key.pem",
scriptFname: "/tmp/server.py",
}); err != nil {
return errors.Wrap(err, "kubectl cp")
}
go func() {
start := time.Now()
log.Debugf("starting https server listening on port %v", TestServerPort)
// This command starts an https SimpleHttpServer and blocks until the server terminates
if _, err := t.Exec("python", "/tmp/server.py"); err != nil {
// if an error happened after 5 seconds, it's probably not an error.. just the pod terminating.
if time.Now().Sub(start).Seconds() < 5.0 {
log.Warnf("failed to start HTTPS Server in Test Server: %v", err)
}
}
}()
return nil
}