2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
2014-06-13 00:18:19 +00:00
|
|
|
// 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) {
|
2014-06-06 23:40:48 +00:00
|
|
|
l, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
2014-06-13 00:18:19 +00:00
|
|
|
return "", fmt.Errorf("failed to start echo service: %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
go func() {
|
2014-06-13 00:18:19 +00:00
|
|
|
defer l.Close()
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to accept new conn to echo service: %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-13 00:18:19 +00:00
|
|
|
io.Copy(conn, conn)
|
|
|
|
conn.Close()
|
2014-06-06 23:40:48 +00:00
|
|
|
}()
|
2014-06-13 00:18:19 +00:00
|
|
|
_, port, err := net.SplitHostPort(l.Addr().String())
|
|
|
|
return port, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxy(t *testing.T) {
|
2014-06-13 23:04:17 +00:00
|
|
|
port, err := echoServer(t, "127.0.0.1:0")
|
2014-06-13 00:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
lb := NewLoadBalancerRR()
|
2014-07-23 01:53:41 +00:00
|
|
|
lb.OnUpdate([]api.Endpoints{
|
|
|
|
{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})
|
2014-06-06 23:40:48 +00:00
|
|
|
|
|
|
|
p := NewProxier(lb)
|
2014-06-13 00:18:19 +00:00
|
|
|
|
|
|
|
proxyPort, err := p.addServiceOnUnusedPort("echo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error adding new service: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-13 00:18:19 +00:00
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", proxyPort))
|
2014-06-06 23:40:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error connecting to proxy: %v", err)
|
|
|
|
}
|
2014-07-30 06:24:57 +00:00
|
|
|
magic := "aaaaa"
|
|
|
|
if _, err := conn.Write([]byte(magic)); err != nil {
|
|
|
|
t.Fatalf("error writing to proxy: %v", err)
|
2014-07-29 12:15:43 +00:00
|
|
|
}
|
2014-07-30 06:24:57 +00:00
|
|
|
buf := make([]byte, 5)
|
|
|
|
if _, err := conn.Read(buf); err != nil {
|
|
|
|
t.Fatalf("error reading from proxy: %v", err)
|
2014-07-29 12:15:43 +00:00
|
|
|
}
|
2014-07-30 06:24:57 +00:00
|
|
|
if string(buf) != magic {
|
|
|
|
t.Fatalf("bad echo from proxy: got: %q, expected %q", string(buf), magic)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|