k3s/pkg/agent/loadbalancer/loadbalancer_test.go

175 lines
3.3 KiB
Go
Raw Normal View History

2019-07-24 07:22:31 +00:00
package loadbalancer
import (
"bufio"
"context"
2019-07-24 07:22:31 +00:00
"fmt"
"net"
"net/url"
"strings"
"testing"
"time"
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/sirupsen/logrus"
2019-07-24 07:22:31 +00:00
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
}
type testServer struct {
2019-07-24 07:22:31 +00:00
listener net.Listener
conns []net.Conn
prefix string
}
func createServer(prefix string) (*testServer, error) {
2019-07-24 07:22:31 +00:00
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
s := &testServer{
2019-07-24 07:22:31 +00:00
prefix: prefix,
listener: listener,
}
go s.serve()
return s, nil
}
func (s *testServer) serve() {
2019-07-24 07:22:31 +00:00
for {
conn, err := s.listener.Accept()
if err != nil {
return
}
s.conns = append(s.conns, conn)
go s.echo(conn)
}
}
func (s *testServer) close() {
2019-07-24 07:22:31 +00:00
s.listener.Close()
for _, conn := range s.conns {
conn.Close()
}
}
func (s *testServer) echo(conn net.Conn) {
2019-07-24 07:22:31 +00:00
for {
result, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return
}
conn.Write([]byte(s.prefix + ":" + result))
}
}
func ping(conn net.Conn) (string, error) {
fmt.Fprintf(conn, "ping\n")
result, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(result), nil
}
func Test_UnitFailOver(t *testing.T) {
tmpDir := t.TempDir()
2019-07-24 07:22:31 +00:00
ogServe, err := createServer("og")
if err != nil {
t.Fatalf("createServer(og) failed: %v", err)
2019-07-24 07:22:31 +00:00
}
lbServe, err := createServer("lb")
if err != nil {
t.Fatalf("createServer(lb) failed: %v", err)
2019-07-24 07:22:31 +00:00
}
cfg := cmds.Agent{
ServerURL: fmt.Sprintf("http://%s/", ogServe.listener.Addr().String()),
DataDir: tmpDir,
}
lb, err := New(context.TODO(), cfg.DataDir, SupervisorServiceName, cfg.ServerURL, RandomPort, false)
2019-07-24 07:22:31 +00:00
if err != nil {
t.Fatalf("New() failed: %v", err)
2019-07-24 07:22:31 +00:00
}
parsedURL, err := url.Parse(lb.LoadBalancerServerURL())
if err != nil {
t.Fatalf("url.Parse failed: %v", err)
2019-07-24 07:22:31 +00:00
}
localAddress := parsedURL.Host
lb.Update([]string{lbServe.listener.Addr().String()})
conn1, err := net.Dial("tcp", localAddress)
if err != nil {
t.Fatalf("net.Dial failed: %v", err)
2019-07-24 07:22:31 +00:00
}
result1, err := ping(conn1)
if err != nil {
t.Fatalf("ping(conn1) failed: %v", err)
}
if result1 != "lb:ping" {
t.Fatalf("Unexpected ping result: %v", result1)
2019-07-24 07:22:31 +00:00
}
lbServe.close()
_, err = ping(conn1)
if err == nil {
t.Fatal("Unexpected successful ping on closed connection conn1")
}
2019-07-24 07:22:31 +00:00
conn2, err := net.Dial("tcp", localAddress)
if err != nil {
t.Fatalf("net.Dial failed: %v", err)
2019-07-24 07:22:31 +00:00
}
result2, err := ping(conn2)
if err != nil {
t.Fatalf("ping(conn2) failed: %v", err)
}
if result2 != "og:ping" {
t.Fatalf("Unexpected ping result: %v", result2)
2019-07-24 07:22:31 +00:00
}
}
func Test_UnitFailFast(t *testing.T) {
tmpDir := t.TempDir()
2019-07-24 07:22:31 +00:00
cfg := cmds.Agent{
2020-10-24 00:03:51 +00:00
ServerURL: "http://127.0.0.1:0/",
2019-07-24 07:22:31 +00:00
DataDir: tmpDir,
}
lb, err := New(context.TODO(), cfg.DataDir, SupervisorServiceName, cfg.ServerURL, RandomPort, false)
2019-07-24 07:22:31 +00:00
if err != nil {
t.Fatalf("New() failed: %v", err)
2019-07-24 07:22:31 +00:00
}
conn, err := net.Dial("tcp", lb.localAddress)
if err != nil {
t.Fatalf("net.Dial failed: %v", err)
2019-07-24 07:22:31 +00:00
}
done := make(chan error)
go func() {
_, err = ping(conn)
done <- err
}()
timeout := time.After(10 * time.Millisecond)
select {
case err := <-done:
if err == nil {
t.Fatal("Unexpected successful ping from invalid address")
}
2019-07-24 07:22:31 +00:00
case <-timeout:
t.Fatal("Test timed out")
2019-07-24 07:22:31 +00:00
}
}