2019-07-24 07:22:31 +00:00
|
|
|
package loadbalancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-03-08 22:10:00 +00:00
|
|
|
"context"
|
2019-07-24 07:22:31 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/cli/cmds"
|
2023-04-03 20:49:01 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-07-24 07:22:31 +00:00
|
|
|
)
|
|
|
|
|
2023-04-03 20:49:01 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
type testServer struct {
|
2019-07-24 07:22:31 +00:00
|
|
|
listener net.Listener
|
|
|
|
conns []net.Conn
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
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
|
|
|
|
}
|
2023-03-31 20:51:27 +00:00
|
|
|
s := &testServer{
|
2019-07-24 07:22:31 +00:00
|
|
|
prefix: prefix,
|
|
|
|
listener: listener,
|
|
|
|
}
|
|
|
|
go s.serve()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
func (s *testServer) close() {
|
2019-07-24 07:22:31 +00:00
|
|
|
s.listener.Close()
|
|
|
|
for _, conn := range s.conns {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:51:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitFailOver(t *testing.T) {
|
2023-04-03 20:49:01 +00:00
|
|
|
tmpDir := t.TempDir()
|
2019-07-24 07:22:31 +00:00
|
|
|
|
|
|
|
ogServe, err := createServer("og")
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatalf("createServer(og) failed: %v", err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lbServe, err := createServer("lb")
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:49:30 +00:00
|
|
|
lb, err := New(context.TODO(), cfg.DataDir, SupervisorServiceName, cfg.ServerURL, RandomPort, false)
|
2019-07-24 07:22:31 +00:00
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatalf("New() failed: %v", err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsedURL, err := url.Parse(lb.LoadBalancerServerURL())
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
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 {
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatalf("net.Dial failed: %v", err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
result1, err := ping(conn1)
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
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)
|
2023-04-03 20:49:01 +00:00
|
|
|
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 {
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatalf("net.Dial failed: %v", err)
|
|
|
|
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
result2, err := ping(conn2)
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:59:33 +00:00
|
|
|
func Test_UnitFailFast(t *testing.T) {
|
2023-04-03 20:49:01 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2022-03-31 09:49:30 +00:00
|
|
|
lb, err := New(context.TODO(), cfg.DataDir, SupervisorServiceName, cfg.ServerURL, RandomPort, false)
|
2019-07-24 07:22:31 +00:00
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatalf("New() failed: %v", err)
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", lb.localAddress)
|
|
|
|
if err != nil {
|
2023-04-03 20:49:01 +00:00
|
|
|
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:
|
2023-04-03 20:49:01 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected successful ping from invalid address")
|
|
|
|
}
|
2019-07-24 07:22:31 +00:00
|
|
|
case <-timeout:
|
2023-04-03 20:49:01 +00:00
|
|
|
t.Fatal("Test timed out")
|
2019-07-24 07:22:31 +00:00
|
|
|
}
|
|
|
|
}
|