mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 12:43:23 +00:00
deflake tests.
This commit is contained in:
parent
9355fae71e
commit
7ea2a37219
@ -18,8 +18,11 @@ package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
@ -38,49 +41,40 @@ func waitForClosedPort(p *Proxier, proxyPort string) error {
|
||||
return fmt.Errorf("port %s still open", proxyPort)
|
||||
}
|
||||
|
||||
// a simple echoServer that only accepts one connection. Returns port actually
|
||||
// being listened on, or an error.
|
||||
func echoServer(t *testing.T, addr string) (string, error) {
|
||||
l, err := net.Listen("tcp", addr)
|
||||
var port string
|
||||
|
||||
func init() {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(r.URL.Path[1:]))
|
||||
}))
|
||||
u, err := url.Parse(ts.URL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to start echo service: %v", err)
|
||||
panic(fmt.Sprintf("failed to parse: %v", err))
|
||||
}
|
||||
_, port, err = net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed to parse: %v", err))
|
||||
}
|
||||
go func() {
|
||||
defer l.Close()
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
t.Errorf("failed to accept new conn to echo service: %v", err)
|
||||
}
|
||||
io.Copy(conn, conn)
|
||||
conn.Close()
|
||||
}()
|
||||
_, port, err := net.SplitHostPort(l.Addr().String())
|
||||
return port, err
|
||||
}
|
||||
|
||||
func testEchoConnection(t *testing.T, address, port string) {
|
||||
conn, err := net.Dial("tcp", net.JoinHostPort(address, port))
|
||||
path := "aaaaa"
|
||||
res, err := http.Get("http://" + address + ":" + port + "/" + path)
|
||||
if err != nil {
|
||||
t.Fatalf("error connecting to proxy: %v", err)
|
||||
t.Fatalf("error connecting to server: %v", err)
|
||||
}
|
||||
magic := "aaaaa"
|
||||
if _, err := conn.Write([]byte(magic)); err != nil {
|
||||
t.Fatalf("error writing to proxy: %v", err)
|
||||
defer res.Body.Close()
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Errorf("error reading data: %v %v", err, string(data))
|
||||
}
|
||||
buf := make([]byte, 5)
|
||||
if _, err := conn.Read(buf); err != nil {
|
||||
t.Fatalf("error reading from proxy: %v", err)
|
||||
}
|
||||
if string(buf) != magic {
|
||||
t.Fatalf("bad echo from proxy: got: %q, expected %q", string(buf), magic)
|
||||
if string(data) != path {
|
||||
t.Errorf("expected: %s, got %s", path, string(data))
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxy(t *testing.T) {
|
||||
port, err := echoServer(t, "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
lb := NewLoadBalancerRR()
|
||||
lb.OnUpdate([]api.Endpoints{
|
||||
{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})
|
||||
@ -95,11 +89,6 @@ func TestProxy(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProxyStop(t *testing.T) {
|
||||
port, err := echoServer(t, "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lb := NewLoadBalancerRR()
|
||||
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})
|
||||
|
||||
@ -123,11 +112,6 @@ func TestProxyStop(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProxyUpdateDelete(t *testing.T) {
|
||||
port, err := echoServer(t, "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lb := NewLoadBalancerRR()
|
||||
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})
|
||||
|
||||
@ -150,11 +134,6 @@ func TestProxyUpdateDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestProxyUpdatePort(t *testing.T) {
|
||||
port, err := echoServer(t, "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lb := NewLoadBalancerRR()
|
||||
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})
|
||||
|
||||
@ -167,19 +146,22 @@ func TestProxyUpdatePort(t *testing.T) {
|
||||
|
||||
// add a new dummy listener in order to get a port that is free
|
||||
l, _ := net.Listen("tcp", ":0")
|
||||
_, port, _ = net.SplitHostPort(l.Addr().String())
|
||||
portNum, _ := strconv.Atoi(port)
|
||||
_, newPort, _ := net.SplitHostPort(l.Addr().String())
|
||||
portNum, _ := strconv.Atoi(newPort)
|
||||
l.Close()
|
||||
|
||||
// Wait for the socket to actually get free.
|
||||
if err := waitForClosedPort(p, port); err != nil {
|
||||
if err := waitForClosedPort(p, newPort); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
if proxyPort == newPort {
|
||||
t.Errorf("expected difference, got %s %s", newPort, proxyPort)
|
||||
}
|
||||
p.OnUpdate([]api.Service{
|
||||
{JSONBase: api.JSONBase{ID: "echo"}, Port: portNum},
|
||||
})
|
||||
if err := waitForClosedPort(p, proxyPort); err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
testEchoConnection(t, "127.0.0.1", port)
|
||||
testEchoConnection(t, "127.0.0.1", newPort)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user