portforwardtester.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // A tiny binary for testing port forwarding. The following environment variables
  14. // control the binary's logic:
  15. //
  16. // BIND_PORT - the TCP port to use for the listener
  17. // EXPECTED_CLIENT_DATA - data that we expect to receive from the client; may be "".
  18. // CHUNKS - how many chunks of data we should send to the client
  19. // CHUNK_SIZE - how large each chunk should be
  20. // CHUNK_INTERVAL - the delay in between sending each chunk
  21. //
  22. // Log messages are written to stdout at various stages of the binary's execution.
  23. // Test code can retrieve this container's log and validate that the expected
  24. // behavior is taking place.
  25. package main
  26. import (
  27. "fmt"
  28. "net"
  29. "os"
  30. "strconv"
  31. "strings"
  32. "time"
  33. )
  34. func getEnvInt(name string) int {
  35. s := os.Getenv(name)
  36. value, err := strconv.Atoi(s)
  37. if err != nil {
  38. fmt.Printf("Error parsing %s %q: %v\n", name, s, err)
  39. os.Exit(1)
  40. }
  41. return value
  42. }
  43. // taken from net/http/server.go:
  44. //
  45. // rstAvoidanceDelay is the amount of time we sleep after closing the
  46. // write side of a TCP connection before closing the entire socket.
  47. // By sleeping, we increase the chances that the client sees our FIN
  48. // and processes its final data before they process the subsequent RST
  49. // from closing a connection with known unread data.
  50. // This RST seems to occur mostly on BSD systems. (And Windows?)
  51. // This timeout is somewhat arbitrary (~latency around the planet).
  52. const rstAvoidanceDelay = 500 * time.Millisecond
  53. func main() {
  54. bindAddress := os.Getenv("BIND_ADDRESS")
  55. if bindAddress == "" {
  56. bindAddress = "localhost"
  57. }
  58. bindPort := os.Getenv("BIND_PORT")
  59. addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(bindAddress, bindPort))
  60. if err != nil {
  61. fmt.Printf("Error resolving: %v\n", err)
  62. os.Exit(1)
  63. }
  64. listener, err := net.ListenTCP("tcp", addr)
  65. if err != nil {
  66. fmt.Printf("Error listening: %v\n", err)
  67. os.Exit(1)
  68. }
  69. conn, err := listener.AcceptTCP()
  70. if err != nil {
  71. fmt.Printf("Error accepting connection: %v\n", err)
  72. os.Exit(1)
  73. }
  74. fmt.Println("Accepted client connection")
  75. expectedClientData := os.Getenv("EXPECTED_CLIENT_DATA")
  76. if len(expectedClientData) > 0 {
  77. buf := make([]byte, len(expectedClientData))
  78. read, err := conn.Read(buf)
  79. if read != len(expectedClientData) {
  80. fmt.Printf("Expected to read %d bytes from client, but got %d instead. err=%v\n", len(expectedClientData), read, err)
  81. os.Exit(2)
  82. }
  83. if expectedClientData != string(buf) {
  84. fmt.Printf("Expect to read %q, but got %q. err=%v\n", expectedClientData, string(buf), err)
  85. os.Exit(3)
  86. }
  87. if err != nil {
  88. fmt.Printf("Read err: %v\n", err)
  89. }
  90. fmt.Println("Received expected client data")
  91. }
  92. chunks := getEnvInt("CHUNKS")
  93. chunkSize := getEnvInt("CHUNK_SIZE")
  94. chunkInterval := getEnvInt("CHUNK_INTERVAL")
  95. stringData := strings.Repeat("x", chunkSize)
  96. data := []byte(stringData)
  97. for i := 0; i < chunks; i++ {
  98. written, err := conn.Write(data)
  99. if written != chunkSize {
  100. fmt.Printf("Expected to write %d bytes from client, but wrote %d instead. err=%v\n", chunkSize, written, err)
  101. os.Exit(4)
  102. }
  103. if err != nil {
  104. fmt.Printf("Write err: %v\n", err)
  105. }
  106. if i+1 < chunks {
  107. time.Sleep(time.Duration(chunkInterval) * time.Millisecond)
  108. }
  109. }
  110. fmt.Println("Shutting down connection")
  111. // set linger timeout to flush buffers. This is the official way according to the go api docs. But
  112. // there are controversial discussions whether this value has any impact on most platforms
  113. // (compare https://codereview.appspot.com/95320043).
  114. conn.SetLinger(-1)
  115. // 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:
  116. // 1. close write half of connection which sends a FIN packet
  117. // 2. give client some time to receive the FIN
  118. // 3. close the complete connection
  119. conn.CloseWrite()
  120. time.Sleep(rstAvoidanceDelay)
  121. conn.Close()
  122. fmt.Println("Done")
  123. }