mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
remove funky panic/recover in agent tests (#6442)
This commit is contained in:
parent
1909566534
commit
74461406e0
@ -66,7 +66,7 @@ func TestAgent_ConnectClusterIDConfig(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
hcl string
|
hcl string
|
||||||
wantClusterID string
|
wantClusterID string
|
||||||
wantPanic bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "default TestAgent has fixed cluster id",
|
name: "default TestAgent has fixed cluster id",
|
||||||
@ -87,30 +87,27 @@ func TestAgent_ConnectClusterIDConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}`,
|
}`,
|
||||||
wantClusterID: "",
|
wantClusterID: "",
|
||||||
wantPanic: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Indirection to support panic recovery cleanly
|
a := &TestAgent{Name: tt.name, HCL: tt.hcl}
|
||||||
testFn := func() {
|
err := a.Start()
|
||||||
a := &TestAgent{Name: "test", HCL: tt.hcl}
|
if tt.wantErr {
|
||||||
a.ExpectConfigError = tt.wantPanic
|
if err == nil {
|
||||||
if err := a.Start(); err != nil {
|
t.Fatal("expected error, got nil")
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
defer a.Shutdown()
|
return // don't run the rest of the test
|
||||||
|
|
||||||
cfg := a.consulConfig()
|
|
||||||
assert.Equal(t, tt.wantClusterID, cfg.CAConfig.ClusterID)
|
|
||||||
}
|
}
|
||||||
|
if !tt.wantErr && err != nil {
|
||||||
if tt.wantPanic {
|
t.Fatal(err)
|
||||||
require.Panics(t, testFn)
|
|
||||||
} else {
|
|
||||||
testFn()
|
|
||||||
}
|
}
|
||||||
|
defer a.Shutdown()
|
||||||
|
|
||||||
|
cfg := a.consulConfig()
|
||||||
|
assert.Equal(t, tt.wantClusterID, cfg.CAConfig.ClusterID)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,12 +45,6 @@ type TestAgent struct {
|
|||||||
|
|
||||||
HCL string
|
HCL string
|
||||||
|
|
||||||
// ExpectConfigError can be set to prevent the agent retrying Start on errors
|
|
||||||
// and eventually blowing up with runtime.Goexit. This enables tests to assert
|
|
||||||
// that some specific bit of config actually does prevent startup entirely in
|
|
||||||
// a reasonable way without reproducing a lot of the boilerplate here.
|
|
||||||
ExpectConfigError bool
|
|
||||||
|
|
||||||
// Config is the agent configuration. If Config is nil then
|
// Config is the agent configuration. If Config is nil then
|
||||||
// TestConfig() is used. If Config.DataDir is set then it is
|
// TestConfig() is used. If Config.DataDir is set then it is
|
||||||
// the callers responsibility to clean up the data directory.
|
// the callers responsibility to clean up the data directory.
|
||||||
@ -199,19 +193,6 @@ func (a *TestAgent) Start() (err error) {
|
|||||||
agent.ShutdownAgent()
|
agent.ShutdownAgent()
|
||||||
agent.ShutdownEndpoints()
|
agent.ShutdownEndpoints()
|
||||||
|
|
||||||
if a.ExpectConfigError {
|
|
||||||
// Panic the error since this can be caught if needed. Pretty gross way to
|
|
||||||
// detect errors but enough for now and this is a tiny edge case that I'd
|
|
||||||
// otherwise not have a way to test at all...
|
|
||||||
//
|
|
||||||
// TODO(sadams): This can be refactored away by returning an
|
|
||||||
// error here instead of panicing, removing the `ExpectConfigError`
|
|
||||||
// field from `TestAgent`, and having the test that uses this
|
|
||||||
// (TestAgent_ConnectClusterIDConfig) check for an error instead of
|
|
||||||
// catching a panic.
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("%s %s Error starting agent: %s", id, a.Name, err)
|
return fmt.Errorf("%s %s Error starting agent: %s", id, a.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,6 +275,11 @@ func (a *TestAgent) Shutdown() error {
|
|||||||
}
|
}
|
||||||
}()*/
|
}()*/
|
||||||
|
|
||||||
|
// already shut down
|
||||||
|
if a.Agent == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// shutdown agent before endpoints
|
// shutdown agent before endpoints
|
||||||
defer a.Agent.ShutdownEndpoints()
|
defer a.Agent.ShutdownEndpoints()
|
||||||
return a.Agent.ShutdownAgent()
|
return a.Agent.ShutdownAgent()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user