Files
teleport/lib/utils/loadbalancer_test.go
T
Andrew Lytvynov bdd388e0d0 Fix remaining staticcheck findings in lib/...
Fixed findings:
```
lib/sshutils/server_test.go:163:2: SA4006: this value of `clt` is never used (staticcheck)
	clt, err := ssh.Dial("tcp", srv.Addr(), &cc)
	^
lib/sshutils/server_test.go:91:3: SA5001: should check returned error before deferring ch.Close() (staticcheck)
		defer ch.Close()
		^
lib/shell/shell_test.go:33:2: SA4006: this value of `shell` is never used (staticcheck)
	shell, err = GetLoginShell("non-existent-user")
	^
lib/cgroup/cgroup_test.go:111:2: SA9003: empty branch (staticcheck)
	if err != nil {
	^
lib/cgroup/cgroup_test.go:119:2: SA5001: should check returned error before deferring service.Close() (staticcheck)
	defer service.Close()
	^
lib/client/keystore_test.go:138:2: SA4006: this value of `keyCopy` is never used (staticcheck)
	keyCopy, err = s.store.GetKey("host.a", "bob")
	^
lib/client/api.go:1604:3: SA4004: the surrounding loop is unconditionally terminated (staticcheck)
		return makeProxyClient(sshClient, m), nil
		^
lib/backend/test/suite.go:156:2: SA4006: this value of `err` is never used (staticcheck)
	result, err = s.B.GetRange(ctx, prefix("/prefix/c/c1"), backend.RangeEnd(prefix("/prefix/c/cz")), backend.NoLimit)
	^
lib/utils/timeout_test.go:84:2: SA1019: t.Dial is deprecated: Use DialContext instead, which allows the transport to cancel dials as soon as they are no longer needed. If both are set, DialContext takes priority.  (staticcheck)
	t.Dial = func(network string, addr string) (net.Conn, error) {
	^
lib/utils/websocketwriter.go:83:3: SA4006: this value of `err` is never used (staticcheck)
		utf8, err = w.encoder.String(string(data))
		^
lib/utils/loadbalancer_test.go:134:2: SA4006: this value of `out` is never used (staticcheck)
	out, err = Roundtrip(frontend.String())
	^
lib/utils/loadbalancer_test.go:209:2: SA4006: this value of `out` is never used (staticcheck)
	out, err = RoundtripWithConn(conn)
	^
lib/srv/forward/sshserver.go:582:3: SA4004: the surrounding loop is unconditionally terminated (staticcheck)
		return
		^
lib/service/service.go:347:4: SA4006: this value of `err` is never used (staticcheck)
			i, err = auth.GenerateIdentity(process.localAuth, id, principals, dnsNames)
			^
lib/service/signals.go:60:3: SA1016: syscall.SIGKILL cannot be trapped (did you mean syscall.SIGTERM?) (staticcheck)
		syscall.SIGKILL, // fast shutdown
		^
lib/config/configuration_test.go:184:2: SA4006: this value of `conf` is never used (staticcheck)
	conf, err = ReadFromFile(s.configFileBadContent)
	^
lib/config/configuration.go:129:2: SA5001: should check returned error before deferring reader.Close() (staticcheck)
	defer reader.Close()
	^
lib/kube/kubeconfig/kubeconfig_test.go:227:2: SA4006: this value of `err` is never used (staticcheck)
	tlsCert, err := ca.GenerateCertificate(tlsca.CertificateRequest{
	^
lib/srv/sess.go:720:3: SA4006: this value of `err` is never used (staticcheck)
		result, err := s.term.Wait()
		^
lib/multiplexer/multiplexer_test.go:169:11: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
	_, err = fmt.Fprintf(conn, proxyLine.String())
	        ^
lib/multiplexer/multiplexer_test.go:221:11: SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
	_, err = fmt.Fprintf(conn, proxyLine.String())
	        ^
```
2020-04-28 15:17:44 +00:00

224 lines
5.5 KiB
Go

