-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
158 lines (147 loc) · 4.01 KB
/
integration_test.go
File metadata and controls
158 lines (147 loc) · 4.01 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
package tun
import (
"bufio"
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
)
// pickFreePort reserves a free TCP port by binding :0 and closing.
func pickFreePort(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen :0: %v", err)
}
addr := ln.Addr().(*net.TCPAddr)
p := fmt.Sprintf("%d", addr.Port)
_ = ln.Close()
return p
}
func TestEndToEnd_TunnelForwardsRequest(t *testing.T) {
// Local HTTP service to receive tunneled requests
got := make(chan struct{}, 1)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/slack/events" {
t.Fatalf("unexpected %s %s", r.Method, r.URL.Path)
}
w.Header().Set("X-Test", "ok")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("{}"))
select {
case got <- struct{}{}:
default:
}
}))
t.Cleanup(srv.Close)
port := pickFreePort(t)
wsURL := fmt.Sprintf("ws://127.0.0.1:%s/tunnel", port)
httpURL := fmt.Sprintf("http://127.0.0.1:%s/health", port)
forwardURL := fmt.Sprintf("http://127.0.0.1:%s/slack/events", port)
// Start tund (absolute path, run from a clean temp dir so it won't read repo .env)
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
t.Cleanup(cancel)
root, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
absTund := "./cmd/tund"
tund := exec.CommandContext(ctx, "go", "run", absTund)
tund.Dir = root
tund.Env = []string{"PORT=" + port, "TUN_TOKEN=itest", "PATH=" + os.Getenv("PATH"), "HOME=" + os.Getenv("HOME")}
stderr, _ := tund.StderrPipe()
stdout, _ := tund.StdoutPipe()
if err := tund.Start(); err != nil {
t.Fatalf("start tund: %v", err)
}
t.Cleanup(func() { _ = tund.Process.Kill() })
// Wait for /health ready
deadline := time.Now().Add(8 * time.Second)
for time.Now().Before(deadline) {
resp, err := http.Get(httpURL)
if err == nil && resp.StatusCode == 200 {
_ = resp.Body.Close()
break
}
if resp != nil {
_ = resp.Body.Close()
}
time.Sleep(100 * time.Millisecond)
}
absTun := "./cmd/tun"
tunCmd := exec.CommandContext(ctx, "go", "run", absTun)
tunCmd.Dir = root
tunCmd.Env = []string{
"TUN_SERVER=" + wsURL,
"TUN_LOCAL=" + srv.URL,
"TUN_ALLOW=POST /slack/events",
"TUN_TOKEN=itest",
"PATH=" + os.Getenv("PATH"),
"HOME=" + os.Getenv("HOME"),
}
tunStdout, _ := tunCmd.StdoutPipe()
tunStderr, _ := tunCmd.StderrPipe()
if err := tunCmd.Start(); err != nil {
t.Fatalf("start tun: %v", err)
}
t.Cleanup(func() { _ = tunCmd.Process.Kill() })
// Poll until tunnel connected (server stops returning 503), then assert 200/ok
readyDeadline := time.Now().Add(8 * time.Second)
for {
resp, err := http.Post(forwardURL, "application/json", strings.NewReader("{}"))
if err == nil {
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusServiceUnavailable { // not 503 => connected
if resp.StatusCode != 200 || resp.Header.Get("X-Test") != "ok" {
dump(t, "tund", stdout, stderr)
dump(t, "tun", tunStdout, tunStderr)
t.Fatalf("forward status=%d header[X-Test]=%q body=%s", resp.StatusCode, resp.Header.Get("X-Test"), string(b))
}
break
}
}
if time.Now().After(readyDeadline) {
dump(t, "tund", stdout, stderr)
dump(t, "tun", tunStdout, tunStderr)
t.Fatal("tunnel did not become ready in time")
}
time.Sleep(150 * time.Millisecond)
}
select {
case <-got:
// ok
case <-time.After(2 * time.Second):
dump(t, "tund", stdout, stderr)
dump(t, "tun", tunStdout, tunStderr)
t.Fatal("local server did not receive request")
}
}
func dump(t *testing.T, name string, out, err io.Reader) {
t.Helper()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
sc := bufio.NewScanner(out)
for sc.Scan() {
t.Logf("[%s stdout] %s", name, sc.Text())
}
}()
go func() {
defer wg.Done()
sc := bufio.NewScanner(err)
for sc.Scan() {
t.Logf("[%s stderr] %s", name, sc.Text())
}
}()
wg.Wait()
}