diff --git a/backend/ldap_server_test.go b/backend/ldap_server_test.go index 8f1ba78221ed2200e27b84e44dd393c1bae5476c..f02c60194ba99b0354146733db7077acd8b7a718 100644 --- a/backend/ldap_server_test.go +++ b/backend/ldap_server_test.go @@ -1,8 +1,11 @@ package backend import ( + "errors" "fmt" "io/ioutil" + "net" + "os" "os/exec" "testing" "time" @@ -14,6 +17,22 @@ type testLDAPServerConfig struct { LDIFs []string } +func waitForPort(port int, timeout time.Duration) error { + addr := fmt.Sprintf("127.0.0.1:%d", port) + deadline := time.Now().Add(timeout) + for { + conn, err := net.Dial("tcp", addr) + if err == nil { + conn.Close() + return nil + } + time.Sleep(200 * time.Millisecond) + if time.Now().After(deadline) { + return errors.New("server did not come up within the deadline") + } + } +} + func startTestLDAPServer(t testing.TB, config *testLDAPServerConfig) func() { tmpf, err := ioutil.TempFile("", "") if err != nil { @@ -29,14 +48,18 @@ ldap.port=%d args := []string{tmpf.Name()} args = append(args, config.LDIFs...) proc := exec.Command("./unboundid-ldap-server/bin/unboundid-ldap-server", args...) + proc.Stdout = os.Stdout + proc.Stderr = os.Stderr if err := proc.Start(); err != nil { t.Fatalf("error starting LDAP server: %v", err) } - time.Sleep(1 * time.Second) + waitForPort(config.Port, 5*time.Second) return func() { proc.Process.Kill() + proc.Wait() + os.Remove(tmpf.Name()) } }