Commit 62655bdc authored by ale's avatar ale

Wait for the test LDAP server to start

parent 02d7c9c6
package backend package backend
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"os"
"os/exec" "os/exec"
"testing" "testing"
"time" "time"
...@@ -14,6 +17,22 @@ type testLDAPServerConfig struct { ...@@ -14,6 +17,22 @@ type testLDAPServerConfig struct {
LDIFs []string 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() { func startTestLDAPServer(t testing.TB, config *testLDAPServerConfig) func() {
tmpf, err := ioutil.TempFile("", "") tmpf, err := ioutil.TempFile("", "")
if err != nil { if err != nil {
...@@ -29,14 +48,18 @@ ldap.port=%d ...@@ -29,14 +48,18 @@ ldap.port=%d
args := []string{tmpf.Name()} args := []string{tmpf.Name()}
args = append(args, config.LDIFs...) args = append(args, config.LDIFs...)
proc := exec.Command("./unboundid-ldap-server/bin/unboundid-ldap-server", args...) 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 { if err := proc.Start(); err != nil {
t.Fatalf("error starting LDAP server: %v", err) t.Fatalf("error starting LDAP server: %v", err)
} }
time.Sleep(1 * time.Second) waitForPort(config.Port, 5*time.Second)
return func() { return func() {
proc.Process.Kill() proc.Process.Kill()
proc.Wait()
os.Remove(tmpf.Name())
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment