Commit bdfa295b authored by ale's avatar ale

Move ldap test server to its own package

parent 23b73199
...@@ -4,8 +4,10 @@ import ( ...@@ -4,8 +4,10 @@ import (
"context" "context"
"testing" "testing"
"git.autistici.org/ai3/accountserver"
"github.com/go-test/deep" "github.com/go-test/deep"
"git.autistici.org/ai3/accountserver"
"git.autistici.org/ai3/accountserver/ldaptest"
) )
const ( const (
...@@ -15,7 +17,8 @@ const ( ...@@ -15,7 +17,8 @@ const (
) )
func startServerAndGetUser(t testing.TB) (func(), accountserver.Backend, *accountserver.User) { func startServerAndGetUser(t testing.TB) (func(), accountserver.Backend, *accountserver.User) {
stop := startTestLDAPServer(t, &testLDAPServerConfig{ stop := ldaptest.StartServer(t, &ldaptest.Config{
Dir: "../ldaptest",
Port: testLDAPPort, Port: testLDAPPort,
Base: "dc=example,dc=com", Base: "dc=example,dc=com",
LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"}, LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"},
...@@ -132,7 +135,8 @@ func TestModel_GetUser_Resources(t *testing.T) { ...@@ -132,7 +135,8 @@ func TestModel_GetUser_Resources(t *testing.T) {
} }
func TestModel_SetResourceStatus(t *testing.T) { func TestModel_SetResourceStatus(t *testing.T) {
stop := startTestLDAPServer(t, &testLDAPServerConfig{ stop := ldaptest.StartServer(t, &ldaptest.Config{
Dir: "../ldaptest",
Port: testLDAPPort, Port: testLDAPPort,
Base: "dc=example,dc=com", Base: "dc=example,dc=com",
LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"}, LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"},
...@@ -164,7 +168,8 @@ func TestModel_SetResourceStatus(t *testing.T) { ...@@ -164,7 +168,8 @@ func TestModel_SetResourceStatus(t *testing.T) {
} }
func TestModel_HasAnyResource(t *testing.T) { func TestModel_HasAnyResource(t *testing.T) {
stop := startTestLDAPServer(t, &testLDAPServerConfig{ stop := ldaptest.StartServer(t, &ldaptest.Config{
Dir: "../ldaptest",
Port: testLDAPPort, Port: testLDAPPort,
Base: "dc=example,dc=com", Base: "dc=example,dc=com",
LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"}, LDIFs: []string{"testdata/base.ldif", "testdata/test1.ldif"},
......
package backend package ldaptest
import ( import (
"errors" "errors"
...@@ -7,11 +7,14 @@ import ( ...@@ -7,11 +7,14 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"testing" "testing"
"time" "time"
) )
type testLDAPServerConfig struct { // Config for the test in-memory LDAP server.
type Config struct {
Dir string
Base string Base string
Port int Port int
LDIFs []string LDIFs []string
...@@ -33,7 +36,8 @@ func waitForPort(port int, timeout time.Duration) error { ...@@ -33,7 +36,8 @@ func waitForPort(port int, timeout time.Duration) error {
} }
} }
func startTestLDAPServer(t testing.TB, config *testLDAPServerConfig) func() { // StartServer starts a test LDAP server with the specified configuration.
func StartServer(t testing.TB, config *Config) func() {
tmpf, err := ioutil.TempFile("", "") tmpf, err := ioutil.TempFile("", "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -47,7 +51,7 @@ ldap.port=%d ...@@ -47,7 +51,7 @@ 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(filepath.Join(config.Dir, "unboundid-ldap-server/bin/unboundid-ldap-server"), args...)
proc.Stdout = os.Stdout proc.Stdout = os.Stdout
proc.Stderr = os.Stderr proc.Stderr = os.Stderr
......
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