-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdata_plane.go
More file actions
655 lines (558 loc) · 15.9 KB
/
data_plane.go
File metadata and controls
655 lines (558 loc) · 15.9 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright The HTNN Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dataplane
import (
"bufio"
"bytes"
"context"
"crypto/md5"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"mosn.io/htnn/api/pkg/log"
"mosn.io/htnn/api/plugins/tests/integration/helper"
)
var (
logger = log.DefaultLogger.WithName("dataplane")
testRootDirName = "test-envoy"
containerName = "run_envoy_for_test"
validationCache = map[[16]byte]struct{}{}
)
type DataPlane struct {
cmd *exec.Cmd
t *testing.T
opt *Option
done chan error
latestRouteVersion string
dataPlanePort string
adminAPIPort string
soPath string
}
type Option struct {
LogLevel string
Envs map[string]string
Bootstrap *bootstrap
NoErrorLogCheck bool
ExpectLogPattern []string
ExpectNoLogPattern []string
}
func addEnvironemntVariables(cmd *exec.Cmd, envs map[string]string) {
for k, v := range envs {
cmd.Env = append(cmd.Env, k+"="+v)
}
}
type serializableFileWriter struct {
*os.File
lock sync.Mutex
}
func (w *serializableFileWriter) Write(p []byte) (n int, err error) {
w.lock.Lock()
defer w.lock.Unlock()
return w.File.Write(p)
}
func (w *serializableFileWriter) Unbox() *os.File {
return w.File
}
func StartDataPlane(t *testing.T, opt *Option) (*DataPlane, error) {
if opt == nil {
opt = &Option{}
}
if opt.LogLevel == "" {
opt.LogLevel = "info"
}
if opt.Bootstrap == nil {
opt.Bootstrap = Bootstrap()
}
dp := &DataPlane{
t: t,
opt: opt,
}
opt.Bootstrap.SetDataPlane(dp)
err := dp.cleanup(t)
if err != nil {
return nil, err
}
dir := dp.root()
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
coverDir := helper.CoverDir()
_, err = os.Stat(coverDir)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
if err := os.MkdirAll(coverDir, 0755); err != nil {
return nil, err
}
// When the integration test is run with `go test ...`, the previous coverage files are not removed.
// Since we only care about the coverage in CI, it is fine so far.
}
pwd, _ := os.Getwd()
soPath := filepath.Join(pwd, "libgolang.so")
st, err := os.Stat(soPath)
if err != nil {
if os.IsNotExist(err) {
logger.Error(err, "Shared library not found. Please build the shared library before running integration test, for example calling `make build-test-so`",
"shared library path", soPath)
}
return nil, err
}
if st.IsDir() {
err := errors.New("bad shared library detected")
logger.Error(err, "Please remove the bad file and build the shared library before running integration test, for example calling `make build-test-so`",
"shared library path", soPath)
return nil, err
}
dp.soPath = soPath
adminAPIPort := "9998"
adminAPIPortEnv := os.Getenv("TEST_ENVOY_ADMIN_API_PORT")
if adminAPIPortEnv != "" {
adminAPIPort = adminAPIPortEnv
}
dp.adminAPIPort = adminAPIPort
dataPlanePort := "10000"
dataPlanePortEnv := os.Getenv("TEST_ENVOY_DATA_PLANE_PORT")
if dataPlanePortEnv != "" {
dataPlanePort = dataPlanePortEnv
}
dp.dataPlanePort = dataPlanePort
cfgFilename := "envoy.yaml"
cfgFile, err := os.Create(filepath.Join(dir, cfgFilename))
if err != nil {
return nil, err
}
err = opt.Bootstrap.WriteTo(cfgFile)
cfgFile.Close()
if err != nil {
return nil, err
}
var cmdline, envoyCmd string
if isBinaryMode() {
envoyCmd = binaryPath + " -c " + cfgFile.Name()
if len(opt.Envs) == 0 {
opt.Envs = map[string]string{}
}
opt.Envs["GOCOVERDIR"] = coverDir
} else {
hostAddr := ""
if runtime.GOOS == "linux" {
// We use this special domain to access the control plane on host.
// It works with Docker for Win/Mac (--network host doesn't work).
// For Linux's Docker, a special option is used instead
hostAddr = "--add-host=host.docker.internal:host-gateway"
}
currentUser, err := user.Current()
if err != nil {
return nil, err
}
networkName := "services_service"
err = exec.Command("docker", "network", "inspect", networkName).Run()
if err != nil {
logger.Info("docker network used by test not found, create one")
err = exec.Command("docker", "network", "create", networkName).Run()
if err != nil {
return nil, err
}
}
image := "m.daocloud.io/docker.io/envoyproxy/envoy:contrib-v1.32.0"
specifiedImage := os.Getenv("PROXY_IMAGE")
if specifiedImage != "" {
image = specifiedImage
}
b, err := exec.Command("docker", "images", image).Output()
if err != nil {
return nil, err
}
if len(strings.Split(string(b), "\n")) < 3 {
cmd := exec.Command("docker", "pull", image)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logger.Info("pull envoy image", "cmdline", cmd.String())
err = cmd.Run()
if err != nil {
return nil, err
}
}
envs := []string{}
for k, v := range opt.Envs {
envs = append(envs, "-e", k+"="+v)
}
cmdline = "docker run" +
" --name " + containerName +
" --network " + networkName +
" --user " + currentUser.Uid +
" --rm -t -v " +
cfgFile.Name() + ":/etc/envoy.yaml -v " +
soPath + ":/etc/libgolang.so" +
" -v /tmp:/tmp" +
" -e GOCOVERDIR=" + coverDir +
" " + strings.Join(envs, " ") +
" -p " + dataPlanePort + ":" + dataPlanePort +
" -p " + adminAPIPort + ":" + adminAPIPort + " " +
hostAddr + " " +
image
envoyCmd = "envoy -c /etc/envoy.yaml"
}
// show why the configuration is invalid
envoyValidateCmd := envoyCmd + " --mode validate -l error"
content, _ := os.ReadFile(cfgFile.Name())
digest := md5.Sum(content)
if _, ok := validationCache[digest]; !ok {
// Workaround for https://github.com/envoyproxy/envoy/issues/35961
// TODO: drop this once we upgrade to Envoy 1.30+
cfgFile, _ := os.Create(cfgFile.Name())
opt.Bootstrap.WriteToForValidation(cfgFile)
validateCmd := cmdline + " " + envoyValidateCmd
cmds := strings.Fields(validateCmd)
logger.Info("run validate cmd", "cmdline", validateCmd)
cmd := exec.Command(cmds[0], cmds[1:]...)
if isBinaryMode() {
addEnvironemntVariables(cmd, opt.Envs)
}
out, err := cmd.CombinedOutput()
if err != nil {
logger.Info("bad envoy bootstrap configuration", "cmd", validateCmd, "output", string(out))
return nil, err
}
validationCache[digest] = struct{}{}
cfgFile, _ = os.Create(cfgFile.Name())
cfgFile.Write(content)
}
if opt.LogLevel != "" {
envoyCmd += " -l " + opt.LogLevel
}
cmdline = cmdline + " " + envoyCmd
logger.Info("run cmd", "cmdline", cmdline)
cmds := strings.Fields(cmdline)
cmd := exec.Command(cmds[0], cmds[1:]...)
if isBinaryMode() {
addEnvironemntVariables(cmd, opt.Envs)
}
if isBinaryMode() {
// Like the standard mode, we use stdout file to store the log of Envoy
stdout, err := os.Create(filepath.Join(dir, "stdout"))
if err != nil {
return nil, err
}
// wrap writer to ensure the stderr and stdout won't affect each other
w := &serializableFileWriter{File: stdout}
cmd.Stdout = w
// We don't need stderr file, which is used to store docker output in the standard mode.
// Just left an empty file here to keep the same structure.
_, err = os.Create(filepath.Join(dir, "stderr"))
if err != nil {
return nil, err
}
cmd.Stderr = w
} else {
stdout, err := os.Create(filepath.Join(dir, "stdout"))
if err != nil {
return nil, err
}
cmd.Stdout = stdout
stderr, err := os.Create(filepath.Join(dir, "stderr"))
if err != nil {
return nil, err
}
cmd.Stderr = stderr
}
dp.cmd = cmd
done := make(chan error)
go func() {
logger.Info("start envoy")
err := dp.cmd.Start()
if err != nil {
logger.Error(err, "failed to start envoy")
return
}
go func() { done <- cmd.Wait() }()
}()
if isBinaryMode() {
// In binary mode, the port is open only after the control plane is up, which is called after
// the data plane is up. So we don't check if the port is open. Instead, we wait for a while
// to ensure the data plane can be started.
waitTime := 1 * time.Second
waitTimeEnv, _ := time.ParseDuration(os.Getenv("TEST_ENVOY_WAIT_BINARY_TO_START_TIME"))
if waitTimeEnv != 0 {
waitTime = waitTimeEnv
}
time.Sleep(waitTime)
} else {
helper.WaitServiceUp(t, ":"+dataPlanePort, "")
}
select {
case err := <-done:
return nil, err
default:
}
dp.done = done
return dp, nil
}
func (dp *DataPlane) root() string {
pwd, _ := os.Getwd()
name := dp.t.Name()
dir := filepath.Join(pwd, testRootDirName, name)
return dir
}
func (dp *DataPlane) cleanup(_ *testing.T) error {
if !isBinaryMode() {
cmd := exec.Command("docker", "stop", containerName)
// ignore error when the containerName is not left over
_ = cmd.Run()
}
dir := dp.root()
_, err := os.Stat(dir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
if err := os.RemoveAll(dir); err != nil {
return err
}
}
// now the dir is not exist
return nil
}
func (dp *DataPlane) Stop() {
err := dp.FlushCoverage()
if err != nil {
logger.Error(err, "failed to flush coverage")
}
logger.Info("stop envoy")
if isBinaryMode() {
dp.cmd.Process.Signal(syscall.SIGTERM)
} else {
cmd := exec.Command("docker", "stop", containerName)
err = cmd.Run()
}
if err != nil {
logger.Error(err, "failed to terminate envoy")
return
}
// ensure envoy is gone
<-dp.done
logger.Info("envoy stopped")
var f *os.File
if !isBinaryMode() {
f = dp.cmd.Stderr.(*os.File)
f.Close()
f = dp.cmd.Stdout.(*os.File)
} else {
f = dp.cmd.Stdout.(*serializableFileWriter).Unbox()
}
f.Seek(0, 0)
text, err := io.ReadAll(f)
defer f.Close()
if err != nil {
logger.Error(err, "failed to read envoy stdout")
return
}
if !dp.opt.NoErrorLogCheck {
sc := bufio.NewScanner(bytes.NewReader(text))
for sc.Scan() {
s := sc.Text()
if strings.Contains(s, "[error]") || strings.Contains(s, "[critical]") {
assert.Falsef(dp.t, true, "error/critical level log found: %s", s)
break
}
}
}
for _, pattern := range dp.opt.ExpectLogPattern {
re := regexp.MustCompile(pattern)
if !re.Match(text) {
assert.Falsef(dp.t, true, "log pattern %q not found", pattern)
}
}
for _, pattern := range dp.opt.ExpectNoLogPattern {
re := regexp.MustCompile(pattern)
if re.Match(text) {
assert.Falsef(dp.t, true, "log pattern %q found", pattern)
}
}
}
func (dp *DataPlane) Head(path string, header http.Header) (*http.Response, error) {
return dp.do("HEAD", path, header, nil)
}
func (dp *DataPlane) Get(path string, header http.Header) (*http.Response, error) {
return dp.do("GET", path, header, nil)
}
func (dp *DataPlane) Delete(path string, header http.Header) (*http.Response, error) {
return dp.do("DELETE", path, header, nil)
}
func (dp *DataPlane) Post(path string, header http.Header, body io.Reader) (*http.Response, error) {
return dp.do("POST", path, header, body)
}
func (dp *DataPlane) PostWithTrailer(path string, header http.Header, body io.Reader, trailer http.Header) (*http.Response, error) {
return dp.doWithTrailer("POST", path, header, body, trailer)
}
func (dp *DataPlane) Put(path string, header http.Header, body io.Reader) (*http.Response, error) {
return dp.do("PUT", path, header, body)
}
func (dp *DataPlane) Patch(path string, header http.Header, body io.Reader) (*http.Response, error) {
return dp.do("PATCH", path, header, body)
}
func (dp *DataPlane) SendAndCancelRequest(path string, after time.Duration) error {
conn, err := net.DialTimeout("tcp", ":"+dp.dataPlanePort, 1*time.Second)
if err != nil {
return err
}
defer conn.Close()
for _, s := range []string{
fmt.Sprintf("POST %s HTTP/1.1\r\n", path),
"Host: localhost\r\n",
"Content-Length: 10000\r\n",
"\r\n",
} {
_, err = conn.Write([]byte(s))
if err != nil {
return err
}
}
time.Sleep(after)
return nil
}
func (dp *DataPlane) do(method string, path string, header http.Header, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, "http://localhost:"+dp.dataPlanePort+path, body)
if err != nil {
return nil, err
}
req.Header = header
tr := &http.Transport{DialContext: func(ctx context.Context, proto, addr string) (conn net.Conn, err error) {
return net.DialTimeout("tcp", ":"+dp.dataPlanePort, 1*time.Second)
}}
client := &http.Client{Transport: tr,
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
return resp, err
}
func (dp *DataPlane) GetAdmin(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", "http://localhost:"+dp.adminAPIPort+path, nil)
if err != nil {
return nil, err
}
tr := &http.Transport{
DialContext: func(ctx context.Context, proto, addr string) (conn net.Conn, err error) {
return net.DialTimeout("tcp", ":"+dp.adminAPIPort, 1*time.Second)
},
}
client := &http.Client{Transport: tr,
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
return resp, err
}
func (dp *DataPlane) doWithTrailer(method string, path string, header http.Header, body io.Reader, trailer http.Header) (*http.Response, error) {
req, err := http.NewRequest(method, "http://localhost:"+dp.dataPlanePort+path, body)
if err != nil {
return nil, err
}
req.Header = header
req.Header.Add("TE", "trailers")
req.Trailer = trailer
req.TransferEncoding = []string{"chunked"}
tr := &http.Transport{
DialContext: func(ctx context.Context, proto, addr string) (conn net.Conn, err error) {
return net.DialTimeout("tcp", ":"+dp.dataPlanePort, 1*time.Second)
},
}
client := &http.Client{Transport: tr,
Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
return resp, err
}
// Use grpcurl so that the caller can specify the proto file without building the Go code.
// TODO: we can rewrite this in Go.
func (dp *DataPlane) Grpcurl(importPath, protoFile, fullMethodName, req string) ([]byte, error) {
cmd := exec.Command("grpcurl", "-v", "-format-error", "-import-path", importPath, "-proto", protoFile, "-plaintext", "-d", req, ":"+dp.dataPlanePort, fullMethodName)
dp.t.Logf("run grpcurl command: %s", cmd.String())
return cmd.CombinedOutput()
}
func (dp *DataPlane) Configured() bool {
resp, err := dp.Head("/detect_if_the_rds_takes_effect", nil)
if err != nil {
return false
}
if resp.StatusCode != 200 {
return false
}
name := resp.Header.Get("route-version")
if name == dp.latestRouteVersion {
return false
}
dp.latestRouteVersion = name
return true
}
func (dp *DataPlane) FlushCoverage() error {
resp, err := dp.Post("/flush_coverage", nil, nil)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func (dp *DataPlane) SetLogLevel(loggerName string, level string) error {
req, err := http.NewRequest("POST", fmt.Sprintf("http://0.0.0.0:%s/logging?%s=%s", dp.adminAPIPort, loggerName, level), bytes.NewReader([]byte{}))
if err != nil {
return err
}
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func (dp *DataPlane) AdminAPIPort() int {
p, _ := strconv.Atoi(dp.adminAPIPort)
return p
}
func (dp *DataPlane) Port() int {
p, _ := strconv.Atoi(dp.dataPlanePort)
return p
}