forked from msazurestackworkloads/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portforwardtester.go
141 lines (123 loc) · 4.37 KB
/
portforwardtester.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
/*
Copyright 2015 The Kubernetes 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.
*/
// A tiny binary for testing port forwarding. The following environment variables
// control the binary's logic:
//
// BIND_PORT - the TCP port to use for the listener
// EXPECTED_CLIENT_DATA - data that we expect to receive from the client; may be "".
// CHUNKS - how many chunks of data we should send to the client
// CHUNK_SIZE - how large each chunk should be
// CHUNK_INTERVAL - the delay in between sending each chunk
//
// Log messages are written to stdout at various stages of the binary's execution.
// Test code can retrieve this container's log and validate that the expected
// behavior is taking place.
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func getEnvInt(name string) int {
s := os.Getenv(name)
value, err := strconv.Atoi(s)
if err != nil {
fmt.Printf("Error parsing %s %q: %v\n", name, s, err)
os.Exit(1)
}
return value
}
// taken from net/http/server.go:
//
// rstAvoidanceDelay is the amount of time we sleep after closing the
// write side of a TCP connection before closing the entire socket.
// By sleeping, we increase the chances that the client sees our FIN
// and processes its final data before they process the subsequent RST
// from closing a connection with known unread data.
// This RST seems to occur mostly on BSD systems. (And Windows?)
// This timeout is somewhat arbitrary (~latency around the planet).
const rstAvoidanceDelay = 500 * time.Millisecond
func main() {
bindAddress := os.Getenv("BIND_ADDRESS")
if bindAddress == "" {
bindAddress = "localhost"
}
bindPort := os.Getenv("BIND_PORT")
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(bindAddress, bindPort))
if err != nil {
fmt.Printf("Error resolving: %v\n", err)
os.Exit(1)
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
fmt.Printf("Error listening: %v\n", err)
os.Exit(1)
}
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Printf("Error accepting connection: %v\n", err)
os.Exit(1)
}
fmt.Println("Accepted client connection")
expectedClientData := os.Getenv("EXPECTED_CLIENT_DATA")
if len(expectedClientData) > 0 {
buf := make([]byte, len(expectedClientData))
read, err := conn.Read(buf)
if read != len(expectedClientData) {
fmt.Printf("Expected to read %d bytes from client, but got %d instead. err=%v\n", len(expectedClientData), read, err)
os.Exit(2)
}
if expectedClientData != string(buf) {
fmt.Printf("Expect to read %q, but got %q. err=%v\n", expectedClientData, string(buf), err)
os.Exit(3)
}
if err != nil {
fmt.Printf("Read err: %v\n", err)
}
fmt.Println("Received expected client data")
}
chunks := getEnvInt("CHUNKS")
chunkSize := getEnvInt("CHUNK_SIZE")
chunkInterval := getEnvInt("CHUNK_INTERVAL")
stringData := strings.Repeat("x", chunkSize)
data := []byte(stringData)
for i := 0; i < chunks; i++ {
written, err := conn.Write(data)
if written != chunkSize {
fmt.Printf("Expected to write %d bytes from client, but wrote %d instead. err=%v\n", chunkSize, written, err)
os.Exit(4)
}
if err != nil {
fmt.Printf("Write err: %v\n", err)
}
if i+1 < chunks {
time.Sleep(time.Duration(chunkInterval) * time.Millisecond)
}
}
fmt.Println("Shutting down connection")
// set linger timeout to flush buffers. This is the official way according to the go api docs. But
// there are controversial discussions whether this value has any impact on most platforms
// (compare https://codereview.appspot.com/95320043).
conn.SetLinger(-1)
// Flush the connection cleanly, following https://blog.netherlabs.nl/articles/2009/01/18/the-ultimate-so_linger-page-or-why-is-my-tcp-not-reliable:
// 1. close write half of connection which sends a FIN packet
// 2. give client some time to receive the FIN
// 3. close the complete connection
conn.CloseWrite()
time.Sleep(rstAvoidanceDelay)
conn.Close()
fmt.Println("Done")
}