Load balancers
router.loadBalancers: services on several hosts (HAProxy) with HTTP checks, TLS terminated on the router or passed through, the client in X-Forwarded-For or the PROXY protocol, host names that choose a service on a shared port and are published in the local DNS. A status page lists the backends.
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// What `show stat` says for two services (columns cut down to the ones that matter).
|
||||
const sampleStat = `# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,addr
|
||||
fe_8443,FRONTEND,,,0,5,,12,0,0,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,1,,,,,
|
||||
be_api,hv1,0,0,2,3,,7,0,0,,0,,0,0,0,0,UP,1,1,0,0,0,300,0,,1,3,1,,7,,2,0,,1,L7OK,200,3,
|
||||
be_api,hv2,0,0,0,0,,5,0,0,,0,,0,0,0,0,DOWN,1,1,0,3,1,40,40,,1,3,2,,5,,2,0,,1,L7STS,503,2,
|
||||
be_api,BACKEND,0,0,2,3,,12,0,0,0,0,,0,0,0,0,UP,1,1,0,,0,300,0,,1,3,0,,12,,1,0,,1,,,,
|
||||
be_raw,hv1,0,0,0,0,,0,0,0,,0,,0,0,0,0,DOWN,1,1,0,5,1,10,10,,1,4,1,,0,,2,0,,0,L4CON,,0,
|
||||
be_raw,BACKEND,0,0,0,0,,0,0,0,0,0,,0,0,0,0,DOWN,1,1,0,,1,10,10,,1,4,0,,0,,1,0,,0,,,,
|
||||
`
|
||||
|
||||
func TestParseHAProxyStat(t *testing.T) {
|
||||
got, err := parseHAProxyStat(sampleStat)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
api := got["api"]
|
||||
if len(api) != 3 || api[0].Name != "hv1" || !api[0].Up || api[0].Address != "" ||
|
||||
api[0].Check != "L7OK/200" || api[0].Current != 2 || api[0].Total != 7 || api[0].Since != 300 {
|
||||
t.Fatalf("api: %+v", api)
|
||||
}
|
||||
if api[1].Up || api[1].Status != "DOWN" || api[1].Check != "L7STS/503" {
|
||||
t.Fatalf("a down backend: %+v", api[1])
|
||||
}
|
||||
if _, ok := got["8443"]; ok || len(got["raw"]) != 2 {
|
||||
t.Fatalf("frontends or raw: %+v", got)
|
||||
}
|
||||
if _, err := parseHAProxyStat("# a,b\n1,2\n"); err == nil {
|
||||
t.Error("statistics without the columns were accepted")
|
||||
}
|
||||
if _, err := parseHAProxyStat(""); err == nil {
|
||||
t.Error("nothing was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// The addresses are not in the CSV; `show servers state` has them.
|
||||
const sampleState = `1
|
||||
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_port
|
||||
3 be_api 1 hv1 192.168.10.21 2 8443
|
||||
3 be_api 2 hv2 192.168.10.22 0 8443
|
||||
4 be_raw 1 hv1 192.168.10.21 0 8443
|
||||
`
|
||||
|
||||
func TestParseServerAddresses(t *testing.T) {
|
||||
got := parseServerAddresses(sampleState)
|
||||
if got["be_api/hv2"] != "192.168.10.22:8443" || got["be_raw/hv1"] != "192.168.10.21:8443" || len(got) != 3 {
|
||||
t.Fatalf("%v", got)
|
||||
}
|
||||
if len(parseServerAddresses("")) != 0 || len(parseServerAddresses("1\n3 be_api 1 hv1 x\n")) != 0 {
|
||||
t.Error("addresses out of nothing")
|
||||
}
|
||||
}
|
||||
|
||||
// serveStat answers `show stat` (and `show servers state`) on a socket, like HAProxy.
|
||||
func serveStat(t *testing.T, path, answer string) {
|
||||
t.Helper()
|
||||
l, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { l.Close() })
|
||||
go func() {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
buf := make([]byte, 64)
|
||||
n, _ := c.Read(buf)
|
||||
if strings.HasPrefix(string(buf[:n]), "show servers state") {
|
||||
c.Write([]byte(sampleState))
|
||||
} else {
|
||||
c.Write([]byte(answer))
|
||||
}
|
||||
c.Close()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TestLoadBalancersPage(t *testing.T) {
|
||||
a, sys := newTestApp(t)
|
||||
// (A short path: a unix socket's name is limited.)
|
||||
sys.HAProxySocket = filepath.Join(os.TempDir(), "lb-test-"+filepath.Base(t.TempDir())+".sock")
|
||||
a.sys = sys
|
||||
h := a.routes()
|
||||
cookie := login(t, h)
|
||||
menu := do(h, "GET", "/", nil, cookie).Body.String()
|
||||
if !strings.Contains(menu, `href="/loadbalancers"`) {
|
||||
t.Fatalf("no menu entry:\n%s", menu)
|
||||
}
|
||||
|
||||
// Nothing configured: the page says how.
|
||||
body := do(h, "GET", "/loadbalancers", nil, cookie).Body.String()
|
||||
if !strings.Contains(body, "No load balancer is set up") || !strings.Contains(body, "router.loadBalancers") {
|
||||
t.Fatalf("empty page:\n%s", body)
|
||||
}
|
||||
|
||||
settings := `{"system":{"hostName":"router","stateVersion":"26.11"},"router":{"loadBalancers":{
|
||||
"api":{"listen":{"port":8443,"zones":["wan","lan"]},"hosts":["api.example.org"],"backends":["hv1","hv2"]},
|
||||
"raw":{"listen":{"port":9443},"tls":"passthrough","backends":["hv1"]}}}}`
|
||||
os.WriteFile(sys.settingsPath(), []byte(settings), 0o600)
|
||||
|
||||
// Saved, HAProxy not running: say so, show the configuration.
|
||||
body = do(h, "GET", "/loadbalancers", nil, cookie).Body.String()
|
||||
for _, want := range []string{"HAProxy is not running", "<h2>api", "port 8443 in wan, lan", "api.example.org", "<h2>raw", "passed through", "roundrobin"} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("without HAProxy the page lacks %q:\n%s", want, body)
|
||||
}
|
||||
}
|
||||
if strings.Contains(body, `class="badge`) {
|
||||
t.Errorf("a status badge without knowing:\n%s", body)
|
||||
}
|
||||
|
||||
// Running: one service up (one backend down), one down.
|
||||
serveStat(t, sys.HAProxySocket, sampleStat)
|
||||
body = do(h, "GET", "/loadbalancers", nil, cookie).Body.String()
|
||||
for _, want := range []string{"192.168.10.21:8443", "L7OK/200", "L7STS/503", "badge ok", "badge bad", "7 in all", "300 s", `data-live="10"`} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("the page lacks %q:\n%s", want, body)
|
||||
}
|
||||
}
|
||||
if strings.Contains(body, "HAProxy is not running") {
|
||||
t.Errorf("claims HAProxy is down:\n%s", body)
|
||||
}
|
||||
|
||||
// A service HAProxy does not know yet is "not applied".
|
||||
os.WriteFile(sys.settingsPath(), []byte(strings.Replace(settings, `"raw"`, `"new"`, 1)), 0o600)
|
||||
if body = do(h, "GET", "/loadbalancers", nil, cookie).Body.String(); !strings.Contains(body, `badge">not applied`) {
|
||||
t.Errorf("a new service is not marked:\n%s", body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user