Use spiffy new Go 1.8 subtest feature

This commit is contained in:
Michael Schurter 2017-04-19 10:53:22 -07:00
parent 3e8dd386ee
commit 4910f867e7
1 changed files with 37 additions and 34 deletions

View File

@ -229,48 +229,51 @@ func TestConsulScript_Exec_Shutdown(t *testing.T) {
} }
func TestConsulScript_Exec_Codes(t *testing.T) { func TestConsulScript_Exec_Codes(t *testing.T) {
run := func(code int, err error, expected string) { run := func(code int, err error, expected string) func(t *testing.T) {
serviceCheck := structs.ServiceCheck{ return func(t *testing.T) {
Name: "test", t.Parallel()
Interval: time.Hour, serviceCheck := structs.ServiceCheck{
Timeout: 3 * time.Second, Name: "test",
} Interval: time.Hour,
Timeout: 3 * time.Second,
}
hb := newFakeHeartbeater() hb := newFakeHeartbeater()
shutdown := make(chan struct{}) shutdown := make(chan struct{})
exec := newSimpleExec(code, err) exec := newSimpleExec(code, err)
check := newScriptCheck("allocid", "testtask", "checkid", &serviceCheck, exec, hb, testLogger(), shutdown) check := newScriptCheck("allocid", "testtask", "checkid", &serviceCheck, exec, hb, testLogger(), shutdown)
handle := check.run() handle := check.run()
defer handle.cancel() defer handle.cancel()
select { select {
case update := <-hb.updates: case update := <-hb.updates:
if update.status != expected { if update.status != expected {
t.Errorf("expected %q but received %q", expected, update) t.Errorf("expected %q but received %q", expected, update)
}
// assert output is being reported
expectedOutput := fmt.Sprintf("code=%d err=%v", code, err)
if err != nil {
expectedOutput = err.Error()
}
if update.output != expectedOutput {
t.Errorf("expected output=%q but found: %q", expectedOutput, update.output)
}
case <-time.After(3 * time.Second):
t.Fatalf("timed out waiting for script check to exec")
} }
// assert output is being reported
expectedOutput := fmt.Sprintf("code=%d err=%v", code, err)
if err != nil {
expectedOutput = err.Error()
}
if update.output != expectedOutput {
t.Errorf("expected output=%q but found: %q", expectedOutput, update.output)
}
case <-time.After(3 * time.Second):
t.Fatalf("timed out waiting for script check to exec")
} }
} }
// Test exit codes with errors // Test exit codes with errors
run(0, nil, api.HealthPassing) t.Run("Passing", run(0, nil, api.HealthPassing))
run(1, nil, api.HealthWarning) t.Run("Warning", run(1, nil, api.HealthWarning))
run(2, nil, api.HealthCritical) t.Run("Critical-2", run(2, nil, api.HealthCritical))
run(9000, nil, api.HealthCritical) t.Run("Critical-9000", run(9000, nil, api.HealthCritical))
// Errors should always cause Critical status // Errors should always cause Critical status
err := fmt.Errorf("test error") err := fmt.Errorf("test error")
run(0, err, api.HealthCritical) t.Run("Error-0", run(0, err, api.HealthCritical))
run(1, err, api.HealthCritical) t.Run("Error-1", run(1, err, api.HealthCritical))
run(2, err, api.HealthCritical) t.Run("Error-2", run(2, err, api.HealthCritical))
run(9000, err, api.HealthCritical) t.Run("Error-9000", run(9000, err, api.HealthCritical))
} }