2020-06-24 16:40:16 +00:00
|
|
|
package leakcheck
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-29 21:36:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
|
2020-06-24 16:40:16 +00:00
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testTLSCertificates(serverName string) (cert string, key string, cacert string, err error) {
|
2021-03-22 09:16:41 +00:00
|
|
|
signer, _, err := tlsutil.GeneratePrivateKey()
|
2020-06-24 16:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:16:41 +00:00
|
|
|
ca, _, err := tlsutil.GenerateCA(tlsutil.CAOpts{Signer: signer})
|
2020-06-24 16:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
2021-03-22 09:16:41 +00:00
|
|
|
cert, privateKey, err := tlsutil.GenerateCert(tlsutil.CertOpts{
|
|
|
|
Signer: signer,
|
|
|
|
CA: ca,
|
|
|
|
Name: "Test Cert Name",
|
|
|
|
Days: 365,
|
|
|
|
DNSNames: []string{serverName},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
|
|
|
|
})
|
2020-06-24 16:40:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert, privateKey, ca, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupPrimaryServer(t *testing.T) *agent.TestAgent {
|
|
|
|
d := testutil.TempDir(t, "leaks-primary-server")
|
|
|
|
|
|
|
|
certPEM, keyPEM, caPEM, err := testTLSCertificates("server.primary.consul")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
certPath := filepath.Join(d, "cert.pem")
|
|
|
|
keyPath := filepath.Join(d, "key.pem")
|
|
|
|
caPath := filepath.Join(d, "cacert.pem")
|
|
|
|
|
|
|
|
require.NoError(t, ioutil.WriteFile(certPath, []byte(certPEM), 0600))
|
|
|
|
require.NoError(t, ioutil.WriteFile(keyPath, []byte(keyPEM), 0600))
|
|
|
|
require.NoError(t, ioutil.WriteFile(caPath, []byte(caPEM), 0600))
|
|
|
|
|
2021-12-07 12:48:50 +00:00
|
|
|
aclParams := agent.DefaultTestACLConfigParams()
|
2020-06-24 16:40:16 +00:00
|
|
|
aclParams.PrimaryDatacenter = "primary"
|
|
|
|
aclParams.EnableTokenReplication = true
|
|
|
|
|
|
|
|
config := `
|
|
|
|
server = true
|
|
|
|
datacenter = "primary"
|
|
|
|
primary_datacenter = "primary"
|
|
|
|
|
|
|
|
connect {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_encrypt {
|
|
|
|
allow_tls = true
|
|
|
|
}
|
|
|
|
` + agent.TestACLConfigWithParams(aclParams)
|
|
|
|
|
|
|
|
a := agent.NewTestAgent(t, config)
|
|
|
|
t.Cleanup(func() { a.Shutdown() })
|
|
|
|
|
2021-12-07 12:48:50 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "primary", testrpc.WithToken(agent.TestDefaultInitialManagementToken))
|
2020-06-24 16:40:16 +00:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:50:03 +00:00
|
|
|
func TestAgentLeaks_Server(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:40:16 +00:00
|
|
|
/*
|
|
|
|
Eventually go routine leak checking should be moved into other packages such as the agent
|
|
|
|
and agent/consul packages. However there are too many leaks for the test to run properly.
|
|
|
|
|
|
|
|
Many of the leaks are due to blocking queries from clients to servers being uncancellable.
|
|
|
|
Until we can move away from net/rpc and fix some of the other issues we don't want a
|
|
|
|
completely unbounded test which is guaranteed to fail 100% of the time. For now this
|
|
|
|
test will do. When we do update it we should add this in a *_test.go file in the packages
|
|
|
|
that we want to enable leak checking within:
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
goleak.IgnoreTopFunction("k8s.io/klog.(*loggingT).flushDaemon"),
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
goleak.IgnoreTopFunction("github.com/hashicorp/consul/sdk/freeport.checkFreedPorts"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
defer goleak.VerifyNone(t,
|
|
|
|
goleak.IgnoreTopFunction("k8s.io/klog.(*loggingT).flushDaemon"),
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
goleak.IgnoreTopFunction("github.com/hashicorp/consul/sdk/freeport.checkFreedPorts"),
|
|
|
|
)
|
|
|
|
|
|
|
|
primaryServer := setupPrimaryServer(t)
|
|
|
|
primaryServer.Shutdown()
|
|
|
|
}
|