/*
Copyright 2017 Gravitational, Inc.
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 utils
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/url"
"gopkg.in/check.v1"
)
type LBSuite struct {
}
var _ = check.Suite(&LBSuite{})
func (s *LBSuite) SetUpSuite(c *check.C) {
InitLoggerForTests()
}
func (s *LBSuite) TestSingleBackendLB(c *check.C) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 1")
}))
defer backend1.Close()
ports, err := GetFreeTCPPorts(1)
c.Assert(err, check.IsNil)
frontend := localAddr(ports[0])
lb, err := NewLoadBalancer(context.TODO(), frontend, urlToNetAddr(backend1.URL))
c.Assert(err, check.IsNil)
err = lb.Listen()
c.Assert(err, check.IsNil)
go lb.Serve()
defer lb.Close()
out, err := Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
}
func (s *LBSuite) TestTwoBackendsLB(c *check.C) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 1")
}))
defer backend1.Close()
backend2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 2")
}))
defer backend2.Close()
backend1Addr, backend2Addr := urlToNetAddr(backend1.URL), urlToNetAddr(backend2.URL)
ports, err := GetFreeTCPPorts(1)
c.Assert(err, check.IsNil)
frontend := localAddr(ports[0])
lb, err := NewLoadBalancer(context.TODO(), frontend)
c.Assert(err, check.IsNil)
err = lb.Listen()
c.Assert(err, check.IsNil)
go lb.Serve()
defer lb.Close()
// no endpoints
_, err = Roundtrip(frontend.String())
c.Assert(err, check.NotNil)
lb.AddBackend(backend1Addr)
out, err := Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
lb.AddBackend(backend2Addr)
out, err = Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 2")
}
func (s *LBSuite) TestOneFailingBackend(c *check.C) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 1")
}))
defer backend1.Close()
backend2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 2")
}))
backend2.Close()
backend1Addr, backend2Addr := urlToNetAddr(backend1.URL), urlToNetAddr(backend2.URL)
ports, err := GetFreeTCPPorts(1)
c.Assert(err, check.IsNil)
frontend := localAddr(ports[0])
lb, err := NewLoadBalancer(context.TODO(), frontend)
c.Assert(err, check.IsNil)
err = lb.Listen()
c.Assert(err, check.IsNil)
go lb.Serve()
defer lb.Close()
lb.AddBackend(backend1Addr)
lb.AddBackend(backend2Addr)
out, err := Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
_, err = Roundtrip(frontend.String())
c.Assert(err, check.NotNil)
out, err = Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
}
func (s *LBSuite) TestClose(c *check.C) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 1")
}))
defer backend1.Close()
ports, err := GetFreeTCPPorts(1)
c.Assert(err, check.IsNil)
frontend := localAddr(ports[0])
lb, err := NewLoadBalancer(context.TODO(), frontend, urlToNetAddr(backend1.URL))
c.Assert(err, check.IsNil)
err = lb.Listen()
c.Assert(err, check.IsNil)
go lb.Serve()
defer lb.Close()
out, err := Roundtrip(frontend.String())
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
lb.Close()
// second close works
lb.Close()
lb.Wait()
// requests are failing
out, err = Roundtrip(frontend.String())
c.Assert(err, check.NotNil, check.Commentf("output: %v, err: %v", string(out), err))
}
func (s *LBSuite) TestDropConnections(c *check.C) {
backend1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "backend 1")
}))
defer backend1.Close()
ports, err := GetFreeTCPPorts(1)
c.Assert(err, check.IsNil)
frontend := localAddr(ports[0])
backendAddr := urlToNetAddr(backend1.URL)
lb, err := NewLoadBalancer(context.TODO(), frontend, backendAddr)
c.Assert(err, check.IsNil)
err = lb.Listen()
c.Assert(err, check.IsNil)
go lb.Serve()
defer lb.Close()
conn, err := net.Dial("tcp", frontend.String())
c.Assert(err, check.IsNil)
defer conn.Close()
out, err := RoundtripWithConn(conn)
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
// to make sure multiple requests work on the same wire
out, err = RoundtripWithConn(conn)
c.Assert(err, check.IsNil)
c.Assert(out, check.Equals, "backend 1")
// removing backend results in dropped connection to this backend
lb.RemoveBackend(backendAddr)
_, err = RoundtripWithConn(conn)
c.Assert(err, check.NotNil)
}
func urlToNetAddr(u string) NetAddr {
parsed, err := url.Parse(u)
if err != nil {
panic(err)
}
return *MustParseAddr(parsed.Host)
}
func localAddr(port string) NetAddr {
return *MustParseAddr(fmt.Sprintf("127.0.0.1:%v", port))
}