You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
k3s/pkg/agent/loadbalancer/loadbalancer_test.go

175 lines
3.3 KiB

5 years ago
package loadbalancer
import (
"bufio"
"context"
5 years ago
"fmt"
"net"
"net/url"
"strings"
"testing"
"time"
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/sirupsen/logrus"
5 years ago
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
}
type testServer struct {
5 years ago
listener net.Listener
conns []net.Conn
prefix string
}
func createServer(prefix string) (*testServer, error) {
5 years ago
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, err
}
s := &testServer{
5 years ago
prefix: prefix,
listener: listener,
}
go s.serve()
return s, nil
}
func (s *testServer) serve() {
5 years ago
for {
conn, err := s.listener.Accept()
if err != nil {
return
}
s.conns = append(s.conns, conn)
go s.echo(conn)
}
}
func (s *testServer) close() {
5 years ago
s.listener.Close()
for _, conn := range s.conns {
conn.Close()
}
}
func (s *testServer) echo(conn net.Conn) {
5 years ago
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()
5 years ago
ogServe, err := createServer("og")
if err != nil {
t.Fatalf("createServer(og) failed: %v", err)
5 years ago
}
lbServe, err := createServer("lb")
if err != nil {
t.Fatalf("createServer(lb) failed: %v", err)
5 years ago
}
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)
5 years ago
if err != nil {
t.Fatalf("New() failed: %v", err)
5 years ago
}
parsedURL, err := url.Parse(lb.LoadBalancerServerURL())
if err != nil {
t.Fatalf("url.Parse failed: %v", err)
5 years ago
}
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)
5 years ago
}
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)
5 years ago
}
lbServe.close()
_, err = ping(conn1)
if err == nil {
t.Fatal("Unexpected successful ping on closed connection conn1")
}
5 years ago
conn2, err := net.Dial("tcp", localAddress)
if err != nil {
t.Fatalf("net.Dial failed: %v", err)
5 years ago
}
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)
5 years ago
}
}
func Test_UnitFailFast(t *testing.T) {
tmpDir := t.TempDir()
5 years ago
cfg := cmds.Agent{
ServerURL: "http://127.0.0.1:0/",
5 years ago
DataDir: tmpDir,
}
lb, err := New(context.TODO(), cfg.DataDir, SupervisorServiceName, cfg.ServerURL, RandomPort, false)
5 years ago
if err != nil {
t.Fatalf("New() failed: %v", err)
5 years ago
}
conn, err := net.Dial("tcp", lb.localAddress)
if err != nil {
t.Fatalf("net.Dial failed: %v", err)
5 years ago
}
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")
}
5 years ago
case <-timeout:
t.Fatal("Test timed out")
5 years ago
}
}