mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 22:06:20 +00:00
testing: Deprecate functions for creating a server.
These helper functions actually end up hiding important setup details that should be visible from the test case. We already have a convenient way of setting this config when calling newTestServerWithConfig.
This commit is contained in:
parent
c9a992f5e8
commit
0a9cb62859
@ -6,12 +6,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
||||||
"github.com/hashicorp/consul/testrpc"
|
|
||||||
"github.com/hashicorp/raft"
|
"github.com/hashicorp/raft"
|
||||||
"github.com/hashicorp/serf/serf"
|
"github.com/hashicorp/serf/serf"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
||||||
|
"github.com/hashicorp/consul/testrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAutopilot_IdempotentShutdown(t *testing.T) {
|
func TestAutopilot_IdempotentShutdown(t *testing.T) {
|
||||||
@ -19,7 +20,7 @@ func TestAutopilot_IdempotentShutdown(t *testing.T) {
|
|||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
}
|
}
|
||||||
|
|
||||||
dir1, s1 := testServerWithConfig(t, nil)
|
dir1, s1 := testServerWithConfig(t)
|
||||||
defer os.RemoveAll(dir1)
|
defer os.RemoveAll(dir1)
|
||||||
defer s1.Shutdown()
|
defer s1.Shutdown()
|
||||||
retry.Run(t, func(r *retry.R) { r.Check(waitForLeader(s1)) })
|
retry.Run(t, func(r *retry.R) { r.Check(waitForLeader(s1)) })
|
||||||
|
@ -1599,7 +1599,7 @@ func TestIntentionList_acl(t *testing.T) {
|
|||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
dir1, s1 := testServerWithConfig(t, testServerACLConfig(nil))
|
dir1, s1 := testServerWithConfig(t, testServerACLConfig)
|
||||||
defer os.RemoveAll(dir1)
|
defer os.RemoveAll(dir1)
|
||||||
defer s1.Shutdown()
|
defer s1.Shutdown()
|
||||||
codec := rpcClient(t, s1)
|
codec := rpcClient(t, s1)
|
||||||
|
@ -1784,7 +1784,7 @@ func TestInternal_GatewayIntentions_aclDeny(t *testing.T) {
|
|||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
}
|
}
|
||||||
|
|
||||||
dir1, s1 := testServerWithConfig(t, testServerACLConfig(nil))
|
dir1, s1 := testServerWithConfig(t, testServerACLConfig)
|
||||||
defer os.RemoveAll(dir1)
|
defer os.RemoveAll(dir1)
|
||||||
defer s1.Shutdown()
|
defer s1.Shutdown()
|
||||||
codec := rpcClient(t, s1)
|
codec := rpcClient(t, s1)
|
||||||
|
@ -66,21 +66,12 @@ func testTLSCertificates(serverName string) (cert string, key string, cacert str
|
|||||||
return cert, privateKey, ca, nil
|
return cert, privateKey, ca, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// testServerACLConfig wraps another arbitrary Config altering callback
|
// testServerACLConfig setup some common ACL configurations.
|
||||||
// to setup some common ACL configurations. A new callback func will
|
func testServerACLConfig(c *Config) {
|
||||||
// be returned that has the original callback invoked after setting
|
c.PrimaryDatacenter = "dc1"
|
||||||
// up all of the ACL configurations (so they can still be overridden)
|
c.ACLsEnabled = true
|
||||||
func testServerACLConfig(cb func(*Config)) func(*Config) {
|
c.ACLInitialManagementToken = TestDefaultMasterToken
|
||||||
return func(c *Config) {
|
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
||||||
c.PrimaryDatacenter = "dc1"
|
|
||||||
c.ACLsEnabled = true
|
|
||||||
c.ACLInitialManagementToken = TestDefaultMasterToken
|
|
||||||
c.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
|
||||||
|
|
||||||
if cb != nil {
|
|
||||||
cb(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureTLS(config *Config) {
|
func configureTLS(config *Config) {
|
||||||
@ -185,14 +176,12 @@ func testServerConfig(t *testing.T) (string, *Config) {
|
|||||||
return dir, config
|
return dir, config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use testServerWithConfig instead. It does the same thing and more.
|
||||||
func testServer(t *testing.T) (string, *Server) {
|
func testServer(t *testing.T) (string, *Server) {
|
||||||
return testServerWithConfig(t, func(c *Config) {
|
return testServerWithConfig(t)
|
||||||
c.Datacenter = "dc1"
|
|
||||||
c.PrimaryDatacenter = "dc1"
|
|
||||||
c.Bootstrap = true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use testServerWithConfig
|
||||||
func testServerDC(t *testing.T, dc string) (string, *Server) {
|
func testServerDC(t *testing.T, dc string) (string, *Server) {
|
||||||
return testServerWithConfig(t, func(c *Config) {
|
return testServerWithConfig(t, func(c *Config) {
|
||||||
c.Datacenter = dc
|
c.Datacenter = dc
|
||||||
@ -200,6 +189,7 @@ func testServerDC(t *testing.T, dc string) (string, *Server) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use testServerWithConfig
|
||||||
func testServerDCBootstrap(t *testing.T, dc string, bootstrap bool) (string, *Server) {
|
func testServerDCBootstrap(t *testing.T, dc string, bootstrap bool) (string, *Server) {
|
||||||
return testServerWithConfig(t, func(c *Config) {
|
return testServerWithConfig(t, func(c *Config) {
|
||||||
c.Datacenter = dc
|
c.Datacenter = dc
|
||||||
@ -208,6 +198,7 @@ func testServerDCBootstrap(t *testing.T, dc string, bootstrap bool) (string, *Se
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use testServerWithConfig
|
||||||
func testServerDCExpect(t *testing.T, dc string, expect int) (string, *Server) {
|
func testServerDCExpect(t *testing.T, dc string, expect int) (string, *Server) {
|
||||||
return testServerWithConfig(t, func(c *Config) {
|
return testServerWithConfig(t, func(c *Config) {
|
||||||
c.Datacenter = dc
|
c.Datacenter = dc
|
||||||
@ -216,16 +207,7 @@ func testServerDCExpect(t *testing.T, dc string, expect int) (string, *Server) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testServerDCExpectNonVoter(t *testing.T, dc string, expect int) (string, *Server) {
|
func testServerWithConfig(t *testing.T, configOpts ...func(*Config)) (string, *Server) {
|
||||||
return testServerWithConfig(t, func(c *Config) {
|
|
||||||
c.Datacenter = dc
|
|
||||||
c.Bootstrap = false
|
|
||||||
c.BootstrapExpect = expect
|
|
||||||
c.ReadReplica = true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) {
|
|
||||||
var dir string
|
var dir string
|
||||||
var srv *Server
|
var srv *Server
|
||||||
|
|
||||||
@ -233,8 +215,8 @@ func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) {
|
|||||||
retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) {
|
retry.RunWith(retry.ThreeTimes(), t, func(r *retry.R) {
|
||||||
var config *Config
|
var config *Config
|
||||||
dir, config = testServerConfig(t)
|
dir, config = testServerConfig(t)
|
||||||
if cb != nil {
|
for _, fn := range configOpts {
|
||||||
cb(config)
|
fn(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply config to copied fields because many tests only set the old
|
// Apply config to copied fields because many tests only set the old
|
||||||
@ -255,8 +237,11 @@ func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) {
|
|||||||
|
|
||||||
// cb is a function that can alter the test servers configuration prior to the server starting.
|
// cb is a function that can alter the test servers configuration prior to the server starting.
|
||||||
func testACLServerWithConfig(t *testing.T, cb func(*Config), initReplicationToken bool) (string, *Server, rpc.ClientCodec) {
|
func testACLServerWithConfig(t *testing.T, cb func(*Config), initReplicationToken bool) (string, *Server, rpc.ClientCodec) {
|
||||||
dir, srv := testServerWithConfig(t, testServerACLConfig(cb))
|
opts := []func(*Config){testServerACLConfig}
|
||||||
t.Cleanup(func() { srv.Shutdown() })
|
if cb != nil {
|
||||||
|
opts = append(opts, cb)
|
||||||
|
}
|
||||||
|
dir, srv := testServerWithConfig(t, opts...)
|
||||||
|
|
||||||
if initReplicationToken {
|
if initReplicationToken {
|
||||||
// setup some tokens here so we get less warnings in the logs
|
// setup some tokens here so we get less warnings in the logs
|
||||||
@ -264,7 +249,6 @@ func testACLServerWithConfig(t *testing.T, cb func(*Config), initReplicationToke
|
|||||||
}
|
}
|
||||||
|
|
||||||
codec := rpcClient(t, srv)
|
codec := rpcClient(t, srv)
|
||||||
t.Cleanup(func() { codec.Close() })
|
|
||||||
return dir, srv, codec
|
return dir, srv, codec
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1282,7 +1266,11 @@ func TestServer_Expect_NonVoters(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
dir1, s1 := testServerDCExpectNonVoter(t, "dc1", 2)
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
||||||
|
c.Bootstrap = false
|
||||||
|
c.BootstrapExpect = 2
|
||||||
|
c.ReadReplica = true
|
||||||
|
})
|
||||||
defer os.RemoveAll(dir1)
|
defer os.RemoveAll(dir1)
|
||||||
defer s1.Shutdown()
|
defer s1.Shutdown()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user